Skip to content

Commit

Permalink
Merge pull request #257 from amosproj/hotfix-overview-page
Browse files Browse the repository at this point in the history
apply hotfix
  • Loading branch information
engelharddirk authored Jan 15, 2025
2 parents 3dc2bdb + b5a7f4f commit 79c7d29
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
</div>
</div>
<div class="card-footer">
{{ alerts.length }} Alerts have been triggered in the last
{{ total }} Alerts have been triggered in the last
{{ DAYS }} days.
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ describe('AlertComponent', () => {
},
];

mockAlertService.getAllAlerts.mockReturnValue(of(mockAlerts));
const mockReturnValue = {
alerts: mockAlerts,
count: 1,
};
mockAlertService.getAllAlerts.mockReturnValue(of(mockReturnValue));


component.loadAlerts();

Expand All @@ -104,7 +109,11 @@ describe('AlertComponent', () => {
});

it('should set status to OK when no alerts', () => {
mockAlertService.getAllAlerts.mockReturnValue(of([]));
const mockReturnValue = {
alerts: [],
count: 0,
};
mockAlertService.getAllAlerts.mockReturnValue(of(mockReturnValue));

component.loadAlerts();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import { SeverityType } from '../../../../shared/enums/severityType';
export class AlertComponent implements OnInit, OnDestroy {
protected readonly SeverityType = SeverityType;
readonly DAYS = 7;
private readonly fromDate = new Date(Date.now() - this.DAYS * 24 * 60 * 60 * 1000);

alerts: Alert[] = [];
total: number = 0;
criticalAlertsCount = 0;
warningAlertsCount = 0;
infoAlertsCount = 0;
Expand All @@ -47,20 +49,22 @@ export class AlertComponent implements OnInit, OnDestroy {

loadAlerts(): void {
this.alertService
.getAllAlerts(this.DAYS)
.getAllAlerts(this.fromDate.toISOString())
.pipe(takeUntil(this.destroy$))
.subscribe((data: Alert[]) => {
this.alerts = this.filterAlerts(data);
this.criticalAlertsCount = this.alerts.filter(
(alert) => alert.alertType.severity === SeverityType.CRITICAL
).length;
this.warningAlertsCount = this.alerts.filter(
(alert) => alert.alertType.severity === SeverityType.WARNING
).length;
this.infoAlertsCount = this.alerts.filter(
(alert) => alert.alertType.severity === SeverityType.INFO
).length;
this.status = this.getStatus();
.subscribe((data: { alerts: Alert[], total: number }) => {
console.log("Data: ", data);
this.alerts = this.filterAlerts(data.alerts);
this.total = data.total;
this.criticalAlertsCount = this.alerts.filter(
(alert) => alert.alertType.severity === SeverityType.CRITICAL
).length;
this.warningAlertsCount = this.alerts.filter(
(alert) => alert.alertType.severity === SeverityType.WARNING
).length;
this.infoAlertsCount = this.alerts.filter(
(alert) => alert.alertType.severity === SeverityType.INFO
).length;
this.status = this.getStatus();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,24 @@ describe('AlertServiceService', () => {
];

service.getAllAlerts().subscribe((alerts) => {
expect(alerts).toEqual(mockAlerts);
expect(alerts).toEqual({ alerts: mockAlerts, total: 2 });
});

const req = httpMock.expectOne(`${baseUrl}/alerting`);
expect(req.request.method).toBe('GET');
req.flush(mockAlerts);
req.flush({ data: mockAlerts, paginationData: { total: 2 } });
});

it('should fetch alerts with days parameter', () => {
const mockAlerts: Alert[] = [
const mockAlerts = [
{
id: randomUUID().toString(),
alertType: {
id: randomUUID().toString(),
name: 'test',
severity: SeverityType.INFO,
user_active: false,
master_active: false,
user_active: true,
master_active: true,
},
backup: {
id: randomUUID().toString(),
Expand Down Expand Up @@ -126,13 +126,17 @@ describe('AlertServiceService', () => {
},
];
const days = 7;
const fromDate = new Date(Date.now() - days * 24 * 60 * 60 * 1000).toISOString();

service.getAllAlerts(days).subscribe((alerts) => {
expect(alerts).toEqual(mockAlerts);
service.getAllAlerts(fromDate).subscribe((alerts) => {
expect(alerts).toEqual({
alerts: mockAlerts,
total: 2,
});
});

const req = httpMock.expectOne(`${baseUrl}/alerting?days=${days}`);
const req = httpMock.expectOne(`${baseUrl}/alerting?offset=0&limit=10&fromDate=${fromDate}`);
expect(req.request.method).toBe('GET');
req.flush(mockAlerts);
req.flush({ data: mockAlerts, paginationData: { total: 2 } });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Inject, Injectable } from '@angular/core';
import { BASE_URL } from '../../types/configuration';
import { HttpClient } from '@angular/common/http';
import { Observable, Subject } from 'rxjs';
import { map } from 'rxjs/operators';
import { Alert } from '../../types/alert';

@Injectable({
Expand All @@ -14,11 +15,21 @@ export class AlertServiceService {
private readonly http: HttpClient
) {}

getAllAlerts(days?: number): Observable<Alert[]> {
if (days) {
return this.http.get<Alert[]>(`${this.baseUrl}/alerting?days=${days}`);
getAllAlerts(fromDate?: string, offset?: number): Observable<{ alerts: Alert[], total: number }> {
if (fromDate) {
return this.http.get<{ data: Alert[], paginationData: { total: number } }>(`${this.baseUrl}/alerting?offset=0&limit=10&fromDate=${fromDate}`).pipe(
map(response => ({
alerts: response.data,
total: response.paginationData.total
}))
);
}
return this.http.get<Alert[]>(`${this.baseUrl}/alerting`);
return this.http.get<{ data: Alert[], paginationData: { total: number } }>(`${this.baseUrl}/alerting`).pipe(
map(response => ({
alerts: response.data,
total: response.paginationData.total
}))
);
}

refresh() {
Expand Down

0 comments on commit 79c7d29

Please sign in to comment.