From 067f18aaa18123fdeb3e09b8f3155c56c298603c Mon Sep 17 00:00:00 2001 From: luulam Date: Sun, 2 Aug 2026 22:48:15 +0700 Subject: [PATCH] fix(usage): ReferenceError in byProvider lastUsed overlay broke daily-summary periods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The provider lastUsed overlay loop referenced histRows before its const declaration (temporal dead zone), throwing ReferenceError for 7d/30d/60d/all which use the usageDaily summary path — leaving the overview cards and tables empty regardless of the selected period. Merged the provider overlay into the existing histRows loop. --- src/lib/db/repos/usageRepo.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/lib/db/repos/usageRepo.js b/src/lib/db/repos/usageRepo.js index eb39be29..3f0aea24 100644 --- a/src/lib/db/repos/usageRepo.js +++ b/src/lib/db/repos/usageRepo.js @@ -772,13 +772,6 @@ export async function getUsageStats(period = "all") { } // Overlay precise lastUsed timestamps from history - for (const e of histRows) { - if ( - stats.byProvider[e.provider] && - new Date(e.timestamp) > new Date(stats.byProvider[e.provider].lastUsed) - ) - stats.byProvider[e.provider].lastUsed = e.timestamp; - } const overlayCutoff = maxDays ? Date.now() - maxDays * 86400000 : 0; const histRows = db.all( `SELECT timestamp, provider, model, connectionId, apiKey, endpoint FROM usageHistory WHERE timestamp >= ?`, @@ -786,6 +779,11 @@ export async function getUsageStats(period = "all") { ); for (const e of histRows) { const ts = e.timestamp; + if ( + stats.byProvider[e.provider] && + new Date(ts) > new Date(stats.byProvider[e.provider].lastUsed) + ) + stats.byProvider[e.provider].lastUsed = ts; const modelKey = e.provider ? `${e.model} (${e.provider})` : e.model; if ( stats.byModel[modelKey] &&