opencode-statboard 1.0.3 → 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.
- package/package.json +1 -1
- package/server.js +45 -19
package/package.json
CHANGED
package/server.js
CHANGED
|
@@ -56,35 +56,58 @@ 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
|
|
59
|
+
const now = Date.now();
|
|
60
|
+
|
|
61
|
+
async function getCumulativeUntil(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
|
+
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,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const cumulative = new Array(points + 1);
|
|
60
82
|
let next = 0;
|
|
61
83
|
const worker = async () => {
|
|
62
|
-
while (next
|
|
84
|
+
while (next <= points) {
|
|
63
85
|
const i = next++;
|
|
64
|
-
|
|
65
|
-
results[i] = await runStats(['stats', '--days', String(Math.min(d, days))]);
|
|
86
|
+
cumulative[i] = await getCumulativeUntil(i * step);
|
|
66
87
|
}
|
|
67
88
|
};
|
|
68
89
|
await Promise.all(Array.from({ length: 4 }, worker));
|
|
69
|
-
if (
|
|
90
|
+
if (cumulative.some((r) => !r)) {
|
|
70
91
|
if (retries > 0) {
|
|
71
92
|
await new Promise((r) => setTimeout(r, 1000));
|
|
72
93
|
return generateTrend(days, retries - 1);
|
|
73
94
|
}
|
|
74
95
|
return null;
|
|
75
96
|
}
|
|
76
|
-
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
97
|
+
|
|
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
|
+
|
|
88
111
|
return {
|
|
89
112
|
generatedAt: now,
|
|
90
113
|
step,
|
|
@@ -93,7 +116,6 @@ async function generateTrend(days, retries = 2) {
|
|
|
93
116
|
return (d.getMonth() + 1) + '/' + d.getDate();
|
|
94
117
|
}),
|
|
95
118
|
sessions: buckets.map((b) => b.sessions),
|
|
96
|
-
messages: buckets.map((b) => b.messages),
|
|
97
119
|
input: buckets.map((b) => b.input),
|
|
98
120
|
output: buckets.map((b) => b.output),
|
|
99
121
|
cacheRead: buckets.map((b) => b.cacheRead),
|
|
@@ -261,7 +283,11 @@ async function buildReport(days) {
|
|
|
261
283
|
const label = days === 0 ? 'Today' : days <= 1 ? 'Daily' : days <= 7 ? 'Weekly' : 'Monthly';
|
|
262
284
|
const timeFilter = days === 0
|
|
263
285
|
? `(strftime('%s','now','start of day') * 1000)`
|
|
264
|
-
:
|
|
286
|
+
: days <= 1
|
|
287
|
+
? `(strftime('%s','now','start of day') * 1000)`
|
|
288
|
+
: days <= 7
|
|
289
|
+
? `(strftime('%s','now','start of day', '-${days - 1} day') * 1000)`
|
|
290
|
+
: `(strftime('%s','now','start of month') * 1000)`;
|
|
265
291
|
let statsR = await runStats(['stats', '--days', String(Math.max(days, 1)), '--models', '20']);
|
|
266
292
|
if (!statsR) {
|
|
267
293
|
await new Promise((r) => setTimeout(r, 2000));
|