1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-04 00:18:01 +01:00

chore: add timers to count queries (#8393)

## About the changes
These might be some heavy queries, adding timers to them to validate
that assumption and get some insights
This commit is contained in:
Gastón Fournier 2024-10-08 15:41:00 +02:00 committed by GitHub
parent 18ae49900b
commit cf5e492dab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -105,6 +105,16 @@ export class ApiTokenStore implements IApiTokenStore {
this.flagResolver = flagResolver; this.flagResolver = flagResolver;
} }
// helper function that we can move to utils
async withTimer<T>(timerName: string, fn: () => Promise<T>): Promise<T> {
const stopTimer = this.timer(timerName);
try {
return await fn();
} finally {
stopTimer();
}
}
async count(): Promise<number> { async count(): Promise<number> {
return this.db(TABLE) return this.db(TABLE)
.count('*') .count('*')
@ -248,18 +258,22 @@ export class ApiTokenStore implements IApiTokenStore {
legacyTokens: number; legacyTokens: number;
activeLegacyTokens: number; activeLegacyTokens: number;
}> { }> {
const allLegacyCount = this.db<ITokenRow>(`${TABLE} as tokens`) const allLegacyCount = this.withTimer('allLegacyCount', () =>
.where('tokens.secret', 'NOT LIKE', '%:%') this.db<ITokenRow>(`${TABLE} as tokens`)
.count() .where('tokens.secret', 'NOT LIKE', '%:%')
.first() .count()
.then((res) => Number(res?.count) || 0); .first()
.then((res) => Number(res?.count) || 0),
);
const activeLegacyCount = this.db<ITokenRow>(`${TABLE} as tokens`) const activeLegacyCount = this.withTimer('activeLegacyCount', () =>
.where('tokens.secret', 'NOT LIKE', '%:%') this.db<ITokenRow>(`${TABLE} as tokens`)
.andWhereRaw("tokens.seen_at > NOW() - INTERVAL '3 MONTH'") .where('tokens.secret', 'NOT LIKE', '%:%')
.count() .andWhereRaw("tokens.seen_at > NOW() - INTERVAL '3 MONTH'")
.first() .count()
.then((res) => Number(res?.count) || 0); .first()
.then((res) => Number(res?.count) || 0),
);
const orphanedTokensQuery = this.db<ITokenRow>(`${TABLE} as tokens`) const orphanedTokensQuery = this.db<ITokenRow>(`${TABLE} as tokens`)
.leftJoin( .leftJoin(
@ -276,18 +290,22 @@ export class ApiTokenStore implements IApiTokenStore {
.orWhere('tokens.type', ApiTokenType.FRONTEND); .orWhere('tokens.type', ApiTokenType.FRONTEND);
}); });
const allOrphanedCount = orphanedTokensQuery const allOrphanedCount = this.withTimer('allOrphanedCount', () =>
.clone() orphanedTokensQuery
.count() .clone()
.first() .count()
.then((res) => Number(res?.count) || 0); .first()
.then((res) => Number(res?.count) || 0),
);
const activeOrphanedCount = orphanedTokensQuery const activeOrphanedCount = this.withTimer('activeOrphanedCount', () =>
.clone() orphanedTokensQuery
.andWhereRaw("tokens.seen_at > NOW() - INTERVAL '3 MONTH'") .clone()
.count() .andWhereRaw("tokens.seen_at > NOW() - INTERVAL '3 MONTH'")
.first() .count()
.then((res) => Number(res?.count) || 0); .first()
.then((res) => Number(res?.count) || 0),
);
const [ const [
orphanedTokens, orphanedTokens,