mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +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:
		
							parent
							
								
									18ae49900b
								
							
						
					
					
						commit
						cf5e492dab
					
				@ -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,
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user