From cba9a922a6fdb70ce2e0db9fee7a22667a6fba95 Mon Sep 17 00:00:00 2001 From: Brayden Wilmoth Date: Sat, 25 Jan 2025 09:53:06 -0500 Subject: [PATCH 1/2] feat: Omit queries from QueryLog plugin --- plugins/query-log/index.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/plugins/query-log/index.ts b/plugins/query-log/index.ts index 0bc6d5a..be09386 100644 --- a/plugins/query-log/index.ts +++ b/plugins/query-log/index.ts @@ -20,6 +20,10 @@ export class QueryLogPlugin extends StarbasePlugin { executionContext?: ExecutionContext // Add TTL configuration (default 1 day) private readonly ttl: number = 1 + // Enables the omission of queries who contain the prefix + omitEnabled?: boolean = false + // Prefix that if a query contains it won't be logged + omitPrefix: string = '--OMIT' state = { startTime: new Date(), @@ -28,9 +32,18 @@ export class QueryLogPlugin extends StarbasePlugin { query: '', } - constructor(opts?: { ctx?: ExecutionContext }) { + constructor(opts?: { + ctx?: ExecutionContext + enableOmit?: boolean + omitPrefix?: string + }) { super('starbasedb:query-log') this.executionContext = opts?.ctx + this.omitEnabled = opts?.enableOmit + + if (opts?.omitPrefix) { + this.omitPrefix = opts?.omitPrefix + } } override async register(app: StarbaseApp) { @@ -74,7 +87,12 @@ export class QueryLogPlugin extends StarbasePlugin { this.state.totalTime = this.state.endTime.getTime() - this.state.startTime.getTime() - if (opts.dataSource) { + // Queries can be omitted from + if ( + opts.dataSource && + !opts.sql.toUpperCase().trim().startsWith(this.omitPrefix) && + this.omitEnabled + ) { this.addQuery(opts?.dataSource) } From 58749c5227de58ac8af102f4997bfb041fe10504 Mon Sep 17 00:00:00 2001 From: Brayden Wilmoth Date: Sun, 26 Jan 2025 16:24:09 -0500 Subject: [PATCH 2/2] Fix logic --- plugins/query-log/index.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/plugins/query-log/index.ts b/plugins/query-log/index.ts index be09386..1dc42f5 100644 --- a/plugins/query-log/index.ts +++ b/plugins/query-log/index.ts @@ -88,12 +88,17 @@ export class QueryLogPlugin extends StarbasePlugin { this.state.endTime.getTime() - this.state.startTime.getTime() // Queries can be omitted from - if ( - opts.dataSource && - !opts.sql.toUpperCase().trim().startsWith(this.omitPrefix) && - this.omitEnabled - ) { - this.addQuery(opts?.dataSource) + if (opts.dataSource) { + if ( + (this.omitEnabled && + !opts.sql + .toUpperCase() + .trim() + .startsWith(this.omitPrefix)) || + !this.omitEnabled + ) { + this.addQuery(opts?.dataSource) + } } // Do a purge action for older than TTL items