Skip to content

Commit

Permalink
Merge pull request #275 from amosproj/274-backend-module-alert-overvi…
Browse files Browse the repository at this point in the history
…ew-page-severity-counter-api

Endpoint for getting Alert Severities count
  • Loading branch information
chrisklg authored Jan 18, 2025
2 parents 062a97a + bdc3b31 commit 3c605dd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
15 changes: 15 additions & 0 deletions apps/backend/src/app/alerting/alerting.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ApiNotFoundResponse,
ApiOperation,
ApiQuery,
ApiResponse,
ApiTags,
} from '@nestjs/swagger';
import { AlertingService } from './alerting.service';
Expand All @@ -29,6 +30,7 @@ import { PaginationOptionsDto } from '../utils/pagination/PaginationOptionsDto';
import { AlertFilterDto } from './dto/alertFilter.dto';
import { AlertOrderOptionsDto } from './dto/alertOrderOptions.dto';
import { PaginationDto } from '../utils/pagination/PaginationDto';
import { AlertStatisticsDto } from './dto/alertStatistics.dto';

@ApiTags('Alerting')
@Controller('alerting')
Expand All @@ -37,6 +39,19 @@ export class AlertingController {

constructor(private readonly alertingService: AlertingService) {}

@Get('statistics')
@ApiOperation({
summary: 'Returns the number of Info, Warning and Critical alerts.',
})
@ApiResponse({
status: 200,
description: 'The number of Info, Warning and Critical alerts.',
type: AlertStatisticsDto,
})
async getStatistics(): Promise<AlertStatisticsDto> {
return this.alertingService.getStatistics();
}

@Post('type')
@ApiOperation({ summary: 'Create a new alert type.' })
@ApiConflictResponse({ description: 'Alert type already exists' })
Expand Down
24 changes: 24 additions & 0 deletions apps/backend/src/app/alerting/alerting.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { PaginationOptionsDto } from '../utils/pagination/PaginationOptionsDto';
import { AlertOrderOptionsDto } from './dto/alertOrderOptions.dto';
import { AlertFilterDto } from './dto/alertFilter.dto';
import { PaginationService } from '../utils/pagination/paginationService';
import { AlertStatisticsDto } from './dto/alertStatistics.dto';

@Injectable()
export class AlertingService extends PaginationService implements OnModuleInit {
Expand Down Expand Up @@ -99,6 +100,29 @@ export class AlertingService extends PaginationService implements OnModuleInit {
}
}

async getStatistics(): Promise<AlertStatisticsDto> {
const alertStatisticsDto: AlertStatisticsDto = {
infoAlerts: 0,
warningAlerts: 0,
criticalAlerts: 0,
};
for (const repo of this.alertRepositories) {
const infoAlerts = await repo.count({
where: { alertType: { severity: SeverityType.INFO } },
});
const warningAlerts = await repo.count({
where: { alertType: { severity: SeverityType.WARNING } },
});
const criticalAlerts = await repo.count({
where: { alertType: { severity: SeverityType.CRITICAL } },
});
alertStatisticsDto.infoAlerts += infoAlerts;
alertStatisticsDto.warningAlerts += warningAlerts;
alertStatisticsDto.criticalAlerts += criticalAlerts;
}
return alertStatisticsDto;
}

async createAlertType(createAlertTypeDto: CreateAlertTypeDto) {
const entity = await this.alertTypeRepository.findOneBy({
name: createAlertTypeDto.name,
Expand Down
12 changes: 12 additions & 0 deletions apps/backend/src/app/alerting/dto/alertStatistics.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ApiProperty } from '@nestjs/swagger';

export class AlertStatisticsDto {
@ApiProperty({ description: 'Number of Info alerts', example: 0 })
infoAlerts!: number;

@ApiProperty({ description: 'Number of Warning alerts', example: 0 })
warningAlerts!: number;

@ApiProperty({ description: 'Number of Critical alerts', example: 0 })
criticalAlerts!: number;
}

0 comments on commit 3c605dd

Please sign in to comment.