opencode-statboard 2.4.0 → 2.6.0
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/pricing.js +67 -33
- package/public/index.html +699 -470
- package/server.js +386 -173
package/public/index.html
CHANGED
|
@@ -8,14 +8,15 @@
|
|
|
8
8
|
:root {
|
|
9
9
|
--bg: #0d1117; --panel: #161b22; --border: #30363d; --text: #e6edf3; --muted: #8b949e;
|
|
10
10
|
--in: #58a6ff; --out: #3fb950; --cache: #d29922; --cost: #f778ba; --tool: #a371f7;
|
|
11
|
+
--warn: #e3b341; --dim: #6e7681;
|
|
11
12
|
}
|
|
12
13
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
13
|
-
body { background: var(--bg); color: var(--text); font-family: "Segoe UI", "Microsoft JhengHei", sans-serif; padding: 24px; }
|
|
14
|
-
header { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 10px; margin-bottom:
|
|
14
|
+
body { background: var(--bg); color: var(--text); font-family: "Segoe UI", "Microsoft JhengHei", sans-serif; padding: 24px; max-width: 1560px; margin: 0 auto; }
|
|
15
|
+
header { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 10px; margin-bottom: 8px; }
|
|
15
16
|
h1 { font-size: 20px; font-weight: 600; }
|
|
16
17
|
h1 .dot { color: #3fb950; }
|
|
17
|
-
.status { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
|
|
18
|
-
.pill { padding: 4px 12px; border-radius: 999px; font-size: 12px; border: 1px solid var(--border); cursor: pointer; user-select: none; }
|
|
18
|
+
.status { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; flex-wrap: wrap; }
|
|
19
|
+
.pill { padding: 4px 12px; border-radius: 999px; font-size: 12px; border: 1px solid var(--border); cursor: pointer; user-select: none; white-space: nowrap; }
|
|
19
20
|
.pill.ok { color: #3fb950; border-color: #238636; background: rgba(63,185,80,.08); }
|
|
20
21
|
.pill.err { color: #f85149; border-color: #da3633; background: rgba(248,81,73,.08); }
|
|
21
22
|
.pill.lang { color: var(--text); border-color: var(--border); background: rgba(139,148,158,.08); }
|
|
@@ -23,145 +24,193 @@
|
|
|
23
24
|
.pill.backup { color: var(--cache); border-color: var(--cache); background: rgba(210,153,34,.08); }
|
|
24
25
|
.pill.backup:hover { background: rgba(210,153,34,.18); }
|
|
25
26
|
.pill.backup.busy { opacity: .6; pointer-events: none; }
|
|
26
|
-
.
|
|
27
|
-
|
|
27
|
+
.version { font-size: 11px; color: var(--muted); background: var(--panel); border: 1px solid var(--border); padding: 2px 8px; border-radius: 4px; }
|
|
28
|
+
|
|
29
|
+
/* ---- caveat line ---- */
|
|
30
|
+
.caveat { font-size: 11px; color: var(--dim); line-height: 1.6; margin-bottom: 14px; letter-spacing: .1px; }
|
|
31
|
+
|
|
32
|
+
/* ---- single global scope bar ---- */
|
|
33
|
+
.scopebar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 12px 16px; margin-bottom: 18px; position: sticky; top: 0; z-index: 20; box-shadow: 0 6px 20px rgba(0,0,0,.35); }
|
|
34
|
+
.scopebar .sb-label { font-size: 11px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); font-weight: 600; }
|
|
35
|
+
.range-btns { display: flex; gap: 4px; flex-wrap: wrap; }
|
|
36
|
+
.range-btns .pill.active { color: var(--text); border-color: #58a6ff; background: rgba(88,166,255,.12); font-weight: 600; }
|
|
28
37
|
.range-btns .pill:not(.active) { color: var(--muted); border-color: var(--border); background: transparent; }
|
|
29
38
|
.range-btns .pill:not(.active):hover { background: rgba(139,148,158,.08); }
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
39
|
+
.scopebar .sb-now { font-size: 12px; color: var(--text); margin-left: auto; font-weight: 600; }
|
|
40
|
+
|
|
41
|
+
h2 { font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; margin-bottom: 14px; }
|
|
42
|
+
|
|
43
|
+
/* ---- headline cards: the dominant tier ---- */
|
|
44
|
+
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 14px; }
|
|
45
|
+
.card { background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 16px; }
|
|
46
|
+
.card.hero { border-color: #2d3748; box-shadow: inset 0 1px 0 rgba(255,255,255,.03); }
|
|
47
|
+
.card .v { font-size: 26px; font-weight: 700; margin-top: 4px; line-height: 1.15; letter-spacing: -.5px; }
|
|
48
|
+
.card.hero .v { font-size: 34px; }
|
|
35
49
|
.card .l { font-size: 12px; color: var(--muted); }
|
|
36
|
-
.
|
|
37
|
-
.
|
|
38
|
-
.
|
|
39
|
-
.
|
|
40
|
-
.
|
|
50
|
+
.card .subs { display: flex; flex-wrap: wrap; gap: 4px 12px; margin-top: 8px; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
|
|
51
|
+
.card .subs b { color: var(--text); font-weight: 600; }
|
|
52
|
+
.subs .k-in { color: var(--in); } .subs .k-out { color: var(--out); }
|
|
53
|
+
.subs .k-reasoning { color: var(--tool); } .subs .k-cache { color: var(--cache); }
|
|
54
|
+
.subs .warn { color: var(--warn); cursor: pointer; border-bottom: 1px dashed var(--warn); }
|
|
55
|
+
.subs .warn:hover { color: #f0c674; }
|
|
56
|
+
.foot-note { display: flex; flex-wrap: wrap; gap: 6px 16px; align-items: center; margin-top: 14px; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
|
|
57
|
+
|
|
58
|
+
/* ---- breakdown / pricing tables ---- */
|
|
41
59
|
.scrollx { overflow-x: auto; }
|
|
42
|
-
.scrollx table { min-width:
|
|
43
|
-
td.clip { max-width:
|
|
44
|
-
.rep-head { color: var(--muted); font-size: 12px; margin-bottom: 12px; }
|
|
45
|
-
.rep-hint { color: var(--muted); font-size: 12px; font-weight: 400; }
|
|
46
|
-
.rep-pre { font-family: monospace; font-size: 13px; line-height: 1.6; white-space: pre; overflow-x: auto; color: var(--text); margin: 0; }
|
|
60
|
+
.scrollx table { min-width: 1080px; }
|
|
61
|
+
td.clip { max-width: 300px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
47
62
|
.track { background: #21262d; border-radius: 4px; height: 12px; overflow: hidden; }
|
|
48
|
-
.
|
|
63
|
+
.track.share { min-width: 90px; height: 10px; }
|
|
64
|
+
/* horizontal bar list (tools). The container must NOT be a .bar-row — each
|
|
65
|
+
child is a grid item, so a grid container here collapses all rows into
|
|
66
|
+
one column and renders as a single giant bar. */
|
|
67
|
+
.bar-list { display: block; }
|
|
68
|
+
.bar-row { display: grid; grid-template-columns: 170px 1fr 150px; align-items: center; gap: 12px; padding: 5px 0; font-size: 13px; }
|
|
69
|
+
.bar-row .name { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
|
70
|
+
.bar-row .val { text-align: right; font-variant-numeric: tabular-nums; }
|
|
49
71
|
.stack { display: flex; height: 100%; }
|
|
50
72
|
.stack > div { height: 100%; }
|
|
51
|
-
.legend { display: flex; gap: 16px; font-size: 12px; color: var(--muted); margin-bottom: 12px; flex-wrap: wrap; }
|
|
52
|
-
.legend span { display: flex; align-items: center; gap: 6px; }
|
|
53
|
-
.sw { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
|
|
54
73
|
table { width: 100%; border-collapse: collapse; font-size: 13px; }
|
|
55
|
-
th, td { padding: 8px 10px; text-align: right; border-bottom: 1px solid var(--border); font-variant-numeric: tabular-nums; }
|
|
74
|
+
th, td { padding: 8px 10px; text-align: right; border-bottom: 1px solid var(--border); font-variant-numeric: tabular-nums; white-space: nowrap; }
|
|
56
75
|
th:first-child, td:first-child { text-align: left; }
|
|
57
76
|
th { color: var(--muted); font-weight: 500; font-size: 12px; }
|
|
77
|
+
tbody tr:hover td { background: rgba(139,148,158,.06); }
|
|
78
|
+
td.bd-name { max-width: 320px; overflow: hidden; text-overflow: ellipsis; }
|
|
79
|
+
td.na { color: var(--dim); }
|
|
80
|
+
|
|
81
|
+
.rep-hint { color: var(--muted); font-size: 12px; font-weight: 400; }
|
|
82
|
+
.rep-pre { font-family: monospace; font-size: 12.5px; line-height: 1.6; white-space: pre; overflow-x: auto; color: var(--text); margin: 0; }
|
|
83
|
+
|
|
84
|
+
/* ---- trend ---- */
|
|
58
85
|
.chart { position: relative; display: flex; align-items: flex-end; gap: 2px; height: 200px; padding-top: 10px; }
|
|
59
86
|
.chart .col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; height: 100%; justify-content: flex-end; min-width: 0; }
|
|
60
87
|
.chart .bars { display: flex; gap: 2px; align-items: flex-end; justify-content: center; height: 170px; width: 100%; }
|
|
61
|
-
.chart .bars > div { border-radius: 2px 2px 0 0; min-width: 4px; transition: height .4s ease; }
|
|
62
|
-
.chart .x { font-size: 10px; color: var(--muted); line-height: 1; }
|
|
63
|
-
.chart .tip { font-size: 10px; color: var(--muted); line-height: 1.2; text-align: center; }
|
|
64
88
|
.trend-x { display: flex; height: 16px; margin-top: 4px; }
|
|
65
89
|
.trend-x span { flex: 1; text-align: center; font-size: 10px; color: var(--muted); line-height: 16px; overflow: hidden; text-overflow: clip; white-space: nowrap; }
|
|
66
|
-
.
|
|
67
|
-
|
|
90
|
+
.trend-wrap { display: flex; gap: 24px; flex-wrap: wrap; }
|
|
91
|
+
.trend-wrap > div { flex: 1; min-width: 300px; }
|
|
92
|
+
.trend-wrap #costSection { min-width: 240px; }
|
|
93
|
+
.legend { display: flex; gap: 16px; font-size: 12px; color: var(--muted); margin-bottom: 12px; flex-wrap: wrap; }
|
|
94
|
+
.legend span { display: flex; align-items: center; gap: 6px; }
|
|
95
|
+
.sw { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
|
|
96
|
+
|
|
68
97
|
.error-banner { display: none; background: rgba(248,81,73,.1); border: 1px solid #da3633; color: #f85149; padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; font-size: 13px; }
|
|
69
98
|
.error-banner.show { display: block; }
|
|
99
|
+
|
|
100
|
+
/* ---- collapsible sections: primary vs secondary tier ---- */
|
|
70
101
|
details.tool-section { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 18px; margin-bottom: 18px; }
|
|
71
|
-
details.tool-section summary { cursor: pointer; list-style: none; display: flex; align-items: center; gap: 8px; }
|
|
102
|
+
details.tool-section summary { cursor: pointer; list-style: none; display: flex; align-items: center; gap: 8px; justify-content: space-between; }
|
|
72
103
|
details.tool-section summary::-webkit-details-marker { display: none; }
|
|
73
|
-
details.tool-section summary::before { content: '
|
|
104
|
+
details.tool-section summary::before { content: '\25B8'; font-size: 12px; color: var(--muted); transition: transform .2s; flex: none; }
|
|
74
105
|
details.tool-section[open] summary::before { transform: rotate(90deg); }
|
|
75
106
|
details.tool-section summary h2 { margin: 0; }
|
|
76
|
-
.
|
|
77
|
-
|
|
78
|
-
.
|
|
79
|
-
.
|
|
107
|
+
details.tool-section.secondary { background: transparent; padding: 12px 18px; }
|
|
108
|
+
details.tool-section.secondary[open] { padding: 18px; }
|
|
109
|
+
details.tool-section.secondary summary h2 { font-size: 11px; }
|
|
110
|
+
.scope-tag { font-size: 11px; color: var(--muted); border: 1px solid var(--border); border-radius: 999px; padding: 2px 10px; font-weight: 500; white-space: nowrap; flex: none; margin-left: 12px; font-variant-numeric: tabular-nums; }
|
|
111
|
+
|
|
112
|
+
.subtabs { display: flex; gap: 4px; margin: 0 0 14px 20px; }
|
|
113
|
+
.subtabs .pill { color: var(--muted); background: transparent; }
|
|
114
|
+
.subtabs .pill.active { color: var(--text); border-color: #58a6ff; background: rgba(88,166,255,.12); font-weight: 600; }
|
|
115
|
+
|
|
116
|
+
.badge { display: inline-block; font-size: 11px; padding: 1px 8px; border-radius: 999px; border: 1px solid var(--border); color: var(--muted); white-space: nowrap; }
|
|
117
|
+
.badge.exact { color: #3fb950; border-color: rgba(63,185,80,.4); }
|
|
118
|
+
.badge.sibling { color: #58a6ff; border-color: rgba(88,166,255,.4); }
|
|
119
|
+
.badge.proxy { color: #d29922; border-color: rgba(210,153,34,.4); }
|
|
120
|
+
.badge.partial { color: #f778ba; border-color: rgba(247,120,186,.4); }
|
|
121
|
+
.badge.unpriced { color: #f85149; border-color: rgba(248,81,73,.4); }
|
|
122
|
+
.empty { color: var(--muted); font-size: 13px; padding: 12px 0; }
|
|
123
|
+
|
|
80
124
|
.trend-bar { position: absolute; bottom: 0; border-radius: 2px 2px 0 0; transition: height .4s ease; }
|
|
81
125
|
.trend-bar-cache { left: 0; right: 0; opacity: .2; }
|
|
82
|
-
.version { font-size: 11px; color: var(--muted); background: var(--panel); border: 1px solid var(--border); padding: 2px 8px; border-radius: 4px; }
|
|
83
126
|
.trend-bar-input { left: 25%; width: 22%; z-index: 1; }
|
|
84
127
|
.trend-bar-output { left: 53%; width: 22%; z-index: 1; }
|
|
85
128
|
.trend-bar-reasoning { left: 76%; width: 22%; z-index: 1; }
|
|
129
|
+
@media (max-width: 640px) { body { padding: 14px; } .card.hero .v { font-size: 26px; } }
|
|
86
130
|
</style>
|
|
87
131
|
</head>
|
|
88
132
|
<body>
|
|
89
133
|
<header>
|
|
90
|
-
<h1><span class="dot"
|
|
134
|
+
<h1><span class="dot">●</span> <span data-i18n="title">opencode Dashboard</span> <span class="version">v__VERSION__</span></h1>
|
|
91
135
|
<div class="status">
|
|
92
|
-
<span id="updated">Loading
|
|
136
|
+
<span id="updated">Loading…</span>
|
|
93
137
|
<span class="pill ok" id="pill" data-i18n="live">Live</span>
|
|
94
138
|
<span class="pill lang" id="langBtn" onclick="toggleLang()"></span>
|
|
95
139
|
<span class="pill backup" id="backupBtn" onclick="backupDb()" data-i18n="backup">Backup DB</span>
|
|
96
140
|
</div>
|
|
97
141
|
</header>
|
|
98
142
|
|
|
143
|
+
<div class="caveat" id="caveat"></div>
|
|
144
|
+
|
|
145
|
+
<div class="scopebar">
|
|
146
|
+
<span class="sb-label" data-i18n="scope">Scope</span>
|
|
147
|
+
<div class="range-btns" id="scopeBtns">
|
|
148
|
+
<span class="pill" data-days="-1" onclick="setScope(-1)" data-i18n="all">All time</span>
|
|
149
|
+
<span class="pill" data-days="1" onclick="setScope(1)" data-i18n="today">Today</span>
|
|
150
|
+
<span class="pill" data-days="7" onclick="setScope(7)" data-i18n="last7">7 days</span>
|
|
151
|
+
<span class="pill" data-days="30" onclick="setScope(30)" data-i18n="mtd">Month to date</span>
|
|
152
|
+
</div>
|
|
153
|
+
<span class="sb-now" id="scopeNow"></span>
|
|
154
|
+
</div>
|
|
155
|
+
|
|
99
156
|
<div class="error-banner" id="errbanner"></div>
|
|
100
157
|
|
|
101
|
-
|
|
102
|
-
|
|
158
|
+
<!-- 1. HEADLINE — follows the global scope -->
|
|
159
|
+
<details class="tool-section" open id="sec-headline">
|
|
160
|
+
<summary><h2 data-i18n="headline">HEADLINE</h2><span class="scope-tag" id="tag-headline"></span></summary>
|
|
103
161
|
<div class="cards">
|
|
104
|
-
<div class="card
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
162
|
+
<div class="card hero">
|
|
163
|
+
<div class="l" data-i18n="tokens">Tokens</div>
|
|
164
|
+
<div class="v" id="hl-tokens">—</div>
|
|
165
|
+
<div class="subs">
|
|
166
|
+
<span class="k-in"><span data-i18n="tIn">in</span> <b id="hl-in">—</b></span>
|
|
167
|
+
<span class="k-out"><span data-i18n="tOut">out</span> <b id="hl-out">—</b></span>
|
|
168
|
+
<span class="k-reasoning"><span data-i18n="tReasoning">reasoning</span> <b id="hl-reasoning">—</b></span>
|
|
169
|
+
<span class="k-cache"><span data-i18n="tCache">cache</span> <b id="hl-cache">—</b></span>
|
|
170
|
+
</div>
|
|
171
|
+
</div>
|
|
172
|
+
<div class="card hero">
|
|
173
|
+
<div class="l" data-i18n="totalCost">Total Cost</div>
|
|
174
|
+
<div class="v" id="hl-cost">—</div>
|
|
175
|
+
<div class="subs"><span data-i18n="costShare">actual paid</span></div>
|
|
176
|
+
</div>
|
|
177
|
+
<div class="card hero">
|
|
178
|
+
<div class="l" data-i18n="marketValue">Market Value</div>
|
|
179
|
+
<div class="v" id="hl-market">—</div>
|
|
180
|
+
<div class="subs">
|
|
181
|
+
<span data-i18n="atCatalog">same usage at catalog list prices</span>
|
|
182
|
+
<span id="hl-priceAsOf"></span>
|
|
183
|
+
<span class="warn" id="hl-unpriced" onclick="openPricing()"></span>
|
|
184
|
+
</div>
|
|
185
|
+
</div>
|
|
186
|
+
<div class="card hero">
|
|
187
|
+
<div class="l" data-i18n="saved">Saved</div>
|
|
188
|
+
<div class="v" id="hl-saved">—</div>
|
|
189
|
+
<div class="subs"><span id="hl-savedPct"></span></div>
|
|
190
|
+
</div>
|
|
113
191
|
</div>
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
<
|
|
117
|
-
<summary><h2 data-i18n="tokenStats">TOKEN STATISTICS</h2></summary>
|
|
118
|
-
<div class="legend">
|
|
119
|
-
<span><i class="sw" style="background:var(--in)"></i>Input</span>
|
|
120
|
-
<span><i class="sw" style="background:var(--out)"></i>Output</span>
|
|
121
|
-
<span><i class="sw" style="background:var(--tool)"></i>Reasoning</span>
|
|
122
|
-
<span><i class="sw" style="background:var(--cache)"></i>Cache Read</span>
|
|
123
|
-
<span><i class="sw" style="background:var(--cost)"></i>Cache Write</span>
|
|
192
|
+
<div class="foot-note">
|
|
193
|
+
<span id="hl-meta"></span>
|
|
194
|
+
<span id="hl-proxy"></span>
|
|
124
195
|
</div>
|
|
125
|
-
<div id="token-bars"></div>
|
|
126
|
-
</details>
|
|
127
|
-
|
|
128
|
-
<details class="tool-section" open id="sec-tools">
|
|
129
|
-
<summary><h2 data-i18n="toolUsage">TOOL USAGE</h2></summary>
|
|
130
|
-
<div id="tool-bars"></div>
|
|
131
|
-
</details>
|
|
132
|
-
|
|
133
|
-
<details class="tool-section" open id="sec-models">
|
|
134
|
-
<summary><h2 data-i18n="modelUsage">MODEL USAGE</h2></summary>
|
|
135
|
-
<table>
|
|
136
|
-
<thead>
|
|
137
|
-
<tr><th data-i18n="thModel">Model</th><th data-i18n="thMsgs">Messages</th><th>Input</th><th>Output</th><th>Cache Read</th><th data-i18n="thSpeed">Speed</th><th style="width:26%" data-i18n="thUsage">Usage</th><th data-i18n="thCost">Cost</th></tr>
|
|
138
|
-
</thead>
|
|
139
|
-
<tbody id="model-rows"></tbody>
|
|
140
|
-
</table>
|
|
141
196
|
</details>
|
|
142
197
|
|
|
198
|
+
<!-- 2. TREND — bucket count follows the scope -->
|
|
143
199
|
<details class="tool-section" open id="sec-trend">
|
|
144
|
-
<summary><
|
|
145
|
-
|
|
146
|
-
<div
|
|
147
|
-
<span class="pill active" data-days="7" onclick="setRange(7)">7D</span>
|
|
148
|
-
<span class="pill" data-days="30" onclick="setRange(30)">30D</span>
|
|
149
|
-
<span class="pill" data-days="90" onclick="setRange(90)">90D</span>
|
|
150
|
-
</div>
|
|
151
|
-
</div></summary>
|
|
152
|
-
<div style="display:flex;gap:24px;flex-wrap:wrap">
|
|
153
|
-
<div style="flex:1;min-width:300px">
|
|
200
|
+
<summary><h2 data-i18n="trend">TREND</h2><span class="scope-tag" id="tag-trend"></span></summary>
|
|
201
|
+
<div class="trend-wrap">
|
|
202
|
+
<div>
|
|
154
203
|
<div class="legend">
|
|
155
204
|
<span><i class="sw" style="background:var(--in)"></i><span data-i18n="inputDay">Input / Day</span></span>
|
|
156
205
|
<span><i class="sw" style="background:var(--out)"></i><span data-i18n="outputDay">Output / Day</span></span>
|
|
157
|
-
<span><i class="sw" style="background:var(--tool)"></i>Reasoning / Day</span>
|
|
206
|
+
<span><i class="sw" style="background:var(--tool)"></i><span data-i18n="reasoningDay">Reasoning / Day</span></span>
|
|
158
207
|
<span><i class="sw" style="background:var(--cache)"></i><span data-i18n="cacheReadDay">Cache Read / Day</span></span>
|
|
159
208
|
<span id="trendStatus" style="color:var(--cache);font-size:11px"></span>
|
|
160
209
|
</div>
|
|
161
210
|
<div class="chart" id="trendTokensChart"><div class="bars" id="trend-tokens"></div></div>
|
|
162
211
|
<div class="trend-x" id="trend-token-x"></div>
|
|
163
212
|
</div>
|
|
164
|
-
<div id="costSection"
|
|
213
|
+
<div id="costSection">
|
|
165
214
|
<div class="legend"><span><i class="sw" style="background:var(--cost)"></i><span data-i18n="costDay">Cost / Day</span></span></div>
|
|
166
215
|
<div class="chart" id="trendCostChart"><div class="bars" id="trend-cost"></div></div>
|
|
167
216
|
<div class="trend-x" id="trend-cost-x"></div>
|
|
@@ -170,105 +219,249 @@
|
|
|
170
219
|
</div>
|
|
171
220
|
</details>
|
|
172
221
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
222
|
+
<!-- 3. BREAKDOWN — one table, three sub-tabs, sorted by tokens -->
|
|
223
|
+
<details class="tool-section" open id="sec-breakdown">
|
|
224
|
+
<summary><h2 data-i18n="breakdown">BREAKDOWN</h2><span class="scope-tag" id="tag-breakdown"></span></summary>
|
|
225
|
+
<div class="subtabs" id="bdTabs">
|
|
226
|
+
<span class="pill active" data-tab="models" onclick="setTab('models')" data-i18n="tabModels">Models</span>
|
|
227
|
+
<span class="pill" data-tab="agents" onclick="setTab('agents')" data-i18n="tabAgents">Agents</span>
|
|
228
|
+
<span class="pill" data-tab="sessions" onclick="setTab('sessions')" data-i18n="tabSessions">Sessions</span>
|
|
229
|
+
</div>
|
|
230
|
+
<div class="legend">
|
|
231
|
+
<span><i class="sw" style="background:var(--in)"></i><span data-i18n="tIn">in</span></span>
|
|
232
|
+
<span><i class="sw" style="background:var(--out)"></i><span data-i18n="tOut">out</span></span>
|
|
233
|
+
<span><i class="sw" style="background:var(--tool)"></i><span data-i18n="tReasoning">reasoning</span></span>
|
|
234
|
+
<span><i class="sw" style="background:var(--cache)"></i><span data-i18n="tCache">cache</span></span>
|
|
235
|
+
<span id="bdSortNote" style="color:var(--dim)"></span>
|
|
236
|
+
</div>
|
|
237
|
+
<div class="scrollx">
|
|
238
|
+
<table>
|
|
239
|
+
<thead>
|
|
240
|
+
<tr>
|
|
241
|
+
<th id="th-name" data-i18n="thName">Name</th>
|
|
242
|
+
<th style="width:120px" data-i18n="thShare">Share</th>
|
|
243
|
+
<th id="th-count" data-i18n="thSessions">Sessions</th>
|
|
244
|
+
<th data-i18n="tIn">in</th>
|
|
245
|
+
<th data-i18n="tOut">out</th>
|
|
246
|
+
<th data-i18n="tReasoning">reasoning</th>
|
|
247
|
+
<th data-i18n="tCache">cache</th>
|
|
248
|
+
<th data-i18n="thSpeed">Speed</th>
|
|
249
|
+
<th data-i18n="thWall">Wall</th>
|
|
250
|
+
<th data-i18n="marketValue">Market</th>
|
|
251
|
+
<th data-i18n="saved">Saved</th>
|
|
252
|
+
</tr>
|
|
253
|
+
</thead>
|
|
254
|
+
<tbody id="bd-models"></tbody>
|
|
255
|
+
<tbody id="bd-agents" style="display:none"></tbody>
|
|
256
|
+
<tbody id="bd-sessions" style="display:none"></tbody>
|
|
257
|
+
</table>
|
|
258
|
+
</div>
|
|
259
|
+
<div id="bd-empty" class="empty" style="display:none"></div>
|
|
184
260
|
</details>
|
|
185
261
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
<
|
|
262
|
+
<!-- 4. TOOLS -->
|
|
263
|
+
<details class="tool-section secondary" open id="sec-tools">
|
|
264
|
+
<summary><h2 data-i18n="toolUsage">TOOLS</h2><span class="scope-tag" id="tag-tools"></span></summary>
|
|
265
|
+
<div class="bar-list" id="tool-bars"></div>
|
|
189
266
|
</details>
|
|
190
267
|
|
|
191
|
-
|
|
192
|
-
|
|
268
|
+
<!-- 5. PRICING BASIS (collapsed by default) -->
|
|
269
|
+
<details class="tool-section secondary" id="sec-pricing">
|
|
270
|
+
<summary><h2 data-i18n="pricingBasis">PRICING BASIS</h2><span class="scope-tag" id="tag-pricing"></span></summary>
|
|
193
271
|
<div class="legend">
|
|
194
|
-
<span><
|
|
195
|
-
<span><
|
|
272
|
+
<span><span class="badge exact" data-i18n="kindExact">exact</span> <span data-i18n="kindExactTip">own provider + own list price</span></span>
|
|
273
|
+
<span><span class="badge sibling" data-i18n="kindSibling">sibling</span> <span data-i18n="kindSiblingTip">same provider, -free variant stripped</span></span>
|
|
274
|
+
<span><span class="badge proxy" data-i18n="kindProxy">proxy</span> <span data-i18n="kindProxyTip">borrowed price from another provider (estimate)</span></span>
|
|
275
|
+
<span><span class="badge partial" data-i18n="kindPartial">partial</span> <span data-i18n="kindPartialTip">part priced, part not in catalog</span></span>
|
|
276
|
+
<span><span class="badge unpriced" data-i18n="kindUnpriced">unpriced</span> <span data-i18n="kindUnpricedTip">not in catalog — market/saved unknown</span></span>
|
|
196
277
|
</div>
|
|
197
|
-
<div
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
<
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
<span><i class="sw" style="background:var(--out)"></i>active</span>
|
|
278
|
+
<div class="scrollx">
|
|
279
|
+
<table>
|
|
280
|
+
<thead>
|
|
281
|
+
<tr><th data-i18n="thUsageModel">Usage model</th><th data-i18n="thPricedAs">Priced as</th><th data-i18n="thKind">Basis</th><th data-i18n="marketValue">Market</th><th data-i18n="saved">Saved</th></tr>
|
|
282
|
+
</thead>
|
|
283
|
+
<tbody id="pricing-rows"></tbody>
|
|
284
|
+
</table>
|
|
205
285
|
</div>
|
|
206
|
-
<div id="
|
|
286
|
+
<div id="pricing-empty" class="empty" style="display:none"></div>
|
|
207
287
|
</details>
|
|
208
288
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
<
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
<div id="savings-bars"></div>
|
|
289
|
+
<!-- 6. RAW TEXT (collapsed by default) -->
|
|
290
|
+
<details class="tool-section secondary" id="sec-raw">
|
|
291
|
+
<summary><h2 data-i18n="rawText">RAW TEXT</h2><span class="scope-tag" id="tag-raw"></span></summary>
|
|
292
|
+
<div class="rep-hint" style="margin-bottom:10px" data-i18n="rawHint">Plain-text statement, same numbers as the tables above.</div>
|
|
293
|
+
<pre class="rep-pre" id="rawText"></pre>
|
|
215
294
|
</details>
|
|
216
295
|
|
|
217
296
|
<script>
|
|
218
297
|
const STRINGS = {
|
|
219
298
|
en: {
|
|
220
299
|
title: 'opencode Dashboard',
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
300
|
+
scope: 'Scope', all: 'All time', today: 'Today', last7: '7 days', mtd: 'Month to date',
|
|
301
|
+
scopeSuffix: { '-1': 'All time', '1': 'Today (local)', '7': 'Last 7 days (local)', '30': 'Month to date (local)' },
|
|
302
|
+
trendScope: '{n}-day buckets · today leftmost',
|
|
303
|
+
toolsOther: '{n} more tools',
|
|
304
|
+
allTimeTrend: 'All time · 90-day buckets · today leftmost',
|
|
305
|
+
toolsScope: '{scope} · tool counts are all-time',
|
|
306
|
+
headline: 'HEADLINE', trend: 'TREND', breakdown: 'BREAKDOWN', toolUsage: 'TOOLS',
|
|
307
|
+
pricingBasis: 'PRICING BASIS', rawText: 'RAW TEXT',
|
|
308
|
+
rawHint: 'Plain-text statement, same numbers as the tables above.',
|
|
309
|
+
tokens: 'Tokens (excl. cache re-reads)', tIn: 'in', tOut: 'out', tReasoning: 'reasoning', tCache: 'cache re-reads',
|
|
310
|
+
totalCost: 'Total Cost', marketValue: 'Market Value', saved: 'Saved',
|
|
311
|
+
costShare: 'actual paid', atCatalog: 'same usage at catalog list prices',
|
|
312
|
+
savedOf: '{pct}% of market value',
|
|
313
|
+
sessionsLabel: 'Sessions', messagesLabel: 'Messages', daysLabel: 'Days',
|
|
314
|
+
avgCostDay: 'Avg cost / day', avgTokensSession: 'Avg tokens / session', medianTokensSession: 'Median tokens / session',
|
|
315
|
+
activeLabel: 'active',
|
|
316
|
+
pricesAsOf: 'prices as of {d}',
|
|
317
|
+
unpricedModels: '{n} models unpriced ({t} tokens not valued)',
|
|
318
|
+
proxyShare: '{pct}% of market value is proxy estimate',
|
|
319
|
+
caveat: 'V1+V2 merged · per-message token authority · session residual spread across the session\'s days',
|
|
320
|
+
tabModels: 'Models', tabAgents: 'Agents', tabSessions: 'Sessions',
|
|
321
|
+
thName: 'Name', thShare: 'Share', thSessions: 'Sessions', thMsgs: 'Msgs',
|
|
322
|
+
thSpeed: 'Speed', thWall: 'Wall',
|
|
323
|
+
sortNote: 'sorted by tokens (in + out + reasoning + cache) — cost is 0 for free models, so it cannot sort',
|
|
324
|
+
sessionsTop: 'top {n} sessions',
|
|
325
|
+
thUsageModel: 'Usage model', thPricedAs: 'Priced as', thKind: 'Basis',
|
|
326
|
+
kindExact: 'exact', kindSibling: 'sibling', kindProxy: 'proxy',
|
|
327
|
+
kindPartial: 'partial', kindUnpriced: 'unpriced',
|
|
328
|
+
kindExactTip: 'own provider + own list price',
|
|
329
|
+
kindSiblingTip: 'same provider, -free variant stripped',
|
|
330
|
+
kindProxyTip: 'borrowed price from another provider (estimate)',
|
|
331
|
+
kindPartialTip: 'part of the usage priced, rest not in catalog',
|
|
332
|
+
kindUnpricedTip: 'not in catalog — market/saved unknown',
|
|
333
|
+
legacyAgent: 'legacy',
|
|
334
|
+
inputDay: 'Input / Day', outputDay: 'Output / Day', reasoningDay: 'Reasoning / Day',
|
|
335
|
+
cacheReadDay: 'Cache Read / Day', costDay: 'Cost / Day',
|
|
336
|
+
noData: 'No data for this scope',
|
|
337
|
+
updating: 'updating…', generating: 'Generating trend data…', cachedAt: 'cached at',
|
|
338
|
+
allFree: 'All models free — no cost recorded in this scope',
|
|
231
339
|
live: 'Live', loading: 'Loading…', lastUpdated: 'Last updated', dataAgo: 'data generated',
|
|
232
|
-
statsFail: 'Failed to read opencode stats', connFail: 'Connection failed',
|
|
340
|
+
statsFail: 'Failed to read opencode stats', connFail: 'Connection failed', reportFail: 'Failed to read usage report',
|
|
233
341
|
langBtn: '繁中',
|
|
234
342
|
backup: 'Backup DB', backingUp: 'Backing up…', backupFail: 'Backup failed',
|
|
235
343
|
},
|
|
236
344
|
'zh-TW': {
|
|
237
345
|
title: 'opencode 用量儀表板',
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
346
|
+
scope: '範圍', all: '全期', today: '今天', last7: '近 7 天', mtd: '本月至今',
|
|
347
|
+
scopeSuffix: { '-1': '全期', '1': '今天(本地)', '7': '近 7 天(本地)', '30': '本月至今(本地)' },
|
|
348
|
+
trendScope: '{n} 天分桶 · 今天在最左',
|
|
349
|
+
toolsOther: '其他 {n} 個工具',
|
|
350
|
+
allTimeTrend: '全期 · 90 天分桶 · 今天在最左',
|
|
351
|
+
toolsScope: '{scope} · 工具統計一律為全期',
|
|
352
|
+
headline: '重點指標', trend: '趨勢', breakdown: '明細', toolUsage: '工具',
|
|
353
|
+
pricingBasis: '定價依據', rawText: '原始文字',
|
|
354
|
+
rawHint: '純文字報表,數字與上方表格一致。',
|
|
355
|
+
tokens: 'Tokens(不含快取重讀)', tIn: '輸入', tOut: '輸出', tReasoning: '推理', tCache: '快取重讀',
|
|
356
|
+
totalCost: '總費用', marketValue: '市價', saved: '已節省',
|
|
357
|
+
costShare: '實際付費', atCatalog: '同等用量依牌價換算',
|
|
358
|
+
savedOf: '占市價 {pct}%',
|
|
359
|
+
sessionsLabel: '工作階段', messagesLabel: '訊息數', daysLabel: '天數',
|
|
360
|
+
avgCostDay: '平均費用 / 天', avgTokensSession: '平均 tokens / 場', medianTokensSession: '中位數 tokens / 場',
|
|
361
|
+
activeLabel: '實際執行',
|
|
362
|
+
pricesAsOf: '牌價日期 {d}',
|
|
363
|
+
unpricedModels: '{n} 個模型查無牌價({t} tokens 未計價)',
|
|
364
|
+
proxyShare: '市價中有 {pct}% 屬借價代估',
|
|
365
|
+
caveat: 'V1+V2 資料合併 · 以單則訊息 token 為準 · 場次餘額依該場涵蓋的天數分攤',
|
|
366
|
+
tabModels: '模型', tabAgents: '代理', tabSessions: '場次',
|
|
367
|
+
thName: '名稱', thShare: '占比', thSessions: '場次', thMsgs: '訊息',
|
|
368
|
+
thSpeed: '速度', thWall: '耗時',
|
|
369
|
+
sortNote: '依 tokens(輸入+輸出+推理+快取)排序 —— 免費模型費用皆為 0,無法作為排序依據',
|
|
370
|
+
sessionsTop: '前 {n} 場',
|
|
371
|
+
thUsageModel: '使用模型', thPricedAs: '採用牌價', thKind: '依據',
|
|
372
|
+
kindExact: 'exact', kindSibling: 'sibling', kindProxy: 'proxy',
|
|
373
|
+
kindPartial: 'partial', kindUnpriced: 'unpriced',
|
|
374
|
+
kindExactTip: '同供應商同名牌價',
|
|
375
|
+
kindSiblingTip: '同供應商,去掉 -free 變體',
|
|
376
|
+
kindProxyTip: '借用其他供應商牌價(代估)',
|
|
377
|
+
kindPartialTip: '部分用量有牌價,其餘查無',
|
|
378
|
+
kindUnpricedTip: '目錄查無 — 市價/節省未知',
|
|
379
|
+
legacyAgent: '舊版',
|
|
380
|
+
inputDay: '輸入 / 天', outputDay: '輸出 / 天', reasoningDay: '推理 / 天',
|
|
381
|
+
cacheReadDay: '快取讀取 / 天', costDay: '費用 / 天',
|
|
382
|
+
noData: '此範圍沒有資料',
|
|
383
|
+
updating: '更新中…', generating: '趨勢資料產生中…', cachedAt: '快取於',
|
|
384
|
+
allFree: '此範圍全部使用免費模型,無費用紀錄',
|
|
248
385
|
live: '即時更新', loading: '載入中…', lastUpdated: '更新於', dataAgo: '數據產生於',
|
|
249
|
-
statsFail: 'opencode stats 讀取失敗', connFail: '連線失敗',
|
|
386
|
+
statsFail: 'opencode stats 讀取失敗', connFail: '連線失敗', reportFail: '用量報告讀取失敗',
|
|
250
387
|
langBtn: 'EN',
|
|
251
388
|
backup: '備份 DB', backingUp: '備份中…', backupFail: '備份失敗',
|
|
252
389
|
}
|
|
253
390
|
};
|
|
254
391
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
392
|
+
const $ = (id) => document.getElementById(id);
|
|
393
|
+
let lang = localStorage.getItem('lang') || (navigator.language.startsWith('zh') ? 'zh-TW' : 'en');
|
|
258
394
|
const t = (key) => (STRINGS[lang] || STRINGS.en)[key] || key;
|
|
395
|
+
const fmtLang = (key, vars) => Object.keys(vars || {}).reduce((s, k) => s.split('{' + k + '}').join(vars[k]), t(key));
|
|
396
|
+
|
|
397
|
+
// ---- scope model -------------------------------------------------------
|
|
398
|
+
// One global scope drives the whole page: -1 all time, 1 today,
|
|
399
|
+
// 7 last 7 local days, 30 month to date. No section owns a range button.
|
|
400
|
+
const SCOPE_DAYS = [-1, 1, 7, 30];
|
|
401
|
+
const ALL_TIME_TREND_DAYS = 90;
|
|
402
|
+
const SESSION_TOP_N = 20;
|
|
403
|
+
const savedScope = parseInt(localStorage.getItem('scopeDays') || '', 10);
|
|
404
|
+
let scopeDays = SCOPE_DAYS.indexOf(savedScope) >= 0 ? savedScope : 7;
|
|
405
|
+
const savedTab = localStorage.getItem('bdTab');
|
|
406
|
+
let bdTab = ['models', 'agents', 'sessions'].indexOf(savedTab || '') >= 0 ? savedTab : 'models';
|
|
407
|
+
|
|
408
|
+
const scopeLabel = (days) => (STRINGS[lang] || STRINGS.en).scopeSuffix[String(days)] || String(days);
|
|
409
|
+
const trendDays = (days) => (days === -1 ? ALL_TIME_TREND_DAYS : days);
|
|
410
|
+
|
|
411
|
+
// ---- formatting --------------------------------------------------------
|
|
412
|
+
const fmt = (n) => {
|
|
413
|
+
if (n == null || isNaN(n)) return '—';
|
|
414
|
+
if (n >= 1e9) return (n / 1e9).toFixed(2) + 'B';
|
|
415
|
+
if (n >= 1e6) return (n / 1e6).toFixed(1) + 'M';
|
|
416
|
+
if (n >= 1e3) return (n / 1e3).toFixed(1) + 'K';
|
|
417
|
+
return String(Math.round(n));
|
|
418
|
+
};
|
|
419
|
+
const fmtCost = (n) => (n == null || isNaN(n)) ? '—' : '$' + n.toFixed(2);
|
|
420
|
+
const fmtDur = (ms) => {
|
|
421
|
+
if (ms == null || isNaN(ms)) return '—';
|
|
422
|
+
const v = Math.round(ms / 1000);
|
|
423
|
+
const d = Math.floor(v / 86400), h = Math.floor(v % 86400 / 3600), m = Math.floor(v % 3600 / 60), s = v % 60;
|
|
424
|
+
if (d > 0) return d + 'd' + h + 'h';
|
|
425
|
+
if (h > 0) return h + 'h' + String(m).padStart(2, '0') + 'm';
|
|
426
|
+
if (m > 0) return m + 'm' + String(s).padStart(2, '0') + 's';
|
|
427
|
+
return s + 's';
|
|
428
|
+
};
|
|
429
|
+
const fmtSpeed = (n) => (n == null || isNaN(n) || !isFinite(n)) ? '—' : ((n >= 100 ? Math.round(n) : Math.round(n * 10) / 10) + ' tok/s');
|
|
430
|
+
const fmtPct = (n) => (n == null || isNaN(n)) ? '—' : (Math.round(n * 10) / 10) + '%';
|
|
431
|
+
const fmtDay = (iso) => (iso ? String(iso).slice(0, 10) : '—');
|
|
432
|
+
const esc = (s) => String(s == null ? '' : s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
|
|
433
|
+
const dispAgent = (n) => (n === 'unknown' ? t('legacyAgent') : (n == null || n === '' ? t('legacyAgent') : n));
|
|
434
|
+
const fullName = (r) => ((r && r.provider) || '?') + '/' + ((r && r.model) || '?');
|
|
435
|
+
|
|
436
|
+
// ---- pricing basis -----------------------------------------------------
|
|
437
|
+
// `kind` is the server's verdict (exact/sibling/proxy/partial/unpriced).
|
|
438
|
+
// Fall back to deriving it from pricedAs so the UI still tells the truth on
|
|
439
|
+
// older builds. Anything with a null market is unpriced — never render 0.
|
|
440
|
+
function kindOf(row, name) {
|
|
441
|
+
if (row && row.kind) return row.kind;
|
|
442
|
+
if (!row || row.market == null || row.saved == null) return 'unpriced';
|
|
443
|
+
const pa = row.pricedAs;
|
|
444
|
+
if (typeof pa !== 'string') return pa === true ? 'exact' : 'unpriced';
|
|
445
|
+
if (pa.indexOf('proxy:') === 0) return 'proxy';
|
|
446
|
+
return pa === name ? 'exact' : 'sibling';
|
|
447
|
+
}
|
|
448
|
+
const kindLabel = (k) => (STRINGS[lang] || STRINGS.en)['kind' + k.charAt(0).toUpperCase() + k.slice(1)] || k;
|
|
449
|
+
const tokenTotal = (r) => (+(r && r.tok_in) || 0) + (+(r && r.tok_out) || 0) + (+(r && r.tok_reasoning) || 0) + (+(r && r.cache_read) || 0);
|
|
259
450
|
|
|
451
|
+
// ---- i18n / section persistence ---------------------------------------
|
|
260
452
|
function applyI18n() {
|
|
261
453
|
document.documentElement.lang = lang === 'zh-TW' ? 'zh-Hant' : 'en';
|
|
262
|
-
document.querySelectorAll('[data-i18n]').forEach((el) => {
|
|
263
|
-
el.textContent = t(el.dataset.i18n);
|
|
264
|
-
});
|
|
454
|
+
document.querySelectorAll('[data-i18n]').forEach((el) => { el.textContent = t(el.dataset.i18n); });
|
|
265
455
|
$('langBtn').textContent = t('langBtn');
|
|
266
|
-
$('
|
|
267
|
-
$('
|
|
456
|
+
$('caveat').textContent = t('caveat');
|
|
457
|
+
$('bdSortNote').textContent = t('sortNote');
|
|
458
|
+
applyScopeTags();
|
|
459
|
+
setTab(bdTab);
|
|
460
|
+
renderAll();
|
|
268
461
|
}
|
|
269
462
|
|
|
270
463
|
function restoreSections() {
|
|
271
|
-
document.querySelectorAll('#
|
|
464
|
+
document.querySelectorAll('#scopeBtns .pill, #bdTabs .pill').forEach((el) => {
|
|
272
465
|
el.addEventListener('click', (e) => e.stopPropagation());
|
|
273
466
|
});
|
|
274
467
|
document.querySelectorAll('details.tool-section[id]').forEach((el) => {
|
|
@@ -281,12 +474,33 @@ function restoreSections() {
|
|
|
281
474
|
});
|
|
282
475
|
}
|
|
283
476
|
|
|
477
|
+
// Every section title carries the scope it is currently showing. Tools is
|
|
478
|
+
// the honest exception: the API cannot slice tool counts, so it says so.
|
|
479
|
+
function applyScopeTags() {
|
|
480
|
+
const sl = scopeLabel(scopeDays);
|
|
481
|
+
$('tag-headline').textContent = sl;
|
|
482
|
+
$('tag-breakdown').textContent = sl;
|
|
483
|
+
$('tag-pricing').textContent = sl;
|
|
484
|
+
$('tag-raw').textContent = sl;
|
|
485
|
+
$('tag-trend').textContent = scopeDays === -1 ? t('allTimeTrend') : fmtLang('trendScope', { n: trendDays(scopeDays) });
|
|
486
|
+
$('tag-tools').textContent = fmtLang('toolsScope', { scope: sl });
|
|
487
|
+
$('scopeNow').textContent = sl;
|
|
488
|
+
$('updated').textContent = lastUpdatedText || t('loading');
|
|
489
|
+
}
|
|
490
|
+
|
|
284
491
|
function toggleLang() {
|
|
285
492
|
lang = lang === 'en' ? 'zh-TW' : 'en';
|
|
286
|
-
localStorage.setItem('lang', lang);
|
|
493
|
+
try { localStorage.setItem('lang', lang); } catch (_) {}
|
|
287
494
|
applyI18n();
|
|
288
495
|
}
|
|
289
496
|
|
|
497
|
+
function openPricing() {
|
|
498
|
+
const d = $('sec-pricing');
|
|
499
|
+
d.open = true;
|
|
500
|
+
try { localStorage.setItem('oc-sec-' + d.id, '1'); } catch (_) {}
|
|
501
|
+
try { d.scrollIntoView({ block: 'nearest' }); } catch (_) {}
|
|
502
|
+
}
|
|
503
|
+
|
|
290
504
|
async function backupDb() {
|
|
291
505
|
const btn = $('backupBtn');
|
|
292
506
|
btn.classList.add('busy');
|
|
@@ -294,340 +508,385 @@ async function backupDb() {
|
|
|
294
508
|
try {
|
|
295
509
|
const r = await fetch('/api/backup', { cache: 'no-store' });
|
|
296
510
|
const j = await r.json();
|
|
297
|
-
if (j.ok && j.file) {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
} else {
|
|
301
|
-
btn.textContent = '✗ ' + t('backupFail');
|
|
302
|
-
btn.title = j.error || '';
|
|
303
|
-
}
|
|
304
|
-
} catch (e) {
|
|
305
|
-
btn.textContent = '✗ ' + t('backupFail');
|
|
306
|
-
btn.title = e.message;
|
|
307
|
-
}
|
|
511
|
+
if (j.ok && j.file) { btn.textContent = '✓ ' + j.file; btn.title = (j.size / 1024 / 1024).toFixed(1) + ' MB'; }
|
|
512
|
+
else { btn.textContent = '✗ ' + t('backupFail'); btn.title = j.error || ''; }
|
|
513
|
+
} catch (e) { btn.textContent = '✗ ' + t('backupFail'); btn.title = e.message; }
|
|
308
514
|
btn.classList.remove('busy');
|
|
309
515
|
setTimeout(() => { btn.textContent = t('backup'); }, 5000);
|
|
310
516
|
}
|
|
311
517
|
|
|
312
|
-
|
|
313
|
-
let
|
|
518
|
+
// ---- caches (scope switches re-render without hitting the API) ---------
|
|
519
|
+
let lastStats = null;
|
|
520
|
+
let lastUpdatedText = '';
|
|
521
|
+
let lastTools = [];
|
|
522
|
+
const reportCache = Object.create(null); // days -> report
|
|
523
|
+
const trendCache = Object.create(null); // bucketDays -> trend payload
|
|
524
|
+
|
|
525
|
+
// ---- headline + breakdown + pricing + raw ------------------------------
|
|
526
|
+
function renderReport(rep) {
|
|
527
|
+
window.__lastReport = rep;
|
|
528
|
+
if (!rep) return;
|
|
529
|
+
const c = (rep.stats && rep.stats.cost) || {};
|
|
530
|
+
const o = (rep.stats && rep.stats.overview) || {};
|
|
531
|
+
const cacheAll = (c.cacheRead || 0) + (c.cacheWrite || 0);
|
|
532
|
+
|
|
533
|
+
// Cache reads are context RE-reads (the same prefix counted again on every
|
|
534
|
+
// turn), so they must not inflate the headline token count.
|
|
535
|
+
$('hl-tokens').textContent = fmt((c.input || 0) + (c.output || 0) + (c.reasoning || 0));
|
|
536
|
+
$('hl-in').textContent = fmt(c.input);
|
|
537
|
+
$('hl-out').textContent = fmt(c.output);
|
|
538
|
+
$('hl-reasoning').textContent = fmt(c.reasoning);
|
|
539
|
+
$('hl-cache').textContent = fmt(cacheAll);
|
|
540
|
+
$('hl-cost').textContent = fmtCost(c.total);
|
|
541
|
+
$('hl-market').textContent = fmtCost(c.marketTotal);
|
|
542
|
+
$('hl-saved').textContent = fmtCost(c.savedTotal);
|
|
543
|
+
$('hl-savedPct').textContent = (c.marketTotal > 0 && c.savedPct != null) ? fmtLang('savedOf', { pct: fmtPct(c.savedPct) }) : '';
|
|
544
|
+
|
|
545
|
+
const meta = [t('sessionsLabel') + ' ' + fmt(o.sessions), t('messagesLabel') + ' ' + fmt(o.messages),
|
|
546
|
+
t('daysLabel') + ' ' + fmt(o.days), t('avgCostDay') + ' ' + fmtCost(c.avgDay),
|
|
547
|
+
t('avgTokensSession') + ' ' + fmt(c.avgTokensSession), t('medianTokensSession') + ' ' + fmt(c.medianTokensSession)];
|
|
548
|
+
$('hl-meta').textContent = meta.join(' · ');
|
|
549
|
+
|
|
550
|
+
// Pricing credibility: catalog date, proxy-estimated share, unpriced rows.
|
|
551
|
+
const priceAsOf = c.priceAsOf || rep.priceAsOf || null;
|
|
552
|
+
$('hl-priceAsOf').textContent = priceAsOf ? fmtLang('pricesAsOf', { d: fmtDay(priceAsOf) }) : '';
|
|
553
|
+
const prov = rep.providers || [];
|
|
554
|
+
const unpriced = prov.filter((p) => kindOf(p, fullName(p)) === 'unpriced');
|
|
555
|
+
const unpricedRows = c.unpricedRows != null ? c.unpricedRows : unpriced.length;
|
|
556
|
+
const unpricedTokens = c.unpricedTokens != null
|
|
557
|
+
? c.unpricedTokens
|
|
558
|
+
: unpriced.reduce((s, p) => s + tokenTotal(p), 0);
|
|
559
|
+
const warn = $('hl-unpriced');
|
|
560
|
+
warn.textContent = unpricedRows > 0
|
|
561
|
+
? fmtLang('unpricedModels', { n: unpricedRows, t: fmt(unpricedTokens) }) : '';
|
|
562
|
+
warn.title = warn.textContent ? t('pricingBasis') : '';
|
|
563
|
+
let proxyShare = c.proxyShare;
|
|
564
|
+
if (proxyShare == null) {
|
|
565
|
+
const proxyTok = prov.filter((p) => kindOf(p, fullName(p)) === 'proxy').reduce((s, p) => s + tokenTotal(p), 0);
|
|
566
|
+
const allTok = prov.reduce((s, p) => s + tokenTotal(p), 0);
|
|
567
|
+
proxyShare = allTok > 0 ? (proxyTok / allTok) * 100 : null;
|
|
568
|
+
}
|
|
569
|
+
$('hl-proxy').textContent = (proxyShare != null && proxyShare > 0) ? fmtLang('proxyShare', { pct: fmtPct(proxyShare) }) : '';
|
|
314
570
|
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
if (n >= 1e6) return (n / 1e6).toFixed(1) + 'M';
|
|
319
|
-
if (n >= 1e3) return (n / 1e3).toFixed(1) + 'K';
|
|
320
|
-
return String(Math.round(n));
|
|
321
|
-
};
|
|
322
|
-
const fmtCost = (n) => (n == null || isNaN(n)) ? '—' : '$' + n.toFixed(2);
|
|
323
|
-
const fmtDur = (ms) => { const v = Math.round((ms || 0) / 1000); if (!isFinite(v) || v < 0) return '—'; const d = Math.floor(v / 86400), h = Math.floor(v % 86400 / 3600), m = Math.floor(v % 3600 / 60), s = v % 60; if (d > 0) return d + 'd' + h + 'h'; if (h > 0) return h + 'h' + String(m).padStart(2, '0') + 'm'; if (m > 0) return m + 'm' + String(s).padStart(2, '0') + 's'; return s + 's'; };
|
|
324
|
-
const esc = (s) => String(s == null ? '' : s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
325
|
-
const fmtSpeed = (n) => (n == null || isNaN(n) || !isFinite(n)) ? '—' : ((n >= 100 ? Math.round(n) : Math.round(n * 10) / 10) + ' tok/s');
|
|
326
|
-
const dispAgent = (n) => (n === 'unknown' ? 'legacy' : n);
|
|
327
|
-
const LEGACY_TIP = 'pre-2026-08 V1 legacy, $0';
|
|
328
|
-
function agentTip(base, name) {
|
|
329
|
-
if (name !== 'unknown') return base || '';
|
|
330
|
-
return (base ? base + ' · ' : '') + LEGACY_TIP;
|
|
571
|
+
renderBreakdown(rep);
|
|
572
|
+
renderPricing(rep);
|
|
573
|
+
$('rawText').textContent = formatReport(rep);
|
|
331
574
|
}
|
|
332
575
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
const
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
return `<div class="bar-row"><div class="name">${name}</div>
|
|
345
|
-
<div class="track"><div class="fill" style="width:${pct}%;background:${color}"></div></div>
|
|
346
|
-
<div class="val">${fmt(value)}</div></div>`;
|
|
576
|
+
function shareCell(row, total) {
|
|
577
|
+
const tk = tokenTotal(row);
|
|
578
|
+
const seg = (v) => (tk > 0 ? (v / tk) * 100 : 0);
|
|
579
|
+
const w = total > 0 ? Math.max(0.4, (tk / total) * 100) : 0;
|
|
580
|
+
if (tk <= 0) return '<div class="track share"></div>';
|
|
581
|
+
return `<div class="track share" title="${esc(fmt(tk))}"><div class="stack" style="width:${w}%">`
|
|
582
|
+
+ `<div style="width:${seg(+row.tok_in || 0)}%;background:var(--in)"></div>`
|
|
583
|
+
+ `<div style="width:${seg(+row.tok_out || 0)}%;background:var(--out)"></div>`
|
|
584
|
+
+ `<div style="width:${seg(+row.tok_reasoning || 0)}%;background:var(--tool)"></div>`
|
|
585
|
+
+ `<div style="width:${seg(+row.cache_read || 0)}%;background:var(--cache)"></div>`
|
|
586
|
+
+ '</div></div>';
|
|
347
587
|
}
|
|
348
588
|
|
|
349
|
-
function
|
|
350
|
-
const
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
$('token-bars').innerHTML =
|
|
365
|
-
barRow('Input', d.cost.input, max, 'var(--in)') +
|
|
366
|
-
barRow('Output', d.cost.output, max, 'var(--out)') +
|
|
367
|
-
barRow('Reasoning', d.cost.reasoning, max, 'var(--tool)') +
|
|
368
|
-
barRow('Cache Read', d.cost.cacheRead, max, 'var(--cache)') +
|
|
369
|
-
barRow('Cache Write', d.cost.cacheWrite, max, 'var(--cost)');
|
|
370
|
-
}
|
|
589
|
+
function bdRow(name, title, row, total, countVal) {
|
|
590
|
+
const wall = row.wallMs;
|
|
591
|
+
const active = row.activeMs;
|
|
592
|
+
return `<tr><td class="bd-name" title="${esc(title)}">${esc(name)}</td>`
|
|
593
|
+
+ `<td>${shareCell(row, total)}</td>`
|
|
594
|
+
+ `<td>${countVal}</td>`
|
|
595
|
+
+ `<td>${fmt(row.tok_in)}</td>`
|
|
596
|
+
+ `<td>${fmt(row.tok_out)}</td>`
|
|
597
|
+
+ `<td>${fmt(row.tok_reasoning)}</td>`
|
|
598
|
+
+ `<td>${fmt(row.cache_read)}</td>`
|
|
599
|
+
+ `<td>${fmtSpeed(row.speed)}</td>`
|
|
600
|
+
+ `<td title="${esc(active == null ? '' : t('activeLabel') + ' ' + fmtDur(active))}">${fmtDur(wall)}</td>`
|
|
601
|
+
+ `<td>${fmtCost(row.market)}</td>`
|
|
602
|
+
+ `<td>${fmtCost(row.saved)}</td></tr>`;
|
|
603
|
+
}
|
|
371
604
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
605
|
+
const sortByTokens = (a, b) => tokenTotal(b) - tokenTotal(a);
|
|
606
|
+
const sumTokens = (rows) => rows.reduce((s, r) => s + tokenTotal(r), 0);
|
|
607
|
+
|
|
608
|
+
function renderBreakdown(rep) {
|
|
609
|
+
const prov = (rep.providers || []).slice().sort(sortByTokens);
|
|
610
|
+
const ags = (rep.agents || []).slice().sort(sortByTokens);
|
|
611
|
+
const sess = (rep.sessions || []).slice(0, SESSION_TOP_N).sort(sortByTokens);
|
|
612
|
+
|
|
613
|
+
$('bd-models').innerHTML = prov.map((p) =>
|
|
614
|
+
bdRow(fullName(p), fullName(p) + ' · ' + (p.pricedAs || kindOf(p, fullName(p))), p, sumTokens(prov), fmt(p.sessions))).join('');
|
|
615
|
+
$('bd-agents').innerHTML = ags.map((a) =>
|
|
616
|
+
bdRow(dispAgent(a.agent), (a.agent || '') + ' · ' + t('sessionsLabel') + ' ' + fmt(a.sessions), a, sumTokens(ags), fmt(a.sessions))).join('');
|
|
617
|
+
$('bd-sessions').innerHTML = sess.map((s) =>
|
|
618
|
+
bdRow(String(s.id).slice(0, 12), String(s.id) + ' · ' + (s.title || '') + ' · ' + (s.agent || '') + ' · ' + fullName(s),
|
|
619
|
+
s, sumTokens(sess), fmt(s.messages))).join('');
|
|
620
|
+
|
|
621
|
+
const empty = !prov.length && !ags.length && !sess.length;
|
|
622
|
+
$('bd-empty').style.display = empty ? '' : 'none';
|
|
623
|
+
$('bd-empty').textContent = empty ? t('noData') : '';
|
|
624
|
+
$('bdSortNote').textContent = t('sortNote');
|
|
625
|
+
setTab(bdTab);
|
|
626
|
+
}
|
|
375
627
|
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
628
|
+
function setTab(tab) {
|
|
629
|
+
bdTab = tab;
|
|
630
|
+
try { localStorage.setItem('bdTab', tab); } catch (_) {}
|
|
631
|
+
['models', 'agents', 'sessions'].forEach((k) => { $('bd-' + k).style.display = k === tab ? '' : 'none'; });
|
|
632
|
+
const heads = {
|
|
633
|
+
models: [t('thUsageModel'), t('thSessions')],
|
|
634
|
+
agents: [t('tabAgents'), t('thSessions')],
|
|
635
|
+
sessions: [t('tabSessions'), t('thMsgs')],
|
|
636
|
+
};
|
|
637
|
+
const h = heads[tab] || heads.models;
|
|
638
|
+
$('th-name').textContent = h[0];
|
|
639
|
+
$('th-count').textContent = h[1];
|
|
640
|
+
document.querySelectorAll('#bdTabs .pill').forEach((el) => {
|
|
641
|
+
el.classList.toggle('active', el.dataset.tab === tab);
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
function renderPricing(rep) {
|
|
646
|
+
const prov = (rep.providers || []).slice().sort(sortByTokens);
|
|
647
|
+
$('pricing-rows').innerHTML = prov.map((p) => {
|
|
648
|
+
const name = fullName(p);
|
|
649
|
+
const kind = kindOf(p, name);
|
|
650
|
+
return `<tr><td class="clip" title="${esc(name)}">${esc(name)}</td>`
|
|
651
|
+
+ `<td class="clip" title="${esc(p.pricedAs || '')}">${kind === 'unpriced' ? '—' : esc(p.pricedAs || '—')}</td>`
|
|
652
|
+
+ `<td><span class="badge ${esc(kind)}">${esc(kindLabel(kind))}</span></td>`
|
|
653
|
+
+ `<td>${fmtCost(p.market)}</td>`
|
|
654
|
+
+ `<td>${fmtCost(p.saved)}</td></tr>`;
|
|
655
|
+
}).join('');
|
|
656
|
+
$('pricing-empty').style.display = prov.length ? 'none' : '';
|
|
657
|
+
$('pricing-empty').textContent = prov.length ? '' : t('noData');
|
|
658
|
+
}
|
|
394
659
|
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
} else {
|
|
660
|
+
// ---- trend (buckets follow scope; newest day leftmost) -----------------
|
|
661
|
+
function renderTrend(data) {
|
|
662
|
+
if (!data || !data.labels || !data.labels.length) {
|
|
399
663
|
$('trend-tokens').innerHTML = '';
|
|
400
664
|
$('trend-token-x').innerHTML = '';
|
|
401
665
|
$('trend-cost').innerHTML = '';
|
|
402
666
|
$('trend-cost-x').innerHTML = '';
|
|
403
667
|
$('costZeroNote').style.display = 'none';
|
|
404
|
-
$('costSection').querySelector('.legend').style.display = '';
|
|
405
668
|
$('trendCostChart').style.display = '';
|
|
406
669
|
$('trend-cost-x').style.display = '';
|
|
407
670
|
$('trendStatus').textContent = '⏳ ' + t('generating');
|
|
671
|
+
return;
|
|
408
672
|
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
const
|
|
413
|
-
const
|
|
414
|
-
const
|
|
415
|
-
|
|
416
|
-
const
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
673
|
+
const ts = data.generatedAt || 0;
|
|
674
|
+
const day = (s) => String(s == null ? 0 : s);
|
|
675
|
+
// Server already emits newest→oldest, so today sits at the left edge as-is.
|
|
676
|
+
const labels = data.labels || [];
|
|
677
|
+
const input = data.input || [];
|
|
678
|
+
const output = data.output || [];
|
|
679
|
+
const reasoning = data.reasoning || [];
|
|
680
|
+
const cache = data.cacheRead || [];
|
|
681
|
+
const cost = data.cost || [];
|
|
682
|
+
const sessions = data.sessions || [];
|
|
683
|
+
const n = labels.length;
|
|
684
|
+
|
|
685
|
+
const maxAll = Math.max(1, ...input, ...output, ...reasoning, ...cache);
|
|
686
|
+
const genTime = ts ? new Date(ts).toLocaleTimeString(lang === 'zh-TW' ? 'zh-TW' : 'en', { hour12: false }) : '?';
|
|
687
|
+
|
|
688
|
+
$('trend-tokens').innerHTML = labels.map((l, i) => {
|
|
689
|
+
const hi = (input[i] / maxAll) * 100, ho = (output[i] / maxAll) * 100;
|
|
690
|
+
const hr = (reasoning[i] / maxAll) * 100, hc = (cache[i] / maxAll) * 100;
|
|
691
|
+
const tip = [l, t('sessionsLabel') + ' ' + day(sessions[i]),
|
|
692
|
+
t('tIn') + ' ' + fmt(input[i]), t('tOut') + ' ' + fmt(output[i]),
|
|
693
|
+
t('tReasoning') + ' ' + fmt(reasoning[i]), t('tCache') + ' ' + fmt(cache[i]),
|
|
694
|
+
t('totalCost') + ' ' + fmtCost(cost[i])].join(' · ') + ' · ' + genTime;
|
|
695
|
+
return `<div class="col" title="${esc(tip)}">`
|
|
696
|
+
+ '<div style="display:flex;gap:2px;align-items:flex-end;width:100%;height:170px;justify-content:center">'
|
|
697
|
+
+ '<div style="position:relative;width:100%;height:100%">'
|
|
698
|
+
+ `<div class="trend-bar trend-bar-cache" style="height:${Math.max(hc, 1)}%;background:var(--cache)"></div>`
|
|
699
|
+
+ `<div class="trend-bar trend-bar-input" style="height:${Math.max(hi, 1)}%;background:var(--in)"></div>`
|
|
700
|
+
+ `<div class="trend-bar trend-bar-output" style="height:${Math.max(ho, 1)}%;background:var(--out)"></div>`
|
|
701
|
+
+ `<div class="trend-bar trend-bar-reasoning" style="height:${Math.max(hr, 1)}%;background:var(--tool)"></div>`
|
|
702
|
+
+ '</div></div></div>';
|
|
433
703
|
}).join('');
|
|
434
704
|
|
|
435
705
|
const labelStep = Math.max(1, Math.ceil(n / 14));
|
|
436
|
-
$('trend-token-x').innerHTML =
|
|
437
|
-
|
|
438
|
-
return `<span style="${vis}">${l}</span>`;
|
|
439
|
-
}).join('');
|
|
706
|
+
$('trend-token-x').innerHTML = labels.map((l, i) =>
|
|
707
|
+
`<span style="${i % labelStep === 0 ? '' : 'visibility:hidden'}">${esc(l)}</span>`).join('');
|
|
440
708
|
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
$('costZeroNote').style.display = 'none';
|
|
446
|
-
const costThreshold = 0.0001;
|
|
447
|
-
const displayCost = data.cost.map((v) => v < costThreshold ? 0 : v);
|
|
448
|
-
const hasRealCost = displayCost.some((v) => v > 0);
|
|
449
|
-
if (!hasRealCost) {
|
|
450
|
-
$('costSection').querySelector('.legend').style.display = 'none';
|
|
451
|
-
$('trendCostChart').style.display = 'none';
|
|
452
|
-
$('trend-cost-x').style.display = 'none';
|
|
453
|
-
$('costZeroNote').style.display = '';
|
|
454
|
-
$('costZeroNote').textContent = t('allFree');
|
|
455
|
-
} else {
|
|
456
|
-
const maxCost = Math.max(...displayCost);
|
|
457
|
-
$('trend-cost').innerHTML = data.labels.map((l, i) => {
|
|
458
|
-
const h = maxCost > 0 ? (displayCost[i] / maxCost) * 100 : 0;
|
|
459
|
-
const tip = l + ': $' + (displayCost[i] || 0).toFixed(4);
|
|
460
|
-
return `<div class="col" title="${tip}">
|
|
461
|
-
<div style="display:flex;align-items:flex-end;width:100%;height:170px;justify-content:center">
|
|
462
|
-
<div style="width:50%;height:${Math.max(h, 1)}%;background:var(--cost);border-radius:2px 2px 0 0"></div>
|
|
463
|
-
</div>
|
|
464
|
-
</div>`;
|
|
465
|
-
}).join('');
|
|
466
|
-
$('trend-cost-x').innerHTML = data.labels.map((l, i) => {
|
|
467
|
-
const vis = i % labelStep === 0 ? '' : 'visibility:hidden';
|
|
468
|
-
return `<span style="${vis}">${l}</span>`;
|
|
469
|
-
}).join('');
|
|
470
|
-
}
|
|
471
|
-
} else {
|
|
472
|
-
$('costSection').querySelector('.legend').style.display = 'none';
|
|
709
|
+
$('trendStatus').textContent = data.stale ? ' ⟳ ' + t('updating') : '';
|
|
710
|
+
const anyCost = cost.some((v) => v > 0);
|
|
711
|
+
const costLegend = $('costSection').querySelector('.legend');
|
|
712
|
+
if (!anyCost) {
|
|
473
713
|
$('trendCostChart').style.display = 'none';
|
|
474
714
|
$('trend-cost-x').style.display = 'none';
|
|
715
|
+
costLegend.style.display = 'none';
|
|
475
716
|
$('costZeroNote').style.display = '';
|
|
476
717
|
$('costZeroNote').textContent = t('allFree');
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
costLegend.style.display = '';
|
|
721
|
+
$('trendCostChart').style.display = '';
|
|
722
|
+
$('trend-cost-x').style.display = '';
|
|
723
|
+
$('costZeroNote').style.display = 'none';
|
|
724
|
+
const maxCost = Math.max(...cost);
|
|
725
|
+
$('trend-cost').innerHTML = labels.map((l, i) => {
|
|
726
|
+
const h = maxCost > 0 ? (cost[i] / maxCost) * 100 : 0;
|
|
727
|
+
return `<div class="col" title="${esc(l + ' · ' + fmtCost(cost[i]) + ' · ' + genTime)}">`
|
|
728
|
+
+ '<div style="display:flex;align-items:flex-end;width:100%;height:170px;justify-content:center">'
|
|
729
|
+
+ `<div style="width:50%;height:${Math.max(h, 1)}%;background:var(--cost);border-radius:2px 2px 0 0"></div>`
|
|
730
|
+
+ '</div></div>';
|
|
731
|
+
}).join('');
|
|
732
|
+
$('trend-cost-x').innerHTML = labels.map((l, i) =>
|
|
733
|
+
`<span style="${i % labelStep === 0 ? '' : 'visibility:hidden'}">${esc(l)}</span>`).join('');
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
// ---- tools (all-time; the API cannot slice them) -----------------------
|
|
737
|
+
// Long tail (48 tools, most under 0.1%) is folded into one "other" row:
|
|
738
|
+
// a 48-row list of near-empty bars is visual noise, not information.
|
|
739
|
+
const TOOL_TOP_N = 12;
|
|
740
|
+
|
|
741
|
+
function renderTools(tools) {
|
|
742
|
+
if (!tools || !tools.length) {
|
|
743
|
+
$('tool-bars').innerHTML = lastStats ? '<div class="empty">' + esc(t('noData')) + '</div>' : '';
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
const sorted = tools.slice().sort((a, b) => (b.count || 0) - (a.count || 0));
|
|
747
|
+
const top = sorted.slice(0, TOOL_TOP_N);
|
|
748
|
+
const rest = sorted.slice(TOOL_TOP_N);
|
|
749
|
+
const rows = top.slice();
|
|
750
|
+
if (rest.length) {
|
|
751
|
+
const restCount = rest.reduce((s, x) => s + (x.count || 0), 0);
|
|
752
|
+
const total = tools.reduce((s, x) => s + (x.count || 0), 0);
|
|
753
|
+
const restPct = total > 0 ? Math.round((restCount / total) * 1000) / 10 : 0;
|
|
754
|
+
rows.push({ name: t('toolsOther').replace('{n}', String(rest.length)), count: restCount, pct: restPct, rest: true });
|
|
477
755
|
}
|
|
756
|
+
const max = Math.max.apply(null, rows.map((x) => x.count || 0));
|
|
757
|
+
$('tool-bars').innerHTML = rows.map((tool) => {
|
|
758
|
+
const pct = max > 0 ? Math.max(0.5, ((tool.count || 0) / max) * 100) : 0;
|
|
759
|
+
const color = tool.rest ? 'var(--muted)' : 'var(--tool)';
|
|
760
|
+
const dim = tool.rest ? ' style="color:var(--muted)"' : '';
|
|
761
|
+
return `<div class="bar-row">`
|
|
762
|
+
+ `<div class="name"${dim} title="${esc(tool.name || '')}">${esc(tool.name || '')}</div>`
|
|
763
|
+
+ `<div class="track"><div class="fill" style="width:${pct}%;background:${color}"></div></div>`
|
|
764
|
+
+ `<div class="val">${fmt(tool.count)} · ${fmtPct(tool.pct)}</div></div>`;
|
|
765
|
+
}).join('');
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
// ---- one render pass over everything (no network) ---------------------
|
|
769
|
+
function renderAll() {
|
|
770
|
+
applyScopeTags();
|
|
771
|
+
if (window.__lastReport) renderReport(window.__lastReport);
|
|
772
|
+
const tr = trendCache[trendDays(scopeDays)];
|
|
773
|
+
renderTrend(tr || null);
|
|
774
|
+
renderTools(lastTools);
|
|
478
775
|
}
|
|
479
776
|
|
|
777
|
+
// ---- data loading ------------------------------------------------------
|
|
480
778
|
async function poll() {
|
|
779
|
+
const bucket = trendDays(scopeDays);
|
|
481
780
|
try {
|
|
482
|
-
const r = await fetch('/api/stats?days=' +
|
|
781
|
+
const r = await fetch('/api/stats?days=' + bucket + '&_=' + Date.now(), { cache: 'no-store' });
|
|
483
782
|
const j = await r.json();
|
|
484
783
|
if (j.loading) {
|
|
485
784
|
$('pill').textContent = t('updating');
|
|
486
785
|
$('pill').className = 'pill lang';
|
|
487
|
-
|
|
488
|
-
|
|
786
|
+
lastUpdatedText = t('updating');
|
|
787
|
+
$('updated').textContent = lastUpdatedText;
|
|
788
|
+
return null;
|
|
489
789
|
}
|
|
490
790
|
if (!j.ok) {
|
|
491
791
|
$('errbanner').textContent = t('statsFail') + ': ' + (j.error || '');
|
|
492
792
|
$('errbanner').classList.add('show');
|
|
493
793
|
$('pill').textContent = t('statsFail');
|
|
494
794
|
$('pill').className = 'pill err';
|
|
495
|
-
return;
|
|
795
|
+
return null;
|
|
496
796
|
}
|
|
497
797
|
$('errbanner').classList.remove('show');
|
|
498
798
|
$('pill').textContent = t('live');
|
|
499
799
|
$('pill').className = 'pill ok';
|
|
500
|
-
|
|
800
|
+
lastStats = j;
|
|
801
|
+
lastTools = (j.data && j.data.tools) || [];
|
|
802
|
+
if (j.trend && j.trend.labels && j.trend.labels.length) {
|
|
803
|
+
trendCache[bucket] = j.trend;
|
|
804
|
+
trendCache[trendDays(j.trend.step || bucket)] = j.trend;
|
|
805
|
+
renderTrend(trendCache[trendDays(scopeDays)] || j.trend);
|
|
806
|
+
} else {
|
|
807
|
+
renderTrend(null);
|
|
808
|
+
}
|
|
809
|
+
renderTools(lastTools);
|
|
501
810
|
const ago = Math.round((Date.now() - (j.generatedAt || 0)) / 1000);
|
|
502
811
|
const time = new Date().toLocaleTimeString(lang === 'zh-TW' ? 'zh-TW' : 'en', { hour12: false });
|
|
503
|
-
|
|
812
|
+
lastUpdatedText = t('lastUpdated') + ' ' + time + ' (' + t('dataAgo') + ' ' + ago + 's)';
|
|
813
|
+
$('updated').textContent = lastUpdatedText;
|
|
814
|
+
return j;
|
|
504
815
|
} catch (e) {
|
|
505
816
|
$('updated').textContent = t('connFail') + ': ' + e.message;
|
|
817
|
+
lastUpdatedText = $('updated').textContent;
|
|
818
|
+
return null;
|
|
506
819
|
}
|
|
507
820
|
}
|
|
508
821
|
|
|
509
|
-
function setRange(days) {
|
|
510
|
-
currentDays = days;
|
|
511
|
-
localStorage.setItem('trendDays', days);
|
|
512
|
-
document.querySelectorAll('#rangeBtns .pill').forEach((el) => {
|
|
513
|
-
el.classList.toggle('active', parseInt(el.dataset.days, 10) === days);
|
|
514
|
-
});
|
|
515
|
-
$('trendTitle').textContent = t('dailyTrend').replace('{days}', days);
|
|
516
|
-
poll();
|
|
517
|
-
}
|
|
518
|
-
|
|
519
822
|
let reportLoading = false;
|
|
520
823
|
let reportRetry = 0;
|
|
521
824
|
const MAX_REPORT_RETRIES = 12;
|
|
522
825
|
|
|
523
|
-
function
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
const sess = r.sessions || [];
|
|
552
|
-
const xmax = Math.max(0, ...sess.map((s) => s.wallMs || 0));
|
|
553
|
-
$('session-bars').innerHTML = sess.length ? sess.map((s) =>
|
|
554
|
-
twoBars(String(s.id).slice(0, 12), s.wallMs || 0, s.activeMs || 0, xmax,
|
|
555
|
-
fmtDur(s.wallMs) + ' · ' + fmtSpeed(s.speed),
|
|
556
|
-
(s.title || '') + ' · ' + s.agent + ' · ' + s.provider + '/' + s.model + ' · ' + (s.messages || 0) + ' msgs')
|
|
557
|
-
).join('') : '<div class="val">—</div>';
|
|
558
|
-
|
|
559
|
-
const mkt = (r.providers || []).slice().sort((a, b) => (b.market || 0) - (a.market || 0));
|
|
560
|
-
const mmax = Math.max(0, ...mkt.map((p) => p.market || 0));
|
|
561
|
-
$('savings-bars').innerHTML = mkt.length ? mkt.map((p) => {
|
|
562
|
-
const pct = mmax > 0 ? Math.max(0.5, ((p.market || 0) / mmax) * 100) : 0;
|
|
563
|
-
const sv = p.saved == null ? '—' : fmtCost(p.saved) + ' saved';
|
|
564
|
-
const tip = 'market ' + fmtCost(p.market) + ' · actual ' + fmtCost(p.cost) + ' · via ' + (p.pricedAs || '?');
|
|
565
|
-
return `<div class="bar-row wide"><div class="name" title="${esc(fullName(p))}">${esc(fullName(p))}</div>
|
|
566
|
-
<div class="track"><div class="fill" style="width:${pct}%;background:var(--cost)"></div></div>
|
|
567
|
-
<div class="val" title="${esc(tip)}">${sv}</div></div>`;
|
|
568
|
-
}).join('') : '<div class="val">—</div>';
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
function repTable(heads, rows) {
|
|
572
|
-
return `<div class="scrollx"><table><thead><tr>${heads.map((h) => `<th>${esc(h)}</th>`).join('')}</tr></thead><tbody>${rows.join('')}</tbody></table></div>`;
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
function repBlock(title, hint, body, isOpen) {
|
|
576
|
-
return `<details class="tool-section"${isOpen ? ' open' : ''}><summary><h2>${esc(title)}</h2><span class="rep-hint">${esc(hint)}</span></summary>${body}</details>`;
|
|
826
|
+
async function loadReport(days) {
|
|
827
|
+
if (reportLoading) return reportCache[days] || null;
|
|
828
|
+
reportLoading = true;
|
|
829
|
+
try {
|
|
830
|
+
const r = await fetch('/api/report?days=' + days + '&_=' + Date.now(), { cache: 'no-store' });
|
|
831
|
+
const j = await r.json();
|
|
832
|
+
if (j.ok === false) { $('rawText').textContent = t('reportFail'); return null; }
|
|
833
|
+
if (!j.ready || !j.report) {
|
|
834
|
+
$('rawText').textContent = '⏳ ' + t('generating') + '…';
|
|
835
|
+
reportRetry++;
|
|
836
|
+
if (reportRetry < MAX_REPORT_RETRIES) {
|
|
837
|
+
const delay = Math.min(1000 * Math.pow(1.5, reportRetry), 15000);
|
|
838
|
+
setTimeout(() => loadReport(days), delay);
|
|
839
|
+
}
|
|
840
|
+
return null;
|
|
841
|
+
}
|
|
842
|
+
reportRetry = 0;
|
|
843
|
+
reportCache[days] = j.report;
|
|
844
|
+
applyScopeTags();
|
|
845
|
+
renderReport(j.report);
|
|
846
|
+
return j.report;
|
|
847
|
+
} catch (e) {
|
|
848
|
+
$('rawText').textContent = t('connFail') + ': ' + e.message;
|
|
849
|
+
return null;
|
|
850
|
+
} finally {
|
|
851
|
+
reportLoading = false;
|
|
852
|
+
}
|
|
577
853
|
}
|
|
578
854
|
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
})), true);
|
|
597
|
-
|
|
598
|
-
const ags = r.agents || [];
|
|
599
|
-
h += repBlock(t('repAgents'), ags.length + ' agents' + (ags.length ? ' · ' + ags[0].agent : ''),
|
|
600
|
-
repTable(['Agent', 'Sess', 'Cost', 'In', 'Out', 'Speed', 'Wall', 'Active', 'Market', 'Saved'], ags.map((a) =>
|
|
601
|
-
`<tr><td>${esc(a.agent)}</td><td>${a.sessions}</td><td>${fmtCost(a.cost)}</td><td>${fmt(a.tok_in)}</td><td>${fmt(a.tok_out)}</td><td>${fmtSpeed(a.speed)}</td><td>${fmtDur(a.wallMs)}</td><td>${fmtDur(a.activeMs)}</td><td>${fmtCost(a.market)}</td><td>${fmtCost(a.saved)}</td></tr>`)), true);
|
|
602
|
-
|
|
603
|
-
const sess = r.sessions || [];
|
|
604
|
-
h += repBlock(t('repSessions'), 'top ' + sess.length + ' by wall',
|
|
605
|
-
repTable(['Session', 'Agent', 'Model', 'Wall', 'Active', 'Msgs', 'In', 'Out', 'Speed', 'Saved'], sess.map((s) => {
|
|
606
|
-
const name = s.provider + '/' + s.model;
|
|
607
|
-
return `<tr><td class="clip" title="${esc(s.id + ' · ' + (s.title || ''))}">${esc(String(s.id).slice(0, 12))}</td><td>${esc(s.agent)}</td><td class="clip" title="${esc(name)}">${esc(name)}</td><td>${fmtDur(s.wallMs)}</td><td>${fmtDur(s.activeMs)}</td><td>${s.messages}</td><td>${fmt(s.tok_in)}</td><td>${fmt(s.tok_out)}</td><td>${fmtSpeed(s.speed)}</td><td>${fmtCost(s.saved)}</td></tr>`;
|
|
608
|
-
})), true);
|
|
609
|
-
|
|
610
|
-
const basis = prov.filter((p) => p.pricedAs && p.pricedAs !== (p.provider + '/' + p.model));
|
|
611
|
-
if (basis.length) {
|
|
612
|
-
h += repBlock(t('repPricing'), basis.length + ' mappings',
|
|
613
|
-
repTable(['Usage', 'Basis'], basis.map((p) =>
|
|
614
|
-
`<tr><td class="clip" title="${esc(p.provider + '/' + p.model)}">${esc(p.provider + '/' + p.model)}</td><td class="clip" title="${esc(p.pricedAs)}">${esc(p.pricedAs)}</td></tr>`)), false);
|
|
855
|
+
// ---- global scope switch ----------------------------------------------
|
|
856
|
+
// Always repaints every section's scope tag first (cache-backed, instant),
|
|
857
|
+
// then refreshes from the API only for a scope we have not seen yet.
|
|
858
|
+
async function setScope(days) {
|
|
859
|
+
days = parseInt(days, 10);
|
|
860
|
+
if (SCOPE_DAYS.indexOf(days) < 0) return null;
|
|
861
|
+
scopeDays = days;
|
|
862
|
+
try { localStorage.setItem('scopeDays', String(days)); } catch (_) {}
|
|
863
|
+
document.querySelectorAll('#scopeBtns .pill').forEach((el) => {
|
|
864
|
+
el.classList.toggle('active', parseInt(el.dataset.days, 10) === days);
|
|
865
|
+
});
|
|
866
|
+
const cached = reportCache[days];
|
|
867
|
+
if (cached) {
|
|
868
|
+
window.__lastReport = cached;
|
|
869
|
+
applyScopeTags();
|
|
870
|
+
renderAll();
|
|
871
|
+
return cached;
|
|
615
872
|
}
|
|
616
|
-
|
|
617
|
-
|
|
873
|
+
applyScopeTags();
|
|
874
|
+
$('rawText').textContent = '⏳ ' + t('generating') + '…';
|
|
875
|
+
const [rep] = await Promise.all([loadReport(days), poll()]);
|
|
876
|
+
return rep;
|
|
618
877
|
}
|
|
619
878
|
|
|
879
|
+
// ---- plain-text statement (RAW TEXT) ----------------------------------
|
|
620
880
|
function formatReport(jr) {
|
|
621
881
|
const genTime = jr.generatedAt ? new Date(jr.generatedAt).toLocaleTimeString(lang === 'zh-TW' ? 'zh-TW' : 'en', { hour12: false }) : '?';
|
|
622
|
-
const cachedNote = jr.stale ? ' [
|
|
623
|
-
const
|
|
624
|
-
const
|
|
882
|
+
const cachedNote = jr.stale ? ' [' + t('updating') + ']' : ' [' + t('cachedAt') + ' ' + genTime + ']';
|
|
883
|
+
const fCost = (n) => { const v = parseFloat(n); if (isNaN(v)) return '—'; return v === 0 ? '$0.00' : '$' + v.toFixed(4); };
|
|
884
|
+
const fT = (n) => { const v = parseInt(n, 10); if (isNaN(v)) return '—'; if (v >= 1e9) return (v / 1e9).toFixed(2) + 'B'; if (v >= 1e6) return (v / 1e6).toFixed(1) + 'M'; if (v >= 1e3) return (v / 1e3).toFixed(1) + 'K'; return String(v); };
|
|
625
885
|
const pad = (s, w) => { s = String(s); return s.length >= w ? s : s + ' '.repeat(w - s.length); };
|
|
626
886
|
const rpad = (s, w) => { s = String(s); return s.length >= w ? s : ' '.repeat(w - s.length) + s; };
|
|
627
887
|
let out = '';
|
|
628
888
|
out += 'OPENCODE USAGE STATEMENT\n';
|
|
629
|
-
|
|
630
|
-
out += 'Period: ' + jr.label + ' | ' + periodText + cachedNote + '\n';
|
|
889
|
+
out += 'Period: ' + jr.label + ' | ' + scopeLabel(jr.days) + cachedNote + '\n';
|
|
631
890
|
out += '---\n';
|
|
632
891
|
const s = jr.stats;
|
|
633
892
|
if (s && s.ok) {
|
|
@@ -637,83 +896,53 @@ function formatReport(jr) {
|
|
|
637
896
|
out += ' Sessions ........... ' + (o.sessions || 0) + '\n';
|
|
638
897
|
out += ' Messages ........... ' + (o.messages || 0) + '\n';
|
|
639
898
|
out += ' Total Cost ......... $' + (c.total || 0).toFixed(2) + '\n';
|
|
640
|
-
out += ' Tokens In/Out ...... ' +
|
|
641
|
-
out += ' Tokens Reasoning ... ' +
|
|
642
|
-
out += ' Cache Read/Write ... ' +
|
|
643
|
-
out += ' Market value
|
|
644
|
-
out += ' Saved
|
|
899
|
+
out += ' Tokens In/Out ...... ' + fT(c.input) + ' / ' + fT(c.output) + '\n';
|
|
900
|
+
out += ' Tokens Reasoning ... ' + fT(c.reasoning) + '\n';
|
|
901
|
+
out += ' Cache Read/Write ... ' + fT(c.cacheRead) + ' / ' + fT(c.cacheWrite) + '\n';
|
|
902
|
+
out += ' Market value ....... ' + (c.marketTotal == null ? '—' : '$' + c.marketTotal.toFixed(2)) + '\n';
|
|
903
|
+
out += ' Saved .............. ' + (c.savedTotal == null ? '—' : '$' + c.savedTotal.toFixed(2)) + (c.marketTotal > 0 && c.savedPct != null ? ' (' + c.savedPct + '%)' : '') + '\n';
|
|
904
|
+
if (c.priceAsOf) out += ' Prices as of ....... ' + fmtDay(c.priceAsOf) + '\n';
|
|
645
905
|
out += '\n';
|
|
646
906
|
}
|
|
647
907
|
if (jr.providers && jr.providers.length) {
|
|
648
908
|
out += 'PROVIDERS & MODELS\n';
|
|
649
909
|
jr.providers.forEach((p) => {
|
|
650
|
-
|
|
910
|
+
const name = p.provider + '/' + p.model;
|
|
911
|
+
out += ' ' + pad(name, 45) + pad(p.sessions + ' sessions', 14) + ' ' + rpad(fCost(p.cost), 10) + ' ' + rpad(fT(p.tok_in), 8) + ' in / ' + rpad(fT(p.tok_out), 8) + ' out cache: ' + rpad(fT(p.cache_read), 8) + ' ' + rpad(fmtSpeed(p.speed), 11) + ' mkt:' + rpad(fCost(p.market), 11) + ' svd:' + rpad(fCost(p.saved), 11) + ' [' + kindOf(p, name) + ']\n';
|
|
651
912
|
});
|
|
652
913
|
out += '\n';
|
|
653
|
-
const basis = (jr.providers || []).filter((p) => p.pricedAs && p.pricedAs !== (p.provider + '/' + p.model));
|
|
654
|
-
if (basis.length) {
|
|
655
|
-
out += 'PRICING (free → paid basis' + (jr.priceAsOf ? ', prices as of ' + jr.priceAsOf.slice(0, 10) : '') + ')\n';
|
|
656
|
-
basis.forEach((p) => {
|
|
657
|
-
out += ' ' + pad(p.provider + '/' + p.model, 45) + '→ ' + p.pricedAs + '\n';
|
|
658
|
-
});
|
|
659
|
-
out += '\n';
|
|
660
|
-
}
|
|
661
914
|
}
|
|
662
915
|
if (jr.agents && jr.agents.length) {
|
|
663
916
|
out += 'AGENTS\n';
|
|
664
917
|
jr.agents.forEach((a) => {
|
|
665
|
-
out += ' ' + pad(a.agent, 45) + pad(a.sessions + ' sessions', 14) + ' ' + rpad(
|
|
918
|
+
out += ' ' + pad(a.agent, 45) + pad(a.sessions + ' sessions', 14) + ' ' + rpad(fCost(a.cost), 10) + ' ' + rpad(fT(a.tok_in), 8) + ' in / ' + rpad(fT(a.tok_out), 8) + ' out cache: ' + rpad(fT(a.cache_read), 8) + ' ' + rpad(fmtSpeed(a.speed), 11) + ' wall:' + rpad(fmtDur(a.wallMs), 9) + 'act:' + rpad(fmtDur(a.activeMs), 9) + ' mkt:' + rpad(fCost(a.market), 11) + ' svd:' + rpad(fCost(a.saved), 11) + '\n';
|
|
666
919
|
});
|
|
667
920
|
}
|
|
668
921
|
if (jr.sessions && jr.sessions.length) {
|
|
669
|
-
out += 'SESSIONS (top ' + jr.sessions.length + ' by
|
|
922
|
+
out += 'SESSIONS (top ' + jr.sessions.length + ' by tokens)\n';
|
|
670
923
|
jr.sessions.forEach((s) => {
|
|
671
|
-
out += ' ' + pad(String(s.id).slice(0, 12), 13) + pad((s.title || '').slice(0, 28), 29) + pad(s.agent, 9) + pad((s.provider + '/' + s.model).slice(0, 30), 31) + 'wall:' + rpad(fmtDur(s.wallMs), 9) + 'act:' + rpad(fmtDur(s.activeMs), 9) + rpad(s.messages + 'msgs', 8) + rpad(
|
|
924
|
+
out += ' ' + pad(String(s.id).slice(0, 12), 13) + pad((s.title || '').slice(0, 28), 29) + pad(s.agent, 9) + pad((s.provider + '/' + s.model).slice(0, 30), 31) + 'wall:' + rpad(fmtDur(s.wallMs), 9) + 'act:' + rpad(fmtDur(s.activeMs), 9) + rpad(s.messages + 'msgs', 8) + rpad(fT(s.tok_in), 8) + ' in / ' + rpad(fT(s.tok_out), 8) + ' out ' + rpad(fmtSpeed(s.speed), 11) + ' ' + rpad(fCost(s.cost), 10) + ' mkt:' + rpad(fCost(s.market), 11) + ' svd:' + rpad(fCost(s.saved), 11) + '\n';
|
|
672
925
|
});
|
|
673
926
|
out += '\n';
|
|
674
927
|
}
|
|
675
928
|
return out || 'No data for this period.';
|
|
676
929
|
}
|
|
677
930
|
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
});
|
|
684
|
-
try {
|
|
685
|
-
const r = await fetch('/api/report?days=' + days + '&_=' + Date.now(), { cache: 'no-store' });
|
|
686
|
-
const j = await r.json();
|
|
687
|
-
if (!j.ok) { $('reportContent').textContent = 'Error'; reportLoading = false; return; }
|
|
688
|
-
if (!j.ready || !j.report) {
|
|
689
|
-
$('reportContent').textContent = '⏳ ' + t('generating') + '…';
|
|
690
|
-
reportRetry++;
|
|
691
|
-
if (reportRetry < MAX_REPORT_RETRIES) {
|
|
692
|
-
const delay = Math.min(1000 * Math.pow(1.5, reportRetry), 15000);
|
|
693
|
-
setTimeout(() => loadReport(days), delay);
|
|
694
|
-
}
|
|
695
|
-
reportLoading = false;
|
|
696
|
-
return;
|
|
697
|
-
}
|
|
698
|
-
reportRetry = 0;
|
|
699
|
-
$('reportContent').innerHTML = renderReportTables(j.report);
|
|
700
|
-
renderReportCharts(j.report);
|
|
701
|
-
} catch (e) {
|
|
702
|
-
$('reportContent').textContent = t('connFail') + ': ' + e.message;
|
|
703
|
-
}
|
|
704
|
-
reportLoading = false;
|
|
705
|
-
}
|
|
931
|
+
// ---- boot ---------------------------------------------------------------
|
|
932
|
+
window.__setScope = setScope; // days = -1 | 1 | 7 | 30
|
|
933
|
+
window.__setTab = setTab;
|
|
934
|
+
window.__renderAll = renderAll;
|
|
935
|
+
window.__lastReport = null;
|
|
706
936
|
|
|
707
937
|
applyI18n();
|
|
708
938
|
restoreSections();
|
|
939
|
+
document.querySelectorAll('#scopeBtns .pill').forEach((el) => {
|
|
940
|
+
el.classList.toggle('active', parseInt(el.dataset.days, 10) === scopeDays);
|
|
941
|
+
});
|
|
942
|
+
renderAll();
|
|
709
943
|
poll();
|
|
710
944
|
setInterval(poll, 15000);
|
|
711
|
-
loadReport(
|
|
712
|
-
// Preload report data for other periods so tab switching is instant.
|
|
713
|
-
// Load with delays to avoid competing with the first poll for bandwidth.
|
|
714
|
-
setTimeout(() => loadReport(7), 1500);
|
|
715
|
-
setTimeout(() => loadReport(30), 3000);
|
|
716
|
-
setTimeout(() => loadReport(-1), 4500);
|
|
945
|
+
loadReport(scopeDays);
|
|
717
946
|
</script>
|
|
718
947
|
</body>
|
|
719
|
-
</html>
|
|
948
|
+
</html>
|