mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-18 15:00:00 +00:00
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import { AdvancedConsoleLogger } from "typeorm/logger/AdvancedConsoleLogger";
|
|
|
|
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): 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;
|
|
}
|