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';
10 PrometheusNotificationAlert
11 } from '../models/prometheus-alerts';
12 import { NotificationService } from './notification.service';
13 import { ServicesModule } from './services.module';
16 providedIn: ServicesModule
18 export class PrometheusAlertFormatter {
19 constructor(private notificationService: NotificationService) {}
21 sendNotifications(notifications: CdNotificationConfig[]) {
22 if (notifications.length > 0) {
23 this.notificationService.queueNotifications(notifications);
27 convertToCustomAlerts(
28 alerts: (PrometheusNotificationAlert | PrometheusAlert)[]
29 ): PrometheusCustomAlert[] {
31 alerts.map((alert) => {
33 status: _.isObject(alert.status)
34 ? (alert as PrometheusAlert).status.state
35 : this.getPrometheusNotificationStatus(alert as PrometheusNotificationAlert),
36 name: alert.labels.alertname,
37 url: alert.generatorURL,
38 summary: alert.annotations.summary,
39 fingerprint: _.isObject(alert.status) && (alert as PrometheusAlert).fingerprint
43 ) as PrometheusCustomAlert[];
47 * This is needed because NotificationAlerts don't use 'active'
49 private getPrometheusNotificationStatus(alert: PrometheusNotificationAlert): string {
50 const state = alert.status;
51 return state === 'firing' ? 'active' : state;
54 convertAlertToNotification(alert: PrometheusCustomAlert): CdNotificationConfig {
55 return new CdNotificationConfig(
56 this.formatType(alert.status),
57 `${alert.name} (${alert.status})`,
58 this.appendSourceLink(alert, alert.summary),
64 private formatType(status: string): NotificationType {
66 error: ['firing', 'active'],
67 info: ['suppressed', 'unprocessed'],
70 return NotificationType[_.findKey(types, (type) => type.includes(status))];
73 private appendSourceLink(alert: PrometheusCustomAlert, message: string): string {
74 return `${message} <a href="${alert.url}" target="_blank"><i class="fa fa-line-chart"></i></a>`;