From 3ee103818e4a3123fbaf0b18171a0e54c3d838e6 Mon Sep 17 00:00:00 2001 From: Matt <52470716+matt-raffel-kiva@users.noreply.github.com> Date: Mon, 18 Apr 2022 11:18:14 -0600 Subject: [PATCH] adding basic uptime stats (#59) * adding basic uptime stats Signed-off-by: Matt Raffel * adding basic uptime stats Signed-off-by: Matt Raffel --- src/app/app.controller.ts | 13 +++++++++++++ src/app/app.service.ts | 16 ++++++++++++++++ src/app/dtos/service.report.dto.ts | 12 ++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 src/app/dtos/service.report.dto.ts diff --git a/src/app/app.controller.ts b/src/app/app.controller.ts index 0256bd7..19d6ad5 100644 --- a/src/app/app.controller.ts +++ b/src/app/app.controller.ts @@ -1,6 +1,8 @@ import { Get, Controller } from '@nestjs/common'; import { HttpConstants } from 'protocol-common/http-context/http.constants'; import { DisableAutoLogging } from 'protocol-common/disable.auto.logging.decorator'; +import { AppService } from './app.service'; +import { ServiceReportDto } from './dtos/service.report.dto'; /** * Base route is just for various health check endpoints @@ -9,6 +11,9 @@ import { DisableAutoLogging } from 'protocol-common/disable.auto.logging.decorat @Controller() export class AppController { + constructor(private readonly service: AppService) { + } + @Get() base(): string { return process.env.SERVICE_NAME; @@ -23,4 +28,12 @@ export class AppController { healthz(): string { return HttpConstants.HEALTHZ_RESPONSE; } + + /** + * For the uptime statistics report (see Uptime feature brief) + */ + @Get('stats') + async generateStatsReport() : Promise { + return await this.service.generateStatsReport(); + } } diff --git a/src/app/app.service.ts b/src/app/app.service.ts index 22b5641..35d6075 100644 --- a/src/app/app.service.ts +++ b/src/app/app.service.ts @@ -7,6 +7,7 @@ import { traceware } from 'protocol-common/tracer'; import rateLimit from 'express-rate-limit'; import swaggerUi from 'swagger-ui-express'; import swaggerDocument from '../docs/swagger.json'; +import { ServiceReportDto } from './dtos/service.report.dto'; /** * Sets up the gateway to handle external traffic, eg cors, rate-limiting, etc @@ -14,6 +15,8 @@ import swaggerDocument from '../docs/swagger.json'; @Injectable() export class AppService { + private static startedAt: Date; + /** * Sets up app in a way that can be used by main.ts and e2e tests */ @@ -38,6 +41,8 @@ export class AppService { app.use(traceware('gateway')); + AppService.startedAt = new Date(); + // Default is 100 requests per minute app.use(rateLimit({ windowMs: process.env.RATE_LIMIT_WINDOW_MS, @@ -47,4 +52,15 @@ export class AppService { // Load swagger docs and display app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); }; + + public async generateStatsReport(): Promise { + Logger.info('stats report generated'); + const report: ServiceReportDto = new ServiceReportDto(); + report.serviceName = process.env.SERVICE_NAME; + report.startedAt = AppService.startedAt.toDateString(); + report.currentTime = new Date().toDateString(); + report.versions = ['none']; + + return Promise.resolve(report); + } } diff --git a/src/app/dtos/service.report.dto.ts b/src/app/dtos/service.report.dto.ts new file mode 100644 index 0000000..95c52ae --- /dev/null +++ b/src/app/dtos/service.report.dto.ts @@ -0,0 +1,12 @@ +import { IsArray, IsString } from 'class-validator'; + +/** + * Please note: this structure is expected to be used across all services + * which report statistics + */ +export class ServiceReportDto { + @IsString() serviceName: string; + @IsString() startedAt: string; + @IsString() currentTime: string; + @IsArray() versions: string[]; +}