-
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
9 changed files
with
181 additions
and
15 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
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,99 @@ | ||
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.utils"; | ||
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", async () => { | ||
if (isGraphqlRequest(req)) { | ||
try { | ||
await createGraphqlLog({ | ||
req, | ||
res, | ||
duration: Date.now() - start, | ||
prisma: this.prisma, | ||
}); | ||
} catch (error) { | ||
console.error("Failed to create Graphql log", error); | ||
} | ||
} else { | ||
try { | ||
await createRestLog({ | ||
req, | ||
res, | ||
duration: Date.now() - start, | ||
prisma: this.prisma, | ||
}); | ||
} catch (error) { | ||
console.error("Failed to create REST log", error); | ||
} | ||
} | ||
}); | ||
|
||
if (next) { | ||
next(); | ||
} | ||
} | ||
} | ||
|
||
const createRestLog = async ({ | ||
req, | ||
res, | ||
duration, | ||
prisma, | ||
}: { | ||
req: Request; | ||
res: Response; | ||
duration: number; | ||
prisma: PrismaService; | ||
}): Promise<void> => { | ||
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> => { | ||
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
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,10 @@ | ||
/** | ||
* Checks if the given HTTP status code represents a successful response. | ||
* @param statusCode - The HTTP status code to check. | ||
* @returns True if the status code is in the range [200, 300), false otherwise. | ||
*/ | ||
export const isSuccess = (statusCode: number): boolean => { | ||
// Ensure the status code is a positive integer | ||
const code = Math.floor(Math.abs(statusCode)); | ||
return code >= 200 && code < 300; | ||
}; |