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;
}
// 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> {
return this.db(TABLE)
.count('*')
@ -248,18 +258,22 @@ export class ApiTokenStore implements IApiTokenStore {
legacyTokens: number;
activeLegacyTokens: number;
}> {
const allLegacyCount = this.db<ITokenRow>(`${TABLE} as tokens`)
const allLegacyCount = this.withTimer('allLegacyCount', () =>
this.db<ITokenRow>(`${TABLE} as tokens`)
.where('tokens.secret', 'NOT LIKE', '%:%')
.count()
.first()
.then((res) => Number(res?.count) || 0);
.then((res) => Number(res?.count) || 0),
);
const activeLegacyCount = this.db<ITokenRow>(`${TABLE} as tokens`)
const activeLegacyCount = this.withTimer('activeLegacyCount', () =>
this.db<ITokenRow>(`${TABLE} as tokens`)
.where('tokens.secret', 'NOT LIKE', '%:%')
.andWhereRaw("tokens.seen_at > NOW() - INTERVAL '3 MONTH'")
.count()
.first()
.then((res) => Number(res?.count) || 0);
.then((res) => Number(res?.count) || 0),
);
const orphanedTokensQuery = this.db<ITokenRow>(`${TABLE} as tokens`)
.leftJoin(
@ -276,18 +290,22 @@ export class ApiTokenStore implements IApiTokenStore {
.orWhere('tokens.type', ApiTokenType.FRONTEND);
});
const allOrphanedCount = orphanedTokensQuery
const allOrphanedCount = this.withTimer('allOrphanedCount', () =>
orphanedTokensQuery
.clone()
.count()
.first()
.then((res) => Number(res?.count) || 0);
.then((res) => Number(res?.count) || 0),
);
const activeOrphanedCount = orphanedTokensQuery
const activeOrphanedCount = this.withTimer('activeOrphanedCount', () =>
orphanedTokensQuery
.clone()
.andWhereRaw("tokens.seen_at > NOW() - INTERVAL '3 MONTH'")
.count()
.first()
.then((res) => Number(res?.count) || 0);
.then((res) => Number(res?.count) || 0),
);
const [
orphanedTokens,