# All files / src/app/shared/services prometheus-alert-formatter.ts

100% Statements 27/27
88.89% Branches 8/9
100% Functions 9/9
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    7710x

    10x

    10x
    10x

    10x
    10x

    10x
    28x

    10x

    19x

    30x

    30x

    42x

    10x
    25x
    25x

    10x
    28x

    10x

    43x

    10x
    28x

    10x
     import { Injectable } from '@angular/core';

    import * as _ from 'lodash';

    import { NotificationType } from '../enum/notification-type.enum';
    import { CdNotificationConfig } from '../models/cd-notification';
    import {
      PrometheusAlert,
      PrometheusCustomAlert,
      PrometheusNotificationAlert
    } from '../models/prometheus-alerts';
    import { NotificationService } from './notification.service';
    import { ServicesModule } from './services.module';

    @Injectable({
      providedIn: ServicesModule
    })
    export class PrometheusAlertFormatter {
      constructor(private notificationService: NotificationService) {}

      sendNotifications(notifications: CdNotificationConfig[]) {
        if (notifications.length > 0) {
          this.notificationService.queueNotifications(notifications);
        }
      }

      convertToCustomAlerts(
        alerts: (PrometheusNotificationAlert | PrometheusAlert)[]
      ): PrometheusCustomAlert[] {
        return _.uniqWith(
          alerts.map((alert) => {
            return {
              status: _.isObject(alert.status)
                ? (alert as PrometheusAlert).status.state
                : this.getPrometheusNotificationStatus(alert as PrometheusNotificationAlert),
              name: alert.labels.alertname,
              url: alert.generatorURL,
              summary: alert.annotations.summary,
              fingerprint: _.isObject(alert.status) && (alert as PrometheusAlert).fingerprint
            };
          }),
          _.isEqual
        ) as PrometheusCustomAlert[];
      }

      /*
       * This is needed because NotificationAlerts don't use 'active'
       */
      private getPrometheusNotificationStatus(alert: PrometheusNotificationAlert): string {
        const state = alert.status;
        return state === 'firing' ? 'active' : state;
      }

      convertAlertToNotification(alert: PrometheusCustomAlert): CdNotificationConfig {
        return new CdNotificationConfig(
          this.formatType(alert.status),
          `${alert.name} (${alert.status})`,
          this.appendSourceLink(alert, alert.summary),
          undefined,
          'Prometheus'
        );
      }

      private formatType(status: string): NotificationType {
        const types = {
          error: ['firing', 'active'],
          info: ['suppressed', 'unprocessed'],
          success: ['resolved']
        };
        return NotificationType[_.findKey(types, (type) => type.includes(status))];
      }

      private appendSourceLink(alert: PrometheusCustomAlert, message: string): string {
        return `${message} <a href="${alert.url}" target="_blank"><i class="fa fa-line-chart"></i></a>`;
      }
    }