]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert-formatter.ts
import 14.2.4 nautilus point release
[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 * as _ from 'lodash';
4
5 import { NotificationType } from '../enum/notification-type.enum';
6 import { CdNotificationConfig } from '../models/cd-notification';
7 import {
8 AlertmanagerAlert,
9 AlertmanagerNotificationAlert,
10 PrometheusCustomAlert
11 } from '../models/prometheus-alerts';
12 import { NotificationService } from './notification.service';
13
14 @Injectable({
15 providedIn: 'root'
16 })
17 export class PrometheusAlertFormatter {
18 constructor(private notificationService: NotificationService) {}
19
20 sendNotifications(notifications: CdNotificationConfig[]) {
21 notifications.forEach((n) => this.notificationService.show(n));
22 }
23
24 convertToCustomAlerts(
25 alerts: (AlertmanagerNotificationAlert | AlertmanagerAlert)[]
26 ): PrometheusCustomAlert[] {
27 return _.uniqWith(
28 alerts.map((alert) => {
29 return {
30 status: _.isObject(alert.status)
31 ? (alert as AlertmanagerAlert).status.state
32 : this.getPrometheusNotificationStatus(alert as AlertmanagerNotificationAlert),
33 name: alert.labels.alertname,
34 url: alert.generatorURL,
35 summary: alert.annotations.summary,
36 fingerprint: _.isObject(alert.status) && (alert as AlertmanagerAlert).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: AlertmanagerNotificationAlert): 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 }