summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/monitoring/services/monitoring_service.js
blob: fed884d5c94d88520aca84bd1b1150f1b1987afd (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
import Vue from 'vue';
import VueResource from 'vue-resource';
import statusCodes from '../../lib/utils/http_status';
import { backOff } from '../../lib/utils/common_utils';

Vue.use(VueResource);

const MAX_REQUESTS = 3;

function backOffRequest(makeRequestCallback) {
  let requestCounter = 0;
  return backOff((next, stop) => {
    makeRequestCallback().then((resp) => {
      if (resp.status === statusCodes.NO_CONTENT) {
        requestCounter += 1;
        if (requestCounter < MAX_REQUESTS) {
          next();
        } else {
          stop(new Error('Failed to connect to the prometheus server'));
        }
      } else {
        stop(resp);
      }
    }).catch(stop);
  });
}

export default class MonitoringService {
  constructor({ metricsEndpoint, deploymentEndpoint }) {
    this.metricsEndpoint = metricsEndpoint;
    this.deploymentEndpoint = deploymentEndpoint;
  }

  getGraphsData() {
    return backOffRequest(() => Vue.http.get(this.metricsEndpoint))
      .then(resp => resp.json())
      .then((response) => {
        if (!response || !response.data) {
          throw new Error('Unexpected metrics data response from prometheus endpoint');
        }
        return response.data;
      });
  }

  getDeploymentData() {
    return backOffRequest(() => Vue.http.get(this.deploymentEndpoint))
      .then(resp => resp.json())
      .then((response) => {
        if (!response || !response.deployments) {
          throw new Error('Unexpected deployment data response from prometheus endpoint');
        }
        return response.deployments;
      });
  }
}