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