mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-16 22:21:51 +00:00
fix: add missing queryLogger.ts
This commit is contained in:
parent
a7d71e0762
commit
65b87f4e0b
1 changed files with 42 additions and 0 deletions
42
backend/src/data/queryLogger.ts
Normal file
42
backend/src/data/queryLogger.ts
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
import { AdvancedConsoleLogger } from "typeorm/logger/AdvancedConsoleLogger";
|
||||||
|
import type { QueryRunner } from "typeorm";
|
||||||
|
|
||||||
|
let groupedQueryStats: Map<string, number> = new Map();
|
||||||
|
|
||||||
|
const selectTableRegex = /FROM `?([^\s`]+)/i;
|
||||||
|
const updateTableRegex = /UPDATE `?([^\s`]+)/i;
|
||||||
|
const deleteTableRegex = /FROM `?([^\s`]+)/;
|
||||||
|
const insertTableRegex = /INTO `?([^\s`]+)/;
|
||||||
|
|
||||||
|
export class QueryLogger extends AdvancedConsoleLogger {
|
||||||
|
logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner): any {
|
||||||
|
let type: string | undefined;
|
||||||
|
let table: string | undefined;
|
||||||
|
|
||||||
|
if (query.startsWith("SELECT")) {
|
||||||
|
type = "SELECT";
|
||||||
|
table = query.match(selectTableRegex)?.[1];
|
||||||
|
} else if (query.startsWith("UPDATE")) {
|
||||||
|
type = "UPDATE";
|
||||||
|
table = query.match(updateTableRegex)?.[1];
|
||||||
|
} else if (query.startsWith("DELETE")) {
|
||||||
|
type = "DELETE";
|
||||||
|
table = query.match(deleteTableRegex)?.[1];
|
||||||
|
} else if (query.startsWith("INSERT")) {
|
||||||
|
type = "INSERT";
|
||||||
|
table = query.match(insertTableRegex)?.[1];
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const key = `${type} ${table}`;
|
||||||
|
const newCount = (groupedQueryStats.get(key) ?? 0) + 1;
|
||||||
|
groupedQueryStats.set(key, newCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function consumeQueryStats() {
|
||||||
|
const map = groupedQueryStats;
|
||||||
|
groupedQueryStats = new Map();
|
||||||
|
return map;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue