1 import { Component } from '@angular/core';
2 import { Validators } from '@angular/forms';
3 import { ActivatedRoute, Router } from '@angular/router';
5 import { I18n } from '@ngx-translate/i18n-polyfill';
6 import * as _ from 'lodash';
7 import * as moment from 'moment';
9 import { PrometheusService } from '../../../../shared/api/prometheus.service';
12 SucceededActionLabelsI18n
13 } from '../../../../shared/constants/app.constants';
14 import { Icons } from '../../../../shared/enum/icons.enum';
15 import { NotificationType } from '../../../../shared/enum/notification-type.enum';
16 import { CdFormBuilder } from '../../../../shared/forms/cd-form-builder';
17 import { CdFormGroup } from '../../../../shared/forms/cd-form-group';
18 import { CdValidators } from '../../../../shared/forms/cd-validators';
21 AlertmanagerSilenceMatcher,
22 AlertmanagerSilenceMatcherMatch
23 } from '../../../../shared/models/alertmanager-silence';
24 import { Permission } from '../../../../shared/models/permissions';
25 import { AlertmanagerAlert, PrometheusRule } from '../../../../shared/models/prometheus-alerts';
26 import { AuthStorageService } from '../../../../shared/services/auth-storage.service';
27 import { ModalService } from '../../../../shared/services/modal.service';
28 import { NotificationService } from '../../../../shared/services/notification.service';
29 import { PrometheusSilenceMatcherService } from '../../../../shared/services/prometheus-silence-matcher.service';
30 import { TimeDiffService } from '../../../../shared/services/time-diff.service';
31 import { SilenceMatcherModalComponent } from '../silence-matcher-modal/silence-matcher-modal.component';
34 selector: 'cd-prometheus-form',
35 templateUrl: './silence-form.component.html',
36 styleUrls: ['./silence-form.component.scss']
38 export class SilenceFormComponent {
40 permission: Permission;
42 rules: PrometheusRule[];
49 resource = this.i18n('silence');
51 matchers: AlertmanagerSilenceMatcher[] = [];
52 matcherMatch: AlertmanagerSilenceMatcherMatch = undefined;
55 tooltip: this.i18n('Attribute name'),
56 icon: this.icons.paragraph,
60 tooltip: this.i18n('Value'),
61 icon: this.icons.terminal,
65 tooltip: this.i18n('Regular expression'),
66 icon: this.icons.magic,
71 datetimeFormat = 'YYYY-MM-DD HH:mm';
75 private router: Router,
76 private authStorageService: AuthStorageService,
77 private formBuilder: CdFormBuilder,
78 private prometheusService: PrometheusService,
79 private notificationService: NotificationService,
80 private route: ActivatedRoute,
81 private timeDiff: TimeDiffService,
82 private modalService: ModalService,
83 private silenceMatcher: PrometheusSilenceMatcherService,
84 private actionLabels: ActionLabelsI18n,
85 private succeededLabels: SucceededActionLabelsI18n
98 private chooseMode() {
99 this.edit = this.router.url.startsWith('/monitoring/silences/edit');
100 this.recreate = this.router.url.startsWith('/monitoring/silences/recreate');
102 this.action = this.actionLabels.EDIT;
103 } else if (this.recreate) {
104 this.action = this.actionLabels.RECREATE;
106 this.action = this.actionLabels.CREATE;
110 private authenticate() {
111 this.permission = this.authStorageService.getPermissions().prometheus;
113 this.permission.read && (this.edit ? this.permission.update : this.permission.create);
115 this.router.navigate(['/404']);
119 private createForm() {
120 const formatValidator = CdValidators.custom('format', (expiresAt: string) => {
121 const result = expiresAt === '' || moment(expiresAt, this.datetimeFormat).isValid();
124 this.form = this.formBuilder.group(
126 startsAt: ['', [Validators.required, formatValidator]],
127 duration: ['2h', [Validators.min(1)]],
128 endsAt: ['', [Validators.required, formatValidator]],
129 createdBy: [this.authStorageService.getUsername(), [Validators.required]],
130 comment: [null, [Validators.required]]
133 validators: CdValidators.custom('matcherRequired', () => this.matchers.length === 0)
138 private setupDates() {
139 const now = moment().format(this.datetimeFormat);
140 this.form.silentSet('startsAt', now);
142 this.subscribeDateChanges();
145 private updateDate(updateStartDate?: boolean) {
147 this.form.getValue(updateStartDate ? 'endsAt' : 'startsAt'),
150 const next = this.timeDiff.calculateDate(date, this.form.getValue('duration'), updateStartDate);
152 const nextDate = moment(next).format(this.datetimeFormat);
153 this.form.silentSet(updateStartDate ? 'startsAt' : 'endsAt', nextDate);
157 private subscribeDateChanges() {
158 this.form.get('startsAt').valueChanges.subscribe(() => {
161 this.form.get('duration').valueChanges.subscribe(() => {
164 this.form.get('endsAt').valueChanges.subscribe(() => {
165 this.onDateChange(true);
169 private onDateChange(updateStartDate?: boolean) {
170 const startsAt = moment(this.form.getValue('startsAt'), this.datetimeFormat);
171 const endsAt = moment(this.form.getValue('endsAt'), this.datetimeFormat);
172 if (startsAt.isBefore(endsAt)) {
173 this.updateDuration();
175 this.updateDate(updateStartDate);
179 private updateDuration() {
180 const startsAt = moment(this.form.getValue('startsAt'), this.datetimeFormat).toDate();
181 const endsAt = moment(this.form.getValue('endsAt'), this.datetimeFormat).toDate();
182 this.form.silentSet('duration', this.timeDiff.calculateDuration(startsAt, endsAt));
187 this.getModeSpecificData();
191 this.prometheusService.ifPrometheusConfigured(
193 this.prometheusService.getRules().subscribe(
195 this.rules = groups['groups'].reduce(
196 (acc, group) => _.concat<PrometheusRule>(acc, group.rules),
201 this.prometheusService.disablePrometheusConfig();
207 this.notificationService.show(
208 NotificationType.info,
210 'Please add your Prometheus host to the dashboard configuration and refresh the page'
220 private getModeSpecificData() {
221 this.route.params.subscribe((params: { id: string }) => {
225 if (this.edit || this.recreate) {
226 this.prometheusService.getSilences(params).subscribe((silences) => {
227 this.fillFormWithSilence(silences[0]);
230 this.prometheusService.getAlerts(params).subscribe((alerts) => {
231 this.fillFormByAlert(alerts[0]);
237 private fillFormWithSilence(silence: AlertmanagerSilence) {
238 this.id = silence.id;
240 ['startsAt', 'endsAt'].forEach((attr) =>
241 this.form.silentSet(attr, moment(silence[attr]).format(this.datetimeFormat))
243 this.updateDuration();
245 ['createdBy', 'comment'].forEach((attr) => this.form.silentSet(attr, silence[attr]));
246 this.matchers = silence.matchers;
247 this.validateMatchers();
250 private validateMatchers() {
252 window.setTimeout(() => this.validateMatchers(), 100);
255 this.matcherMatch = this.silenceMatcher.multiMatch(this.matchers, this.rules);
256 this.form.markAsDirty();
257 this.form.updateValueAndValidity();
260 private fillFormByAlert(alert: AlertmanagerAlert) {
261 const labels = alert.labels;
262 Object.keys(labels).forEach((key) =>
271 private setMatcher(matcher: AlertmanagerSilenceMatcher, index?: number) {
272 if (_.isNumber(index)) {
273 this.matchers[index] = matcher;
275 this.matchers.push(matcher);
277 this.validateMatchers();
280 showMatcherModal(index?: number) {
281 const modalRef = this.modalService.show(SilenceMatcherModalComponent);
282 const modalComponent = modalRef.componentInstance as SilenceMatcherModalComponent;
283 modalComponent.rules = this.rules;
284 if (_.isNumber(index)) {
285 modalComponent.editMode = true;
286 modalComponent.preFillControls(this.matchers[index]);
288 modalComponent.submitAction.subscribe((matcher: AlertmanagerSilenceMatcher) => {
289 this.setMatcher(matcher, index);
293 deleteMatcher(index: number) {
294 this.matchers.splice(index, 1);
295 this.validateMatchers();
299 if (this.form.invalid) {
302 this.prometheusService.setSilence(this.getSubmitData()).subscribe(
304 this.router.navigate(['/monitoring/silences']);
305 this.notificationService.show(
306 NotificationType.success,
307 this.getNotificationTile(resp.body['silenceId']),
313 () => this.form.setErrors({ cdSubmitButton: true })
317 private getSubmitData(): AlertmanagerSilence {
318 const payload = this.form.value;
319 delete payload.duration;
320 payload.startsAt = moment(payload.startsAt, this.datetimeFormat).toISOString();
321 payload.endsAt = moment(payload.endsAt, this.datetimeFormat).toISOString();
322 payload.matchers = this.matchers;
324 payload.id = this.id;
329 private getNotificationTile(id: string) {
332 action = this.succeededLabels.EDITED;
333 } else if (this.recreate) {
334 action = this.succeededLabels.RECREATED;
336 action = this.succeededLabels.CREATED;
338 return `${action} ${this.resource} ${id}`;