Welcome to mirror list, hosted at ThFree Co, Russian Federation.

runner_count.vue « stat « components « runner « ci « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4ad9259f59d88830c8a6f15c3a71145450222535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<script>
import { fetchPolicies } from '~/lib/graphql';
import allRunnersCountQuery from 'ee_else_ce/ci/runner/graphql/list/all_runners_count.query.graphql';
import groupRunnersCountQuery from 'ee_else_ce/ci/runner/graphql/list/group_runners_count.query.graphql';

import { captureException } from '../../sentry_utils';
import { INSTANCE_TYPE, GROUP_TYPE } from '../../constants';

/**
 * Renderless component that wraps a "count" query for the
 * number of runners that follow a filter criteria.
 *
 * Example usage:
 *
 * Render the count of "online" runners in the instance in a
 * <strong/> tag.
 *
 * ```vue
 * <runner-count-stat
 *   #default="{ count }"
 *   :scope="INSTANCE_TYPE"
 *   :variables="{ status: 'ONLINE' }"
 * >
 *   <strong>{{ count }}</strong>
 * </runner-count-stat>
 * ```
 *
 * Use `:skip="true"` to prevent data from being fetched and
 * even rendered.
 */
export default {
  name: 'RunnerCount',
  props: {
    scope: {
      type: String,
      required: true,
      validator: (val) => [INSTANCE_TYPE, GROUP_TYPE].includes(val),
    },
    variables: {
      type: Object,
      required: false,
      default: () => ({}),
    },
    skip: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  data() {
    return { count: null };
  },
  apollo: {
    count: {
      query() {
        if (this.scope === INSTANCE_TYPE) {
          return allRunnersCountQuery;
        } else if (this.scope === GROUP_TYPE) {
          return groupRunnersCountQuery;
        }
        return null;
      },
      fetchPolicy: fetchPolicies.NETWORK_ONLY,
      variables() {
        return this.variables;
      },
      skip() {
        if (this.skip) {
          // Don't show data for skipped stats
          this.count = null;
        }
        return this.skip;
      },
      update(data) {
        if (this.scope === INSTANCE_TYPE) {
          return data?.runners?.count;
        } else if (this.scope === GROUP_TYPE) {
          return data?.group?.runners?.count;
        }
        return null;
      },
      error(error) {
        this.reportToSentry(error);
      },
    },
  },
  methods: {
    reportToSentry(error) {
      captureException({ error, component: this.$options.name });
    },

    // Component API
    refetch() {
      // Parent components can use this method to refresh the count
      this.$apollo.queries.count.refetch();
    },
  },
  render() {
    return this.$scopedSlots.default({
      count: this.count,
    });
  },
};
</script>