summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/monitoring/prometheus_graph.js
blob: a3419895ab034017719ff978f94c39c29a086454 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* eslint-disable no-new*/

import d3 from 'd3';
import _ from 'underscore';
import statusCodes from '~/lib/utils/http_status';
import '~/lib/utils/common_utils';
import Flash from '~/flash';

const prometheusGraphsContainer = '.prometheus-graph';
const metricsEndpoint = 'metrics.json';
const timeFormat = d3.time.format('%H:%M');
const dayFormat = d3.time.format('%b %e, %a');
const bisectDate = d3.bisector(d => d.time).left;
const extraAddedWidthParent = 100;

class PrometheusGraph {

  constructor() {
    this.margin = { top: 80, right: 180, bottom: 80, left: 100 };
    this.marginLabelContainer = { top: 40, right: 0, bottom: 40, left: 0 };
    const parentContainerWidth = $(prometheusGraphsContainer).parent().width() +
    extraAddedWidthParent;
    this.originalWidth = parentContainerWidth;
    this.originalHeight = 400;
    this.width = parentContainerWidth - this.margin.left - this.margin.right;
    this.height = 400 - this.margin.top - this.margin.bottom;
    this.backOffRequestCounter = 0;
    this.configureGraph();
    this.init();
  }

  createGraph() {
    const self = this;
    _.each(this.data, (value, key) => {
      if (value.length > 0 && (key === 'cpu_values' || key === 'memory_values')) {
        self.plotValues(value, key);
      }
    });
  }

  init() {
    const self = this;
    this.getData().then((metricsResponse) => {
      if (metricsResponse === {}) {
        new Flash('Empty metrics', 'alert');
      } else {
        self.transformData(metricsResponse);
        self.createGraph();
      }
    });
  }

  plotValues(valuesToPlot, key) {
    const x = d3.time.scale()
        .range([0, this.width]);

    const y = d3.scale.linear()
        .range([this.height, 0]);

    const prometheusGraphContainer = `${prometheusGraphsContainer}[graph-type=${key}]`;

    const graphSpecifics = this.graphSpecificProperties[key];

    const chart = d3.select(prometheusGraphContainer)
        .attr('width', this.width + this.margin.left + this.margin.right)
        .attr('height', this.height + this.margin.bottom + this.margin.top)
        .append('g')
          .attr('transform', `translate(${this.margin.left},${this.margin.top})`);

    const axisLabelContainer = d3.select(prometheusGraphContainer)
      .attr('width', this.originalWidth + this.marginLabelContainer.left + this.marginLabelContainer.right)
      .attr('height', this.originalHeight + this.marginLabelContainer.bottom + this.marginLabelContainer.top)
      .append('g')
        .attr('transform', `translate(${this.marginLabelContainer.left},${this.marginLabelContainer.top})`);

    x.domain(d3.extent(valuesToPlot, d => d.time));
    y.domain([0, d3.max(valuesToPlot.map(metricValue => metricValue.value))]);

    const xAxis = d3.svg.axis()
        .scale(x)
        .ticks(this.commonGraphProperties.axis_no_ticks)
        .orient('bottom');

    const yAxis = d3.svg.axis()
        .scale(y)
        .ticks(this.commonGraphProperties.axis_no_ticks)
        .tickSize(-this.width)
        .orient('left');

    this.createAxisLabelContainers(axisLabelContainer, key);

    chart.append('g')
        .attr('class', 'x-axis')
        .attr('transform', `translate(0,${this.height})`)
        .call(xAxis);

    chart.append('g')
        .attr('class', 'y-axis')
        .call(yAxis);

    const area = d3.svg.area()
      .x(d => x(d.time))
      .y0(this.height)
      .y1(d => y(d.value))
      .interpolate('linear');

    const line = d3.svg.line()
    .x(d => x(d.time))
    .y(d => y(d.value));

    chart.append('path')
    .datum(valuesToPlot)
    .attr('d', area)
    .attr('class', 'metric-area')
    .attr('fill', graphSpecifics.area_fill_color);

    chart.append('path')
      .datum(valuesToPlot)
      .attr('class', 'metric-line')
      .attr('stroke', graphSpecifics.line_color)
      .attr('fill', 'none')
      .attr('stroke-width', this.commonGraphProperties.area_stroke_width)
      .attr('d', line);

    // Overlay area for the mouseover events
    chart.append('rect')
      .attr('class', 'prometheus-graph-overlay')
      .attr('width', this.width)
      .attr('height', this.height)
      .on('mousemove', this.handleMouseOverGraph.bind(this, x, y, valuesToPlot, chart, prometheusGraphContainer, key));
  }

