opencode-statboard 2.0.3 → 2.0.5
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/package.json +1 -1
- package/server.js +83 -31
package/package.json
CHANGED
package/server.js
CHANGED
|
@@ -270,10 +270,12 @@ function attributeUsage(startMs, endMs) {
|
|
|
270
270
|
// 2. sessions intersecting the period: spread residual
|
|
271
271
|
const srows = dbQuery(`SELECT id, agent, COALESCE(json_extract(model, '$.providerID'), 'unknown') AS provider, COALESCE(json_extract(model, '$.id'), 'unknown') AS model, time_created AS tc, time_updated AS tu, tokens_input AS si, tokens_output AS so, tokens_reasoning AS sr, tokens_cache_read AS scr, tokens_cache_write AS scw, cost AS sc FROM session WHERE time_updated >= ${startMs} AND time_created < ${endMs};`) || [];
|
|
272
272
|
for (const s of srows) {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
273
|
+
// Use the session's FULL span for residual distribution so that
|
|
274
|
+
// trend (7d window) and report (1d window) distribute the same
|
|
275
|
+
// residual across the same days. This keeps daily breakdown
|
|
276
|
+
// consistent regardless of the caller's time window.
|
|
277
|
+
const d0 = new Date(new Date(s.tc).setHours(0, 0, 0, 0));
|
|
278
|
+
const d1 = new Date(new Date(s.tu).setHours(0, 0, 0, 0));
|
|
277
279
|
const spanKeys = [];
|
|
278
280
|
for (let d = new Date(d0); d <= d1; d.setDate(d.getDate() + 1)) spanKeys.push(localDayKey(d));
|
|
279
281
|
if (!spanKeys.length) continue;
|
|
@@ -293,7 +295,7 @@ function attributeUsage(startMs, endMs) {
|
|
|
293
295
|
if (wTotal > 0) {
|
|
294
296
|
for (const k of spanKeys) if ((w.perDay[k] || 0) > 0) ensureDay(k).sessions.add(s.id);
|
|
295
297
|
} else {
|
|
296
|
-
ensureDay(localDayKey(new Date(
|
|
298
|
+
ensureDay(localDayKey(new Date(s.tu))).sessions.add(s.id);
|
|
297
299
|
}
|
|
298
300
|
const mName = modelNameOf(s.provider, s.model);
|
|
299
301
|
const aName = s.agent || 'unknown';
|
|
@@ -538,18 +540,35 @@ function buildReportFromDb(days) {
|
|
|
538
540
|
const startMs = periodStartMs(days, now);
|
|
539
541
|
const label = days === -1 ? 'All' : days <= 1 ? 'Daily' : days <= 7 ? 'Weekly' : 'Monthly';
|
|
540
542
|
|
|
541
|
-
//
|
|
542
|
-
//
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
const
|
|
543
|
+
// Use ALL-TIME attribution so every session is seen by the same
|
|
544
|
+
// attributeUsage call. Then sum only the perDay entries within the
|
|
545
|
+
// requested period. This keeps per-day breakdown consistent with the
|
|
546
|
+
// trend chart (which also uses the same full attribution).
|
|
547
|
+
const a = getAttrAll();
|
|
548
|
+
const periodDays = {};
|
|
549
|
+
for (const [key, val] of Object.entries(a.perDay)) {
|
|
550
|
+
const dayMs = val.date.getTime();
|
|
551
|
+
if (days === -1 || dayMs >= startMs) periodDays[key] = val;
|
|
552
|
+
}
|
|
553
|
+
// Sum totals from periodDays (not all-time a.totals)
|
|
554
|
+
const pdVals = Object.values(periodDays);
|
|
555
|
+
const periodTotals = { sessions: new Set(), messages: 0, input: 0, output: 0, reasoning: 0, cacheRead: 0, cacheWrite: 0, cost: 0 };
|
|
556
|
+
for (const pd of pdVals) {
|
|
557
|
+
for (const sid of pd.sessions) periodTotals.sessions.add(sid);
|
|
558
|
+
periodTotals.messages += pd.messages;
|
|
559
|
+
periodTotals.input += pd.input; periodTotals.output += pd.output; periodTotals.reasoning += pd.reasoning;
|
|
560
|
+
periodTotals.cacheRead += pd.cacheRead; periodTotals.cacheWrite += pd.cacheWrite; periodTotals.cost += pd.cost;
|
|
561
|
+
}
|
|
562
|
+
const t = periodTotals;
|
|
563
|
+
const activeDays = pdVals.length || 1;
|
|
546
564
|
const sessVals = Object.values(a.perSession)
|
|
565
|
+
.filter((s) => s.input > 0 || s.output > 0 || s.reasoning > 0)
|
|
547
566
|
.map((s) => s.input + s.output + s.reasoning)
|
|
548
567
|
.filter(Number.isFinite);
|
|
549
568
|
const avgTokens = sessVals.length ? sessVals.reduce((x, v) => x + v, 0) / sessVals.length : 0;
|
|
550
569
|
const statsR = {
|
|
551
570
|
ok: true,
|
|
552
|
-
overview: { sessions: t.sessions, messages: t.messages, days: activeDays },
|
|
571
|
+
overview: { sessions: t.sessions.size, messages: t.messages, days: activeDays },
|
|
553
572
|
cost: {
|
|
554
573
|
total: t.cost, avgDay: t.cost / activeDays,
|
|
555
574
|
avgTokensSession: Math.round(avgTokens),
|
|
@@ -561,32 +580,65 @@ function buildReportFromDb(days) {
|
|
|
561
580
|
tools: [], models: [],
|
|
562
581
|
};
|
|
563
582
|
const r4 = (v) => Math.round((v || 0) * 10000) / 10000;
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
583
|
+
|
|
584
|
+
// Rebuild per-agent and per-model for the period.
|
|
585
|
+
// Collect session IDs active in the period, then query their actual
|
|
586
|
+
// agent/model from the session table (since perSession doesn't store that).
|
|
587
|
+
const periodSessionIds = [...periodTotals.sessions];
|
|
588
|
+
if (periodSessionIds.length) {
|
|
589
|
+
const placeholders = periodSessionIds.map(() => '?').join(',');
|
|
590
|
+
const sessMeta = dbQuery(`SELECT id, agent, COALESCE(json_extract(model,'$.providerID'),'unknown') AS provider, COALESCE(json_extract(model,'$.id'),'unknown') AS model FROM session WHERE id IN (${placeholders})`, periodSessionIds) || [];
|
|
591
|
+
const metaMap = {};
|
|
592
|
+
for (const s of sessMeta) metaMap[s.id] = s;
|
|
593
|
+
|
|
594
|
+
const agentAgg = {};
|
|
595
|
+
const modelAgg = {};
|
|
596
|
+
for (const sid of periodSessionIds) {
|
|
597
|
+
const ps = a.perSession[sid];
|
|
598
|
+
if (!ps) continue;
|
|
599
|
+
const meta = metaMap[sid] || { agent: 'unknown', provider: 'unknown', model: 'unknown' };
|
|
600
|
+
const agent = meta.agent || 'unknown';
|
|
601
|
+
const modelName = `${meta.provider}/${meta.model}`;
|
|
602
|
+
if (!agentAgg[agent]) agentAgg[agent] = { sessions: new Set(), cost: 0, input: 0, output: 0, reasoning: 0, cacheRead: 0 };
|
|
603
|
+
agentAgg[agent].sessions.add(sid);
|
|
604
|
+
agentAgg[agent].input += ps.input; agentAgg[agent].output += ps.output;
|
|
605
|
+
agentAgg[agent].reasoning += ps.reasoning; agentAgg[agent].cacheRead += ps.cacheRead;
|
|
606
|
+
agentAgg[agent].cost += ps.cost;
|
|
607
|
+
if (!modelAgg[modelName]) modelAgg[modelName] = { sessions: new Set(), cost: 0, input: 0, output: 0, reasoning: 0, cacheRead: 0 };
|
|
608
|
+
modelAgg[modelName].sessions.add(sid);
|
|
609
|
+
modelAgg[modelName].input += ps.input; modelAgg[modelName].output += ps.output;
|
|
610
|
+
modelAgg[modelName].reasoning += ps.reasoning; modelAgg[modelName].cacheRead += ps.cacheRead;
|
|
611
|
+
modelAgg[modelName].cost += ps.cost;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
var pAgentRows = Object.entries(agentAgg)
|
|
615
|
+
.map(([agent, m]) => ({
|
|
616
|
+
agent, sessions: m.sessions.size, cost: r4(m.cost),
|
|
577
617
|
tok_in: Math.round(m.input), tok_out: Math.round(m.output),
|
|
578
|
-
tok_reasoning: Math.round(m.reasoning),
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
618
|
+
tok_reasoning: Math.round(m.reasoning), cache_read: Math.round(m.cacheRead),
|
|
619
|
+
}))
|
|
620
|
+
.sort((x, y) => y.cost - x.cost);
|
|
621
|
+
var pProviderRows = Object.entries(modelAgg)
|
|
622
|
+
.map(([name, m]) => {
|
|
623
|
+
const { provider, model } = splitModelName(name);
|
|
624
|
+
return {
|
|
625
|
+
provider, model, sessions: m.sessions.size, cost: r4(m.cost),
|
|
626
|
+
tok_in: Math.round(m.input), tok_out: Math.round(m.output),
|
|
627
|
+
tok_reasoning: Math.round(m.reasoning), cache_read: Math.round(m.cacheRead),
|
|
628
|
+
};
|
|
629
|
+
})
|
|
630
|
+
.sort((x, y) => y.cost - x.cost);
|
|
631
|
+
} else {
|
|
632
|
+
var pAgentRows = [];
|
|
633
|
+
var pProviderRows = [];
|
|
634
|
+
}
|
|
583
635
|
|
|
584
636
|
return {
|
|
585
637
|
label,
|
|
586
638
|
days,
|
|
587
639
|
stats: statsR,
|
|
588
|
-
agents:
|
|
589
|
-
providers:
|
|
640
|
+
agents: pAgentRows,
|
|
641
|
+
providers: pProviderRows,
|
|
590
642
|
generatedAt: now,
|
|
591
643
|
};
|
|
592
644
|
}
|