]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/dashboard: use service call instead of form component
authorNizamudeen A <nia@redhat.com>
Fri, 16 Sep 2022 07:20:26 +0000 (12:50 +0530)
committerAashish Sharma <aasharma@redhat.com>
Mon, 19 Sep 2022 05:50:55 +0000 (11:20 +0530)
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 <nia@redhat.com>
src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/silence-form/silence-form.component.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/components/notifications-sidebar/notifications-sidebar.component.ts

index ca9efef0765c36c3f925acf8c0382a5d82e96daa..958039a31dc716fd7d48fdbff9100b7becd49fb7 100644 (file)
@@ -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);
-  }
 }
index 2062d53716871c25a89ce66e30aaeedf97e06057..39369ffd5bebc5005efa52f786f0a43e1fa81efb 100644 (file)
@@ -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) {