1 import { Injectable } from '@angular/core';
3 import _ from 'lodash';
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';
13 export class PrometheusNotificationService {
14 private notifications: AlertmanagerNotification[];
15 private backendFailure = false;
18 private alertFormatter: PrometheusAlertFormatter,
19 private prometheusService: PrometheusService
21 this.notifications = [];
25 if (this.backendFailure) {
28 this.prometheusService.getNotifications(_.last(this.notifications)).subscribe(
29 (notifications) => this.handleNotifications(notifications),
30 () => (this.backendFailure = true)
34 private handleNotifications(notifications: AlertmanagerNotification[]) {
35 if (notifications.length === 0) {
38 if (this.notifications.length > 0) {
39 this.alertFormatter.sendNotifications(
40 _.flatten(notifications.map((notification) => this.formatNotification(notification)))
43 this.notifications = this.notifications.concat(notifications);
46 private formatNotification(notification: AlertmanagerNotification): CdNotificationConfig[] {
47 return this.alertFormatter
48 .convertToCustomAlerts(notification.alerts)
49 .map((alert) => this.alertFormatter.convertAlertToNotification(alert));