1 import { Component, OnInit } 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 = '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 implements OnInit {
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;
64 const selectionExpired = (selection: CdTableSelection) =>
65 selection.first() && selection.first().status.state === 'expired';
70 routerLink: () => this.urlBuilder.getCreate(),
71 canBePrimary: (selection: CdTableSelection) => !selection.hasSingleSelection,
72 name: this.actionLabels.CREATE
76 canBePrimary: (selection: CdTableSelection) =>
77 selection.hasSingleSelection && selectionExpired(selection),
78 disable: (selection: CdTableSelection) =>
79 !selection.hasSingleSelection ||
80 selection.first().cdExecuting ||
81 (selection.first().cdExecuting && selectionExpired(selection)) ||
82 !selectionExpired(selection),
84 routerLink: () => this.urlBuilder.getRecreate(this.selection.first().id),
85 name: this.actionLabels.RECREATE
90 canBePrimary: (selection: CdTableSelection) =>
91 selection.hasSingleSelection && !selectionExpired(selection),
92 disable: (selection: CdTableSelection) =>
93 !selection.hasSingleSelection ||
94 selection.first().cdExecuting ||
95 (selection.first().cdExecuting && !selectionExpired(selection)) ||
96 selectionExpired(selection),
97 routerLink: () => this.urlBuilder.getEdit(this.selection.first().id),
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,
171 actionDescription: this.actionLabels.EXPIRE,
172 submitActionObservable: () =>
173 new Observable((observer: Subscriber<any>) => {
174 this.prometheusService.expireSilence(id).subscribe(
176 this.notificationService.show(
177 NotificationType.success,
178 `${this.succeededLabels.EXPIRED} ${i18nSilence} ${id}`,
185 resp['application'] = applicationName;
186 observer.error(resp);