opencode-statboard 1.0.2 → 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.
- package/package.json +1 -1
- package/server.js +44 -16
package/package.json
CHANGED
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
|
|
65
|
-
results[i] = await
|
|
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
|
-
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
:
|
|
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));
|
|
@@ -316,7 +343,8 @@ const server = http.createServer(async (req, res) => {
|
|
|
316
343
|
trendReady: !!r.trend,
|
|
317
344
|
}));
|
|
318
345
|
} else if (u === '/api/report') {
|
|
319
|
-
const
|
|
346
|
+
const daysParam = url.searchParams.get('days');
|
|
347
|
+
const days = daysParam !== null ? (parseInt(daysParam, 10) || 0) : 0;
|
|
320
348
|
const r = refreshReport(days);
|
|
321
349
|
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
|
322
350
|
res.setHeader('Cache-Control', 'no-store');
|