  // The legends from the metric
  createAxisLabelContainers(axisLabelContainer, key) {
    const graphSpecifics = this.graphSpecificProperties[key];

    axisLabelContainer.append('line')
        .attr('class', 'label-x-axis-line')
        .attr('stroke', '#000000')
        .attr('stroke-width', '1')
        .attr({
          x1: 0,
          y1: this.originalHeight - this.marginLabelContainer.top,
          x2: this.originalWidth - this.margin.right,
          y2: this.originalHeight - this.marginLabelContainer.top,
        });

    axisLabelContainer.append('line')
          .attr('class', 'label-y-axis-line')
          .attr('stroke', '#000000')
          .attr('stroke-width', '1')
          .attr({
            x1: 0,
            y1: 0,
            x2: 0,
            y2: this.originalHeight - this.marginLabelContainer.top,
          });

    axisLabelContainer.append('text')
          .attr('class', 'label-axis-text')
          .attr('text-anchor', 'middle')
          .attr('transform', `translate(15, ${(this.originalHeight - this.marginLabelContainer.top) / 2}) rotate(-90)`)
          .text(graphSpecifics.graph_legend_title);

    axisLabelContainer.append('rect')
          .attr('class', 'rect-axis-text')
          .attr('x', (this.originalWidth / 2) - this.margin.right)
          .attr('y', this.originalHeight - this.marginLabelContainer.top - 20)
          .attr('width', 30)
          .attr('height', 80);

    axisLabelContainer.append('text')
          .attr('class', 'label-axis-text')
          .attr('x', (this.originalWidth / 2) - this.margin.right)
          .attr('y', this.originalHeight - this.marginLabelContainer.top)
          .attr('dy', '.35em')
          .text('Time');

    // Legends

    // Metric Usage
    axisLabelContainer.append('rect')
          .attr('x', this.originalWidth - 170)
          .attr('y', (this.originalHeight / 2) - 60)
          .style('fill', graphSpecifics.area_fill_color)
          .attr('width', 20)
          .attr('height', 35);

    axisLabelContainer.append('text')
          .attr('class', 'label-axis-text')
          .attr('x', this.originalWidth - 140)
          .attr('y', (this.originalHeight / 2) - 50)
          .text('Average');

    axisLabelContainer.append('text')
            .attr('class', 'text-metric-usage')
            .attr('x', this.originalWidth - 140)
            .attr('y', (this.originalHeight / 2) - 25);
  }

