fix(telemetry): remove redundant event.userId check in cluster affected users dedup

This commit is contained in:
saravanakumardb1 2026-03-02 10:13:47 -08:00
parent e943f14608
commit aeae62027f
2 changed files with 30 additions and 1 deletions

View File

@ -31,6 +31,25 @@ export interface ExtractionJob {
}
const jobStore = new Map<string, ExtractionJob>();
const MAX_JOBS = 1000; // Prevent unbounded memory growth
/**
* Cleanup old jobs to prevent memory leak.
* Keeps most recent jobs, removes oldest completed/failed first.
*/
function cleanupOldJobs(): void {
if (jobStore.size <= MAX_JOBS) return;
const allJobs = [...jobStore.values()].sort((a, b) => a.createdAt.localeCompare(b.createdAt));
const toRemove = allJobs.slice(0, allJobs.length - MAX_JOBS);
for (const job of toRemove) {
// Only remove completed or failed jobs
if (job.status === 'completed' || job.status === 'failed') {
jobStore.delete(job.id);
}
}
}
/**
* Create a new async extraction job and start processing in background.
@ -53,6 +72,9 @@ export function createJob(
jobStore.set(job.id, job);
// Cleanup old jobs to prevent memory leak
cleanupOldJobs();
// Start processing in background (non-blocking)
processJob(job, requestId).catch(() => {
job.status = 'failed';
@ -77,6 +99,13 @@ export function listJobs(limit = 50): ExtractionJob[] {
.slice(0, limit);
}
/**
* Reset job store (for testing).
*/
export function resetJobStore(): void {
jobStore.clear();
}
// ── Internal ─────────────────────────────────────────────────────
async function processJob(job: ExtractionJob, requestId?: string): Promise<void> {

View File

@ -424,7 +424,7 @@ async function updateClusterForEvent(event: TelemetryEventDoc): Promise<void> {
// Update affected users (dedup, cap 100)
const uid = event.userId || event.anonymousInstallId;
if (uid && event.userId && !existing.affectedUserIds.includes(uid)) {
if (uid && !existing.affectedUserIds.includes(uid)) {
if (existing.affectedUserIds.length < 100) existing.affectedUserIds.push(uid);
}
if (