]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-notification.service.ts
import ceph 16.2.6
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / prometheus-notification.service.ts
CommitLineData
11fdf7f2
TL
1import { Injectable } from '@angular/core';
2
f67539c2 3import _ from 'lodash';
11fdf7f2
TL
4
5import { PrometheusService } from '../api/prometheus.service';
6import { CdNotificationConfig } from '../models/cd-notification';
494da23a 7import { AlertmanagerNotification } from '../models/prometheus-alerts';
11fdf7f2 8import { PrometheusAlertFormatter } from './prometheus-alert-formatter';
11fdf7f2
TL
9
10@Injectable({
81eedcae 11 providedIn: 'root'
11fdf7f2
TL
12})
13export 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}