]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-notification.service.ts
import ceph nautilus 14.2.2
[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';
7import { PrometheusNotification } from '../models/prometheus-alerts';
8import { PrometheusAlertFormatter } from './prometheus-alert-formatter';
11fdf7f2
TL
9
10@Injectable({
81eedcae 11 providedIn: 'root'
11fdf7f2
TL
12})
13export 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}