]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
daba3e2a8d61c30109807201835f951e8a03281c
[ceph-ci.git] /
1 import { Component, Inject } from '@angular/core';
2
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';
7
8 import { PrometheusService } from '../../../../shared/api/prometheus.service';
9 import { CriticalConfirmationModalComponent } from '../../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
10 import {
11   ActionLabelsI18n,
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';
30
31 const BASE_URL = 'monitoring/silences';
32
33 @Component({
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']
38 })
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;
46   customCss = {
47     'badge badge-danger': 'active',
48     'badge badge-warning': 'pending',
49     'badge badge-default': 'expired'
50   };
51   sorts: SortPropDir[] = [{ prop: 'endsAt', dir: SortDirection.desc }];
52
53   constructor(
54     private authStorageService: AuthStorageService,
55     private i18n: I18n,
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
65   ) {
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';
70     this.tableActions = [
71       {
72         permission: 'create',
73         icon: Icons.add,
74         routerLink: () => this.urlBuilder.getCreate(),
75         canBePrimary: (selection: CdTableSelection) => !selection.hasSingleSelection,
76         name: this.actionLabels.CREATE
77       },
78       {
79         permission: '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),
87         icon: Icons.copy,
88         routerLink: () => this.urlBuilder.getRecreate(this.selection.first().id),
89         name: this.actionLabels.RECREATE
90       },
91       {
92         permission: 'update',
93         icon: Icons.edit,
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
103       },
104       {
105         permission: 'delete',
106         icon: Icons.trash,
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
115       }
116     ];
117     this.columns = [
118       {
119         name: this.i18n('ID'),
120         prop: 'id',
121         flexGrow: 3
122       },
123       {
124         name: this.i18n('Created by'),
125         prop: 'createdBy',
126         flexGrow: 2
127       },
128       {
129         name: this.i18n('Started'),
130         prop: 'startsAt',
131         pipe: this.cdDatePipe
132       },
133       {
134         name: this.i18n('Updated'),
135         prop: 'updatedAt',
136         pipe: this.cdDatePipe
137       },
138       {
139         name: this.i18n('Ends'),
140         prop: 'endsAt',
141         pipe: this.cdDatePipe
142       },
143       {
144         name: this.i18n('Status'),
145         prop: 'status.state',
146         cellTransformation: CellTemplate.classAdding
147       }
148     ];
149   }
150
151   refresh() {
152     this.prometheusService.ifAlertmanagerConfigured(() => {
153       this.prometheusService.getSilences().subscribe(
154         (silences) => {
155           this.silences = silences;
156         },
157         () => {
158           this.prometheusService.disableAlertmanagerConfig();
159         }
160       );
161     });
162   }
163
164   updateSelection(selection: CdTableSelection) {
165     this.selection = selection;
166   }
167
168   expireSilence() {
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,
174       itemNames: [id],
175       actionDescription: this.actionLabels.EXPIRE,
176       submitActionObservable: () =>
177         new Observable((observer: Subscriber<any>) => {
178           this.prometheusService.expireSilence(id).subscribe(
179             () => {
180               this.notificationService.show(
181                 NotificationType.success,
182                 `${this.succeededLabels.EXPIRED} ${i18nSilence} ${id}`,
183                 undefined,
184                 undefined,
185                 applicationName
186               );
187             },
188             (resp) => {
189               resp['application'] = applicationName;
190               observer.error(resp);
191             },
192             () => {
193               observer.complete();
194               this.refresh();
195             }
196           );
197         })
198     });
199   }
200 }