summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/monitoring/components/monitoring.vue
blob: a6a2d3119e3e7acc0fa6d4b859b3ec9f50ebe4e1 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<script>
  /* global Flash */
  import _ from 'underscore';
  import statusCodes from '../../lib/utils/http_status';
  import MonitoringService from '../services/monitoring_service';
  import monitoringRow from './monitoring_row.vue';
  import monitoringState from './monitoring_state.vue';
  import MonitoringStore from '../stores/monitoring_store';
  import eventHub from '../event_hub';

  export default {

    data() {
      const metricsData = document.querySelector('#prometheus-graphs').dataset;
      const store = new MonitoringStore();

      return {
        store,
        state: 'gettingStarted',
        hasMetrics: gl.utils.convertPermissionToBoolean(metricsData.hasMetrics),
        documentationPath: metricsData.documentationPath,
        settingsPath: metricsData.settingsPath,
        endpoint: metricsData.additionalMetrics,
        deploymentEndpoint: metricsData.deploymentEndpoint,
        showEmptyState: true,
        backOffRequestCounter: 0,
        updateAspectRatio: false,
        updatedAspectRatios: 0,
        resizeThrottled: {},
      };
    },

    components: {
      monitoringRow,
      monitoringState,
    },

    methods: {
      getGraphsData() {
        const maxNumberOfRequests = 3;
        this.state = 'loading';
        gl.utils.backOff((next, stop) => {
          this.service.get().then((resp) => {
            if (resp.status === statusCodes.NO_CONTENT) {
              this.backOffRequestCounter = this.backOffRequestCounter += 1;
              if (this.backOffRequestCounter < maxNumberOfRequests) {
                next();
              } else {
                stop(new Error('Failed to connect to the prometheus server'));
              }
            } else {
              stop(resp);
            }
          }).catch(stop);
        })
        .then((resp) => {
          if (resp.status === statusCodes.NO_CONTENT) {
            this.state = 'unableToConnect';
            return false;
          }
          return resp.json();
        })
        .then((metricGroupsData) => {
          if (!metricGroupsData) return false;
          this.store.storeMetrics(metricGroupsData.data);
          return this.getDeploymentData();
        })
        .then((deploymentData) => {
          if (deploymentData !== false) {
            this.store.storeDeploymentData(deploymentData.deployments);
            this.showEmptyState = false;
          }
          return {};
        })
        .catch(() => {
          this.state = 'unableToConnect';
        });
      },

      getDeploymentData() {
        return this.service.getDeploymentData(this.deploymentEndpoint)
          .then(resp => resp.json())
          .catch(() => new Flash('Error getting deployment information.'));
      },

      resize() {
        this.updateAspectRatio = true;
      },

      toggleAspectRatio() {
        this.updatedAspectRatios = this.updatedAspectRatios += 1;
        if (this.store.getMetricsCount() === this.updatedAspectRatios) {
          this.updateAspectRatio = !this.updateAspectRatio;
          this.updatedAspectRatios = 0;
        }
      },

    },

    created() {
      this.service = new MonitoringService(this.endpoint);
      eventHub.$on('toggleAspectRatio', this.toggleAspectRatio);
    },

    beforeDestroy() {
      eventHub.$off('toggleAspectRatio', this.toggleAspectRatio);
      window.removeEventListener('resize', this.resizeThrottled, false);
    },

    mounted() {
      this.resizeThrottled = _.throttle(this.resize, 600);
      if (!this.hasMetrics) {
        this.state = 'gettingStarted';
      } else {
        this.getGraphsData();
        window.addEventListener('resize', this.resizeThrottled, false);
      }
    },
  };
</script>
<template>
  <div 
    class="prometheus-graphs" 
    v-if="!showEmptyState">
    <div 
      class="row"
      v-for="(groupData, index) in store.groups"
      :key="index">
      <div 
        class="col-md-12">
        <div 
          class="panel panel-default prometheus-panel">
          <div 
            class="panel-heading">
            <h4>{{groupData.group}}</h4>
          </div>
          <div 
            class="panel-body">
            <monitoring-row
              v-for="(row, index) in groupData.metrics" 
              :key="index"
              :row-data="row"
              :update-aspect-ratio="updateAspectRatio"
              :deployment-data="store.deploymentData"
            />
          </div>
        </div>
      </div>
    </div>
  </div>
  <monitoring-state 
    :selected-state="state"
    :documentation-path="documentationPath"
    :settings-path="settingsPath"
    v-else
  />
</template>