opencode-statboard 1.0.3 → 1.0.4

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 +42 -15
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-statboard",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
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
@@ -56,13 +56,38 @@ function cleanExpiredCache(cache, ttl) {
56
56
  async function generateTrend(days, retries = 2) {
57
57
  const step = days > 30 ? 3 : 1;
58
58
  const points = Math.ceil(days / step);
59
+ const now = Date.now();
60
+
61
+ async function getStatsForDay(dayOffset) {
62
+ const startOfDay = new Date();
63
+ startOfDay.setHours(0, 0, 0, 0);
64
+ startOfDay.setDate(startOfDay.getDate() - dayOffset);
65
+ const startMs = startOfDay.getTime();
66
+
67
+ const r = await run(['db', `SELECT COUNT(*) AS sessions, COALESCE(SUM(tokens_input),0) AS input, COALESCE(SUM(tokens_output),0) AS output, COALESCE(SUM(tokens_cache_read),0) AS cacheRead, COALESCE(SUM(cost),0) AS total FROM session WHERE time_updated >= ${startMs};`, '--format', 'tsv']);
68
+ if (!r.ok) return null;
69
+ const rows = parseTSV(r.out);
70
+ if (!rows.length) return null;
71
+ const row = rows[0];
72
+ return {
73
+ overview: { sessions: parseInt(row.sessions) || 0, messages: 0 },
74
+ cost: {
75
+ total: parseFloat(row.total) || 0,
76
+ input: parseFloat(row.input) || 0,
77
+ output: parseFloat(row.output) || 0,
78
+ cacheRead: parseFloat(row.cacheRead) || 0,
79
+ cacheWrite: 0,
80
+ },
81
+ };
82
+ }
83
+
59
84
  const results = new Array(points);
60
85
  let next = 0;
61
86
  const worker = async () => {
62
87
  while (next < points) {
63
88
  const i = next++;
64
- const d = i * step + 1;
65
- results[i] = await runStats(['stats', '--days', String(Math.min(d, days))]);
89
+ const dayOffset = (i + 1) * step - 1;
90
+ results[i] = await getStatsForDay(dayOffset);
66
91
  }
67
92
  };
68
93
  await Promise.all(Array.from({ length: 4 }, worker));
@@ -73,18 +98,16 @@ async function generateTrend(days, retries = 2) {
73
98
  }
74
99
  return null;
75
100
  }
76
- const rows = results;
77
- const first = rows[0];
78
- const buckets = [{
79
- sessions: first.overview.sessions,
80
- messages: first.overview.messages,
81
- total: first.cost.total,
82
- input: first.cost.input,
83
- output: first.cost.output,
84
- cacheRead: first.cost.cacheRead,
85
- }];
86
- for (let i = 1; i < rows.length; i++) buckets.push(sub(rows[i], rows[i - 1]));
87
- const now = Date.now();
101
+
102
+ const buckets = results.map((r) => ({
103
+ sessions: r.overview.sessions,
104
+ messages: r.overview.messages,
105
+ total: r.cost.total,
106
+ input: r.cost.input,
107
+ output: r.cost.output,
108
+ cacheRead: r.cost.cacheRead,
109
+ }));
110
+
88
111
  return {
89
112
  generatedAt: now,
90
113
  step,
@@ -261,7 +284,11 @@ async function buildReport(days) {
261
284
  const label = days === 0 ? 'Today' : days <= 1 ? 'Daily' : days <= 7 ? 'Weekly' : 'Monthly';
262
285
  const timeFilter = days === 0
263
286
  ? `(strftime('%s','now','start of day') * 1000)`
264
- : `(strftime('%s','now','-${days} day') * 1000)`;
287
+ : days <= 1
288
+ ? `(strftime('%s','now','start of day') * 1000)`
289
+ : days <= 7
290
+ ? `(strftime('%s','now','start of day', '-${days - 1} day') * 1000)`
291
+ : `(strftime('%s','now','start of month') * 1000)`;
265
292
  let statsR = await runStats(['stats', '--days', String(Math.max(days, 1)), '--models', '20']);
266
293
  if (!statsR) {
267
294
  await new Promise((r) => setTimeout(r, 2000));