1 import { Injectable } from '@angular/core';
3 import * as _ from 'lodash';
5 import { NotificationType } from '../enum/notification-type.enum';
6 import { CdNotificationConfig } from '../models/cd-notification';
9 AlertmanagerNotificationAlert,
11 } from '../models/prometheus-alerts';
12 import { NotificationService } from './notification.service';
17 export class PrometheusAlertFormatter {
18 constructor(private notificationService: NotificationService) {}
20 sendNotifications(notifications: CdNotificationConfig[]) {
21 notifications.forEach((n) => this.notificationService.show(n));
24 convertToCustomAlerts(
25 alerts: (AlertmanagerNotificationAlert | AlertmanagerAlert)[]
26 ): PrometheusCustomAlert[] {
28 alerts.map((alert) => {
30 status: _.isObject(alert.status)
31 ? (alert as AlertmanagerAlert).status.state
32 : this.getPrometheusNotificationStatus(alert as AlertmanagerNotificationAlert),
33 name: alert.labels.alertname,
34 url: alert.generatorURL,
35 summary: alert.annotations.summary,
36 fingerprint: _.isObject(alert.status) && (alert as AlertmanagerAlert).fingerprint
40 ) as PrometheusCustomAlert[];
44 * This is needed because NotificationAlerts don't use 'active'
46 private getPrometheusNotificationStatus(alert: AlertmanagerNotificationAlert): string {
47 const state = alert.status;
48 return state === 'firing' ? 'active' : state;
51 convertAlertToNotification(alert: PrometheusCustomAlert): CdNotificationConfig {
52 return new CdNotificationConfig(
53 this.formatType(alert.status),
54 `${alert.name} (${alert.status})`,
55 this.appendSourceLink(alert, alert.summary),
61 private formatType(status: string): NotificationType {
63 error: ['firing', 'active'],
64 info: ['suppressed', 'unprocessed'],
67 return NotificationType[_.findKey(types, (type) => type.includes(status))];
70 private appendSourceLink(alert: PrometheusCustomAlert, message: string): string {
71 return `${message} <a href="${alert.url}" target="_blank"><i class="fa fa-line-chart"></i></a>`;