fix: performance hardening — eliminate full table scans (#9)
Some checks failed
Security Checks / dependency-audit (push) Has been cancelled
Security Checks / secret-scanning (push) Has been cancelled
Security Checks / dockerfile-lint (push) Has been cancelled

Inventory:
- /issues: replaced full scan + client filter with NocoDB server-side
  WHERE filter (Received eq Issues/Issue). Single query, ~200 rows max.
- /needs-review-count: replaced full scan with server-side WHERE +
  limit=1 + pageInfo.totalRows. Returns count without fetching data.

Budget:
- buildLookups(): added 2-minute cache for payee/account/category maps.
  Eliminates 3 API calls per request for repeated queries.
- /summary cache (added earlier): 1-minute TTL still active.

Files: services/inventory/server.js, services/budget/server.js
This commit is contained in:
Yusuf Suleman
2026-03-29 13:50:07 -05:00
parent 7a7286ac1c
commit 9e13984b05
2 changed files with 34 additions and 59 deletions

View File

@@ -102,8 +102,12 @@ function currentMonth() {
return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`;
}
/** Build lookup maps for payees, accounts, and categories. */
/** Build lookup maps for payees, accounts, and categories (cached 2 min). */
let _lookupsCache = { data: null, expiresAt: 0 };
async function buildLookups() {
if (_lookupsCache.data && Date.now() < _lookupsCache.expiresAt) {
return _lookupsCache.data;
}
const [payees, accounts, categories] = await Promise.all([
api.getPayees(),
api.getAccounts(),
@@ -115,7 +119,9 @@ async function buildLookups() {
for (const a of accounts) accountMap[a.id] = a.name;
const categoryMap = {};
for (const c of categories) categoryMap[c.id] = c.name;
return { payeeMap, accountMap, categoryMap };
const result = { payeeMap, accountMap, categoryMap };
_lookupsCache = { data: result, expiresAt: Date.now() + 120000 };
return result;
}
/** Enrich a transaction with resolved names. */