1 import { Component, Inject } from '@angular/core';
3 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
4 import { SortDirection, SortPropDir } from '@swimlane/ngx-datatable';
5 import { Observable, Subscriber } from 'rxjs';
7 import { PrometheusService } from '~/app/shared/api/prometheus.service';
8 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
11 SucceededActionLabelsI18n
12 } from '~/app/shared/constants/app.constants';
13 import { CellTemplate } from '~/app/shared/enum/cell-template.enum';
14 import { Icons } from '~/app/shared/enum/icons.enum';
15 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
16 import { AlertmanagerSilence } from '~/app/shared/models/alertmanager-silence';
17 import { CdTableAction } from '~/app/shared/models/cd-table-action';
18 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
19 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
20 import { Permission } from '~/app/shared/models/permissions';
21 import { CdDatePipe } from '~/app/shared/pipes/cd-date.pipe';
22 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
23 import { ModalService } from '~/app/shared/services/modal.service';
24 import { NotificationService } from '~/app/shared/services/notification.service';
25 import { URLBuilderService } from '~/app/shared/services/url-builder.service';
26 import { PrometheusListHelper } from '../prometheus-list-helper';
28 const BASE_URL = 'monitoring/silences';
31 providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }],
32 selector: 'cd-silences-list',
33 templateUrl: './silence-list.component.html',
34 styleUrls: ['./silence-list.component.scss']
36 export class SilenceListComponent extends PrometheusListHelper {
37 silences: AlertmanagerSilence[] = [];
38 columns: CdTableColumn[];
39 tableActions: CdTableAction[];
40 permission: Permission;
41 selection = new CdTableSelection();
42 modalRef: NgbModalRef;
44 'badge badge-danger': 'active',
45 'badge badge-warning': 'pending',
46 'badge badge-default': 'expired'
48 sorts: SortPropDir[] = [{ prop: 'endsAt', dir: SortDirection.desc }];
51 private authStorageService: AuthStorageService,
52 private cdDatePipe: CdDatePipe,
53 private modalService: ModalService,
54 private notificationService: NotificationService,
55 private urlBuilder: URLBuilderService,
56 private actionLabels: ActionLabelsI18n,
57 private succeededLabels: SucceededActionLabelsI18n,
58 @Inject(PrometheusService) prometheusService: PrometheusService
60 super(prometheusService);
61 this.permission = this.authStorageService.getPermissions().prometheus;
62 const selectionExpired = (selection: CdTableSelection) =>
63 selection.first() && selection.first().status && selection.first().status.state === 'expired';
68 routerLink: () => this.urlBuilder.getCreate(),
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 name: this.actionLabels.RECREATE
88 canBePrimary: (selection: CdTableSelection) =>
89 selection.hasSingleSelection && !selectionExpired(selection),
90 disable: (selection: CdTableSelection) =>
91 !selection.hasSingleSelection ||
92 selection.first().cdExecuting ||
93 (selection.first().cdExecuting && !selectionExpired(selection)) ||
94 selectionExpired(selection),
95 routerLink: () => this.urlBuilder.getEdit(this.selection.first().id),
96 name: this.actionLabels.EDIT
101 canBePrimary: (selection: CdTableSelection) =>
102 selection.hasSingleSelection && !selectionExpired(selection),
103 disable: (selection: CdTableSelection) =>
104 !selection.hasSingleSelection ||
105 selection.first().cdExecuting ||
106 selectionExpired(selection),
107 click: () => this.expireSilence(),
108 name: this.actionLabels.EXPIRE
118 name: $localize`Created by`,
123 name: $localize`Started`,
125 pipe: this.cdDatePipe
128 name: $localize`Updated`,
130 pipe: this.cdDatePipe
133 name: $localize`Ends`,
135 pipe: this.cdDatePipe
138 name: $localize`Status`,
139 prop: 'status.state',
140 cellTransformation: CellTemplate.classAdding
146 this.prometheusService.ifAlertmanagerConfigured(() => {
147 this.prometheusService.getSilences().subscribe(
149 this.silences = silences;
152 this.prometheusService.disableAlertmanagerConfig();
158 updateSelection(selection: CdTableSelection) {
159 this.selection = selection;
163 const id = this.selection.first().id;
164 const i18nSilence = $localize`Silence`;
165 const applicationName = 'Prometheus';
166 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
167 itemDescription: i18nSilence,
169 actionDescription: this.actionLabels.EXPIRE,
170 submitActionObservable: () =>
171 new Observable((observer: Subscriber<any>) => {
172 this.prometheusService.expireSilence(id).subscribe(
174 this.notificationService.show(
175 NotificationType.success,
176 `${this.succeededLabels.EXPIRED} ${i18nSilence} ${id}`,
183 resp['application'] = applicationName;
184 observer.error(resp);