gitlab-org--gitlab-foss/app/assets/javascripts/prometheus_metrics/prometheus_metrics.js

110 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-06-01 11:54:56 -04:00
import PANEL_STATE from './constants';
2017-05-31 07:38:22 -04:00
export default class PrometheusMetrics {
constructor(wrapperSelector) {
this.backOffRequestCounter = 0;
this.$wrapper = $(wrapperSelector);
this.$monitoredMetricsPanel = this.$wrapper.find('.js-panel-monitored-metrics');
this.$monitoredMetricsCount = this.$monitoredMetricsPanel.find('.js-monitored-count');
this.$monitoredMetricsLoading = this.$monitoredMetricsPanel.find('.js-loading-metrics');
this.$monitoredMetricsEmpty = this.$monitoredMetricsPanel.find('.js-empty-metrics');
this.$monitoredMetricsList = this.$monitoredMetricsPanel.find('.js-metrics-list');
2017-05-31 09:35:26 -04:00
this.$missingEnvVarPanel = this.$wrapper.find('.js-panel-missing-env-vars');
this.$panelToggle = this.$missingEnvVarPanel.find('.js-panel-toggle');
this.$missingEnvVarMetricCount = this.$missingEnvVarPanel.find('.js-env-var-count');
this.$missingEnvVarMetricsList = this.$missingEnvVarPanel.find('.js-missing-var-metrics-list');
2017-05-31 07:38:22 -04:00
this.activeMetricsEndpoint = this.$monitoredMetricsPanel.data('active-metrics');
this.$panelToggle.on('click', e => this.handlePanelToggle(e));
}
/* eslint-disable class-methods-use-this */
2017-05-31 07:38:22 -04:00
handlePanelToggle(e) {
const $toggleBtn = $(e.currentTarget);
2017-06-01 11:54:56 -04:00
const $currentPanelBody = $toggleBtn.closest('.panel').find('.panel-body');
$currentPanelBody.toggleClass('hidden');
if ($toggleBtn.hasClass('fa-caret-down')) {
2017-05-31 07:38:22 -04:00
$toggleBtn.removeClass('fa-caret-down').addClass('fa-caret-right');
} else {
$toggleBtn.removeClass('fa-caret-right').addClass('fa-caret-down');
}
}
2017-06-01 11:54:56 -04:00
showMonitoringMetricsPanelState(stateName) {
switch (stateName) {
case PANEL_STATE.LOADING:
this.$monitoredMetricsLoading.removeClass('hidden');
this.$monitoredMetricsEmpty.addClass('hidden');
this.$monitoredMetricsList.addClass('hidden');
break;
case PANEL_STATE.LIST:
this.$monitoredMetricsLoading.addClass('hidden');
this.$monitoredMetricsEmpty.addClass('hidden');
this.$monitoredMetricsList.removeClass('hidden');
break;
default:
this.$monitoredMetricsLoading.addClass('hidden');
this.$monitoredMetricsEmpty.removeClass('hidden');
this.$monitoredMetricsList.addClass('hidden');
break;
}
}
2017-05-31 07:38:22 -04:00
populateActiveMetrics(metrics) {
let totalMonitoredMetrics = 0;
2017-05-31 09:35:26 -04:00
let totalMissingEnvVarMetrics = 0;
2017-05-31 07:38:22 -04:00
metrics.forEach((metric) => {
2017-06-01 11:54:56 -04:00
this.$monitoredMetricsList.append(`<li>${metric.group}<span class="badge">${metric.active_metrics}</span></li>`);
2017-05-31 07:38:22 -04:00
totalMonitoredMetrics += metric.active_metrics;
2017-05-31 09:35:26 -04:00
if (metric.metrics_missing_requirements > 0) {
this.$missingEnvVarMetricsList.append(`<li>${metric.group}</li>`);
totalMissingEnvVarMetrics += 1;
}
2017-05-31 07:38:22 -04:00
});
this.$monitoredMetricsCount.text(totalMonitoredMetrics);
2017-06-01 11:54:56 -04:00
this.showMonitoringMetricsPanelState(PANEL_STATE.LIST);
2017-05-31 09:35:26 -04:00
if (totalMissingEnvVarMetrics > 0) {
this.$missingEnvVarPanel.removeClass('hidden');
2017-06-01 11:54:56 -04:00
this.$missingEnvVarPanel.find('.flash-container').off('click');
2017-05-31 09:35:26 -04:00
this.$missingEnvVarMetricCount.text(totalMissingEnvVarMetrics);
}
2017-05-31 07:38:22 -04:00
}
loadActiveMetrics() {
2017-06-01 11:54:56 -04:00
this.showMonitoringMetricsPanelState(PANEL_STATE.LOADING);
2017-05-31 07:38:22 -04:00
gl.utils.backOff((next, stop) => {
$.getJSON(this.activeMetricsEndpoint)
.done((res) => {
if (res && res.success) {
stop(res);
} else {
this.backOffRequestCounter = this.backOffRequestCounter += 1;
if (this.backOffRequestCounter < 3) {
next();
} else {
stop(res);
}
}
})
.fail(stop);
})
.then((res) => {
if (res && res.data && res.data.length) {
this.populateActiveMetrics(res.data);
} else {
2017-06-01 11:54:56 -04:00
this.showMonitoringMetricsPanelState(PANEL_STATE.EMPTY);
2017-05-31 07:38:22 -04:00
}
})
.catch(() => {
2017-06-01 11:54:56 -04:00
this.showMonitoringMetricsPanelState(PANEL_STATE.EMPTY);
2017-05-31 07:38:22 -04:00
});
}
}