Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add timers to count queries #8393

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading