opencode-statboard 1.0.4 → 1.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/server.js +22 -23
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-statboard",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
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
@@ -58,7 +58,7 @@ async function generateTrend(days, retries = 2) {
58
58
  const points = Math.ceil(days / step);
59
59
  const now = Date.now();
60
60
 
61
- async function getStatsForDay(dayOffset) {
61
+ async function getCumulativeUntil(dayOffset) {
62
62
  const startOfDay = new Date();
63
63
  startOfDay.setHours(0, 0, 0, 0);
64
64
  startOfDay.setDate(startOfDay.getDate() - dayOffset);
@@ -70,28 +70,24 @@ async function generateTrend(days, retries = 2) {
70
70
  if (!rows.length) return null;
71
71
  const row = rows[0];
72
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
- },
73
+ sessions: parseInt(row.sessions) || 0,
74
+ input: parseFloat(row.input) || 0,
75
+ output: parseFloat(row.output) || 0,
76
+ cacheRead: parseFloat(row.cacheRead) || 0,
77
+ total: parseFloat(row.total) || 0,
81
78
  };
82
79
  }
83
80
 
84
- const results = new Array(points);
81
+ const cumulative = new Array(points + 1);
85
82
  let next = 0;
86
83
  const worker = async () => {
87
- while (next < points) {
84
+ while (next <= points) {
88
85
  const i = next++;
89
- const dayOffset = (i + 1) * step - 1;
90
- results[i] = await getStatsForDay(dayOffset);
86
+ cumulative[i] = await getCumulativeUntil(i * step);
91
87
  }
92
88
  };
93
89
  await Promise.all(Array.from({ length: 4 }, worker));
94
- if (results.some((r) => !r)) {
90
+ if (cumulative.some((r) => !r)) {
95
91
  if (retries > 0) {
96
92
  await new Promise((r) => setTimeout(r, 1000));
97
93
  return generateTrend(days, retries - 1);
@@ -99,14 +95,18 @@ async function generateTrend(days, retries = 2) {
99
95
  return null;
100
96
  }
101
97
 
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
- }));
98
+ const buckets = [];
99
+ for (let i = 0; i < points; i++) {
100
+ const cur = cumulative[i];
101
+ const prev = cumulative[i + 1];
102
+ buckets.push({
103
+ sessions: cur.sessions - prev.sessions,
104
+ input: cur.input - prev.input,
105
+ output: cur.output - prev.output,
106
+ cacheRead: cur.cacheRead - prev.cacheRead,
107
+ total: cur.total - prev.total,
108
+ });
109
+ }
110
110
 
111
111
  return {
112
112
  generatedAt: now,
@@ -116,7 +116,6 @@ async function generateTrend(days, retries = 2) {
116
116
  return (d.getMonth() + 1) + '/' + d.getDate();
117
117
  }),
118
118
  sessions: buckets.map((b) => b.sessions),
119
- messages: buckets.map((b) => b.messages),
120
119
  input: buckets.map((b) => b.input),
121
120
  output: buckets.map((b) => b.output),
122
121
  cacheRead: buckets.map((b) => b.cacheRead),