1 import { Component, Inject } from '@angular/core';
3 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
4 import { I18n } from '@ngx-translate/i18n-polyfill';
5 import { SortDirection, SortPropDir } from '@swimlane/ngx-datatable';
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 { CephReleaseNamePipe } from '../../../../shared/pipes/ceph-release-name.pipe';
24 import { AuthStorageService } from '../../../../shared/services/auth-storage.service';
25 import { ModalService } from '../../../../shared/services/modal.service';
26 import { NotificationService } from '../../../../shared/services/notification.service';
27 import { SummaryService } from '../../../../shared/services/summary.service';
28 import { URLBuilderService } from '../../../../shared/services/url-builder.service';
29 import { PrometheusListHelper } from '../prometheus-list-helper';
31 const BASE_URL = 'monitoring/silences';
34 providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }],
35 selector: 'cd-silences-list',
36 templateUrl: './silence-list.component.html',
37 styleUrls: ['./silence-list.component.scss']
39 export class SilenceListComponent extends PrometheusListHelper {
40 silences: AlertmanagerSilence[] = [];
41 columns: CdTableColumn[];
42 tableActions: CdTableAction[];
43 permission: Permission;
44 selection = new CdTableSelection();
45 modalRef: NgbModalRef;
47 'badge badge-danger': 'active',
48 'badge badge-warning': 'pending',
49 'badge badge-default': 'expired'
51 sorts: SortPropDir[] = [{ prop: 'endsAt', dir: SortDirection.desc }];
54 private authStorageService: AuthStorageService,
56 private cdDatePipe: CdDatePipe,
57 private modalService: ModalService,
58 private notificationService: NotificationService,
59 private urlBuilder: URLBuilderService,
60 private actionLabels: ActionLabelsI18n,
61 private succeededLabels: SucceededActionLabelsI18n,
62 @Inject(PrometheusService) prometheusService: PrometheusService,
63 @Inject(SummaryService) summaryService: SummaryService,
64 @Inject(CephReleaseNamePipe) cephReleaseNamePipe: CephReleaseNamePipe
66 super(prometheusService, summaryService, cephReleaseNamePipe);
67 this.permission = this.authStorageService.getPermissions().prometheus;
68 const selectionExpired = (selection: CdTableSelection) =>
69 selection.first() && selection.first().status && selection.first().status.state === 'expired';
74 routerLink: () => this.urlBuilder.getCreate(),
75 canBePrimary: (selection: CdTableSelection) => !selection.hasSingleSelection,
76 name: this.actionLabels.CREATE
80 canBePrimary: (selection: CdTableSelection) =>
81 selection.hasSingleSelection && selectionExpired(selection),
82 disable: (selection: CdTableSelection) =>
83 !selection.hasSingleSelection ||
84 selection.first().cdExecuting ||
85 (selection.first().cdExecuting && selectionExpired(selection)) ||
86 !selectionExpired(selection),
88 routerLink: () => this.urlBuilder.getRecreate(this.selection.first().id),
89 name: this.actionLabels.RECREATE
94 canBePrimary: (selection: CdTableSelection) =>
95 selection.hasSingleSelection && !selectionExpired(selection),
96 disable: (selection: CdTableSelection) =>
97 !selection.hasSingleSelection ||
98 selection.first().cdExecuting ||
99 (selection.first().cdExecuting && !selectionExpired(selection)) ||
100 selectionExpired(selection),
101 routerLink: () => this.urlBuilder.getEdit(this.selection.first().id),
102 name: this.actionLabels.EDIT
105 permission: 'delete',
107 canBePrimary: (selection: CdTableSelection) =>
108 selection.hasSingleSelection && !selectionExpired(selection),
109 disable: (selection: CdTableSelection) =>
110 !selection.hasSingleSelection ||
111 selection.first().cdExecuting ||
112 selectionExpired(selection),
113 click: () => this.expireSilence(),
114 name: this.actionLabels.EXPIRE
119 name: this.i18n('ID'),
124 name: this.i18n('Created by'),
129 name: this.i18n('Started'),
131 pipe: this.cdDatePipe
134 name: this.i18n('Updated'),
136 pipe: this.cdDatePipe
139 name: this.i18n('Ends'),
141 pipe: this.cdDatePipe
144 name: this.i18n('Status'),
145 prop: 'status.state',
146 cellTransformation: CellTemplate.classAdding
152 this.prometheusService.ifAlertmanagerConfigured(() => {
153 this.prometheusService.getSilences().subscribe(
155 this.silences = silences;
158 this.prometheusService.disableAlertmanagerConfig();
164 updateSelection(selection: CdTableSelection) {
165 this.selection = selection;
169 const id = this.selection.first().id;
170 const i18nSilence = this.i18n('Silence');
171 const applicationName = 'Prometheus';
172 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
173 itemDescription: i18nSilence,
175 actionDescription: this.actionLabels.EXPIRE,
176 submitActionObservable: () =>
177 new Observable((observer: Subscriber<any>) => {
178 this.prometheusService.expireSilence(id).subscribe(
180 this.notificationService.show(
181 NotificationType.success,
182 `${this.succeededLabels.EXPIRED} ${i18nSilence} ${id}`,
189 resp['application'] = applicationName;
190 observer.error(resp);