]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-notification.service.ts
import 14.2.4 nautilus point release
[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
3import * as _ from 'lodash';
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 }
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}