]>
Commit | Line | Data |
---|---|---|
494da23a TL |
1 | import { Component, OnInit } from '@angular/core'; |
2 | import { I18n } from '@ngx-translate/i18n-polyfill'; | |
3 | import { SortDirection, SortPropDir } from '@swimlane/ngx-datatable'; | |
4 | ||
5 | import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal'; | |
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 { AuthStorageService } from '../../../../shared/services/auth-storage.service'; | |
24 | import { NotificationService } from '../../../../shared/services/notification.service'; | |
25 | import { URLBuilderService } from '../../../../shared/services/url-builder.service'; | |
26 | ||
27 | const BASE_URL = 'silence'; | |
28 | ||
29 | @Component({ | |
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'] | |
34 | }) | |
35 | export class SilenceListComponent implements OnInit { | |
36 | silences: AlertmanagerSilence[] = []; | |
37 | columns: CdTableColumn[]; | |
38 | tableActions: CdTableAction[]; | |
39 | permission: Permission; | |
40 | selection = new CdTableSelection(); | |
41 | modalRef: BsModalRef; | |
42 | customCss = { | |
43 | 'label label-danger': 'active', | |
44 | 'label label-warning': 'pending', | |
45 | 'label label-default': 'expired' | |
46 | }; | |
47 | sorts: SortPropDir[] = [{ prop: 'endsAt', dir: SortDirection.desc }]; | |
48 | ||
49 | constructor( | |
50 | private authStorageService: AuthStorageService, | |
51 | private i18n: I18n, | |
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 | |
59 | ) { | |
60 | this.permission = this.authStorageService.getPermissions().prometheus; | |
61 | } | |
62 | ||
63 | ngOnInit() { | |
64 | const selectionExpired = (selection: CdTableSelection) => | |
65 | selection.first() && selection.first().status.state === 'expired'; | |
66 | this.tableActions = [ | |
67 | { | |
68 | permission: 'create', | |
69 | icon: Icons.add, | |
70 | routerLink: () => this.urlBuilder.getCreate(), | |
71 | canBePrimary: (selection: CdTableSelection) => !selection.hasSingleSelection, | |
72 | name: this.actionLabels.CREATE | |
73 | }, | |
74 | { | |
75 | permission: '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), | |
83 | icon: Icons.copy, | |
84 | routerLink: () => this.urlBuilder.getRecreate(this.selection.first().id), | |
85 | name: this.actionLabels.RECREATE | |
86 | }, | |
87 | { | |
88 | permission: 'update', | |
89 | icon: Icons.edit, | |
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 | |
99 | }, | |
100 | { | |
101 | permission: 'delete', | |
102 | icon: Icons.trash, | |
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 | |
111 | } | |
112 | ]; | |
113 | this.columns = [ | |
114 | { | |
115 | name: this.i18n('ID'), | |
116 | prop: 'id', | |
117 | flexGrow: 3 | |
118 | }, | |
119 | { | |
120 | name: this.i18n('Created by'), | |
121 | prop: 'createdBy', | |
122 | flexGrow: 2 | |
123 | }, | |
124 | { | |
125 | name: this.i18n('Started'), | |
126 | prop: 'startsAt', | |
127 | pipe: this.cdDatePipe | |
128 | }, | |
129 | { | |
130 | name: this.i18n('Updated'), | |
131 | prop: 'updatedAt', | |
132 | pipe: this.cdDatePipe | |
133 | }, | |
134 | { | |
135 | name: this.i18n('Ends'), | |
136 | prop: 'endsAt', | |
137 | pipe: this.cdDatePipe | |
138 | }, | |
139 | { | |
140 | name: this.i18n('Status'), | |
141 | prop: 'status.state', | |
142 | cellTransformation: CellTemplate.classAdding | |
143 | } | |
144 | ]; | |
145 | } | |
146 | ||
147 | refresh() { | |
148 | this.prometheusService.ifAlertmanagerConfigured(() => { | |
149 | this.prometheusService.getSilences().subscribe( | |
150 | (silences) => { | |
151 | this.silences = silences; | |
152 | }, | |
153 | () => { | |
154 | this.prometheusService.disableAlertmanagerConfig(); | |
155 | } | |
156 | ); | |
157 | }); | |
158 | } | |
159 | ||
160 | updateSelection(selection: CdTableSelection) { | |
161 | this.selection = selection; | |
162 | } | |
163 | ||
164 | expireSilence() { | |
165 | const id = this.selection.first().id; | |
166 | const i18nSilence = this.i18n('Silence'); | |
167 | const applicationName = 'Prometheus'; | |
168 | this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, { | |
169 | initialState: { | |
170 | itemDescription: i18nSilence, | |
eafe8130 | 171 | itemNames: [id], |
494da23a TL |
172 | actionDescription: this.actionLabels.EXPIRE, |
173 | submitActionObservable: () => | |
174 | new Observable((observer: Subscriber<any>) => { | |
175 | this.prometheusService.expireSilence(id).subscribe( | |
176 | () => { | |
177 | this.notificationService.show( | |
178 | NotificationType.success, | |
179 | `${this.succeededLabels.EXPIRED} ${i18nSilence} ${id}`, | |
180 | undefined, | |
181 | undefined, | |
182 | applicationName | |
183 | ); | |
184 | }, | |
185 | (resp) => { | |
186 | resp['application'] = applicationName; | |
187 | observer.error(resp); | |
188 | }, | |
189 | () => { | |
190 | observer.complete(); | |
191 | this.refresh(); | |
192 | } | |
193 | ); | |
194 | }) | |
195 | } | |
196 | }); | |
197 | } | |
198 | } |