]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-notification.service.ts
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / prometheus-notification.service.ts
1 import { Injectable } from '@angular/core';
2
3 import _ from 'lodash';
4
5 import { PrometheusService } from '../api/prometheus.service';
6 import { CdNotificationConfig } from '../models/cd-notification';
7 import { AlertmanagerNotification } from '../models/prometheus-alerts';
8 import { PrometheusAlertFormatter } from './prometheus-alert-formatter';
9
10 @Injectable({
11 providedIn: 'root'
12 })
13 export class PrometheusNotificationService {
14 private notifications: AlertmanagerNotification[];
15 private backendFailure = false;
16
17 constructor(
18 private alertFormatter: PrometheusAlertFormatter,
19 private prometheusService: PrometheusService
20 ) {
21 this.notifications = [];
22 }
23
24 refresh() {
25 if (this.backendFailure) {
26 return;
27 }
28 this.prometheusService.getNotifications(_.last(this.notifications)).subscribe(
29 (notifications) => this.handleNotifications(notifications),
30 () => (this.backendFailure = true)
31 );
32 }
33
34 private handleNotifications(notifications: AlertmanagerNotification[]) {
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
46 private formatNotification(notification: AlertmanagerNotification): CdNotificationConfig[] {
47 return this.alertFormatter
48 .convertToCustomAlerts(notification.alerts)
49 .map((alert) => this.alertFormatter.convertAlertToNotification(alert));
50 }
51 }