1 import { Component } from '@angular/core';
2 import { I18n } from '@ngx-translate/i18n-polyfill';
3 import { SortDirection, SortPropDir } from '@swimlane/ngx-datatable';
5 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
6 import { Observable, Subscriber } from 'rxjs';
8 import { PrometheusService } from '../../../../shared/api/prometheus.service';
9 import { CriticalConfirmationModalComponent } from '../../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
12 SucceededActionLabelsI18n
13 } from '../../../../shared/constants/app.constants';
14 import { CellTemplate } from '../../../../shared/enum/cell-template.enum';
15 import { Icons } from '../../../../shared/enum/icons.enum';
16 import { NotificationType } from '../../../../shared/enum/notification-type.enum';
17 import { AlertmanagerSilence } from '../../../../shared/models/alertmanager-silence';
18 import { CdTableAction } from '../../../../shared/models/cd-table-action';
19 import { CdTableColumn } from '../../../../shared/models/cd-table-column';
20 import { CdTableSelection } from '../../../../shared/models/cd-table-selection';
21 import { Permission } from '../../../../shared/models/permissions';
22 import { CdDatePipe } from '../../../../shared/pipes/cd-date.pipe';
23 import { AuthStorageService } from '../../../../shared/services/auth-storage.service';
24 import { NotificationService } from '../../../../shared/services/notification.service';
25 import { URLBuilderService } from '../../../../shared/services/url-builder.service';
27 const BASE_URL = 'monitoring/silence';
30 providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }],
31 selector: 'cd-silences-list',
32 templateUrl: './silence-list.component.html',
33 styleUrls: ['./silence-list.component.scss']
35 export class SilenceListComponent {
36 silences: AlertmanagerSilence[] = [];
37 columns: CdTableColumn[];
38 tableActions: CdTableAction[];
39 permission: Permission;
40 selection = new CdTableSelection();
43 'badge badge-danger': 'active',
44 'badge badge-warning': 'pending',
45 'badge badge-default': 'expired'
47 sorts: SortPropDir[] = [{ prop: 'endsAt', dir: SortDirection.desc }];
50 private authStorageService: AuthStorageService,
52 private cdDatePipe: CdDatePipe,
53 private prometheusService: PrometheusService,
54 private modalService: BsModalService,
55 private notificationService: NotificationService,
56 private urlBuilder: URLBuilderService,
57 private actionLabels: ActionLabelsI18n,
58 private succeededLabels: SucceededActionLabelsI18n
60 this.permission = this.authStorageService.getPermissions().prometheus;
61 const selectionExpired = (selection: CdTableSelection) =>
62 selection.first() && selection.first().status && selection.first().status.state === 'expired';
67 routerLink: () => this.urlBuilder.getCreate(),
68 preserveFragment: true,
69 canBePrimary: (selection: CdTableSelection) => !selection.hasSingleSelection,
70 name: this.actionLabels.CREATE
74 canBePrimary: (selection: CdTableSelection) =>
75 selection.hasSingleSelection && selectionExpired(selection),
76 disable: (selection: CdTableSelection) =>
77 !selection.hasSingleSelection ||
78 selection.first().cdExecuting ||
79 (selection.first().cdExecuting && selectionExpired(selection)) ||
80 !selectionExpired(selection),
82 routerLink: () => this.urlBuilder.getRecreate(this.selection.first().id),
83 preserveFragment: true,
84 name: this.actionLabels.RECREATE
89 canBePrimary: (selection: CdTableSelection) =>
90 selection.hasSingleSelection && !selectionExpired(selection),
91 disable: (selection: CdTableSelection) =>
92 !selection.hasSingleSelection ||
93 selection.first().cdExecuting ||
94 (selection.first().cdExecuting && !selectionExpired(selection)) ||
95 selectionExpired(selection),
96 routerLink: () => this.urlBuilder.getEdit(this.selection.first().id),
97 preserveFragment: true,
98 name: this.actionLabels.EDIT
101 permission: 'delete',
103 canBePrimary: (selection: CdTableSelection) =>
104 selection.hasSingleSelection && !selectionExpired(selection),
105 disable: (selection: CdTableSelection) =>
106 !selection.hasSingleSelection ||
107 selection.first().cdExecuting ||
108 selectionExpired(selection),
109 click: () => this.expireSilence(),
110 name: this.actionLabels.EXPIRE
115 name: this.i18n('ID'),
120 name: this.i18n('Created by'),
125 name: this.i18n('Started'),
127 pipe: this.cdDatePipe
130 name: this.i18n('Updated'),
132 pipe: this.cdDatePipe
135 name: this.i18n('Ends'),
137 pipe: this.cdDatePipe
140 name: this.i18n('Status'),
141 prop: 'status.state',
142 cellTransformation: CellTemplate.classAdding
148 this.prometheusService.ifAlertmanagerConfigured(() => {
149 this.prometheusService.getSilences().subscribe(
151 this.silences = silences;
154 this.prometheusService.disableAlertmanagerConfig();
160 updateSelection(selection: CdTableSelection) {
161 this.selection = selection;
165 const id = this.selection.first().id;
166 const i18nSilence = this.i18n('Silence');
167 const applicationName = 'Prometheus';
168 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
170 itemDescription: i18nSilence,
172 actionDescription: this.actionLabels.EXPIRE,
173 submitActionObservable: () =>
174 new Observable((observer: Subscriber<any>) => {
175 this.prometheusService.expireSilence(id).subscribe(
177 this.notificationService.show(
178 NotificationType.success,
179 `${this.succeededLabels.EXPIRED} ${i18nSilence} ${id}`,
186 resp['application'] = applicationName;
187 observer.error(resp);