Files
LogiFlow/server/maintenance.ts
T
MichaelandClaude Fable 5 753b301066 perf: add missing indexes and remove N+1 queries
The app degraded progressively over a year of data growth. Four causes,
all cumulative:

1. No indexes. Apart from primary keys, unique constraints and
   session.expire, no table carried an index. PostgreSQL does not index
   foreign keys automatically, so every filter and join on group_id,
   supplier_id, order_id and the date columns did a sequential scan.
   Worst offender: user_groups.user_id, read by getUserWithGroups() on
   every authenticated request.

2. N+1 in the order and delivery listings. getOrders,
   getOrdersByDateRange, getDeliveries and getDeliveriesByDateRange
   issued one to two queries per row to load relations. Relations are now
   loaded in bulk and grouped in Node: three queries regardless of volume.

3. /api/sync-order-delivery-status reloaded the whole deliveries table on
   every iteration of its loop over orders. It now uses the deliveries
   getOrders() already attaches.

4. clearExpiredCache() was implemented but never called, so
   invoice_verification_cache grew without bound. Now scheduled every 6h.

Also replaces the full-history downloads on the Groups and Suppliers
pages, which fetched every order and delivery with nested relations only
to count rows, with aggregate endpoints that count in the database.

Indexes are created via scripts/auto-migrate-production.sh, the script
that actually runs at deploy time, using CREATE INDEX CONCURRENTLY so no
write lock is taken. Note that server/migrations.ts explicitly ignores
the migrations/ directory and runs only hardcoded migrations; the SQL
file added there is for reference and manual application.

Verified: typecheck baseline 440 errors, 430 after, none new in the
changed code; vite build passes; server boots; functional test confirms
the aggregate endpoints match the source data including the delivered
count.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-14 11:55:49 +02:00

28 lines
1.1 KiB
TypeScript

import { storage } from "./storage.js";
// Purge périodique du cache de vérification des factures.
//
// La table invoice_verification_cache porte une colonne expires_at et
// storage.clearExpiredCache() sait la purger, mais rien ne l'appelait : les
// entrées expirées s'accumulaient indéfiniment et alourdissaient chaque lecture
// du cache. On planifie donc la purge ici.
const PURGE_INTERVAL_MS = 6 * 60 * 60 * 1000; // 6 heures
const STARTUP_DELAY_MS = 60 * 1000; // laisse le démarrage se terminer d'abord
async function purgeExpiredInvoiceCache(): Promise<void> {
try {
await storage.clearExpiredCache();
console.log("🧹 [MAINTENANCE] Cache de vérification des factures purgé");
} catch (error) {
// Une purge ratée ne doit jamais interrompre le service.
console.error("❌ [MAINTENANCE] Échec de la purge du cache factures:", error);
}
}
export function startMaintenanceJobs(): void {
setTimeout(purgeExpiredInvoiceCache, STARTUP_DELAY_MS);
setInterval(purgeExpiredInvoiceCache, PURGE_INTERVAL_MS);
console.log("✅ [MAINTENANCE] Purge du cache factures planifiée (toutes les 6h)");
}