Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding basic uptime stats #59

Merged
merged 2 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/app/app.controller.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -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<ServiceReportDto> {
return await this.service.generateStatsReport();
}
}
16 changes: 16 additions & 0 deletions src/app/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ 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
*/
@Injectable()
export class AppService {

private static startedAt: Date;

/**
* Sets up app in a way that can be used by main.ts and e2e tests
*/
Expand All @@ -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,
Expand All @@ -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<ServiceReportDto> {
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);
}
}
12 changes: 12 additions & 0 deletions src/app/dtos/service.report.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { IsArray, IsString } from 'class-validator';

/**
* Please note: this structure is expected to be used across all services
matt-raffel-kiva marked this conversation as resolved.
Show resolved Hide resolved
* which report statistics
*/
export class ServiceReportDto {
@IsString() serviceName: string;
@IsString() startedAt: string;
@IsString() currentTime: string;
@IsArray() versions: string[];
}