From: Afreen Misbah Date: Wed, 6 Aug 2025 07:37:16 +0000 (+0530) Subject: mgr/dashboard: Stop rules api being polled on every page X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=275481ec5f48419ee1a5a70692111563d2c6e37b;p=ceph.git mgr/dashboard: Stop rules api being polled on every page - /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 (cherry picked from commit df984d72df1370181328dc3ec30a22619841c185) --- diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/rules-list/rules-list.component.html b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/rules-list/rules-list.component.html index 7296f669d000a..c602ac1c5808c 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/rules-list/rules-list.component.html +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/rules-list/rules-list.component.html @@ -7,7 +7,7 @@ the  . { + 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(); + } } diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.spec.ts index aa3160b304cca..115802a7d2161 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.spec.ts @@ -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', () => { diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.ts index 6c9ab25cbc23b..f39a53048b9f5 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.ts @@ -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([]); + 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[]) {