opencode-statboard 2.10.34 → 2.10.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/server.js +27 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-statboard",
3
- "version": "2.10.34",
3
+ "version": "2.10.36",
4
4
  "description": "Real-time dashboard for opencode token usage and cost statistics",
5
5
  "main": "server.js",
6
6
  "bin": {
package/server.js CHANGED
@@ -14,6 +14,21 @@ let Database;
14
14
  try { Database = require('better-sqlite3'); } catch (_) { Database = null; }
15
15
  const pricing = require('./pricing');
16
16
 
17
+ // The report/trend endpoints only ever have four real scopes (the four pills in
18
+ // the UI). `days` used to be taken straight from the query string, so
19
+ // `?days=abc` fell through `parseInt(...) || 0` to 0 (= Daily) and every distinct
20
+ // value minted its own reportCache/trendCache entry that nothing ever evicted
21
+ // (cleanup runs every CACHE_CLEANUP_MS = 6h by default). Normalize to the known
22
+ // set so an arbitrary query param cannot pick a scope or grow the cache.
23
+ const SCOPE_DAYS = [-1, 1, 7, 30];
24
+ const DEFAULT_SCOPE = 7;
25
+ // Upper bound on reportCache/trendCache entries (see cleanExpiredCache).
26
+ const CACHE_MAX_KEYS = 16;
27
+ function normalizeScope(days) {
28
+ const n = parseInt(days, 10);
29
+ return SCOPE_DAYS.includes(n) ? n : DEFAULT_SCOPE;
30
+ }
31
+
17
32
  const PORT = parseInt(process.env.OC_PORT || '4868', 10);
18
33
  const REFRESH_MS = 15000;
19
34
  const TREND_MS = 60 * 1000;
@@ -214,6 +229,14 @@ function cleanExpiredCache(cache, ttl) {
214
229
  delete cache[key];
215
230
  }
216
231
  }
232
+ // Hard cap as a backstop: expiry alone is not a bound if a caller keeps
233
+ // minting keys faster than the (6h by default) cleanup interval. Drop the
234
+ // oldest entries first.
235
+ const keys = Object.keys(cache);
236
+ if (keys.length > CACHE_MAX_KEYS) {
237
+ keys.sort((a, b) => (cache[a].generatedAt || 0) - (cache[b].generatedAt || 0));
238
+ for (const k of keys.slice(0, keys.length - CACHE_MAX_KEYS)) delete cache[k];
239
+ }
217
240
  }
218
241
 
219
242
  async function generateTrend(days) {
@@ -1427,7 +1450,7 @@ const server = http.createServer(async (req, res) => {
1427
1450
  };
1428
1451
 
1429
1452
  if (u === '/api/stats') {
1430
- const days = parseInt(url.searchParams.get('days') || '7', 10) || 7;
1453
+ const days = normalizeScope(url.searchParams.get('days') || DEFAULT_SCOPE);
1431
1454
  const r = refreshTrend(days);
1432
1455
  respond({
1433
1456
  ok: !!state.data,
@@ -1441,7 +1464,7 @@ const server = http.createServer(async (req, res) => {
1441
1464
  });
1442
1465
  } else if (u === '/api/report') {
1443
1466
  const daysParam = url.searchParams.get('days');
1444
- const days = daysParam !== null ? (parseInt(daysParam, 10) || 0) : 0;
1467
+ const days = daysParam !== null ? normalizeScope(daysParam) : 0;
1445
1468
  const r = refreshReport(days);
1446
1469
  respond({
1447
1470
  ok: true,
@@ -1577,14 +1600,14 @@ async function fetchOpenRouterQuota() {
1577
1600
  const d = j.data || {};
1578
1601
  const eb = d.effective_budget || {};
1579
1602
  // free_model_daily_requests is account-level (shared across all keys of the same account).
1580
- // Only show RPD for the free tier key; paid keys don't have their own RPD.
1603
+ // Show RPD for all keys so the user can see the account's daily free model usage.
1581
1604
  const isFree = !!d.is_free_tier;
1582
1605
  accounts.push({
1583
1606
  label, limitUsd: d.limit != null ? d.limit : null, usedUsd: d.usage || 0,
1584
1607
  remainingUsd: d.limit_remaining != null ? d.limit_remaining : null,
1585
1608
  dailyUsed: d.usage_daily || 0, weeklyUsed: d.usage_weekly || 0, monthlyUsed: d.usage_monthly || 0,
1586
1609
  isFreeTier: isFree, expiresAt: d.expires_at || null,
1587
- freeModelDaily: isFree ? (d.free_model_daily_requests || null) : null,
1610
+ freeModelDaily: d.free_model_daily_requests || null,
1588
1611
  budgetScope: eb.scope || null, budgetLimit: eb.limit_usd != null ? eb.limit_usd : null,
1589
1612
  budgetSpend: eb.spend_usd != null ? eb.spend_usd : null,
1590
1613
  budgetRemaining: eb.remaining_usd != null ? eb.remaining_usd : null,