the <cd-doc section="prometheus"></cd-doc>.</cd-alert-panel>
<cd-table *ngIf="isPrometheusConfigured"
- [data]="prometheusAlertService.rules"
+ [data]="rules"
[columns]="columns"
[selectionType]="'single'"
[hasDetails]="true"
-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';
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
* always supposed to be 'alerting'.
*/
hideKeys = ['alerts', 'type'];
+ rulesSubscription: Subscription;
constructor(
public prometheusAlertService: PrometheusAlertService,
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 },
{
updateSelection(selection: CdTableSelection) {
this.selection = selection;
}
+
+ ngOnDestroy() {
+ this.rulesSubscription.unsubscribe();
+ }
}
);
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', () => {
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;
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;
})
);
}, []);
+ this.rulesSubject.next(rules);
});
});
}
refresh(clusterFilteredAlerts?: boolean) {
this.getAlerts(clusterFilteredAlerts);
- this.getRules();
}
private handleAlerts(alerts: AlertmanagerAlert[]) {