-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
165 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export { LogType } from "@prisma/client"; | ||
|
||
export enum StopSyncTrigger { | ||
CRON = "CRON", | ||
INIT = "INIT", | ||
} | ||
|
||
export enum LogMessage { | ||
IMPORT_STOPS = "Import stops", | ||
REST = "REST", | ||
GRAPHQL = "GraphQL", | ||
GRAPHQL_INTROSPECTION = "GraphQL - Introspection Query", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { Injectable, NestMiddleware } from "@nestjs/common"; | ||
import { LogType } from "@prisma/client"; | ||
import { NextFunction, Request, Response } from "express"; | ||
|
||
import { LogMessage } from "src/enums/log.enum"; | ||
import { PrismaService } from "src/modules/prisma/prisma.service"; | ||
import { | ||
isGraphqlRequest, | ||
isIntrospectionQuery, | ||
} from "src/utils/graphql.utilts"; | ||
import { isSuccess } from "src/utils/status-code.utils"; | ||
|
||
@Injectable() | ||
export class LoggerMiddleware implements NestMiddleware { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
use(req: Request, res: Response, next: NextFunction) { | ||
const start = Date.now(); | ||
|
||
res.on("finish", () => { | ||
if (isGraphqlRequest(req)) { | ||
createGraphqlLog({ | ||
req, | ||
res, | ||
duration: Date.now() - start, | ||
prisma: this.prisma, | ||
}); | ||
} else { | ||
createRestLog({ | ||
req, | ||
res, | ||
duration: Date.now() - start, | ||
prisma: this.prisma, | ||
}); | ||
} | ||
}); | ||
|
||
if (next) { | ||
next(); | ||
} | ||
} | ||
} | ||
|
||
const createRestLog = async ({ | ||
req, | ||
res, | ||
duration, | ||
prisma, | ||
}: { | ||
req: Request; | ||
res: Response; | ||
duration: number; | ||
prisma: PrismaService; | ||
}): Promise<void> => { | ||
await prisma.log.create({ | ||
data: { | ||
type: isSuccess(res.statusCode) ? LogType.INFO : LogType.ERROR, | ||
message: LogMessage.REST, | ||
statusCode: res.statusCode, | ||
host: req.headers.host ?? null, | ||
path: req.originalUrl, | ||
duration, | ||
}, | ||
}); | ||
}; | ||
|
||
const createGraphqlLog = async ({ | ||
req, | ||
res, | ||
duration, | ||
prisma, | ||
}: { | ||
req: Request; | ||
res: Response; | ||
duration: number; | ||
prisma: PrismaService; | ||
}): Promise<void> => { | ||
await prisma.log.create({ | ||
data: { | ||
type: isSuccess(res.statusCode) ? LogType.INFO : LogType.ERROR, | ||
message: isIntrospectionQuery(req) | ||
? LogMessage.GRAPHQL_INTROSPECTION | ||
: LogMessage.GRAPHQL, | ||
statusCode: res.statusCode, | ||
host: req.headers.host ?? null, | ||
duration, | ||
path: req.originalUrl, | ||
description: req.body.query, | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,55 @@ | ||
import { Controller, OnModuleInit } from "@nestjs/common"; | ||
import { Cron, CronExpression } from "@nestjs/schedule"; | ||
|
||
import { LogMessage, LogType, StopSyncTrigger } from "src/enums/log.enum"; | ||
import { ImportService } from "src/modules/import/import.service"; | ||
import { PrismaService } from "src/modules/prisma/prisma.service"; | ||
|
||
@Controller("import") | ||
export class ImportController implements OnModuleInit { | ||
constructor(private readonly importService: ImportService) {} | ||
constructor( | ||
private readonly importService: ImportService, | ||
private readonly prismaService: PrismaService, | ||
) {} | ||
|
||
async onModuleInit() { | ||
console.log("Starting initial stop sync"); | ||
this.syncStops(); | ||
|
||
this.syncStops(StopSyncTrigger.INIT); | ||
} | ||
|
||
@Cron(CronExpression.EVERY_7_HOURS) | ||
async cronSyncStops(): Promise<void> { | ||
console.log("Starting scheduled stop sync"); | ||
this.syncStops(); | ||
|
||
this.syncStops(StopSyncTrigger.CRON); | ||
} | ||
|
||
async syncStops(): Promise<void> { | ||
async syncStops(trigger: StopSyncTrigger): Promise<void> { | ||
const start = Date.now(); | ||
|
||
try { | ||
await this.importService.syncStops(); | ||
console.log("Scheduled stop sync completed successfully"); | ||
|
||
await this.prismaService.log.create({ | ||
data: { | ||
type: LogType.INFO, | ||
message: LogMessage.IMPORT_STOPS, | ||
duration: Date.now() - start, | ||
description: `Trigger: ${trigger};`, | ||
}, | ||
}); | ||
} catch (error) { | ||
console.error("Error during scheduled stop sync:", error); | ||
await this.prismaService.log.create({ | ||
data: { | ||
type: LogType.ERROR, | ||
message: LogMessage.IMPORT_STOPS, | ||
duration: Date.now() - start, | ||
description: `Trigger: ${trigger}; Error: ${error}`, | ||
}, | ||
}); | ||
} finally { | ||
console.log("Finished stop sync"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Request } from "express"; | ||
|
||
import { GRAPHQL_API_ROOT } from "src/constants/graphql.const"; | ||
|
||
export const isGraphqlRequest = (req: Request): boolean => { | ||
return req.originalUrl === GRAPHQL_API_ROOT; | ||
}; | ||
|
||
export const isIntrospectionQuery = (req: Request): boolean => { | ||
return String(req.body?.query).startsWith("query IntrospectionQuery {"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const isSuccess = (statusCode: number): boolean => { | ||
return 200 <= statusCode && statusCode < 300; | ||
}; |