games_app/backend/src/services/userAdminStatsService.ts
2026-06-29 15:01:23 +02:00

110 lines
3.1 KiB
TypeScript

/**
* 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<string>();
let flushTimer: ReturnType<typeof setTimeout> | null = null;
let flushRunning = false;
let backfillRunning = false;
const BACKFILL_BATCH_SIZE = 250;
export async function refreshUserAdminStats(userId: string): Promise<void> {
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<void> {
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<void> {
if (flushTimer) {
clearTimeout(flushTimer);
flushTimer = null;
}
await flushPendingRefreshes();
}