]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: Stop rules api being polled on every page 65106/head
authorAfreen Misbah <afreen@ibm.com>
Wed, 6 Aug 2025 07:37:16 +0000 (13:07 +0530)
committerAfreen Misbah <afreen@ibm.com>
Mon, 25 Aug 2025 15:15:58 +0000 (20:45 +0530)
- /rules ar epolled every 5 seconds on every page
- it is only required for alerts page where full rules list is shown in `Alerts` tab
- also added observable for getting rules instead of plain array

Signed-off-by: Afreen Misbah <afreen@ibm.com>
(cherry picked from commit df984d72df1370181328dc3ec30a22619841c185)

src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/rules-list/rules-list.component.html
src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/rules-list/rules-list.component.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.ts

index 7296f669d000a5e6f536f2846d12e35c3492f952..c602ac1c5808c5c903233a19202b9cbc36e0a5f1 100644 (file)
@@ -7,7 +7,7 @@
   the &nbsp;<cd-doc section="prometheus"></cd-doc>.</cd-alert-panel>
 
 <cd-table *ngIf="isPrometheusConfigured"
-          [data]="prometheusAlertService.rules"
+          [data]="rules"
           [columns]="columns"
           [selectionType]="'single'"
           [hasDetails]="true"
index e2f36b7edaae0fbad1c2c83af77d6c88a8b90b54..19b27a18edffe3c019d057331606e04f38a6be7b 100644 (file)
@@ -1,6 +1,7 @@
-import { Component, Inject, OnInit } from '@angular/core';
+import { Component, Inject, OnInit, OnDestroy } from '@angular/core';
 
 import _ from 'lodash';
+import { Subscription } from 'rxjs';
 
 import { PrometheusService } from '~/app/shared/api/prometheus.service';
 import { CellTemplate } from '~/app/shared/enum/cell-template.enum';
@@ -16,10 +17,11 @@ import { PrometheusAlertService } from '~/app/shared/services/prometheus-alert.s
   templateUrl: './rules-list.component.html',
   styleUrls: ['./rules-list.component.scss']
 })
-export class RulesListComponent extends PrometheusListHelper implements OnInit {
+export class RulesListComponent extends PrometheusListHelper implements OnInit, OnDestroy {
   columns: CdTableColumn[];
   declare expandedRow: PrometheusRule;
   selection = new CdTableSelection();
+  rules: PrometheusRule[] = [];
 
   /**
    * Hide active alerts in details of alerting rules as they are already shown
@@ -27,6 +29,7 @@ export class RulesListComponent extends PrometheusListHelper implements OnInit {
    * always supposed to be 'alerting'.
    */
   hideKeys = ['alerts', 'type'];
+  rulesSubscription: Subscription;
 
   constructor(
     public prometheusAlertService: PrometheusAlertService,
@@ -37,6 +40,10 @@ export class RulesListComponent extends PrometheusListHelper implements OnInit {
 
   ngOnInit() {
     super.ngOnInit();
+    this.prometheusAlertService.getRules();
+    this.rulesSubscription = this.prometheusAlertService.rules$.subscribe((rules) => {
+      this.rules = rules;
+    });
     this.columns = [
       { prop: 'name', name: $localize`Name`, cellClass: 'fw-bold', flexGrow: 2 },
       {
@@ -66,4 +73,8 @@ export class RulesListComponent extends PrometheusListHelper implements OnInit {
   updateSelection(selection: CdTableSelection) {
     this.selection = selection;
   }
+
+  ngOnDestroy() {
+    this.rulesSubscription.unsubscribe();
+  }
 }
index aa3160b304cca1a875f35c3a128e922a46d8b19d..115802a7d21619cbaf4177abc5a58eef6774bbba 100644 (file)
@@ -93,13 +93,14 @@ describe('PrometheusAlertService', () => {
     );
 
     service.getRules();
-
-    expect(service.rules as any).toEqual([
-      { name: 'nearly_full', type: 'alerting', group: 'group1' },
-      { name: 'load_0', type: 'alerting', group: 'test' },
-      { name: 'load_1', type: 'alerting', group: 'test' },
-      { name: 'load_2', type: 'alerting', group: 'test' }
-    ]);
+    service.rules$.subscribe((rules) => {
+      expect(rules).toEqual([
+        { name: 'nearly_full', type: 'alerting', group: 'group1' },
+        { name: 'load_0', type: 'alerting', group: 'test' },
+        { name: 'load_1', type: 'alerting', group: 'test' },
+        { name: 'load_2', type: 'alerting', group: 'test' }
+      ]);
+    });
   });
 
   describe('refresh', () => {
index 6c9ab25cbc23b7437418464fe06232f3193e6d9f..f39a53048b9f593b2644025edbec62a3401130c1 100644 (file)
@@ -9,14 +9,16 @@ import {
   PrometheusRule
 } from '../models/prometheus-alerts';
 import { PrometheusAlertFormatter } from './prometheus-alert-formatter';
+import { BehaviorSubject } from 'rxjs';
 
 @Injectable({
   providedIn: 'root'
 })
 export class PrometheusAlertService {
   private canAlertsBeNotified = false;
+  private rulesSubject = new BehaviorSubject<PrometheusRule[]>([]);
+  rules$ = this.rulesSubject.asObservable();
   alerts: AlertmanagerAlert[] = [];
-  rules: PrometheusRule[] = [];
   activeAlerts: number;
   activeCriticalAlerts: number;
   activeWarningAlerts: number;
@@ -42,7 +44,7 @@ export class PrometheusAlertService {
   getRules() {
     this.prometheusService.ifPrometheusConfigured(() => {
       this.prometheusService.getRules('alerting').subscribe((groups) => {
-        this.rules = groups['groups'].reduce((acc, group) => {
+        const rules = groups['groups'].reduce((acc, group) => {
           return acc.concat(
             group.rules.map((rule) => {
               rule.group = group.name;
@@ -50,13 +52,13 @@ export class PrometheusAlertService {
             })
           );
         }, []);
+        this.rulesSubject.next(rules);
       });
     });
   }
 
   refresh(clusterFilteredAlerts?: boolean) {
     this.getAlerts(clusterFilteredAlerts);
-    this.getRules();
   }
 
   private handleAlerts(alerts: AlertmanagerAlert[]) {