opencode-statboard 1.0.7 → 1.0.9
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 +9 -2
- package/server.js +13 -5
package/package.json
CHANGED
package/public/index.html
CHANGED
|
@@ -154,6 +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" data-days="-1" onclick="loadReport(-1)" data-i18n="all">All</span>
|
|
157
158
|
<span class="pill active" data-days="1" onclick="loadReport(1)" data-i18n="daily">Daily</span>
|
|
158
159
|
<span class="pill" data-days="7" onclick="loadReport(7)" data-i18n="weekly">Weekly</span>
|
|
159
160
|
<span class="pill" data-days="30" onclick="loadReport(30)" data-i18n="monthly">Monthly</span>
|
|
@@ -174,7 +175,7 @@ const STRINGS = {
|
|
|
174
175
|
thModel: 'Model', thMsgs: 'Messages', thUsage: 'Usage', thCost: 'Cost',
|
|
175
176
|
dailyTrend: 'DAILY TREND ({days} DAYS)',
|
|
176
177
|
inputDay: 'Input / Day', outputDay: 'Output / Day', cacheReadDay: 'Cache Read / Day', costDay: 'Cost / Day',
|
|
177
|
-
report: 'USAGE REPORT', daily: 'Daily', weekly: 'Weekly', monthly: 'Monthly',
|
|
178
|
+
report: 'USAGE REPORT', all: 'All', daily: 'Daily', weekly: 'Weekly', monthly: 'Monthly',
|
|
178
179
|
updating: 'updating…', generating: 'Generating trend data…', cachedAt: 'cached at', allFree: 'All models free — no cost recorded',
|
|
179
180
|
live: 'Live', loading: 'Loading…', lastUpdated: 'Last updated', dataAgo: 'data generated',
|
|
180
181
|
statsFail: 'Failed to read opencode stats', connFail: 'Connection failed',
|
|
@@ -190,7 +191,7 @@ const STRINGS = {
|
|
|
190
191
|
thModel: '模型', thMsgs: '訊息', thUsage: '用量', thCost: '費用',
|
|
191
192
|
dailyTrend: '近 {days} 天趨勢',
|
|
192
193
|
inputDay: 'Input / 天', outputDay: 'Output / 天', cacheReadDay: 'Cache Read / 天', costDay: '費用 / 天',
|
|
193
|
-
report: '用量報告', daily: '每日', weekly: '每週', monthly: '每月',
|
|
194
|
+
report: '用量報告', all: '全部', daily: '每日', weekly: '每週', monthly: '每月',
|
|
194
195
|
updating: '更新中…', generating: '趨勢資料產生中…', cachedAt: '快取於', allFree: '全部使用免費模型,無費用紀錄',
|
|
195
196
|
live: '即時更新', loading: '載入中…', lastUpdated: '更新於', dataAgo: '數據產生於',
|
|
196
197
|
statsFail: 'opencode stats 讀取失敗', connFail: '連線失敗',
|
|
@@ -352,6 +353,12 @@ async function poll() {
|
|
|
352
353
|
try {
|
|
353
354
|
const r = await fetch('/api/stats?days=' + currentDays + '&_=' + Date.now(), { cache: 'no-store' });
|
|
354
355
|
const j = await r.json();
|
|
356
|
+
if (j.loading) {
|
|
357
|
+
$('pill').textContent = t('updating');
|
|
358
|
+
$('pill').className = 'pill lang';
|
|
359
|
+
$('updated').textContent = t('updating');
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
355
362
|
if (!j.ok) {
|
|
356
363
|
$('errbanner').textContent = t('statsFail') + ': ' + (j.error || '');
|
|
357
364
|
$('errbanner').classList.add('show');
|
package/server.js
CHANGED
|
@@ -18,7 +18,7 @@ const EXE = (() => {
|
|
|
18
18
|
return 'opencode';
|
|
19
19
|
})();
|
|
20
20
|
|
|
21
|
-
const state = { data: null, busy: false, error: null, updatedAt: 0, models: null, modelsBusy: false };
|
|
21
|
+
const state = { data: null, busy: false, error: null, updatedAt: 0, models: null, modelsBusy: false, loading: true };
|
|
22
22
|
|
|
23
23
|
let indexHtmlCache = null;
|
|
24
24
|
|
|
@@ -223,6 +223,7 @@ async function refresh() {
|
|
|
223
223
|
} else {
|
|
224
224
|
state.error = state.error || 'opencode stats failed';
|
|
225
225
|
}
|
|
226
|
+
state.loading = false;
|
|
226
227
|
state.busy = false;
|
|
227
228
|
}
|
|
228
229
|
|
|
@@ -271,16 +272,22 @@ function parseTSV(text) {
|
|
|
271
272
|
}
|
|
272
273
|
|
|
273
274
|
async function buildReport(days) {
|
|
274
|
-
const label = days <= 1 ? 'Daily' : days <= 7 ? 'Weekly' : 'Monthly';
|
|
275
|
-
const timeFilter = days
|
|
275
|
+
const label = days === -1 ? 'All' : days <= 1 ? 'Daily' : days <= 7 ? 'Weekly' : 'Monthly';
|
|
276
|
+
const timeFilter = days === -1
|
|
277
|
+
? '1=1'
|
|
278
|
+
: days <= 1
|
|
276
279
|
? `(strftime('%s','now','start of day') * 1000)`
|
|
277
280
|
: days <= 7
|
|
278
281
|
? `(strftime('%s','now','start of day', '-${days - 1} day') * 1000)`
|
|
279
282
|
: `(strftime('%s','now','start of month') * 1000)`;
|
|
280
|
-
let statsR =
|
|
283
|
+
let statsR = days === -1
|
|
284
|
+
? await runStats(['stats', '--models', '20'])
|
|
285
|
+
: await runStats(['stats', '--days', String(Math.max(days, 1)), '--models', '20']);
|
|
281
286
|
if (!statsR) {
|
|
282
287
|
await new Promise((r) => setTimeout(r, 2000));
|
|
283
|
-
statsR =
|
|
288
|
+
statsR = days === -1
|
|
289
|
+
? await runStats(['stats', '--models', '20'])
|
|
290
|
+
: await runStats(['stats', '--days', String(Math.max(days, 1)), '--models', '20']);
|
|
284
291
|
}
|
|
285
292
|
const [agentR, modelR] = await Promise.all([
|
|
286
293
|
run(['db', `SELECT agent, COUNT(*) AS sessions, ROUND(SUM(cost),4) AS cost, SUM(tokens_input) AS tok_in, SUM(tokens_output) AS tok_out, SUM(tokens_cache_read) AS cache_read FROM session WHERE agent IS NOT NULL AND time_updated >= ${timeFilter} GROUP BY agent ORDER BY cost DESC;`, '--format', 'tsv']),
|
|
@@ -323,6 +330,7 @@ const server = http.createServer(async (req, res) => {
|
|
|
323
330
|
res.setHeader('Cache-Control', 'no-store');
|
|
324
331
|
res.end(JSON.stringify({
|
|
325
332
|
ok: !!state.data,
|
|
333
|
+
loading: state.loading,
|
|
326
334
|
error: state.error,
|
|
327
335
|
generatedAt: state.updatedAt,
|
|
328
336
|
data: state.data,
|