diff --git a/convex/reports.ts b/convex/reports.ts index b616f4c..928f998 100644 --- a/convex/reports.ts +++ b/convex/reports.ts @@ -508,9 +508,11 @@ async function forEachScopedTicketByResolvedRangeChunked( }) .order("desc"); - // Limita a 1000 tickets por chunk para evitar OOM - const snapshot = await query.take(1000); - for (const ticket of snapshot) { + // Coleta tickets do chunk (o chunk ja e limitado por periodo) + const snapshot = await query.collect(); + // Limita processamento a 1000 tickets por chunk para evitar timeout + const limitedSnapshot = snapshot.slice(0, 1000); + for (const ticket of limitedSnapshot) { const resolvedAt = typeof ticket.resolvedAt === "number" ? ticket.resolvedAt : null; if (resolvedAt === null) continue; if (resolvedAt < chunkStart || resolvedAt >= chunkEnd) continue; @@ -533,10 +535,11 @@ export async function fetchOpenScopedTickets( // Limita a 500 tickets por status para evitar OOM const MAX_PER_STATUS = 500; for (const status of statuses) { - const snapshot = await ctx.db + const allTickets = await ctx.db .query("tickets") .withIndex("by_tenant_status", (q) => q.eq("tenantId", tenantId).eq("status", status)) - .take(MAX_PER_STATUS); + .collect(); + const snapshot = allTickets.slice(0, MAX_PER_STATUS); for (const ticket of snapshot) { if (!OPEN_STATUSES.has(normalizeStatus(ticket.status))) continue; if (scopedCompanyId && ticket.companyId !== scopedCompanyId) continue; @@ -1417,10 +1420,11 @@ export async function agentProductivityHandler( for (const [agentId, acc] of map) { // Limita a 1000 sessoes por agente para evitar OOM - const sessions = await ctx.db + const allSessions = await ctx.db .query("ticketWorkSessions") .withIndex("by_agent", (q) => q.eq("agentId", agentId as Id<"users">)) - .take(1000) + .collect() + const sessions = allSessions.slice(0, 1000) let total = 0 for (const s of sessions) { const started = s.startedAt