Skip to content

Commit

Permalink
chore: add timers to count queries (#8393)
Browse files Browse the repository at this point in the history
## About the changes
These might be some heavy queries, adding timers to them to validate
that assumption and get some insights
  • Loading branch information
gastonfournier authored Oct 8, 2024
1 parent 18ae499 commit cf5e492
Showing 1 changed file with 42 additions and 24 deletions.
66 changes: 42 additions & 24 deletions src/lib/db/api-token-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('*')
Expand Down Expand Up @@ -248,18 +258,22 @@ export class ApiTokenStore implements IApiTokenStore {
legacyTokens: number;
activeLegacyTokens: number;
}> {
const allLegacyCount = this.db<ITokenRow>(`${TABLE} as tokens`)
.where('tokens.secret', 'NOT LIKE', '%:%')
.count()
.first()
.then((res) => Number(res?.count) || 0);

const 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);
const allLegacyCount = this.withTimer('allLegacyCount', () =>
this.db<ITokenRow>(`${TABLE} as tokens`)
.where('tokens.secret', 'NOT LIKE', '%:%')
.count()
.first()
.then((res) => Number(res?.count) || 0),
);

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),
);

const orphanedTokensQuery = this.db<ITokenRow>(`${TABLE} as tokens`)
.leftJoin(
Expand All @@ -276,18 +290,22 @@ export class ApiTokenStore implements IApiTokenStore {
.orWhere('tokens.type', ApiTokenType.FRONTEND);
});

const allOrphanedCount = orphanedTokensQuery
.clone()
.count()
.first()
.then((res) => Number(res?.count) || 0);

const activeOrphanedCount = orphanedTokensQuery
.clone()
.andWhereRaw("tokens.seen_at > NOW() - INTERVAL '3 MONTH'")
.count()
.first()
.then((res) => Number(res?.count) || 0);
const allOrphanedCount = this.withTimer('allOrphanedCount', () =>
orphanedTokensQuery
.clone()
.count()
.first()
.then((res) => Number(res?.count) || 0),
);

const activeOrphanedCount = this.withTimer('activeOrphanedCount', () =>
orphanedTokensQuery
.clone()
.andWhereRaw("tokens.seen_at > NOW() - INTERVAL '3 MONTH'")
.count()
.first()
.then((res) => Number(res?.count) || 0),
);

const [
orphanedTokens,
Expand Down

0 comments on commit cf5e492

Please sign in to comment.