  handleMouseOverGraph(x, y, valuesToPlot, chart, prometheusGraphContainer, key) {
    const rectOverlay = document.querySelector(`${prometheusGraphContainer} .prometheus-graph-overlay`);
    const timeValueFromOverlay = x.invert(d3.mouse(rectOverlay)[0]);
    const timeValueIndex = bisectDate(valuesToPlot, timeValueFromOverlay, 1);
    const d0 = valuesToPlot[timeValueIndex - 1];
    const d1 = valuesToPlot[timeValueIndex];
    const currentData = timeValueFromOverlay - d0.time > d1.time - timeValueFromOverlay ? d1 : d0;
    const maxValueMetric = y(d3.max(valuesToPlot.map(metricValue => metricValue.value)));
    const currentTimeCoordinate = x(currentData.time);
    const graphSpecifics = this.graphSpecificProperties[key];
    // Remove the current selectors
    d3.selectAll(`${prometheusGraphContainer} .selected-metric-line`).remove();
    d3.selectAll(`${prometheusGraphContainer} .circle-metric`).remove();
    d3.selectAll(`${prometheusGraphContainer} .rect-text-metric`).remove();
    d3.selectAll(`${prometheusGraphContainer} .text-metric`).remove();

    chart.append('line')
    .attr('class', 'selected-metric-line')
    .attr({
      x1: currentTimeCoordinate,
      y1: y(0),
      x2: currentTimeCoordinate,
      y2: maxValueMetric,
    });

    chart.append('circle')
    .attr('class', 'circle-metric')
    .attr('fill', graphSpecifics.line_color)
    .attr('cx', currentTimeCoordinate)
    .attr('cy', y(currentData.value))
    .attr('r', this.commonGraphProperties.circle_radius_metric);

    // The little box with text
    const rectTextMetric = chart.append('g')
    .attr('class', 'rect-text-metric')
    .attr('translate', `(${currentTimeCoordinate}, ${y(currentData.value)})`);

    rectTextMetric.append('rect')
    .attr('class', 'rect-metric')
    .attr('x', currentTimeCoordinate + 10)
    .attr('y', maxValueMetric)
    .attr('width', this.commonGraphProperties.rect_text_width)
    .attr('height', this.commonGraphProperties.rect_text_height);

    rectTextMetric.append('text')
    .attr('class', 'text-metric')
    .attr('x', currentTimeCoordinate + 35)
    .attr('y', maxValueMetric + 35)
    .text(timeFormat(currentData.time));

    rectTextMetric.append('text')
    .attr('class', 'text-metric-date')
    .attr('x', currentTimeCoordinate + 15)
    .attr('y', maxValueMetric + 15)
    .text(dayFormat(currentData.time));

    // Update the text
    d3.select(`${prometheusGraphContainer} .text-metric-usage`)
      .text(currentData.value.substring(0, 8));
  }

  configureGraph() {
    this.graphSpecificProperties = {
      cpu_values: {
        area_fill_color: '#edf3fc',
        line_color: '#5b99f7',
        graph_legend_title: 'CPU Utilization (%)',
      },
      memory_values: {
        area_fill_color: '#fca326',
        line_color: '#fc6d26',
        graph_legend_title: 'Memory Usage (MB)',
      },
    };

    this.commonGraphProperties = {
      area_stroke_width: 2,
      median_total_characters: 8,
      circle_radius_metric: 5,
      rect_text_width: 90,
      rect_text_height: 40,
      axis_no_ticks: 3,
    };
  }

  getData() {
    const maxNumberOfRequests = 3;
    return gl.utils.backOff((next, stop) => {
      $.ajax({
        url: metricsEndpoint,
        dataType: 'json',
      })
      .done((data, statusText, resp) => {
        if (resp.status === statusCodes.NO_CONTENT) {
          this.backOffRequestCounter = this.backOffRequestCounter += 1;
          if (this.backOffRequestCounter < maxNumberOfRequests) {
            next();
          } else {
            stop({
              status: resp.status,
              metrics: data,
            });
          }
        } else {
          stop({
            status: resp.status,
            metrics: data,
          });
        }
      }).fail(stop);
    })
    .then((resp) => {
      if (resp.status === statusCodes.NO_CONTENT) {
        return {};
      }
      return resp.metrics;
    })
    .catch(() => new Flash('An error occurred while fetching metrics.', 'alert'));
  }

  transformData(metricsResponse) {
    const metricTypes = {};
    _.each(metricsResponse.metrics, (value, key) => {
      if (typeof value[0] !== 'undefined') {
        const metricValues = value[0].values;
        metricTypes[key] = _.map(metricValues, metric => ({
          time: new Date(metric[0] * 1000),
          value: metric[1],
        }));
      }
    });
    this.data = metricTypes;
  }
}

export default PrometheusGraph;