]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
d1f2dde88bd318fd4266c9bb2724667e2c4d5d3b
[ceph.git] /
1 import { Component, Inject } from '@angular/core';
2
3 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
4 import { SortDirection, SortPropDir } from '@swimlane/ngx-datatable';
5 import { Observable, Subscriber } from 'rxjs';
6
7 import { PrometheusService } from '~/app/shared/api/prometheus.service';
8 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
9 import {
10   ActionLabelsI18n,
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';
27
28 const BASE_URL = 'monitoring/silences';
29
30 @Component({
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']
35 })
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;
43   customCss = {
44     'badge badge-danger': 'active',
45     'badge badge-warning': 'pending',
46     'badge badge-default': 'expired'
47   };
48   sorts: SortPropDir[] = [{ prop: 'endsAt', dir: SortDirection.desc }];
49
50   constructor(
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
59   ) {
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';
64     this.tableActions = [
65       {
66         permission: 'create',
67         icon: Icons.add,
68         routerLink: () => this.urlBuilder.getCreate(),
69         canBePrimary: (selection: CdTableSelection) => !selection.hasSingleSelection,
70         name: this.actionLabels.CREATE
71       },
72       {
73         permission: '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),
81         icon: Icons.copy,
82         routerLink: () => this.urlBuilder.getRecreate(this.selection.first().id),
83         name: this.actionLabels.RECREATE
84       },
85       {
86         permission: 'update',
87         icon: Icons.edit,
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
97       },
98       {
99         permission: 'delete',
100         icon: Icons.trash,
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
109       }
110     ];
111     this.columns = [
112       {
113         name: $localize`ID`,
114         prop: 'id',
115         flexGrow: 3
116       },
117       {
118         name: $localize`Created by`,
119         prop: 'createdBy',
120         flexGrow: 2
121       },
122       {
123         name: $localize`Started`,
124         prop: 'startsAt',
125         pipe: this.cdDatePipe
126       },
127       {
128         name: $localize`Updated`,
129         prop: 'updatedAt',
130         pipe: this.cdDatePipe
131       },
132       {
133         name: $localize`Ends`,
134         prop: 'endsAt',
135         pipe: this.cdDatePipe
136       },
137       {
138         name: $localize`Status`,
139         prop: 'status.state',
140         cellTransformation: CellTemplate.classAdding
141       }
142     ];
143   }
144
145   refresh() {
146     this.prometheusService.ifAlertmanagerConfigured(() => {
147       this.prometheusService.getSilences().subscribe(
148         (silences) => {
149           this.silences = silences;
150         },
151         () => {
152           this.prometheusService.disableAlertmanagerConfig();
153         }
154       );
155     });
156   }
157
158   updateSelection(selection: CdTableSelection) {
159     this.selection = selection;
160   }
161
162   expireSilence() {
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,
168       itemNames: [id],
169       actionDescription: this.actionLabels.EXPIRE,
170       submitActionObservable: () =>
171         new Observable((observer: Subscriber<any>) => {
172           this.prometheusService.expireSilence(id).subscribe(
173             () => {
174               this.notificationService.show(
175                 NotificationType.success,
176                 `${this.succeededLabels.EXPIRED} ${i18nSilence} ${id}`,
177                 undefined,
178                 undefined,
179                 applicationName
180               );
181             },
182             (resp) => {
183               resp['application'] = applicationName;
184               observer.error(resp);
185             },
186             () => {
187               observer.complete();
188               this.refresh();
189             }
190           );
191         })
192     });
193   }
194 }