All files / src/app/core/navigation/notifications notifications.component.ts

96.97% Statements 32/33
77.27% Branches 17/22
90% Functions 9/10
96.67% Lines 29/30

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 595x   5x     5x 5x 5x 5x             5x         4x 4x 4x 4x 4x   4x     5x 4x     5x 4x 2x 2x 2x 4x 4x         4x 4x       5x 6x 6x     5x     5x  
import { Component, NgZone, OnDestroy, OnInit } from '@angular/core';
 
import * as _ from 'lodash';
 
import { CdNotification } from '../../../shared/models/cd-notification';
import { AuthStorageService } from '../../../shared/services/auth-storage.service';
import { NotificationService } from '../../../shared/services/notification.service';
import { PrometheusAlertService } from '../../../shared/services/prometheus-alert.service';
import { PrometheusNotificationService } from '../../../shared/services/prometheus-notification.service';
 
@Component({
  selector: 'cd-notifications',
  template: require('./notifications.component.html'),
  styles: []
})
export class NotificationsComponent implements OnInit, OnDestroy {
  notifications: CdNotification[];
  private interval: number;
 
  constructor(
    public notificationService: NotificationService,
    private prometheusNotificationService: PrometheusNotificationService,
    private authStorageService: AuthStorageService,
    private prometheusAlertService: PrometheusAlertService,
    private ngZone: NgZone
  ) {
    this.notifications = [];
  }
 
  ngOnDestroy() {
    window.clearInterval(this.interval);
  }
 
  ngOnInit() {
    if (this.authStorageService.getPermissions().prometheus.read) {
      this.triggerPrometheusAlerts();
      this.ngZone.runOutsideAngular(() => {
        this.interval = window.setInterval(() => {
          this.ngZone.run(() => {
            this.triggerPrometheusAlerts();
          });
        }, 5000);
      });
    }
    this.notificationService.data$.subscribe((notifications: CdNotification[]) => {
      this.notifications = _.orderBy(notifications, ['timestamp'], ['desc']);
    });
  }
 
  private triggerPrometheusAlerts() {
    this.prometheusAlertService.refresh();
    this.prometheusNotificationService.refresh();
  }
 
  removeAll() {
    this.notificationService.removeAll();
  }
}