]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert-formatter.ts
import ceph nautilus 14.2.2
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / prometheus-alert-formatter.ts
CommitLineData
11fdf7f2
TL
1import { Injectable } from '@angular/core';
2
3import * as _ from 'lodash';
4
5import { NotificationType } from '../enum/notification-type.enum';
6import { CdNotificationConfig } from '../models/cd-notification';
7import {
8 PrometheusAlert,
9 PrometheusCustomAlert,
10 PrometheusNotificationAlert
11} from '../models/prometheus-alerts';
12import { NotificationService } from './notification.service';
11fdf7f2
TL
13
14@Injectable({
81eedcae 15 providedIn: 'root'
11fdf7f2
TL
16})
17export class PrometheusAlertFormatter {
18 constructor(private notificationService: NotificationService) {}
19
20 sendNotifications(notifications: CdNotificationConfig[]) {
81eedcae 21 notifications.forEach((n) => this.notificationService.show(n));
11fdf7f2
TL
22 }
23
24 convertToCustomAlerts(
25 alerts: (PrometheusNotificationAlert | PrometheusAlert)[]
26 ): PrometheusCustomAlert[] {
27 return _.uniqWith(
28 alerts.map((alert) => {
29 return {
30 status: _.isObject(alert.status)
31 ? (alert as PrometheusAlert).status.state
32 : this.getPrometheusNotificationStatus(alert as PrometheusNotificationAlert),
33 name: alert.labels.alertname,
34 url: alert.generatorURL,
35 summary: alert.annotations.summary,
36 fingerprint: _.isObject(alert.status) && (alert as PrometheusAlert).fingerprint
37 };
38 }),
39 _.isEqual
40 ) as PrometheusCustomAlert[];
41 }
42
43 /*
44 * This is needed because NotificationAlerts don't use 'active'
45 */
46 private getPrometheusNotificationStatus(alert: PrometheusNotificationAlert): string {
47 const state = alert.status;
48 return state === 'firing' ? 'active' : state;
49 }
50
51 convertAlertToNotification(alert: PrometheusCustomAlert): CdNotificationConfig {
52 return new CdNotificationConfig(
53 this.formatType(alert.status),
54 `${alert.name} (${alert.status})`,
55 this.appendSourceLink(alert, alert.summary),
56 undefined,
57 'Prometheus'
58 );
59 }
60
61 private formatType(status: string): NotificationType {
62 const types = {
63 error: ['firing', 'active'],
64 info: ['suppressed', 'unprocessed'],
65 success: ['resolved']
66 };
67 return NotificationType[_.findKey(types, (type) => type.includes(status))];
68 }
69
70 private appendSourceLink(alert: PrometheusCustomAlert, message: string): string {
71 return `${message} <a href="${alert.url}" target="_blank"><i class="fa fa-line-chart"></i></a>`;
72 }
73}