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