fix(convex): corrigir memory leak com .collect() sem limite e adicionar otimizacoes

Problema: Convex backend consumindo 16GB+ de RAM causando OOM kills

Correcoes aplicadas:
- Substituido todos os .collect() por .take(LIMIT) em 27+ arquivos
- Adicionado indice by_usbPolicyStatus para otimizar query de maquinas
- Corrigido N+1 problem em alerts.ts usando Map lookup
- Corrigido full table scan em usbPolicy.ts
- Corrigido subscription leaks no frontend (tickets-view, use-ticket-categories)
- Atualizado versao do Convex backend para precompiled-2025-12-04-cc6af4c

Arquivos principais modificados:
- convex/*.ts - limites em todas as queries .collect()
- convex/schema.ts - novo indice by_usbPolicyStatus
- convex/alerts.ts - N+1 fix com Map
- convex/usbPolicy.ts - uso do novo indice
- src/components/tickets/tickets-view.tsx - skip condicional
- src/hooks/use-ticket-categories.ts - skip condicional

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
esdrasrenan 2025-12-09 21:30:06 -03:00
parent a4b46b08ba
commit 638faeb287
33 changed files with 139 additions and 128 deletions

View file

@ -208,7 +208,7 @@ export const list = query({
const categories = await ctx.db
.query("ticketCategories")
.withIndex("by_tenant_order", (q) => q.eq("tenantId", tenantId))
.collect()
.take(100)
if (categories.length === 0) {
return []
@ -217,7 +217,7 @@ export const list = query({
const subcategories = await ctx.db
.query("ticketSubcategories")
.withIndex("by_tenant_slug", (q) => q.eq("tenantId", tenantId))
.collect()
.take(100)
return categories.map((category) => ({
id: category._id,
@ -249,7 +249,7 @@ export const ensureDefaults = mutation({
const existingCount = await ctx.db
.query("ticketCategories")
.withIndex("by_tenant", (q) => q.eq("tenantId", tenantId))
.collect()
.take(100)
if (existingCount.length > 0) {
return { created: 0 }
@ -408,7 +408,7 @@ export const deleteCategory = mutation({
const subs = await ctx.db
.query("ticketSubcategories")
.withIndex("by_category_order", (q) => q.eq("categoryId", categoryId))
.collect()
.take(100)
for (const sub of subs) {
await ctx.db.patch(sub._id, {
categoryId: transferTo,
@ -418,7 +418,7 @@ export const deleteCategory = mutation({
const ticketsToMove = await ctx.db
.query("tickets")
.withIndex("by_tenant_category", (q) => q.eq("tenantId", tenantId).eq("categoryId", categoryId))
.collect()
.take(100)
for (const ticket of ticketsToMove) {
await ctx.db.patch(ticket._id, {
categoryId: transferTo,
@ -437,7 +437,7 @@ export const deleteCategory = mutation({
const subs = await ctx.db
.query("ticketSubcategories")
.withIndex("by_category_order", (q) => q.eq("categoryId", categoryId))
.collect()
.take(100)
for (const sub of subs) {
await ctx.db.delete(sub._id)
}
@ -530,7 +530,7 @@ export const deleteSubcategory = mutation({
const tickets = await ctx.db
.query("tickets")
.withIndex("by_tenant_subcategory", (q) => q.eq("tenantId", tenantId).eq("subcategoryId", subcategoryId))
.collect()
.take(100)
for (const ticket of tickets) {
await ctx.db.patch(ticket._id, {
subcategoryId: transferTo,