summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/monitoring/monitoring_bundle.js
blob: 6fc83d76b0c0ebda075b981018b91b22fc6e29a6 (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
/* global Breakpoints */

import statusCodes from '~/lib/utils/http_status';
import '~/lib/utils/common_utils';
import Vue from 'vue';
import VueResource from 'vue-resource';
import d3 from 'd3';
import MonitoringService from './services/monitoring_service';
import MonitoringState from './components/monitoring_state.vue';
import MonitoringPanel from './components/monitoring_panel.vue';
import MonitoringStore from './stores/monitoring_store';
import MonitoringNewAPIResponse from './monitoring_mock';

Vue.use(VueResource);

document.addEventListener('DOMContentLoaded', function onLoad() {
  document.removeEventListener('DOMContentLoaded', onLoad, false);

  const $prometheusGraphs = document.querySelector('.prometheus-graphs');

  const PrometheusApp = new Vue({
    el: $prometheusGraphs,

    data() {
      const metricsData = $prometheusGraphs.dataset;
      const store = new MonitoringStore();

      return {
        store,
        isLoading: true,
        unableToConnect: false,
        gettingStarted: false,
        hasMetrics: gl.utils.convertPermissionToBoolean(metricsData.hasMetrics),
        showEmptyState: false,
        testValue: 0,
        endpoint: 'metrics.json',
        backOffRequestCounter: 0,
        bisectDate: d3.bisector(d => d.time).left,
        service: {},
      };
    },

    components: {
      'monitoring-state': MonitoringState,
      'monitoring-panel': MonitoringPanel,
    },

    methods: {
      getGraphsData() {
        const maxNumberOfRequests = 3;
        this.showEmptyState = true;
        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.isLoading = false;
            this.unableToConnect = true;
            this.showEmptyState = true;
            return resp;
          }
          return resp.json();
        })
        .then((resp) => {
          this.store.storeMetrics(resp.metrics);
          if (this.store.enoughMetrics) {
            this.showEmptyState = false;
          }
        })
        .catch(() => {
          this.isLoading = false;
          this.unableToConnect = true;
          this.showEmptyState = true;
        });
      },
      // TODO: Delete the following method, it's just for testing
      getGraphsDataNewApiMock() {
        this.showEmptyState = false;
        this.store.storeMetrics(MonitoringNewAPIResponse);
      },
      increaseValue() {
        this.testValue = this.testValue += 1;
      },
    },

    created() {
      this.service = new MonitoringService();
    },

    mounted() {
      if (!this.hasMetrics) {
        this.isLoading = false;
        this.gettingStarted = true;
        this.showEmptyState = true;
      } else {
        // this.getGraphsData();
        this.getGraphsDataNewApiMock();
      }
    },

    template: `
      <div class="prometheus-graphs" v-if="!showEmptyState">
        <button class="btn btn-primary" @click=increaseValue>
          Increase test value
        </button>
        <monitoring-panel
          v-for="(group, index) in store.groups" 
          :groupData="group"
          :key="index" 
        />
      </div>
      <monitoring-state 
      :isLoading=isLoading 
      :unableToConnect=unableToConnect 
      :gettingStarted=gettingStarted 
      v-else />
    `,
  });
}, false);