import { NgbNavModule } from '@ng-bootstrap/ng-bootstrap';
import { PrometheusAlertService } from '~/app/shared/services/prometheus-alert.service';
+import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
+import { Permission } from '~/app/shared/models/permissions';
import { configureTestBed } from '~/testing/unit-test-helper';
import { PrometheusTabsComponent } from './prometheus-tabs.component';
configureTestBed({
imports: [RouterTestingModule, NgbNavModule],
declarations: [PrometheusTabsComponent],
- providers: [{ provide: PrometheusAlertService, useValue: { alerts: [] } }]
+ providers: [
+ { provide: PrometheusAlertService, useValue: { alerts: [] } },
+ {
+ provide: AuthStorageService,
+ useValue: {
+ getPermissions: () => ({
+ prometheus: new Permission(['read'])
+ })
+ }
+ }
+ ]
});
beforeEach(() => {
it('should create', () => {
expect(component).toBeTruthy();
});
+
+ it('should show silences to users with read access', () => {
+ expect(component.canViewSilences).toBe(true);
+ });
});
import { Component } from '@angular/core';
import { PrometheusAlertService } from '~/app/shared/services/prometheus-alert.service';
+import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
@Component({
selector: 'cd-prometheus-tabs',
standalone: false
})
export class PrometheusTabsComponent {
- constructor(public prometheusAlertService: PrometheusAlertService) {}
+ canViewSilences: boolean;
+
+ constructor(
+ public prometheusAlertService: PrometheusAlertService,
+ private authStorageService: AuthStorageService
+ ) {
+ const prometheusPermission = this.authStorageService.getPermissions().prometheus;
+ this.canViewSilences = prometheusPermission.read;
+ }
}
import { Observable, Subscriber } from 'rxjs';
import { PrometheusListHelper } from '~/app/shared/helpers/prometheus-list-helper';
-import { SilenceFormComponent } from '~/app/ceph/cluster/prometheus/silence-form/silence-form.component';
import { PrometheusService } from '~/app/shared/api/prometheus.service';
import { DeleteConfirmationModalComponent } from '~/app/shared/components/delete-confirmation-modal/delete-confirmation-modal.component';
import { ActionLabelsI18n, SucceededActionLabelsI18n } from '~/app/shared/constants/app.constants';
const BASE_URL = 'monitoring/silences';
@Component({
- providers: [
- { provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) },
- SilenceFormComponent
- ],
+ providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }],
selector: 'cd-silences-list',
templateUrl: './silence-list.component.html',
styleUrls: ['./silence-list.component.scss'],
private urlBuilder: URLBuilderService,
private actionLabels: ActionLabelsI18n,
private succeededLabels: SucceededActionLabelsI18n,
- private silenceFormComponent: SilenceFormComponent,
private silenceMatcher: PrometheusSilenceMatcherService,
@Inject(PrometheusService) prometheusService: PrometheusService
) {
const activeSilences = silences.filter(
(silence: AlertmanagerSilence) => silence.status.state !== 'expired'
);
- this.getAlerts(activeSilences);
+ this.loadRulesAndMatchAlerts(activeSilences);
},
() => {
this.prometheusService.disableAlertmanagerConfig();
this.selection = selection;
}
- getAlerts(silences: any) {
- const rules = this.silenceFormComponent.getRules();
- silences.forEach((silence: any) => {
- silence.matchers.forEach((matcher: any) => {
- this.rules = this.silenceMatcher.getMatchedRules(matcher, rules);
+ private loadRulesAndMatchAlerts(silences: AlertmanagerSilence[]) {
+ this.prometheusService.ifPrometheusConfigured(
+ () =>
+ this.prometheusService.getRules().subscribe(
+ (groups) => {
+ this.rules = groups.groups.flatMap((group) => group.rules);
+ this.getAlerts(silences);
+ },
+ () => {
+ this.rules = [];
+ this.getAlerts(silences);
+ }
+ ),
+ () => {
+ this.rules = [];
+ this.getAlerts(silences);
+ }
+ );
+ }
+
+ getAlerts(silences: AlertmanagerSilence[]) {
+ silences.forEach((silence) => {
+ silence.matchers.forEach((matcher) => {
+ const matchedRules = this.silenceMatcher.getMatchedRules(matcher, this.rules);
const alertNames: string[] = [];
- for (const rule of this.rules) {
+ for (const rule of matchedRules) {
alertNames.push(rule.name);
}
silence.silencedAlerts = alertNames;