/** * Debounced batch refresh queue for user_admin_stats. */ import * as userAdminStatsRepository from '../repositories/userAdminStatsRepository'; const DEBOUNCE_MS = 400; const FLUSH_BATCH_SIZE = 40; const pendingUserIds = new Set(); let flushTimer: ReturnType | null = null; let flushRunning = false; let backfillRunning = false; const BACKFILL_BATCH_SIZE = 250; export async function refreshUserAdminStats(userId: string): Promise { await userAdminStatsRepository.refreshUserStats(userId); } /** * Queues a user for stats refresh; coalesced and flushed in batches. */ export function queueUserAdminStatsRefresh(userId: string): void { if (!userId) return; pendingUserIds.add(userId); scheduleFlush(); } function scheduleFlush(): void { if (flushTimer) return; flushTimer = setTimeout(() => { flushTimer = null; void flushPendingRefreshes(); }, DEBOUNCE_MS); } async function flushPendingRefreshes(): Promise { if (flushRunning) { scheduleFlush(); return; } if (pendingUserIds.size === 0) return; flushRunning = true; try { while (pendingUserIds.size > 0) { const batch = [...pendingUserIds].slice(0, FLUSH_BATCH_SIZE); for (const id of batch) pendingUserIds.delete(id); try { await userAdminStatsRepository.refreshUserStatsForIds(batch); } catch (e) { console.warn(`[admin-stats] batch refresh failed (${batch.length} user(s)):`, e); } } } finally { flushRunning = false; if (pendingUserIds.size > 0) scheduleFlush(); } } export async function backfillAllUserAdminStats(): Promise<{ processed: number; total: number }> { const total = await userAdminStatsRepository.countUsers(); let processed = 0; let offset = 0; while (offset < total) { const n = await userAdminStatsRepository.backfillStatsBatch(BACKFILL_BATCH_SIZE, offset); if (n === 0) break; processed += n; offset += BACKFILL_BATCH_SIZE; } return { processed, total }; } export function scheduleAdminStatsBackfill(): void { if (backfillRunning) return; backfillRunning = true; setImmediate(async () => { try { const missing = await userAdminStatsRepository.countUsersMissingStats(); const total = await userAdminStatsRepository.countUsers(); if (missing === 0 && total > 0) { console.log(`[admin-stats] ${total} user(s) already have stats rows`); return; } console.log( `[admin-stats] Backfilling stats (${missing} missing of ${total} users, batch ${BACKFILL_BATCH_SIZE})…` ); const result = await backfillAllUserAdminStats(); console.log(`[admin-stats] Backfill done: ${result.processed}/${result.total} user(s)`); } catch (e) { console.error('[admin-stats] Backfill error:', e); } finally { backfillRunning = false; } }); } /** Drains pending refreshes (tests / graceful shutdown). */ export async function flushUserAdminStatsQueue(): Promise { if (flushTimer) { clearTimeout(flushTimer); flushTimer = null; } await flushPendingRefreshes(); }