]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { Injectable } from '@angular/core'; |
2 | ||
f67539c2 | 3 | import _ from 'lodash'; |
11fdf7f2 TL |
4 | |
5 | import { PrometheusService } from '../api/prometheus.service'; | |
6 | import { CdNotificationConfig } from '../models/cd-notification'; | |
494da23a | 7 | import { AlertmanagerNotification } from '../models/prometheus-alerts'; |
11fdf7f2 | 8 | import { PrometheusAlertFormatter } from './prometheus-alert-formatter'; |
11fdf7f2 TL |
9 | |
10 | @Injectable({ | |
81eedcae | 11 | providedIn: 'root' |
11fdf7f2 TL |
12 | }) |
13 | export class PrometheusNotificationService { | |
494da23a TL |
14 | private notifications: AlertmanagerNotification[]; |
15 | private backendFailure = false; | |
11fdf7f2 TL |
16 | |
17 | constructor( | |
18 | private alertFormatter: PrometheusAlertFormatter, | |
19 | private prometheusService: PrometheusService | |
20 | ) { | |
21 | this.notifications = []; | |
22 | } | |
23 | ||
24 | refresh() { | |
494da23a TL |
25 | if (this.backendFailure) { |
26 | return; | |
27 | } | |
801d1391 TL |
28 | this.prometheusService.getNotifications(_.last(this.notifications)).subscribe( |
29 | (notifications) => this.handleNotifications(notifications), | |
30 | () => (this.backendFailure = true) | |
31 | ); | |
11fdf7f2 TL |
32 | } |
33 | ||
494da23a | 34 | private handleNotifications(notifications: AlertmanagerNotification[]) { |
11fdf7f2 TL |
35 | if (notifications.length === 0) { |
36 | return; | |
37 | } | |
38 | if (this.notifications.length > 0) { | |
39 | this.alertFormatter.sendNotifications( | |
40 | _.flatten(notifications.map((notification) => this.formatNotification(notification))) | |
41 | ); | |
42 | } | |
43 | this.notifications = this.notifications.concat(notifications); | |
44 | } | |
45 | ||
494da23a | 46 | private formatNotification(notification: AlertmanagerNotification): CdNotificationConfig[] { |
11fdf7f2 TL |
47 | return this.alertFormatter |
48 | .convertToCustomAlerts(notification.alerts) | |
49 | .map((alert) => this.alertFormatter.convertAlertToNotification(alert)); | |
50 | } | |
51 | } |