debian-mirror-gitlab/app/assets/javascripts/monitoring/components/alert_widget.vue

286 lines
8.8 KiB
Vue
Raw Normal View History

2020-05-24 23:13:21 +05:30
<script>
import { GlBadge, GlLoadingIcon, GlModalDirective, GlIcon, GlTooltip, GlSprintf } from '@gitlab/ui';
2020-10-24 23:57:45 +05:30
import { values, get } from 'lodash';
2021-09-04 01:27:46 +05:30
import createFlash from '~/flash';
2021-03-11 19:13:27 +05:30
import { BV_SHOW_MODAL, BV_HIDE_MODAL } from '~/lib/utils/constants';
import { s__ } from '~/locale';
import { OPERATORS } from '../constants';
2020-05-24 23:13:21 +05:30
import AlertsService from '../services/alerts_service';
import { alertsValidator, queriesValidator } from '../validators';
2021-03-11 19:13:27 +05:30
import AlertWidgetForm from './alert_widget_form.vue';
2020-05-24 23:13:21 +05:30
export default {
components: {
AlertWidgetForm,
GlBadge,
GlLoadingIcon,
GlIcon,
GlTooltip,
GlSprintf,
},
directives: {
GlModal: GlModalDirective,
},
props: {
alertsEndpoint: {
type: String,
required: true,
},
showLoadingState: {
type: Boolean,
required: false,
default: true,
},
// { [alertPath]: { alert_attributes } }. Populated from subsequent API calls.
// Includes only the metrics/alerts to be managed by this widget.
alertsToManage: {
type: Object,
required: false,
default: () => ({}),
validator: alertsValidator,
},
// [{ metric+query_attributes }]. Represents queries (and alerts) we know about
// on intial fetch. Essentially used for reference.
relevantQueries: {
type: Array,
required: true,
validator: queriesValidator,
},
modalId: {
type: String,
required: true,
},
},
data() {
return {
service: null,
errorMessage: null,
isLoading: false,
apiAction: 'create',
};
},
i18n: {
alertsCountMsg: s__('PrometheusAlerts|%{count} alerts applied'),
singleFiringMsg: s__('PrometheusAlerts|Firing: %{alert}'),
multipleFiringMsg: s__('PrometheusAlerts|%{firingCount} firing'),
firingAlertsTooltip: s__('PrometheusAlerts|Firing: %{alerts}'),
},
computed: {
singleAlertSummary() {
return {
message: this.isFiring ? this.$options.i18n.singleFiringMsg : this.thresholds[0],
alert: this.thresholds[0],
};
},
multipleAlertsSummary() {
return {
message: this.isFiring
? `${this.$options.i18n.alertsCountMsg}, ${this.$options.i18n.multipleFiringMsg}`
: this.$options.i18n.alertsCountMsg,
count: this.thresholds.length,
firingCount: this.firingAlerts.length,
};
},
shouldShowLoadingIcon() {
return this.showLoadingState && this.isLoading;
},
thresholds() {
const alertsToManage = Object.keys(this.alertsToManage);
return alertsToManage.map(this.formatAlertSummary);
},
hasAlerts() {
return Boolean(Object.keys(this.alertsToManage).length);
},
hasMultipleAlerts() {
return this.thresholds.length > 1;
},
isFiring() {
return Boolean(this.firingAlerts.length);
},
firingAlerts() {
2021-03-08 18:12:59 +05:30
return values(this.alertsToManage).filter((alert) =>
2020-05-24 23:13:21 +05:30
this.passedAlertThreshold(this.getQueryData(alert), alert),
);
},
formattedFiringAlerts() {
2021-03-08 18:12:59 +05:30
return this.firingAlerts.map((alert) => this.formatAlertSummary(alert.alert_path));
2020-05-24 23:13:21 +05:30
},
configuredAlert() {
return this.hasAlerts ? values(this.alertsToManage)[0].metricId : '';
},
},
created() {
this.service = new AlertsService({ alertsEndpoint: this.alertsEndpoint });
this.fetchAlertData();
},
methods: {
fetchAlertData() {
this.isLoading = true;
2021-03-08 18:12:59 +05:30
const queriesWithAlerts = this.relevantQueries.filter((query) => query.alert_path);
2020-05-24 23:13:21 +05:30
return Promise.all(
2021-03-08 18:12:59 +05:30
queriesWithAlerts.map((query) =>
2020-05-24 23:13:21 +05:30
this.service
.readAlert(query.alert_path)
2021-03-08 18:12:59 +05:30
.then((alertAttributes) => this.setAlert(alertAttributes, query.metricId)),
2020-05-24 23:13:21 +05:30
),
)
.then(() => {
this.isLoading = false;
})
.catch(() => {
2021-09-04 01:27:46 +05:30
createFlash({
message: s__('PrometheusAlerts|Error fetching alert'),
});
2020-05-24 23:13:21 +05:30
this.isLoading = false;
});
},
setAlert(alertAttributes, metricId) {
this.$emit('setAlerts', alertAttributes.alert_path, { ...alertAttributes, metricId });
},
removeAlert(alertPath) {
this.$emit('setAlerts', alertPath, null);
},
formatAlertSummary(alertPath) {
const alert = this.alertsToManage[alertPath];
2021-03-08 18:12:59 +05:30
const alertQuery = this.relevantQueries.find((query) => query.metricId === alert.metricId);
2020-05-24 23:13:21 +05:30
return `${alertQuery.label} ${alert.operator} ${alert.threshold}`;
},
passedAlertThreshold(data, alert) {
const { threshold, operator } = alert;
switch (operator) {
case OPERATORS.greaterThan:
2021-03-08 18:12:59 +05:30
return data.some((value) => value > threshold);
2020-05-24 23:13:21 +05:30
case OPERATORS.lessThan:
2021-03-08 18:12:59 +05:30
return data.some((value) => value < threshold);
2020-05-24 23:13:21 +05:30
case OPERATORS.equalTo:
2021-03-08 18:12:59 +05:30
return data.some((value) => value === threshold);
2020-05-24 23:13:21 +05:30
default:
return false;
}
},
getQueryData(alert) {
2021-03-08 18:12:59 +05:30
const alertQuery = this.relevantQueries.find((query) => query.metricId === alert.metricId);
2020-05-24 23:13:21 +05:30
2021-03-08 18:12:59 +05:30
return get(alertQuery, 'result[0].values', []).map((value) => get(value, '[1]', null));
2020-05-24 23:13:21 +05:30
},
showModal() {
2021-03-11 19:13:27 +05:30
this.$root.$emit(BV_SHOW_MODAL, this.modalId);
2020-05-24 23:13:21 +05:30
},
hideModal() {
this.errorMessage = null;
2021-03-11 19:13:27 +05:30
this.$root.$emit(BV_HIDE_MODAL, this.modalId);
2020-05-24 23:13:21 +05:30
},
handleSetApiAction(apiAction) {
this.apiAction = apiAction;
},
2020-10-24 23:57:45 +05:30
handleCreate({ operator, threshold, prometheus_metric_id, runbookUrl }) {
const newAlert = { operator, threshold, prometheus_metric_id, runbookUrl };
2020-05-24 23:13:21 +05:30
this.isLoading = true;
this.service
.createAlert(newAlert)
2021-03-08 18:12:59 +05:30
.then((alertAttributes) => {
2020-05-24 23:13:21 +05:30
this.setAlert(alertAttributes, prometheus_metric_id);
this.isLoading = false;
this.hideModal();
})
.catch(() => {
this.errorMessage = s__('PrometheusAlerts|Error creating alert');
this.isLoading = false;
});
},
2020-10-24 23:57:45 +05:30
handleUpdate({ alert, operator, threshold, runbookUrl }) {
const updatedAlert = { operator, threshold, runbookUrl };
2020-05-24 23:13:21 +05:30
this.isLoading = true;
this.service
.updateAlert(alert, updatedAlert)
2021-03-08 18:12:59 +05:30
.then((alertAttributes) => {
2020-05-24 23:13:21 +05:30
this.setAlert(alertAttributes, this.alertsToManage[alert].metricId);
this.isLoading = false;
this.hideModal();
})
.catch(() => {
this.errorMessage = s__('PrometheusAlerts|Error saving alert');
this.isLoading = false;
});
},
handleDelete({ alert }) {
this.isLoading = true;
this.service
.deleteAlert(alert)
.then(() => {
this.removeAlert(alert);
this.isLoading = false;
this.hideModal();
})
.catch(() => {
this.errorMessage = s__('PrometheusAlerts|Error deleting alert');
this.isLoading = false;
});
},
},
};
</script>
<template>
<div class="prometheus-alert-widget dropdown flex-grow-2 overflow-hidden">
2021-09-30 23:02:18 +05:30
<gl-loading-icon v-if="shouldShowLoadingIcon" :inline="true" size="sm" />
2020-05-24 23:13:21 +05:30
<span v-else-if="errorMessage" ref="alertErrorMessage" class="alert-error-message">{{
errorMessage
}}</span>
<span
v-else-if="hasAlerts"
ref="alertCurrentSetting"
class="alert-current-setting cursor-pointer d-flex"
@click="showModal"
>
2020-06-23 00:09:42 +05:30
<gl-badge :variant="isFiring ? 'danger' : 'neutral'" class="d-flex-center text-truncate">
2020-05-24 23:13:21 +05:30
<gl-icon name="warning" :size="16" class="flex-shrink-0" />
2020-11-24 15:15:51 +05:30
<span class="text-truncate gl-pl-2">
2020-05-24 23:13:21 +05:30
<gl-sprintf
:message="
hasMultipleAlerts ? multipleAlertsSummary.message : singleAlertSummary.message
"
>
<template #alert>
{{ singleAlertSummary.alert }}
</template>
<template #count>
{{ multipleAlertsSummary.count }}
</template>
<template #firingCount>
{{ multipleAlertsSummary.firingCount }}
</template>
</gl-sprintf>
</span>
</gl-badge>
<gl-tooltip v-if="hasMultipleAlerts && isFiring" :target="() => $refs.alertCurrentSetting">
<gl-sprintf :message="$options.i18n.firingAlertsTooltip">
<template #alerts>
<div v-for="alert in formattedFiringAlerts" :key="alert.alert_path">
{{ alert }}
</div>
</template>
</gl-sprintf>
</gl-tooltip>
</span>
<alert-widget-form
ref="widgetForm"
:disabled="isLoading"
:alerts-to-manage="alertsToManage"
:relevant-queries="relevantQueries"
:error-message="errorMessage"
:configured-alert="configuredAlert"
:modal-id="modalId"
@create="handleCreate"
@update="handleUpdate"
@delete="handleDelete"
@cancel="hideModal"
@setAction="handleSetApiAction"
/>
</div>
</template>