2 ChangeDetectionStrategy,
9 } from '@angular/core';
11 import { Mutex } from 'async-mutex';
12 import * as _ from 'lodash';
13 import * as moment from 'moment';
14 import { Subscription } from 'rxjs';
16 import { Icons } from '../../enum/icons.enum';
17 import { CdNotification } from '../../models/cd-notification';
18 import { ExecutingTask } from '../../models/executing-task';
19 import { FinishedTask } from '../../models/finished-task';
20 import { AuthStorageService } from '../../services/auth-storage.service';
21 import { NotificationService } from '../../services/notification.service';
22 import { PrometheusAlertService } from '../../services/prometheus-alert.service';
23 import { PrometheusNotificationService } from '../../services/prometheus-notification.service';
24 import { SummaryService } from '../../services/summary.service';
25 import { TaskMessageService } from '../../services/task-message.service';
28 selector: 'cd-notifications-sidebar',
29 templateUrl: './notifications-sidebar.component.html',
30 styleUrls: ['./notifications-sidebar.component.scss'],
31 changeDetection: ChangeDetectionStrategy.OnPush
33 export class NotificationsSidebarComponent implements OnInit, OnDestroy {
34 @HostBinding('class.active') isSidebarOpened = false;
36 notifications: CdNotification[];
37 private interval: number;
38 private timeout: number;
40 executingTasks: ExecutingTask[] = [];
42 private subs = new Subscription();
55 public notificationService: NotificationService,
56 private summaryService: SummaryService,
57 private taskMessageService: TaskMessageService,
58 private prometheusNotificationService: PrometheusNotificationService,
59 private authStorageService: AuthStorageService,
60 private prometheusAlertService: PrometheusAlertService,
61 private ngZone: NgZone,
62 private cdRef: ChangeDetectorRef
64 this.notifications = [];
68 window.clearInterval(this.interval);
69 window.clearTimeout(this.timeout);
70 this.subs.unsubscribe();
74 this.last_task = window.localStorage.getItem('last_task');
76 const permissions = this.authStorageService.getPermissions();
77 if (permissions.prometheus.read && permissions.configOpt.read) {
78 this.triggerPrometheusAlerts();
79 this.ngZone.runOutsideAngular(() => {
80 this.interval = window.setInterval(() => {
81 this.ngZone.run(() => {
82 this.triggerPrometheusAlerts();
89 this.notificationService.data$.subscribe((notifications: CdNotification[]) => {
90 this.notifications = _.orderBy(notifications, ['timestamp'], ['desc']);
91 this.cdRef.detectChanges();
96 this.notificationService.sidebarSubject.subscribe((forceClose) => {
98 this.isSidebarOpened = false;
100 this.isSidebarOpened = !this.isSidebarOpened;
103 window.clearTimeout(this.timeout);
104 this.timeout = window.setTimeout(() => {
105 this.cdRef.detectChanges();
111 this.summaryService.subscribe((data: any) => {
115 this._handleTasks(data.executing_tasks);
117 this.mutex.acquire().then((release) => {
120 (task: FinishedTask) => !this.last_task || moment(task.end_time).isAfter(this.last_task)
121 ).forEach((task) => {
122 const config = this.notificationService.finishedTaskToNotification(task, task.success);
123 const notification = new CdNotification(config);
124 notification.timestamp = task.end_time;
125 notification.duration = task.duration;
127 if (!this.last_task || moment(task.end_time).isAfter(this.last_task)) {
128 this.last_task = task.end_time;
129 window.localStorage.setItem('last_task', this.last_task);
132 this.notificationService.save(notification);
135 this.cdRef.detectChanges();
143 _handleTasks(executingTasks: ExecutingTask[]) {
144 for (const excutingTask of executingTasks) {
145 excutingTask.description = this.taskMessageService.getRunningTitle(excutingTask);
147 this.executingTasks = executingTasks;
150 private triggerPrometheusAlerts() {
151 this.prometheusAlertService.refresh();
152 this.prometheusNotificationService.refresh();
156 this.notificationService.removeAll();
159 remove(index: number) {
160 this.notificationService.remove(index);
164 this.isSidebarOpened = false;
167 trackByFn(index: number) {