perf(usage): bound lastUsed overlay scan to 2-day window; reach max thinking tier

getUsageStats("all") shipped the entire usageHistory table to JS just to
refine lastUsed (~2s on 290K rows, on every statsEmitter update per SSE
listener). Bound the overlay to a 2-day indexed range scan; older entries
keep day-level lastUsed from usageDaily aggregates. Totals unaffected.

budgetToLevel now maps budgets > 80384 (midpoint of 32768/128000) to
"max" instead of clamping to "xhigh", so the top reasoning tier is
reachable from large budget_tokens requests.
This commit is contained in:
Rafli Ahmad Zulfikar
2026-09-22 15:08:38 +07:00
committed by decolua
parent ce9ac43da5
commit d1de324586
3 changed files with 52 additions and 3 deletions
+9 -2
View File
@@ -537,8 +537,15 @@ export async function getUsageStats(period = "all") {
}
}
// Overlay precise lastUsed timestamps from history
const overlayCutoff = maxDays ? Date.now() - maxDays * 86400000 : 0;
// Overlay precise lastUsed timestamps from history.
// ponytail: overlay scans only a recent window; entries older than that keep
// day-level lastUsed from usageDaily. Upgrade to a materialized per-key
// MAX(timestamp) table if exact old timestamps ever matter.
const OVERLAY_WINDOW_MS = 2 * 86400000;
const overlayCutoff = Math.max(
maxDays ? Date.now() - maxDays * 86400000 : 0,
Date.now() - OVERLAY_WINDOW_MS
);
const histRows = db.all(
`SELECT timestamp, provider, model, connectionId, apiKey, endpoint FROM usageHistory WHERE timestamp >= ?`,
[new Date(overlayCutoff).toISOString()]