]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { Injectable } from '@angular/core'; |
2 | ||
3 | import * as _ from 'lodash'; | |
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 | } | |
11fdf7f2 TL |
28 | this.prometheusService |
29 | .getNotifications(_.last(this.notifications)) | |
494da23a TL |
30 | .subscribe( |
31 | (notifications) => this.handleNotifications(notifications), | |
32 | () => (this.backendFailure = true) | |
33 | ); | |
11fdf7f2 TL |
34 | } |
35 | ||
494da23a | 36 | private handleNotifications(notifications: AlertmanagerNotification[]) { |
11fdf7f2 TL |
37 | if (notifications.length === 0) { |
38 | return; | |
39 | } | |
40 | if (this.notifications.length > 0) { | |
41 | this.alertFormatter.sendNotifications( | |
42 | _.flatten(notifications.map((notification) => this.formatNotification(notification))) | |
43 | ); | |
44 | } | |
45 | this.notifications = this.notifications.concat(notifications); | |
46 | } | |
47 | ||
494da23a | 48 | private formatNotification(notification: AlertmanagerNotification): CdNotificationConfig[] { |
11fdf7f2 TL |
49 | return this.alertFormatter |
50 | .convertToCustomAlerts(notification.alerts) | |
51 | .map((alert) => this.alertFormatter.convertAlertToNotification(alert)); | |
52 | } | |
53 | } |