Skip to content

Commit

Permalink
feat(be): logs cleanup module
Browse files Browse the repository at this point in the history
  • Loading branch information
krystxf committed Dec 7, 2024
1 parent 7ad201f commit 3749a52
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions apps/backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { DepartureModule } from "src/modules/departure/departure.module";
import { GtfsModule } from "src/modules/gtfs/gtfs.module";
import { ImportModule } from "src/modules/import/import.module";
import { LoggerModule } from "src/modules/logger/logger.module";
import { LogsCleanupModule } from "src/modules/logs-cleanup/logs-cleanup.module";
import { PlatformModule } from "src/modules/platform/platform.module";
import { PrismaModule } from "src/modules/prisma/prisma.module";
import { RouteModule } from "src/modules/route/route.module";
Expand All @@ -27,6 +28,7 @@ import { StopModule } from "src/modules/stop/stop.module";
StopModule,
PrismaModule,
LoggerModule,
LogsCleanupModule,
StatusModule,
GtfsModule,
RouteModule,
Expand Down
1 change: 1 addition & 0 deletions apps/backend/src/enums/log.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export enum LogMessage {
IMPORT_STOPS = "Import stops",
REST = "REST",
GRAPHQL = "GraphQL",
REQUEST_LOGS_CLEANUP = "Request logs cleanup",
}

export enum RestLogStatus {
Expand Down
18 changes: 18 additions & 0 deletions apps/backend/src/modules/logs-cleanup/logs-cleanup.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Controller, OnModuleInit } from "@nestjs/common";
import { Cron, CronExpression } from "@nestjs/schedule";

import { LogsCleanupService } from "src/modules/logs-cleanup/logs-cleanup.service";

@Controller("logs-cleanup")
export class LogsCleanupController implements OnModuleInit {
constructor(private readonly logsCleanupService: LogsCleanupService) {}

async onModuleInit(): Promise<void> {
return this.logsCleanupService.cleanupLogs();
}

@Cron(CronExpression.EVERY_10_MINUTES)
async cronLogsCleanup(): Promise<void> {
return this.logsCleanupService.cleanupLogs();
}
}
11 changes: 11 additions & 0 deletions apps/backend/src/modules/logs-cleanup/logs-cleanup.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from "@nestjs/common";

import { LogsCleanupController } from "src/modules/logs-cleanup/logs-cleanup.controller";
import { LogsCleanupService } from "src/modules/logs-cleanup/logs-cleanup.service";

@Module({
controllers: [LogsCleanupController],
providers: [LogsCleanupService],
imports: [],
})
export class LogsCleanupModule {}
60 changes: 60 additions & 0 deletions apps/backend/src/modules/logs-cleanup/logs-cleanup.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Injectable } from "@nestjs/common";

import { LogLevel, LogMessage } from "src/enums/log.enum";
import { LoggerService } from "src/modules/logger/logger.service";
import { PrismaService } from "src/modules/prisma/prisma.service";

const MAX_COUNT = 500_000;

@Injectable()
export class LogsCleanupService {
constructor(
private readonly prisma: PrismaService,
private readonly logger: LoggerService,
) {}

async cleanupLogs(): Promise<void> {
try {
const recordCount = await this.prisma.requestLog.count();

if (recordCount < MAX_COUNT) {
return;
}

const last = await this.prisma.requestLog.findFirst({
orderBy: { createdAt: "desc" },
skip: MAX_COUNT,
});

if (!last) {
return;
}

const { count } = await this.prisma.requestLog.deleteMany({
where: {
createdAt: {
lte: last.createdAt,
},
},
});

await this.logger.createLog(
LogLevel.log,
LogMessage.REQUEST_LOGS_CLEANUP,
{
message: "Successfully removed old logs",
count,
},
);
} catch (error) {
await this.logger.createLog(
LogLevel.error,
LogMessage.REQUEST_LOGS_CLEANUP,
{
message: "Failed to cleanup logs",
error: JSON.stringify(error),
},
);
}
}
}

0 comments on commit 3749a52

Please sign in to comment.