From d30d92298a0122ae04855fe290843046cb106a81 Mon Sep 17 00:00:00 2001 From: Nizamudeen A Date: Fri, 16 Sep 2022 12:50:26 +0530 Subject: [PATCH] mgr/dashboard: use service call instead of form component For creating the silence from the notification sidebar, instead of using the silence form which will require initializing the whole component on the landing page, we can just call the prometheus service and pass on the required data to the service call. This will fix showing the `Prometheus not configured` error everytime we visit the landing page when the prometheus is not configured Fixes: https://tracker.ceph.com/issues/57576 Signed-off-by: Nizamudeen A --- .../silence-form/silence-form.component.ts | 13 ------- .../notifications-sidebar.component.ts | 38 +++++++++++++++++-- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/silence-form/silence-form.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/silence-form/silence-form.component.ts index ca9efef0765..958039a31dc 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/silence-form/silence-form.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/silence-form/silence-form.component.ts @@ -346,17 +346,4 @@ export class SilenceFormComponent { } return `${action} ${this.resource} for ${msg.slice(0, -1)}`; } - - createSilenceFromNotification(data: any) { - this.isNavigate = false; - this.setMatcher({ - name: 'alertname', - value: data['title'].split(' ')[0], - isRegex: false - }); - this.createForm(); - this.form.get('comment').setValue('Silence created from the alert notification'); - this.setupDates(); - this.submit(data); - } } diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/components/notifications-sidebar/notifications-sidebar.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/notifications-sidebar/notifications-sidebar.component.ts index 2062d537168..39369ffd5be 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/components/notifications-sidebar/notifications-sidebar.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/notifications-sidebar/notifications-sidebar.component.ts @@ -13,11 +13,14 @@ import _ from 'lodash'; import moment from 'moment'; import { Subscription } from 'rxjs'; -import { SilenceFormComponent } from '~/app/ceph/cluster/prometheus/silence-form/silence-form.component'; import { PrometheusService } from '~/app/shared/api/prometheus.service'; import { SucceededActionLabelsI18n } from '~/app/shared/constants/app.constants'; import { Icons } from '~/app/shared/enum/icons.enum'; import { NotificationType } from '~/app/shared/enum/notification-type.enum'; +import { + AlertmanagerSilence, + AlertmanagerSilenceMatcher +} from '~/app/shared/models/alertmanager-silence'; import { CdNotification } from '~/app/shared/models/cd-notification'; import { ExecutingTask } from '~/app/shared/models/executing-task'; import { FinishedTask } from '~/app/shared/models/finished-task'; @@ -29,7 +32,6 @@ import { SummaryService } from '~/app/shared/services/summary.service'; import { TaskMessageService } from '~/app/shared/services/task-message.service'; @Component({ - providers: [SilenceFormComponent], selector: 'cd-notifications-sidebar', templateUrl: './notifications-sidebar.component.html', styleUrls: ['./notifications-sidebar.component.scss'], @@ -65,7 +67,6 @@ export class NotificationsSidebarComponent implements OnInit, OnDestroy { private authStorageService: AuthStorageService, private prometheusAlertService: PrometheusAlertService, private prometheusService: PrometheusService, - private silenceFormComponent: SilenceFormComponent, private ngZone: NgZone, private cdRef: ChangeDetectorRef ) { @@ -174,8 +175,37 @@ export class NotificationsSidebarComponent implements OnInit, OnDestroy { } silence(data: CdNotification) { + const datetimeFormat = 'YYYY-MM-DD HH:mm'; + const resource = $localize`silence`; + const matcher: AlertmanagerSilenceMatcher = { + name: 'alertname', + value: data['title'].split(' ')[0], + isRegex: false + }; + const silencePayload: AlertmanagerSilence = { + matchers: [matcher], + startsAt: moment(moment().format(datetimeFormat)).toISOString(), + endsAt: moment(moment().add(2, 'hours').format(datetimeFormat)).toISOString(), + createdBy: this.authStorageService.getUsername(), + comment: 'Silence created from the alert notification' + }; + let msg = ''; + data.alertSilenced = true; - this.silenceFormComponent.createSilenceFromNotification(data); + msg = msg.concat(` ${matcher.name} - ${matcher.value},`); + const title = `${this.succeededLabels.CREATED} ${resource} for ${msg.slice(0, -1)}`; + this.prometheusService.setSilence(silencePayload).subscribe((resp) => { + if (data) { + data.silenceId = resp.body['silenceId']; + } + this.notificationService.show( + NotificationType.success, + title, + undefined, + undefined, + 'Prometheus' + ); + }); } expire(data: CdNotification) { -- 2.39.5