Skip to content

Commit

Permalink
Merge pull request ceph#50214 from rhcs-dashboard/landing-page-prom-e…
Browse files Browse the repository at this point in the history
…rror-fix

mgr/dashboard: fix prometheus api issues on landing page v3

Reviewed-by: Pegonzal <NOT@FOUND>
Reviewed-by: Ernesto Puerta <[email protected]>
  • Loading branch information
nizamial09 authored Mar 15, 2023
2 parents c2cedfb + 6cb0679 commit 4ca0190
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { Component, Inject, OnInit, TemplateRef, ViewChild } from '@angular/core
import { PrometheusService } from '~/app/shared/api/prometheus.service';
import { CellTemplate } from '~/app/shared/enum/cell-template.enum';
import { Icons } from '~/app/shared/enum/icons.enum';
import { PrometheusListHelper } from '~/app/shared/helpers/prometheus-list-helper';
import { CdTableAction } from '~/app/shared/models/cd-table-action';
import { CdTableColumn } from '~/app/shared/models/cd-table-column';
import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
import { Permission } from '~/app/shared/models/permissions';
import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
import { PrometheusAlertService } from '~/app/shared/services/prometheus-alert.service';
import { URLBuilderService } from '~/app/shared/services/url-builder.service';
import { PrometheusListHelper } from '../prometheus-list-helper';

const BASE_URL = 'silences'; // as only silence actions can be used

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import _ from 'lodash';

import { PrometheusService } from '~/app/shared/api/prometheus.service';
import { CellTemplate } from '~/app/shared/enum/cell-template.enum';
import { PrometheusListHelper } from '~/app/shared/helpers/prometheus-list-helper';
import { CdTableColumn } from '~/app/shared/models/cd-table-column';
import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
import { PrometheusRule } from '~/app/shared/models/prometheus-alerts';
import { DurationPipe } from '~/app/shared/pipes/duration.pipe';
import { PrometheusAlertService } from '~/app/shared/services/prometheus-alert.service';
import { PrometheusListHelper } from '../prometheus-list-helper';

