]>
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'; | |
7 | import { PrometheusNotification } from '../models/prometheus-alerts'; | |
8 | import { PrometheusAlertFormatter } from './prometheus-alert-formatter'; | |
11fdf7f2 TL |
9 | |
10 | @Injectable({ | |
81eedcae | 11 | providedIn: 'root' |
11fdf7f2 TL |
12 | }) |
13 | export class PrometheusNotificationService { | |
14 | private notifications: PrometheusNotification[]; | |
15 | ||
16 | constructor( | |
17 | private alertFormatter: PrometheusAlertFormatter, | |
18 | private prometheusService: PrometheusService | |
19 | ) { | |
20 | this.notifications = []; | |
21 | } | |
22 | ||
23 | refresh() { | |
24 | this.prometheusService | |
25 | .getNotifications(_.last(this.notifications)) | |
26 | .subscribe((notifications) => this.handleNotifications(notifications)); | |
27 | } | |
28 | ||
29 | private handleNotifications(notifications: PrometheusNotification[]) { | |
30 | if (notifications.length === 0) { | |
31 | return; | |
32 | } | |
33 | if (this.notifications.length > 0) { | |
34 | this.alertFormatter.sendNotifications( | |
35 | _.flatten(notifications.map((notification) => this.formatNotification(notification))) | |
36 | ); | |
37 | } | |
38 | this.notifications = this.notifications.concat(notifications); | |
39 | } | |
40 | ||
41 | private formatNotification(notification: PrometheusNotification): CdNotificationConfig[] { | |
42 | return this.alertFormatter | |
43 | .convertToCustomAlerts(notification.alerts) | |
44 | .map((alert) => this.alertFormatter.convertAlertToNotification(alert)); | |
45 | } | |
46 | } |