opencode-statboard 2.10.19 → 2.10.21
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.
- package/attr-worker.js +4 -2
- package/package.json +1 -1
- package/run-check.bat +3 -0
- package/server.js +24 -7
package/attr-worker.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
const { parentPort, workerData } = require('worker_threads');
|
|
2
|
+
if (workerData && workerData.dbPath && !process.env.OPENCODE_DATA) {
|
|
3
|
+
process.env.OPENCODE_DATA = workerData.dbPath;
|
|
4
|
+
}
|
|
2
5
|
const srv = require('./server.js');
|
|
3
6
|
|
|
4
|
-
// I/O throttling parameters (from env, with defaults)
|
|
5
7
|
const ITHROTTLE_BATCH = parseInt(process.env.OPENCODE_ITHROTTLE_BATCH || '1000', 10);
|
|
6
8
|
const ITHROTTLE_DELAY_MS = parseInt(process.env.OPENCODE_ITHROTTLE_DELAY_MS || '5', 10);
|
|
7
9
|
|
|
@@ -25,4 +27,4 @@ function throttle(iteration) {
|
|
|
25
27
|
});
|
|
26
28
|
})().catch((e) => {
|
|
27
29
|
parentPort.postMessage({ ok: false, error: (e && e.message) || String(e) });
|
|
28
|
-
});
|
|
30
|
+
});
|
package/package.json
CHANGED
package/run-check.bat
ADDED
package/server.js
CHANGED
|
@@ -294,9 +294,10 @@ function splitModelName(name) {
|
|
|
294
294
|
function marketOf(tok, provider, model) {
|
|
295
295
|
const pr = pricing.priceFor(provider, model);
|
|
296
296
|
if (!pr) return { market: null, saved: null, pricedAs: null, priced: false, kind: 'unpriced' };
|
|
297
|
-
const
|
|
297
|
+
const marketRaw = pricing.marketOf(tok, pr);
|
|
298
298
|
const r4m = (v) => Math.round(v * 10000) / 10000;
|
|
299
|
-
|
|
299
|
+
// Calculate saved from unrounded market to avoid double-rounding error.
|
|
300
|
+
return { market: r4m(marketRaw), saved: r4m(marketRaw - (tok.cost || 0)), pricedAs: pr.source, priced: true, kind: pr.kind };
|
|
300
301
|
}
|
|
301
302
|
|
|
302
303
|
// Hybrid usage attribution over [startMs, endMs).
|
|
@@ -740,7 +741,20 @@ function getAttrAll() {
|
|
|
740
741
|
function getAttrPeriod(startMs) {
|
|
741
742
|
if (!attrAllCache) refreshAttrBundle();
|
|
742
743
|
if (!attrAllCache) return EMPTY_ATTR;
|
|
743
|
-
|
|
744
|
+
const periods = attrAllCache.a.periods;
|
|
745
|
+
const hit = periods.get(startMs);
|
|
746
|
+
if (hit) return hit;
|
|
747
|
+
// Key miss must NEVER fall back to the all-time buckets: that silently
|
|
748
|
+
// rendered lifetime tokens inside a Daily/Weekly card (the "7 days shows
|
|
749
|
+
// all-time models" bug). Pick the deepest slice that starts at or before
|
|
750
|
+
// the requested boundary; only a boundary older than every slice (or a
|
|
751
|
+
// bundle with no slices) legitimately means "all time".
|
|
752
|
+
let best = null;
|
|
753
|
+
for (const [k, sl] of periods) {
|
|
754
|
+
if (k === 0) continue;
|
|
755
|
+
if (k <= startMs && (!best || k > best.start)) best = sl;
|
|
756
|
+
}
|
|
757
|
+
return best || attrAllCache.a;
|
|
744
758
|
}
|
|
745
759
|
// Kick a fresh scan. The scan runs in a WORKER THREAD because it is
|
|
746
760
|
// synchronous better-sqlite3 + JSON work: inline it froze the event loop for
|
|
@@ -803,7 +817,7 @@ function spawnAttrWorker(now, rev) {
|
|
|
803
817
|
}
|
|
804
818
|
let w;
|
|
805
819
|
try {
|
|
806
|
-
w = new Worker(ATTR_WORKER, { workerData: { now, toolsStartMs: 0, dbRevision: rev } });
|
|
820
|
+
w = new Worker(ATTR_WORKER, { workerData: { now, toolsStartMs: 0, dbRevision: rev, dbPath: process.env.OPENCODE_DATA || '' } });
|
|
807
821
|
} catch (e) {
|
|
808
822
|
// Worker unavailable (very old Node / restricted env): build inline.
|
|
809
823
|
console.error('attr worker spawn failed, building inline:', e.message);
|
|
@@ -966,6 +980,7 @@ function getStatsFromDb(days) {
|
|
|
966
980
|
if (info.ok) {
|
|
967
981
|
priceAsOf = new Date(info.mtimeMs).toISOString();
|
|
968
982
|
marketTotal = 0;
|
|
983
|
+
savedTotal = 0;
|
|
969
984
|
for (const [name, m] of Object.entries(attr.perModel)) {
|
|
970
985
|
const { provider, model } = splitModelName(name);
|
|
971
986
|
const tok = { input: m.input, output: m.output, reasoning: m.reasoning, cacheRead: m.cacheRead, cost: m.cost };
|
|
@@ -976,10 +991,11 @@ function getStatsFromDb(days) {
|
|
|
976
991
|
continue;
|
|
977
992
|
}
|
|
978
993
|
marketTotal += mk.market;
|
|
994
|
+
savedTotal += mk.saved;
|
|
979
995
|
if (mk.kind === 'proxy') proxyMarket += mk.market;
|
|
980
996
|
}
|
|
981
997
|
marketTotal = Math.round(marketTotal * 10000) / 10000;
|
|
982
|
-
savedTotal = Math.round(
|
|
998
|
+
savedTotal = Math.round(savedTotal * 10000) / 10000;
|
|
983
999
|
proxyMarket = Math.round(proxyMarket * 10000) / 10000;
|
|
984
1000
|
}
|
|
985
1001
|
}
|
|
@@ -1142,7 +1158,7 @@ function periodStartMs(days, now) {
|
|
|
1142
1158
|
}
|
|
1143
1159
|
|
|
1144
1160
|
function buildReportFromDb(days) {
|
|
1145
|
-
const now = Date.now();
|
|
1161
|
+
const now = attrAllCache ? attrAllCache.at : Date.now(); // use bundle time for consistency
|
|
1146
1162
|
const startMs = periodStartMs(days, now);
|
|
1147
1163
|
const label = days === -1 ? 'All' : days <= 1 ? 'Daily' : days <= 7 ? 'Weekly' : 'Monthly';
|
|
1148
1164
|
|
|
@@ -1318,6 +1334,7 @@ function buildReportFromDb(days) {
|
|
|
1318
1334
|
const sumMkt = (rows) => rows.reduce((s, r) => s + (r.market || 0), 0);
|
|
1319
1335
|
const sumSaved = (rows) => rows.reduce((s, r) => s + (r.saved || 0), 0);
|
|
1320
1336
|
statsR.cost.marketTotal = Math.round(sumMkt(pProviderRows) * 10000) / 10000;
|
|
1337
|
+
// savedTotal = sum of per-model saved so it always matches the model table.
|
|
1321
1338
|
statsR.cost.savedTotal = Math.round(sumSaved(pProviderRows) * 10000) / 10000;
|
|
1322
1339
|
statsR.cost.savedPct = statsR.cost.marketTotal > 0 ? Math.round((statsR.cost.savedTotal / statsR.cost.marketTotal) * 1000) / 10 : null;
|
|
1323
1340
|
statsR.cost.priceAsOf = pricing.catalogInfo().ok ? new Date(pricing.catalogInfo().mtimeMs).toISOString() : null;
|
|
@@ -1334,7 +1351,7 @@ function buildReportFromDb(days) {
|
|
|
1334
1351
|
providers: pProviderRows,
|
|
1335
1352
|
sessions: pSessionRows,
|
|
1336
1353
|
priceAsOf: statsR.cost.priceAsOf,
|
|
1337
|
-
generatedAt:
|
|
1354
|
+
generatedAt: aNow, // bundle build time (consistent with period bucket boundaries)
|
|
1338
1355
|
};
|
|
1339
1356
|
}
|
|
1340
1357
|
|