feat: implement logging enhancements; add projectId and queryId to log entity; update query and logger services for improved logging; refactor query execution to support call stack tracking
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { TLog } from "../logger.types";
|
||||
import { Project } from "../../../project/entities/project.entity";
|
||||
import { Query } from "../../../query/entities/query.entity";
|
||||
|
||||
@Entity()
|
||||
export class Log {
|
||||
@ -18,6 +20,12 @@ export class Log {
|
||||
})
|
||||
content: TLog;
|
||||
|
||||
@ManyToOne(() => Project, (project) => project.logs)
|
||||
project: Project;
|
||||
|
||||
@ManyToOne(() => Query, (query) => query.logs)
|
||||
query: Query;
|
||||
|
||||
@Column({ type: "timestamp", default: () => "CURRENT_TIMESTAMP" })
|
||||
createdAt: Date;
|
||||
}
|
||||
|
||||
@ -1,6 +1,15 @@
|
||||
import { Body, Controller, Get, Inject, Post, UseGuards } from "@nestjs/common";
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Inject,
|
||||
Param,
|
||||
Post,
|
||||
UseGuards,
|
||||
} from "@nestjs/common";
|
||||
import { LoggerService } from "./logger.service";
|
||||
import { ApiTokenGuard } from "src/api/guards/api-token.guard";
|
||||
import { QueryGuard } from "../guards/query.guard";
|
||||
|
||||
@Controller("logger")
|
||||
@UseGuards(ApiTokenGuard)
|
||||
@ -10,13 +19,14 @@ export class LoggerController {
|
||||
private readonly loggerService: LoggerService
|
||||
) {}
|
||||
|
||||
@Get("/:traceId")
|
||||
getByTraceId(@Inject("traceId") traceId: string) {
|
||||
@Get("/:id/:traceId")
|
||||
getByTraceId(@Param("traceId") traceId: string) {
|
||||
return this.loggerService.findByTraceId(traceId);
|
||||
}
|
||||
|
||||
@Post("/find")
|
||||
find(
|
||||
@Post("/:id/findAll")
|
||||
findAll(
|
||||
@Param("id") projectId: string,
|
||||
@Body()
|
||||
body: {
|
||||
traceId?: string;
|
||||
@ -27,6 +37,23 @@ export class LoggerController {
|
||||
offset: number;
|
||||
}
|
||||
) {
|
||||
return this.loggerService.find(body);
|
||||
return this.loggerService.findByProjectId(projectId, body);
|
||||
}
|
||||
|
||||
@Post("/:id/find")
|
||||
@UseGuards(QueryGuard)
|
||||
find(
|
||||
@Param("id") _id: string,
|
||||
@Body()
|
||||
body: {
|
||||
traceId?: string;
|
||||
fromDate?: Date;
|
||||
toDate?: Date;
|
||||
url?: string;
|
||||
limit: number;
|
||||
offset: number;
|
||||
}
|
||||
) {
|
||||
return this.loggerService.find(_id, body);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ import { Log } from "./entities/log.entity";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Repository } from "typeorm";
|
||||
import { TLog, TLogLine } from "./logger.types";
|
||||
import { Query } from "../entities/query.entity";
|
||||
|
||||
@Injectable()
|
||||
export class LoggerService {
|
||||
@ -11,8 +12,13 @@ export class LoggerService {
|
||||
private readonly logRepository: Repository<Log>
|
||||
) {}
|
||||
|
||||
async create(traceId: string, content: TLog): Promise<Log> {
|
||||
const log = this.logRepository.create({ traceId, content });
|
||||
async create(traceId: string, content: TLog, query: Query): Promise<Log> {
|
||||
const log = this.logRepository.create({
|
||||
traceId,
|
||||
content,
|
||||
query,
|
||||
project: query.project,
|
||||
});
|
||||
return await this.logRepository.save(log);
|
||||
}
|
||||
|
||||
@ -20,14 +26,14 @@ export class LoggerService {
|
||||
return await this.logRepository.find({ where: { traceId } });
|
||||
}
|
||||
|
||||
async find(data: {
|
||||
private prepareQuery(data: {
|
||||
traceId?: string;
|
||||
fromDate?: Date;
|
||||
toDate?: Date;
|
||||
url?: string;
|
||||
limit: number;
|
||||
offset: number;
|
||||
}): Promise<Log[]> {
|
||||
}) {
|
||||
const query = this.logRepository.createQueryBuilder("log");
|
||||
|
||||
if (data.traceId) {
|
||||
@ -37,21 +43,57 @@ export class LoggerService {
|
||||
if (data.fromDate) {
|
||||
query.andWhere("log.createdAt >= :fromDate", { fromDate: data.fromDate });
|
||||
}
|
||||
|
||||
if (data.toDate) {
|
||||
query.andWhere("log.createdAt <= :toDate", { toDate: data.toDate });
|
||||
}
|
||||
|
||||
if (data.url) {
|
||||
query.andWhere("log.content LIKE :url", { url: `%${data.url}%` });
|
||||
}
|
||||
|
||||
query.skip(data.offset).take(data.limit).orderBy("log.createdAt", "DESC");
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
async findByProjectId(
|
||||
projectId: string,
|
||||
data: {
|
||||
traceId?: string;
|
||||
fromDate?: Date;
|
||||
toDate?: Date;
|
||||
url?: string;
|
||||
limit: number;
|
||||
offset: number;
|
||||
}
|
||||
): Promise<Log[]> {
|
||||
const query = this.prepareQuery(data);
|
||||
query.where("log.projectId = :projectId", { projectId });
|
||||
|
||||
return await query.getMany();
|
||||
}
|
||||
|
||||
static log(logStack: TLog, log: TLog | TLogLine): TLog {
|
||||
async find(
|
||||
queryId: string,
|
||||
data: {
|
||||
traceId?: string;
|
||||
fromDate?: Date;
|
||||
toDate?: Date;
|
||||
url?: string;
|
||||
limit: number;
|
||||
offset: number;
|
||||
}
|
||||
): Promise<Log[]> {
|
||||
const query = this.prepareQuery(data);
|
||||
query.where("log.queryId = :queryId", { queryId });
|
||||
|
||||
return await query.getMany();
|
||||
}
|
||||
|
||||
static log(logStack: TLog, log: TLogLine): TLog {
|
||||
if (!log.timeStamp) {
|
||||
log.timeStamp = Date.now();
|
||||
}
|
||||
|
||||
logStack.content.push(log);
|
||||
return logStack;
|
||||
}
|
||||
|
||||
@ -10,12 +10,12 @@ export enum TLogType {
|
||||
export type TLogLine = {
|
||||
content: string;
|
||||
type: TLogType;
|
||||
timeStamp: number;
|
||||
timeStamp?: number;
|
||||
};
|
||||
|
||||
export interface TLog {
|
||||
traceId: string;
|
||||
content: (TLogLine | TLog)[];
|
||||
content: TLogLine[];
|
||||
payload?: any;
|
||||
headers: Record<string, string | string[] | undefined>;
|
||||
cookies: Record<string, string>;
|
||||
|
||||
Reference in New Issue
Block a user