summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/monitoring/components/variables_section.vue
blob: e054c9d8e26e4a01706f2eb2514feaefdd854bd7 (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
<script>
import { mapState, mapActions } from 'vuex';
import CustomVariable from './variables/custom_variable.vue';
import TextVariable from './variables/text_variable.vue';
import { setPromCustomVariablesFromUrl } from '../utils';

export default {
  components: {
    CustomVariable,
    TextVariable,
  },
  computed: {
    ...mapState('monitoringDashboard', ['promVariables']),
  },
  methods: {
    ...mapActions('monitoringDashboard', ['fetchDashboardData', 'updateVariableValues']),
    refreshDashboard(variable, value) {
      if (this.promVariables[variable].value !== value) {
        const changedVariable = { key: variable, value };
        // update the Vuex store
        this.updateVariableValues(changedVariable);
        // the below calls can ideally be moved out of the
        // component and into the actions and let the
        // mutation respond directly.
        // This can be further investigate in
        // https://gitlab.com/gitlab-org/gitlab/-/issues/217713
        setPromCustomVariablesFromUrl(this.promVariables);
        // fetch data
        this.fetchDashboardData();
      }
    },
    variableComponent(type) {
      const types = {
        text: TextVariable,
        custom: CustomVariable,
      };
      return types[type] || TextVariable;
    },
  },
};
</script>
<template>
  <div ref="variablesSection" class="d-sm-flex flex-sm-wrap pt-2 pr-1 pb-0 pl-2 variables-section">
    <div v-for="(variable, key) in promVariables" :key="key" class="mb-1 pr-2 d-flex d-sm-block">
      <component
        :is="variableComponent(variable.type)"
        class="mb-0 flex-grow-1"
        :label="variable.label"
        :value="variable.value"
        :name="key"
        :options="variable.options"
        @onUpdate="refreshDashboard"
      />
    </div>
  </div>
</template>