]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { Injectable } from '@angular/core'; |
2 | ||
f67539c2 | 3 | import _ from 'lodash'; |
11fdf7f2 | 4 | |
f67539c2 | 5 | import { Icons } from '../enum/icons.enum'; |
11fdf7f2 TL |
6 | import { NotificationType } from '../enum/notification-type.enum'; |
7 | import { CdNotificationConfig } from '../models/cd-notification'; | |
8 | import { | |
494da23a TL |
9 | AlertmanagerAlert, |
10 | AlertmanagerNotificationAlert, | |
11 | PrometheusCustomAlert | |
11fdf7f2 TL |
12 | } from '../models/prometheus-alerts'; |
13 | import { NotificationService } from './notification.service'; | |
11fdf7f2 TL |
14 | |
15 | @Injectable({ | |
81eedcae | 16 | providedIn: 'root' |
11fdf7f2 TL |
17 | }) |
18 | export class PrometheusAlertFormatter { | |
19 | constructor(private notificationService: NotificationService) {} | |
20 | ||
21 | sendNotifications(notifications: CdNotificationConfig[]) { | |
81eedcae | 22 | notifications.forEach((n) => this.notificationService.show(n)); |
11fdf7f2 TL |
23 | } |
24 | ||
25 | convertToCustomAlerts( | |
494da23a | 26 | alerts: (AlertmanagerNotificationAlert | AlertmanagerAlert)[] |
11fdf7f2 TL |
27 | ): PrometheusCustomAlert[] { |
28 | return _.uniqWith( | |
29 | alerts.map((alert) => { | |
30 | return { | |
31 | status: _.isObject(alert.status) | |
494da23a TL |
32 | ? (alert as AlertmanagerAlert).status.state |
33 | : this.getPrometheusNotificationStatus(alert as AlertmanagerNotificationAlert), | |
11fdf7f2 TL |
34 | name: alert.labels.alertname, |
35 | url: alert.generatorURL, | |
f67539c2 | 36 | description: alert.annotations.description, |
494da23a | 37 | fingerprint: _.isObject(alert.status) && (alert as AlertmanagerAlert).fingerprint |
11fdf7f2 TL |
38 | }; |
39 | }), | |
40 | _.isEqual | |
41 | ) as PrometheusCustomAlert[]; | |
42 | } | |
43 | ||
44 | /* | |
45 | * This is needed because NotificationAlerts don't use 'active' | |
46 | */ | |
494da23a | 47 | private getPrometheusNotificationStatus(alert: AlertmanagerNotificationAlert): string { |
11fdf7f2 TL |
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})`, | |
f67539c2 | 56 | this.appendSourceLink(alert, alert.description), |
11fdf7f2 TL |
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 { | |
9f95a23c | 72 | return `${message} <a href="${alert.url}" target="_blank"><i class="${Icons.lineChart}"></i></a>`; |
11fdf7f2 TL |
73 | } |
74 | } |