# All files / src/app/shared/services prometheus-notification.service.ts

100% Statements 26/26
71.43% Branches 5/7
100% Functions 7/7
100% Lines 21/21

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
    486x

    6x

    6x

    6x
    6x

    6x

    14x
    14x

    14x

    25x
    25x

    25x

    25x

    2x

    14x
    16x

    23x

    16x
    16x

    22x

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

    import * as _ from 'lodash';

    import { PrometheusService } from '../api/prometheus.service';
    import { CdNotificationConfig } from '../models/cd-notification';
    import { PrometheusNotification } from '../models/prometheus-alerts';
    import { PrometheusAlertFormatter } from './prometheus-alert-formatter';
    import { ServicesModule } from './services.module';

    @Injectable({
      providedIn: ServicesModule
    })
    export class PrometheusNotificationService {
      private notifications: PrometheusNotification[];

      constructor(
        private alertFormatter: PrometheusAlertFormatter,
        private prometheusService: PrometheusService
      ) {
        this.notifications = [];
      }

      refresh() {
        this.prometheusService
          .getNotifications(_.last(this.notifications))
          .subscribe((notifications) => this.handleNotifications(notifications));
      }

      private handleNotifications(notifications: PrometheusNotification[]) {
        if (notifications.length === 0) {
          return;
        }
        if (this.notifications.length > 0) {
          this.alertFormatter.sendNotifications(
            _.flatten(notifications.map((notification) => this.formatNotification(notification)))
          );
        }
        this.notifications = this.notifications.concat(notifications);
      }

      private formatNotification(notification: PrometheusNotification): CdNotificationConfig[] {
        return this.alertFormatter
          .convertToCustomAlerts(notification.alerts)
          .map((alert) => this.alertFormatter.convertAlertToNotification(alert));
      }
    }