opencode-statboard 1.0.4 → 1.0.6
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 +18 -28
package/package.json
CHANGED
package/server.js
CHANGED
|
@@ -58,26 +58,27 @@ 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
|
|
61
|
+
async function getDailyStats(dayOffset) {
|
|
62
62
|
const startOfDay = new Date();
|
|
63
63
|
startOfDay.setHours(0, 0, 0, 0);
|
|
64
64
|
startOfDay.setDate(startOfDay.getDate() - dayOffset);
|
|
65
65
|
const startMs = startOfDay.getTime();
|
|
66
66
|
|
|
67
|
-
const
|
|
67
|
+
const endOfDay = new Date(startOfDay);
|
|
68
|
+
endOfDay.setDate(endOfDay.getDate() + 1);
|
|
69
|
+
const endMs = endOfDay.getTime();
|
|
70
|
+
|
|
71
|
+
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} AND time_updated < ${endMs};`, '--format', 'tsv']);
|
|
68
72
|
if (!r.ok) return null;
|
|
69
73
|
const rows = parseTSV(r.out);
|
|
70
74
|
if (!rows.length) return null;
|
|
71
75
|
const row = rows[0];
|
|
72
76
|
return {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
cacheRead: parseFloat(row.cacheRead) || 0,
|
|
79
|
-
cacheWrite: 0,
|
|
80
|
-
},
|
|
77
|
+
sessions: parseInt(row.sessions) || 0,
|
|
78
|
+
input: parseFloat(row.input) || 0,
|
|
79
|
+
output: parseFloat(row.output) || 0,
|
|
80
|
+
cacheRead: parseFloat(row.cacheRead) || 0,
|
|
81
|
+
total: parseFloat(row.total) || 0,
|
|
81
82
|
};
|
|
82
83
|
}
|
|
83
84
|
|
|
@@ -86,8 +87,7 @@ async function generateTrend(days, retries = 2) {
|
|
|
86
87
|
const worker = async () => {
|
|
87
88
|
while (next < points) {
|
|
88
89
|
const i = next++;
|
|
89
|
-
|
|
90
|
-
results[i] = await getStatsForDay(dayOffset);
|
|
90
|
+
results[i] = await getDailyStats(i * step);
|
|
91
91
|
}
|
|
92
92
|
};
|
|
93
93
|
await Promise.all(Array.from({ length: 4 }, worker));
|
|
@@ -99,28 +99,18 @@ async function generateTrend(days, retries = 2) {
|
|
|
99
99
|
return null;
|
|
100
100
|
}
|
|
101
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
|
-
|
|
111
102
|
return {
|
|
112
103
|
generatedAt: now,
|
|
113
104
|
step,
|
|
114
|
-
labels:
|
|
105
|
+
labels: results.map((_, i) => {
|
|
115
106
|
const d = new Date(now - (i * step) * 86400000);
|
|
116
107
|
return (d.getMonth() + 1) + '/' + d.getDate();
|
|
117
108
|
}),
|
|
118
|
-
sessions:
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
cost: buckets.map((b) => b.total),
|
|
109
|
+
sessions: results.map((b) => b.sessions),
|
|
110
|
+
input: results.map((b) => b.input),
|
|
111
|
+
output: results.map((b) => b.output),
|
|
112
|
+
cacheRead: results.map((b) => b.cacheRead),
|
|
113
|
+
cost: results.map((b) => b.total),
|
|
124
114
|
};
|
|
125
115
|
}
|
|
126
116
|
|