opencode-statboard 1.0.5 → 1.0.7
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/public/index.html +4 -5
- package/server.js +18 -29
package/package.json
CHANGED
package/public/index.html
CHANGED
|
@@ -154,8 +154,7 @@
|
|
|
154
154
|
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:14px">
|
|
155
155
|
<h2 data-i18n="report" style="margin:0">USAGE REPORT</h2>
|
|
156
156
|
<div class="range-btns" id="reportBtns">
|
|
157
|
-
<span class="pill active" data-days="
|
|
158
|
-
<span class="pill" data-days="1" onclick="loadReport(1)" data-i18n="daily">Daily</span>
|
|
157
|
+
<span class="pill active" data-days="1" onclick="loadReport(1)" data-i18n="daily">Daily</span>
|
|
159
158
|
<span class="pill" data-days="7" onclick="loadReport(7)" data-i18n="weekly">Weekly</span>
|
|
160
159
|
<span class="pill" data-days="30" onclick="loadReport(30)" data-i18n="monthly">Monthly</span>
|
|
161
160
|
</div>
|
|
@@ -175,7 +174,7 @@ const STRINGS = {
|
|
|
175
174
|
thModel: 'Model', thMsgs: 'Messages', thUsage: 'Usage', thCost: 'Cost',
|
|
176
175
|
dailyTrend: 'DAILY TREND ({days} DAYS)',
|
|
177
176
|
inputDay: 'Input / Day', outputDay: 'Output / Day', cacheReadDay: 'Cache Read / Day', costDay: 'Cost / Day',
|
|
178
|
-
report: 'USAGE REPORT',
|
|
177
|
+
report: 'USAGE REPORT', daily: 'Daily', weekly: 'Weekly', monthly: 'Monthly',
|
|
179
178
|
updating: 'updating…', generating: 'Generating trend data…', cachedAt: 'cached at', allFree: 'All models free — no cost recorded',
|
|
180
179
|
live: 'Live', loading: 'Loading…', lastUpdated: 'Last updated', dataAgo: 'data generated',
|
|
181
180
|
statsFail: 'Failed to read opencode stats', connFail: 'Connection failed',
|
|
@@ -191,7 +190,7 @@ const STRINGS = {
|
|
|
191
190
|
thModel: '模型', thMsgs: '訊息', thUsage: '用量', thCost: '費用',
|
|
192
191
|
dailyTrend: '近 {days} 天趨勢',
|
|
193
192
|
inputDay: 'Input / 天', outputDay: 'Output / 天', cacheReadDay: 'Cache Read / 天', costDay: '費用 / 天',
|
|
194
|
-
report: '用量報告',
|
|
193
|
+
report: '用量報告', daily: '每日', weekly: '每週', monthly: '每月',
|
|
195
194
|
updating: '更新中…', generating: '趨勢資料產生中…', cachedAt: '快取於', allFree: '全部使用免費模型,無費用紀錄',
|
|
196
195
|
live: '即時更新', loading: '載入中…', lastUpdated: '更新於', dataAgo: '數據產生於',
|
|
197
196
|
statsFail: 'opencode stats 讀取失敗', connFail: '連線失敗',
|
|
@@ -456,7 +455,7 @@ async function loadReport(days) {
|
|
|
456
455
|
applyI18n();
|
|
457
456
|
poll();
|
|
458
457
|
setInterval(poll, 15000);
|
|
459
|
-
loadReport(
|
|
458
|
+
loadReport(1);
|
|
460
459
|
</script>
|
|
461
460
|
</body>
|
|
462
461
|
</html>
|
package/server.js
CHANGED
|
@@ -58,13 +58,17 @@ 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;
|
|
@@ -78,16 +82,16 @@ async function generateTrend(days, retries = 2) {
|
|
|
78
82
|
};
|
|
79
83
|
}
|
|
80
84
|
|
|
81
|
-
const
|
|
85
|
+
const results = new Array(points);
|
|
82
86
|
let next = 0;
|
|
83
87
|
const worker = async () => {
|
|
84
|
-
while (next
|
|
88
|
+
while (next < points) {
|
|
85
89
|
const i = next++;
|
|
86
|
-
|
|
90
|
+
results[i] = await getDailyStats(i * step);
|
|
87
91
|
}
|
|
88
92
|
};
|
|
89
93
|
await Promise.all(Array.from({ length: 4 }, worker));
|
|
90
|
-
if (
|
|
94
|
+
if (results.some((r) => !r)) {
|
|
91
95
|
if (retries > 0) {
|
|
92
96
|
await new Promise((r) => setTimeout(r, 1000));
|
|
93
97
|
return generateTrend(days, retries - 1);
|
|
@@ -95,31 +99,18 @@ async function generateTrend(days, retries = 2) {
|
|
|
95
99
|
return null;
|
|
96
100
|
}
|
|
97
101
|
|
|
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
|
-
|
|
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
|
-
input:
|
|
120
|
-
output:
|
|
121
|
-
cacheRead:
|
|
122
|
-
cost:
|
|
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),
|
|
123
114
|
};
|
|
124
115
|
}
|
|
125
116
|
|
|
@@ -280,10 +271,8 @@ function parseTSV(text) {
|
|
|
280
271
|
}
|
|
281
272
|
|
|
282
273
|
async function buildReport(days) {
|
|
283
|
-
const label = days
|
|
284
|
-
const timeFilter = days
|
|
285
|
-
? `(strftime('%s','now','start of day') * 1000)`
|
|
286
|
-
: days <= 1
|
|
274
|
+
const label = days <= 1 ? 'Daily' : days <= 7 ? 'Weekly' : 'Monthly';
|
|
275
|
+
const timeFilter = days <= 1
|
|
287
276
|
? `(strftime('%s','now','start of day') * 1000)`
|
|
288
277
|
: days <= 7
|
|
289
278
|
? `(strftime('%s','now','start of day', '-${days - 1} day') * 1000)`
|