@Component({
selector: 'cd-rules-list',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
import { SortDirection, SortPropDir } from '@swimlane/ngx-datatable';
import { Observable, Subscriber } from 'rxjs';

import { PrometheusListHelper } from '~/app/ceph/cluster/prometheus/prometheus-list-helper';
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 { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
i18n>Cluster</span>
</div>
<section class="border-top mt-5"
*ngIf="isAlertmanagerConfigured && (crticialActiveAlerts || warningActiveAlerts)">
*ngIf="isAlertmanagerConfigured && (prometheusAlertService.activeCriticalAlerts || prometheusAlertService.activeWarningAlerts)">
<div class="d-flex flex-wrap ms-4 me-4">
<span class="pt-2"
i18n>Alerts</span>
Expand All @@ -38,9 +38,9 @@
(click)="toggleAlertsWindow('danger')"
id="dangerAlerts"
i18n-title
*ngIf="crticialActiveAlerts">
*ngIf="prometheusAlertService?.activeCriticalAlerts > 0">
<i [ngClass]="[icons.danger]"></i>
<span>{{ crticialActiveAlerts }}</span>
<span>{{ prometheusAlertService.activeCriticalAlerts }}</span>
</button>

<button class="btn btn-outline-warning rounded-pill ms-2"
Expand All @@ -49,9 +49,9 @@
(click)="toggleAlertsWindow('warning')"
id="warningAlerts"
i18n-title
*ngIf="warningActiveAlerts">
*ngIf="prometheusAlertService?.activeWarningAlerts > 0">
<i [ngClass]="[icons.infoCircle]"></i>
<span>{{ warningActiveAlerts }}</span>
<span>{{ prometheusAlertService.activeWarningAlerts }}</span>
</button>

<div class="pt-0 position-right">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { PrometheusService } from '~/app/shared/api/prometheus.service';
import { CssHelper } from '~/app/shared/classes/css-helper';
import { AlertmanagerAlert } from '~/app/shared/models/prometheus-alerts';
import { FeatureTogglesService } from '~/app/shared/services/feature-toggles.service';
import { PrometheusAlertService } from '~/app/shared/services/prometheus-alert.service';
import { SummaryService } from '~/app/shared/services/summary.service';
import { SharedModule } from '~/app/shared/shared.module';
import { configureTestBed } from '~/testing/unit-test-helper';
Expand Down Expand Up @@ -161,6 +162,13 @@ describe('Dashbord Component', () => {
schemas: [NO_ERRORS_SCHEMA],
providers: [
{ provide: SummaryService, useClass: SummaryServiceMock },
{
provide: PrometheusAlertService,
useValue: {
activeCriticalAlerts: 2,
activeWarningAlerts: 1
}
},
CssHelper,
PgCategoryService
]
Expand Down Expand Up @@ -244,12 +252,12 @@ describe('Dashbord Component', () => {
it('should show the actual alert count on each alerts pill', () => {
fixture.detectChanges();

const successNotification = fixture.debugElement.query(By.css('button[id=warningAlerts] span'));
const warningAlerts = fixture.debugElement.query(By.css('button[id=warningAlerts] span'));

const dangerNotification = fixture.debugElement.query(By.css('button[id=dangerAlerts] span'));
const dangerAlerts = fixture.debugElement.query(By.css('button[id=dangerAlerts] span'));

expect(successNotification.nativeElement.textContent).toBe('1');
expect(dangerNotification.nativeElement.textContent).toBe('2');
expect(warningAlerts.nativeElement.textContent).toBe('1');
expect(dangerAlerts.nativeElement.textContent).toBe('2');
});

it('should show the critical alerts window and its content', () => {
Expand All @@ -275,15 +283,16 @@ describe('Dashbord Component', () => {
});

it('should only show the pills when the alerts are not empty', () => {
getAlertsSpy.and.returnValue(of({}));
spyOn(TestBed.inject(PrometheusAlertService), 'activeCriticalAlerts').and.returnValue(0);
spyOn(TestBed.inject(PrometheusAlertService), 'activeWarningAlerts').and.returnValue(0);
fixture.detectChanges();

const successNotification = fixture.debugElement.query(By.css('button[id=warningAlerts]'));
const warningAlerts = fixture.debugElement.query(By.css('button[id=warningAlerts]'));

const dangerNotification = fixture.debugElement.query(By.css('button[id=dangerAlerts]'));
const dangerAlerts = fixture.debugElement.query(By.css('button[id=dangerAlerts]'));

expect(successNotification).toBe(null);
expect(dangerNotification).toBe(null);
expect(warningAlerts).toBe(null);
expect(dangerAlerts).toBe(null);
});

describe('features disabled', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ import {
} from '~/app/shared/services/feature-toggles.service';
import { RefreshIntervalService } from '~/app/shared/services/refresh-interval.service';
import { SummaryService } from '~/app/shared/services/summary.service';
import { PrometheusListHelper } from '~/app/shared/helpers/prometheus-list-helper';
import { PrometheusAlertService } from '~/app/shared/services/prometheus-alert.service';

@Component({
selector: 'cd-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.scss']
})
export class DashboardComponent implements OnInit, OnDestroy {
export class DashboardComponent extends PrometheusListHelper implements OnInit, OnDestroy {
detailsCardData: DashboardDetails = {};
osdSettingsService: any;
osdSettings: any;
Expand All @@ -42,7 +44,6 @@ export class DashboardComponent implements OnInit, OnDestroy {
healthData$: Observable<Object>;
prometheusAlerts$: Observable<AlertmanagerAlert[]>;

isAlertmanagerConfigured = false;
icons = Icons;
showAlerts = false;
flexHeight = true;
Expand All @@ -53,8 +54,6 @@ export class DashboardComponent implements OnInit, OnDestroy {
borderClass: string;
alertType: string;
alerts: AlertmanagerAlert[];
crticialActiveAlerts: number;
warningActiveAlerts: number;
healthData: any;
categoryPgAmount: Record<string, number> = {};
totalPgs = 0;
Expand Down Expand Up @@ -86,16 +85,18 @@ export class DashboardComponent implements OnInit, OnDestroy {
private featureToggles: FeatureTogglesService,
private healthService: HealthService,
public prometheusService: PrometheusService,
private refreshIntervalService: RefreshIntervalService
private refreshIntervalService: RefreshIntervalService,
public prometheusAlertService: PrometheusAlertService
) {
super(prometheusService);
this.permissions = this.authStorageService.getPermissions();
this.enabledFeature$ = this.featureToggles.get();
}

ngOnInit() {
super.ngOnInit();
this.interval = this.refreshIntervalService.intervalData$.subscribe(() => {
this.getHealth();
this.triggerPrometheusAlerts();
this.getCapacityCardData();
});
this.getPrometheusData(this.lastHourDateObject);
Expand All @@ -113,6 +114,7 @@ export class DashboardComponent implements OnInit, OnDestroy {
}

toggleAlertsWindow(type: string, isToggleButton: boolean = false) {
this.triggerPrometheusAlerts();
if (isToggleButton) {
this.showAlerts = !this.showAlerts;
this.flexHeight = !this.flexHeight;
Expand Down Expand Up @@ -159,56 +161,48 @@ export class DashboardComponent implements OnInit, OnDestroy {

triggerPrometheusAlerts() {
this.prometheusService.ifAlertmanagerConfigured(() => {
this.isAlertmanagerConfigured = true;

this.prometheusService.getAlerts().subscribe((alerts) => {
this.alerts = alerts;
this.crticialActiveAlerts = alerts.filter(
(alert: AlertmanagerAlert) =>
alert.status.state === 'active' && alert.labels.severity === 'critical'
).length;
this.warningActiveAlerts = alerts.filter(
(alert: AlertmanagerAlert) =>
alert.status.state === 'active' && alert.labels.severity === 'warning'
).length;
});
});
}

getPrometheusData(selectedTime: any) {
if (this.timerGetPrometheusDataSub) {
this.timerGetPrometheusDataSub.unsubscribe();
}
this.timerGetPrometheusDataSub = timer(0, this.timerTime).subscribe(() => {
selectedTime = this.updateTimeStamp(selectedTime);

for (const queryName in queries) {
if (queries.hasOwnProperty(queryName)) {
const query = queries[queryName];
let interval = selectedTime.step;

if (query.includes('rate') && selectedTime.step < 20) {
interval = 20;
} else if (query.includes('rate')) {
interval = selectedTime.step * 2;
this.prometheusService.ifPrometheusConfigured(() => {
if (this.timerGetPrometheusDataSub) {
this.timerGetPrometheusDataSub.unsubscribe();
}
this.timerGetPrometheusDataSub = timer(0, this.timerTime).subscribe(() => {
selectedTime = this.updateTimeStamp(selectedTime);

for (const queryName in queries) {
if (queries.hasOwnProperty(queryName)) {
const query = queries[queryName];
let interval = selectedTime.step;

if (query.includes('rate') && selectedTime.step < 20) {
interval = 20;
} else if (query.includes('rate')) {
interval = selectedTime.step * 2;
}

const intervalAdjustedQuery = query.replace(/\[(.*?)\]/g, `[${interval}s]`);

this.prometheusService
.getPrometheusData({
params: intervalAdjustedQuery,
start: selectedTime['start'],
end: selectedTime['end'],
step: selectedTime['step']
})
.subscribe((data: any) => {
if (data.result.length) {
this.queriesResults[queryName] = data.result[0].values;
}
});
}

const intervalAdjustedQuery = query.replace(/\[(.*?)\]/g, `[${interval}s]`);

this.prometheusService
.getPrometheusData({
params: intervalAdjustedQuery,
start: selectedTime['start'],
end: selectedTime['end'],
step: selectedTime['step']
})
.subscribe((data: any) => {
if (data.result.length) {
this.queriesResults[queryName] = data.result[0].values;
}
});
}
}
});
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

@NgModule({
declarations: [],
imports: [CommonModule]
})
export class HelpersModule {}

0 comments on commit 4ca0190

Please sign in to comment.