claude-code-cost 0.1.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/.claude/commands/release.md +54 -0
- package/.github/workflows/pages.yml +39 -0
- package/.husky/pre-commit +1 -0
- package/CLAUDE.md +42 -0
- package/LICENSE +21 -0
- package/PLAN-CONTEXT.md +83 -0
- package/README.md +102 -0
- package/assets/overview-dark.png +0 -0
- package/assets/overview-light.png +0 -0
- package/assets/projects-dark.png +0 -0
- package/assets/projects-light.png +0 -0
- package/assets/session-dark.png +0 -0
- package/assets/sesssion-light.png +0 -0
- package/biome.json +33 -0
- package/docs/assets/overview-dark.png +0 -0
- package/docs/assets/overview-light.png +0 -0
- package/docs/assets/projects-dark.png +0 -0
- package/docs/assets/projects-light.png +0 -0
- package/docs/assets/session-dark.png +0 -0
- package/docs/assets/sesssion-light.png +0 -0
- package/docs/index.html +896 -0
- package/package.json +25 -0
- package/public/app.js +1213 -0
- package/public/icons/icon-svg.svg +7 -0
- package/public/index.html +112 -0
- package/public/manifest.json +14 -0
- package/public/style.css +809 -0
- package/public/sw.js +47 -0
- package/server.js +734 -0
package/public/app.js
ADDED
|
@@ -0,0 +1,1213 @@
|
|
|
1
|
+
// #region STATE
|
|
2
|
+
|
|
3
|
+
let currentView = 'overview';
|
|
4
|
+
let overviewData = null;
|
|
5
|
+
let projectsData = null;
|
|
6
|
+
let sessionsData = null;
|
|
7
|
+
let sessionDetailData = null;
|
|
8
|
+
let currentProjectPath = null;
|
|
9
|
+
let currentProjectName = null;
|
|
10
|
+
let currentSessionId = null;
|
|
11
|
+
let sortField = 'totalCost';
|
|
12
|
+
let sortOrder = 'desc';
|
|
13
|
+
let dateRange = 7;
|
|
14
|
+
const charts = {};
|
|
15
|
+
let lastRenderHash = {};
|
|
16
|
+
let navCounter = 0;
|
|
17
|
+
|
|
18
|
+
// #endregion
|
|
19
|
+
|
|
20
|
+
// #region UTILS
|
|
21
|
+
|
|
22
|
+
function esc(str) {
|
|
23
|
+
const d = document.createElement('div');
|
|
24
|
+
d.textContent = str;
|
|
25
|
+
return d.innerHTML;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function formatCost(usd) {
|
|
29
|
+
if (usd == null || usd === 0) return '$0.00';
|
|
30
|
+
if (usd < 0.01) return '<$0.01';
|
|
31
|
+
if (usd >= 100) return `$${usd.toFixed(0)}`;
|
|
32
|
+
return `$${usd.toFixed(2)}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function formatTokens(count) {
|
|
36
|
+
if (!count) return '0';
|
|
37
|
+
if (count >= 1_000_000) return `${(count / 1_000_000).toFixed(1)}M`;
|
|
38
|
+
if (count >= 1_000) return `${(count / 1_000).toFixed(1)}K`;
|
|
39
|
+
return count.toString();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function formatDuration(minutes) {
|
|
43
|
+
if (!minutes || minutes < 1) return '<1m';
|
|
44
|
+
if (minutes < 60) return `${minutes}m`;
|
|
45
|
+
const h = Math.floor(minutes / 60);
|
|
46
|
+
const m = minutes % 60;
|
|
47
|
+
return m > 0 ? `${h}h ${m}m` : `${h}h`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function timeAgo(ts) {
|
|
51
|
+
if (!ts) return '';
|
|
52
|
+
const diff = Date.now() - new Date(ts).getTime();
|
|
53
|
+
const mins = Math.floor(diff / 60000);
|
|
54
|
+
if (mins < 1) return 'just now';
|
|
55
|
+
if (mins < 60) return `${mins}m ago`;
|
|
56
|
+
const hours = Math.floor(mins / 60);
|
|
57
|
+
if (hours < 24) return `${hours}h ago`;
|
|
58
|
+
const days = Math.floor(hours / 24);
|
|
59
|
+
if (days < 30) return `${days}d ago`;
|
|
60
|
+
return new Date(ts).toLocaleDateString();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function shortDate(dateStr) {
|
|
64
|
+
const d = new Date(`${dateStr}T00:00:00`);
|
|
65
|
+
return d.toLocaleDateString(undefined, { month: 'short', day: 'numeric' });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function shortModel(model) {
|
|
69
|
+
if (!model) return 'unknown';
|
|
70
|
+
return model.replace('anthropic/', '').replace(/-\d{8}$/, '');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function sortCompare(a, b, field, order) {
|
|
74
|
+
let va = a[field],
|
|
75
|
+
vb = b[field];
|
|
76
|
+
if (typeof va === 'string') va = va.toLowerCase();
|
|
77
|
+
if (typeof vb === 'string') vb = vb.toLowerCase();
|
|
78
|
+
if (va < vb) return order === 'asc' ? -1 : 1;
|
|
79
|
+
if (va > vb) return order === 'asc' ? 1 : -1;
|
|
80
|
+
return 0;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function sortArrow(field) {
|
|
84
|
+
if (sortField !== field) return '';
|
|
85
|
+
return `<span class="sort-arrow">${sortOrder === 'asc' ? '\u25B2' : '\u25BC'}</span>`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function thClass(field) {
|
|
89
|
+
return sortField === field ? 'sorted' : '';
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// #endregion
|
|
93
|
+
|
|
94
|
+
// #region URL_STATE
|
|
95
|
+
|
|
96
|
+
function getUrlState() {
|
|
97
|
+
const p = new URLSearchParams(window.location.search);
|
|
98
|
+
return {
|
|
99
|
+
view: p.get('view') || 'overview',
|
|
100
|
+
project: p.get('project'),
|
|
101
|
+
projectName: p.get('projectName'),
|
|
102
|
+
session: p.get('session'),
|
|
103
|
+
sort: p.get('sort'),
|
|
104
|
+
order: p.get('order'),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function loadDateRange() {
|
|
109
|
+
const v = localStorage.getItem('cc-cost:range');
|
|
110
|
+
return v ? parseInt(v, 10) || 7 : 7;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function saveDateRange(val) {
|
|
114
|
+
localStorage.setItem('cc-cost:range', String(val));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function updateUrl() {
|
|
118
|
+
const p = new URLSearchParams();
|
|
119
|
+
if (currentView !== 'overview') p.set('view', currentView);
|
|
120
|
+
if (currentProjectPath) p.set('project', currentProjectPath);
|
|
121
|
+
if (currentProjectName) p.set('projectName', currentProjectName);
|
|
122
|
+
if (currentSessionId) p.set('session', currentSessionId);
|
|
123
|
+
if (sortField !== 'totalCost') p.set('sort', sortField);
|
|
124
|
+
if (sortOrder !== 'desc') p.set('order', sortOrder);
|
|
125
|
+
const qs = p.toString();
|
|
126
|
+
history.replaceState(null, '', qs ? `?${qs}` : '/');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// #endregion
|
|
130
|
+
|
|
131
|
+
// #region FETCH
|
|
132
|
+
|
|
133
|
+
const BROWSER_CACHE_TTL = 5 * 60 * 1000; // 5 min
|
|
134
|
+
const CACHE_VERSION = 3;
|
|
135
|
+
let forceRefresh = false;
|
|
136
|
+
|
|
137
|
+
function getCached(key) {
|
|
138
|
+
if (forceRefresh) return null;
|
|
139
|
+
try {
|
|
140
|
+
const raw = localStorage.getItem(`cc-cost:${key}`);
|
|
141
|
+
if (!raw) return null;
|
|
142
|
+
const { data, ts, v } = JSON.parse(raw);
|
|
143
|
+
if (v !== CACHE_VERSION || Date.now() - ts > BROWSER_CACHE_TTL) return null;
|
|
144
|
+
return data;
|
|
145
|
+
} catch {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function setLocalCache(key, data) {
|
|
151
|
+
try {
|
|
152
|
+
pruneLocalCache();
|
|
153
|
+
localStorage.setItem(`cc-cost:${key}`, JSON.stringify({ data, ts: Date.now(), v: CACHE_VERSION }));
|
|
154
|
+
} catch {
|
|
155
|
+
/* storage full — ignore */
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function pruneLocalCache() {
|
|
160
|
+
const MAX_ENTRIES = 20;
|
|
161
|
+
const entries = [];
|
|
162
|
+
for (let i = 0; i < localStorage.length; i++) {
|
|
163
|
+
const k = localStorage.key(i);
|
|
164
|
+
if (!k.startsWith('cc-cost:')) continue;
|
|
165
|
+
try {
|
|
166
|
+
const { ts } = JSON.parse(localStorage.getItem(k));
|
|
167
|
+
entries.push({ k, ts });
|
|
168
|
+
} catch {
|
|
169
|
+
entries.push({ k, ts: 0 });
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (entries.length <= MAX_ENTRIES) return;
|
|
173
|
+
entries.sort((a, b) => a.ts - b.ts);
|
|
174
|
+
const toRemove = entries.length - MAX_ENTRIES;
|
|
175
|
+
for (let i = 0; i < toRemove; i++) localStorage.removeItem(entries[i].k);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function clearLocalCache() {
|
|
179
|
+
const keys = [];
|
|
180
|
+
for (let i = 0; i < localStorage.length; i++) {
|
|
181
|
+
const k = localStorage.key(i);
|
|
182
|
+
if (k.startsWith('cc-cost:')) keys.push(k);
|
|
183
|
+
}
|
|
184
|
+
keys.forEach((k) => localStorage.removeItem(k));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async function fetchJSON(url, skipCache) {
|
|
188
|
+
if (!skipCache) {
|
|
189
|
+
const cached = getCached(url);
|
|
190
|
+
if (cached) return cached;
|
|
191
|
+
}
|
|
192
|
+
const res = await fetch(url);
|
|
193
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
194
|
+
const data = await res.json();
|
|
195
|
+
if (!skipCache) setLocalCache(url, data);
|
|
196
|
+
return data;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async function fetchOverview() {
|
|
200
|
+
overviewData = await fetchJSON(`/api/overview?range=${dateRange}`);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async function fetchProjects() {
|
|
204
|
+
projectsData = await fetchJSON(`/api/projects?range=${dateRange}`);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async function fetchSessions(encodedPath) {
|
|
208
|
+
sessionsData = await fetchJSON(`/api/projects/${encodeURIComponent(encodedPath)}/sessions?range=${dateRange}`, true);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
async function fetchSessionDetail(sessionId) {
|
|
212
|
+
sessionDetailData = await fetchJSON(`/api/sessions/${encodeURIComponent(sessionId)}`, true);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// #endregion
|
|
216
|
+
|
|
217
|
+
// #region RENDER_OVERVIEW
|
|
218
|
+
|
|
219
|
+
function renderOverview() {
|
|
220
|
+
const el = document.getElementById('overview-view');
|
|
221
|
+
if (!el) return;
|
|
222
|
+
if (!overviewData) {
|
|
223
|
+
el.innerHTML = '<div class="loading-state"><div class="loading-spinner"></div><span>Loading...</span></div>';
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const s = overviewData.summary;
|
|
228
|
+
const h = JSON.stringify({ overviewData, sortField, sortOrder });
|
|
229
|
+
if (lastRenderHash.overview === h) return;
|
|
230
|
+
lastRenderHash.overview = h;
|
|
231
|
+
|
|
232
|
+
const daily = overviewData.daily || [];
|
|
233
|
+
const models = overviewData.modelDistribution || [];
|
|
234
|
+
|
|
235
|
+
el.innerHTML = `
|
|
236
|
+
<div class="dashboard-content">
|
|
237
|
+
<div class="cards-row">
|
|
238
|
+
<div class="stat-card">
|
|
239
|
+
<div class="card-label">Today</div>
|
|
240
|
+
<div class="card-value cost">${formatCost(s.todayCost)}</div>
|
|
241
|
+
</div>
|
|
242
|
+
<div class="stat-card">
|
|
243
|
+
<div class="card-label">${dateRange} Days</div>
|
|
244
|
+
<div class="card-value cost">${formatCost(s.totalCost)}</div>
|
|
245
|
+
</div>
|
|
246
|
+
<div class="stat-card">
|
|
247
|
+
<div class="card-label">Sessions</div>
|
|
248
|
+
<div class="card-value">${s.totalSessions}</div>
|
|
249
|
+
</div>
|
|
250
|
+
<div class="stat-card">
|
|
251
|
+
<div class="card-label">Tokens</div>
|
|
252
|
+
<div class="card-value">${formatTokens(s.totalInput + s.totalOutput)}</div>
|
|
253
|
+
<div class="card-sub">In: ${formatTokens(s.totalInput)} / Out: ${formatTokens(s.totalOutput)}</div>
|
|
254
|
+
</div>
|
|
255
|
+
<div class="stat-card">
|
|
256
|
+
<div class="card-label">Cache Efficiency</div>
|
|
257
|
+
<div class="card-value">${(s.cacheEfficiency * 100).toFixed(1)}%</div>
|
|
258
|
+
<div class="card-sub">Read: ${formatTokens(s.totalCacheRead)} / Created: ${formatTokens(s.totalCacheCreation)}</div>
|
|
259
|
+
</div>
|
|
260
|
+
</div>
|
|
261
|
+
|
|
262
|
+
<div class="charts-row">
|
|
263
|
+
<div class="chart-box">
|
|
264
|
+
<div class="chart-title">Daily Cost</div>
|
|
265
|
+
<canvas id="dailyChart"></canvas>
|
|
266
|
+
</div>
|
|
267
|
+
<div class="chart-box">
|
|
268
|
+
<div class="chart-title">Cost by Model</div>
|
|
269
|
+
<canvas id="modelChart"></canvas>
|
|
270
|
+
</div>
|
|
271
|
+
</div>
|
|
272
|
+
|
|
273
|
+
${
|
|
274
|
+
overviewData.projects?.length
|
|
275
|
+
? `
|
|
276
|
+
<div class="section-title">Projects</div>
|
|
277
|
+
<table class="data-table">
|
|
278
|
+
<thead><tr>
|
|
279
|
+
<th class="${thClass('name')}" onclick="sortBy('name')">Project ${sortArrow('name')}</th>
|
|
280
|
+
<th class="${thClass('totalCost')}" onclick="sortBy('totalCost')">Cost ${sortArrow('totalCost')}</th>
|
|
281
|
+
<th class="${thClass('sessionCount')}" onclick="sortBy('sessionCount')">Sessions ${sortArrow('sessionCount')}</th>
|
|
282
|
+
<th class="${thClass('lastActive')}" onclick="sortBy('lastActive')">Last Active ${sortArrow('lastActive')}</th>
|
|
283
|
+
</tr></thead>
|
|
284
|
+
<tbody>
|
|
285
|
+
${[...overviewData.projects]
|
|
286
|
+
.sort((a, b) => sortCompare(a, b, sortField, sortOrder))
|
|
287
|
+
.map(
|
|
288
|
+
(p) => `
|
|
289
|
+
<tr data-clickable onclick="navigateToSessions('${esc(p.encodedPath)}', '${esc(p.name)}')">
|
|
290
|
+
<td>${esc(p.name)}</td>
|
|
291
|
+
<td class="cost-cell">${formatCost(p.totalCost)}</td>
|
|
292
|
+
<td>${p.sessionCount}</td>
|
|
293
|
+
<td class="muted">${timeAgo(p.lastActive)}</td>
|
|
294
|
+
</tr>`,
|
|
295
|
+
)
|
|
296
|
+
.join('')}
|
|
297
|
+
</tbody>
|
|
298
|
+
</table>`
|
|
299
|
+
: ''
|
|
300
|
+
}
|
|
301
|
+
</div>`;
|
|
302
|
+
|
|
303
|
+
// Render charts after DOM is ready
|
|
304
|
+
requestAnimationFrame(() => {
|
|
305
|
+
renderDailyChart(daily);
|
|
306
|
+
renderModelChart(models);
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// #endregion
|
|
311
|
+
|
|
312
|
+
// #region RENDER_PROJECTS
|
|
313
|
+
|
|
314
|
+
function renderProjects() {
|
|
315
|
+
const el = document.getElementById('projects-view');
|
|
316
|
+
if (!el) return;
|
|
317
|
+
if (!projectsData) {
|
|
318
|
+
el.innerHTML = '<div class="loading-state"><div class="loading-spinner"></div><span>Loading...</span></div>';
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const h = JSON.stringify({ projectsData, sortField, sortOrder });
|
|
323
|
+
if (lastRenderHash.projects === h) return;
|
|
324
|
+
lastRenderHash.projects = h;
|
|
325
|
+
|
|
326
|
+
const sorted = [...projectsData].sort((a, b) => sortCompare(a, b, sortField, sortOrder));
|
|
327
|
+
|
|
328
|
+
if (sorted.length === 0) {
|
|
329
|
+
el.innerHTML = `<div class="dashboard-content"><div class="empty-state">
|
|
330
|
+
<div class="empty-icon">$</div>
|
|
331
|
+
<div>No project data found</div>
|
|
332
|
+
<div>Make sure Claude Code session files exist in ~/.claude/projects/</div>
|
|
333
|
+
</div></div>`;
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
el.innerHTML = `
|
|
338
|
+
<div class="dashboard-content">
|
|
339
|
+
<div class="section-title">All Projects</div>
|
|
340
|
+
<table class="data-table">
|
|
341
|
+
<thead><tr>
|
|
342
|
+
<th class="${thClass('name')}" onclick="sortBy('name')">Project ${sortArrow('name')}</th>
|
|
343
|
+
<th class="${thClass('totalCost')}" onclick="sortBy('totalCost')">Cost ${sortArrow('totalCost')}</th>
|
|
344
|
+
<th class="${thClass('sessionCount')}" onclick="sortBy('sessionCount')">Sessions ${sortArrow('sessionCount')}</th>
|
|
345
|
+
<th class="${thClass('lastActive')}" onclick="sortBy('lastActive')">Last Active ${sortArrow('lastActive')}</th>
|
|
346
|
+
<th>Model</th>
|
|
347
|
+
</tr></thead>
|
|
348
|
+
<tbody>
|
|
349
|
+
${sorted
|
|
350
|
+
.map(
|
|
351
|
+
(p) => `
|
|
352
|
+
<tr data-clickable onclick="navigateToSessions('${esc(p.encodedPath)}', '${esc(p.name)}')">
|
|
353
|
+
<td>${esc(p.name)}</td>
|
|
354
|
+
<td class="cost-cell">${formatCost(p.totalCost)}</td>
|
|
355
|
+
<td>${p.sessionCount}</td>
|
|
356
|
+
<td class="muted">${timeAgo(p.lastActive)}</td>
|
|
357
|
+
<td><span class="model-badge">${esc(shortModel(p.primaryModel))}</span></td>
|
|
358
|
+
</tr>`,
|
|
359
|
+
)
|
|
360
|
+
.join('')}
|
|
361
|
+
</tbody>
|
|
362
|
+
</table>
|
|
363
|
+
</div>`;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// #endregion
|
|
367
|
+
|
|
368
|
+
// #region RENDER_SESSIONS
|
|
369
|
+
|
|
370
|
+
function renderSessions() {
|
|
371
|
+
const el = document.getElementById('sessions-view');
|
|
372
|
+
if (!el) return;
|
|
373
|
+
if (!sessionsData) {
|
|
374
|
+
el.innerHTML = '<div class="loading-state"><div class="loading-spinner"></div><span>Loading...</span></div>';
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
const h = JSON.stringify(sessionsData);
|
|
379
|
+
if (lastRenderHash.sessions === h) return;
|
|
380
|
+
lastRenderHash.sessions = h;
|
|
381
|
+
|
|
382
|
+
if (sessionsData.length === 0) {
|
|
383
|
+
el.innerHTML = `<div class="dashboard-content">
|
|
384
|
+
<div class="breadcrumb">
|
|
385
|
+
<a onclick="navigate('overview')">Overview</a>
|
|
386
|
+
<span class="sep">/</span>
|
|
387
|
+
<a onclick="navigate('projects')">Projects</a>
|
|
388
|
+
<span class="sep">/</span>
|
|
389
|
+
<span class="current">${esc(currentProjectName || 'Project')}</span>
|
|
390
|
+
</div>
|
|
391
|
+
<div class="empty-state"><div>No sessions found for this project</div></div>
|
|
392
|
+
</div>`;
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
el.innerHTML = `
|
|
397
|
+
<div class="dashboard-content">
|
|
398
|
+
<div class="breadcrumb">
|
|
399
|
+
<a onclick="navigate('overview')">Overview</a>
|
|
400
|
+
<span class="sep">/</span>
|
|
401
|
+
<a onclick="navigate('projects')">Projects</a>
|
|
402
|
+
<span class="sep">/</span>
|
|
403
|
+
<span class="current">${esc(currentProjectName || 'Project')}</span>
|
|
404
|
+
</div>
|
|
405
|
+
<table class="data-table">
|
|
406
|
+
<thead><tr>
|
|
407
|
+
<th>Session</th>
|
|
408
|
+
<th>Cost</th>
|
|
409
|
+
<th>Tokens</th>
|
|
410
|
+
<th>Messages</th>
|
|
411
|
+
<th>Duration</th>
|
|
412
|
+
<th>Model</th>
|
|
413
|
+
<th>Last Active</th>
|
|
414
|
+
</tr></thead>
|
|
415
|
+
<tbody>
|
|
416
|
+
${sessionsData
|
|
417
|
+
.map(
|
|
418
|
+
(s) => `
|
|
419
|
+
<tr data-clickable onclick="navigateToDetail('${esc(s.sessionId)}')">
|
|
420
|
+
<td class="truncate" title="${esc(s.firstPrompt || s.sessionId)}">${esc(s.firstPrompt || `${s.sessionId.slice(0, 8)}...`)}</td>
|
|
421
|
+
<td class="cost-cell">${formatCost(s.totalCost)}</td>
|
|
422
|
+
<td>${formatTokens(s.totalTokens)}</td>
|
|
423
|
+
<td>${s.messageCount}</td>
|
|
424
|
+
<td class="muted">${formatDuration(s.durationMinutes)}</td>
|
|
425
|
+
<td><span class="model-badge">${esc(shortModel(s.primaryModel))}</span></td>
|
|
426
|
+
<td class="muted">${timeAgo(s.lastTimestamp)}</td>
|
|
427
|
+
</tr>`,
|
|
428
|
+
)
|
|
429
|
+
.join('')}
|
|
430
|
+
</tbody>
|
|
431
|
+
</table>
|
|
432
|
+
</div>`;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// #endregion
|
|
436
|
+
|
|
437
|
+
// #region RENDER_DETAIL
|
|
438
|
+
|
|
439
|
+
function renderDetail() {
|
|
440
|
+
const el = document.getElementById('detail-view');
|
|
441
|
+
if (!el) return;
|
|
442
|
+
if (!sessionDetailData) {
|
|
443
|
+
el.innerHTML = '<div class="loading-state"><div class="loading-spinner"></div><span>Loading...</span></div>';
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
const d = sessionDetailData;
|
|
448
|
+
const h = JSON.stringify(d);
|
|
449
|
+
if (lastRenderHash.detail === h) return;
|
|
450
|
+
lastRenderHash.detail = h;
|
|
451
|
+
|
|
452
|
+
el.innerHTML = `
|
|
453
|
+
<div class="dashboard-content">
|
|
454
|
+
<div class="breadcrumb">
|
|
455
|
+
<a onclick="navigate('overview')">Overview</a>
|
|
456
|
+
<span class="sep">/</span>
|
|
457
|
+
<a onclick="navigate('projects')">Projects</a>
|
|
458
|
+
<span class="sep">/</span>
|
|
459
|
+
<a onclick="navigateToSessions('${esc(d.encodedProjectPath)}', '${esc(d.projectPath)}')">${esc(d.projectPath)}</a>
|
|
460
|
+
<span class="sep">/</span>
|
|
461
|
+
<span class="current">${esc(d.sessionId.slice(0, 12))}...</span>
|
|
462
|
+
</div>
|
|
463
|
+
|
|
464
|
+
${d.firstPrompt ? `<div style="color:var(--text-tertiary);font-size:12px;margin-bottom:16px;font-style:italic">"${esc(d.firstPrompt)}"</div>` : ''}
|
|
465
|
+
|
|
466
|
+
<div class="detail-header">
|
|
467
|
+
<div class="detail-stat">
|
|
468
|
+
<div class="detail-label">Total Cost</div>
|
|
469
|
+
<div class="detail-value cost">${formatCost(d.totalCost)}</div>
|
|
470
|
+
</div>
|
|
471
|
+
<div class="detail-stat">
|
|
472
|
+
<div class="detail-label">Input</div>
|
|
473
|
+
<div class="detail-value">${formatTokens(d.inputTokens)}</div>
|
|
474
|
+
</div>
|
|
475
|
+
<div class="detail-stat">
|
|
476
|
+
<div class="detail-label">Output</div>
|
|
477
|
+
<div class="detail-value">${formatTokens(d.outputTokens)}</div>
|
|
478
|
+
</div>
|
|
479
|
+
<div class="detail-stat">
|
|
480
|
+
<div class="detail-label">Cache Created</div>
|
|
481
|
+
<div class="detail-value">${formatTokens(d.cacheCreationTokens)}</div>
|
|
482
|
+
</div>
|
|
483
|
+
<div class="detail-stat">
|
|
484
|
+
<div class="detail-label">Cache Read</div>
|
|
485
|
+
<div class="detail-value">${formatTokens(d.cacheReadTokens)}</div>
|
|
486
|
+
</div>
|
|
487
|
+
<div class="detail-stat">
|
|
488
|
+
<div class="detail-label">Messages</div>
|
|
489
|
+
<div class="detail-value">${d.messages.length}</div>
|
|
490
|
+
</div>
|
|
491
|
+
<div class="detail-stat">
|
|
492
|
+
<div class="detail-label">Models</div>
|
|
493
|
+
<div class="detail-value">${d.models.map((m) => shortModel(m)).join(', ')}</div>
|
|
494
|
+
</div>
|
|
495
|
+
</div>
|
|
496
|
+
|
|
497
|
+
<div class="charts-row" style="margin-bottom:20px">
|
|
498
|
+
<div class="chart-box">
|
|
499
|
+
<div class="chart-title">Cumulative Cost</div>
|
|
500
|
+
<canvas id="cumulativeChart"></canvas>
|
|
501
|
+
</div>
|
|
502
|
+
<div class="chart-box">
|
|
503
|
+
<div class="chart-title">Token Breakdown per Message</div>
|
|
504
|
+
<canvas id="tokenBreakdownChart"></canvas>
|
|
505
|
+
</div>
|
|
506
|
+
</div>
|
|
507
|
+
|
|
508
|
+
<div class="section-title">Messages (${d.messages.length})</div>
|
|
509
|
+
<table class="messages-table">
|
|
510
|
+
<thead><tr>
|
|
511
|
+
<th>#</th>
|
|
512
|
+
<th>Time</th>
|
|
513
|
+
<th>Model</th>
|
|
514
|
+
<th>Input</th>
|
|
515
|
+
<th>Output</th>
|
|
516
|
+
<th>Cache Cr.</th>
|
|
517
|
+
<th>Cache Rd.</th>
|
|
518
|
+
<th>Cost</th>
|
|
519
|
+
<th>Cumulative</th>
|
|
520
|
+
</tr></thead>
|
|
521
|
+
<tbody>
|
|
522
|
+
${[...d.messages]
|
|
523
|
+
.reverse()
|
|
524
|
+
.map(
|
|
525
|
+
(m) => `
|
|
526
|
+
<tr>
|
|
527
|
+
<td class="muted">${m.index}</td>
|
|
528
|
+
<td class="muted">${new Date(m.timestamp).toLocaleTimeString()}</td>
|
|
529
|
+
<td><span class="model-badge">${esc(shortModel(m.model))}</span></td>
|
|
530
|
+
<td>${formatTokens(m.inputTokens)}</td>
|
|
531
|
+
<td>${formatTokens(m.outputTokens)}</td>
|
|
532
|
+
<td>${formatTokens(m.cacheCreationTokens)}</td>
|
|
533
|
+
<td>${formatTokens(m.cacheReadTokens)}</td>
|
|
534
|
+
<td class="cost-cell">${formatCost(m.cost)}</td>
|
|
535
|
+
<td class="cumulative">${formatCost(m.cumulativeCost)}</td>
|
|
536
|
+
</tr>`,
|
|
537
|
+
)
|
|
538
|
+
.join('')}
|
|
539
|
+
</tbody>
|
|
540
|
+
</table>
|
|
541
|
+
</div>`;
|
|
542
|
+
|
|
543
|
+
requestAnimationFrame(() => {
|
|
544
|
+
renderCumulativeChart(d.messages);
|
|
545
|
+
renderTokenBreakdownChart(d.messages);
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// #endregion
|
|
550
|
+
|
|
551
|
+
// #region CHARTS
|
|
552
|
+
|
|
553
|
+
function getChartColors() {
|
|
554
|
+
const style = getComputedStyle(document.body);
|
|
555
|
+
return {
|
|
556
|
+
accent: style.getPropertyValue('--accent').trim() || '#e86f33',
|
|
557
|
+
accentDim: style.getPropertyValue('--accent-dim').trim() || 'rgba(232,111,51,0.22)',
|
|
558
|
+
text: style.getPropertyValue('--text-muted').trim() || '#7d808a',
|
|
559
|
+
border: style.getPropertyValue('--border').trim() || '#363840',
|
|
560
|
+
bg: style.getPropertyValue('--bg-elevated').trim() || '#1e2025',
|
|
561
|
+
chart1: style.getPropertyValue('--chart-1').trim() || '#e86f33',
|
|
562
|
+
chart2: style.getPropertyValue('--chart-2').trim() || '#60a5fa',
|
|
563
|
+
chart3: style.getPropertyValue('--chart-3').trim() || '#3ecf8e',
|
|
564
|
+
chart4: style.getPropertyValue('--chart-4').trim() || '#f0b429',
|
|
565
|
+
chart5: style.getPropertyValue('--chart-5').trim() || '#c084fc',
|
|
566
|
+
chart6: style.getPropertyValue('--chart-6').trim() || '#fb7185',
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
function chartDefaults() {
|
|
571
|
+
const c = getChartColors();
|
|
572
|
+
return {
|
|
573
|
+
responsive: true,
|
|
574
|
+
maintainAspectRatio: false,
|
|
575
|
+
plugins: {
|
|
576
|
+
legend: { display: false },
|
|
577
|
+
tooltip: {
|
|
578
|
+
backgroundColor: c.bg,
|
|
579
|
+
titleColor: c.text,
|
|
580
|
+
bodyColor: c.text,
|
|
581
|
+
borderColor: c.border,
|
|
582
|
+
borderWidth: 1,
|
|
583
|
+
},
|
|
584
|
+
},
|
|
585
|
+
scales: {
|
|
586
|
+
x: {
|
|
587
|
+
ticks: { color: c.text, font: { size: 10 } },
|
|
588
|
+
grid: { color: c.border, drawBorder: false },
|
|
589
|
+
},
|
|
590
|
+
y: {
|
|
591
|
+
ticks: { color: c.text, font: { size: 10 } },
|
|
592
|
+
grid: { color: c.border, drawBorder: false },
|
|
593
|
+
},
|
|
594
|
+
},
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
function destroyChart(id) {
|
|
599
|
+
if (charts[id]) {
|
|
600
|
+
charts[id].destroy();
|
|
601
|
+
delete charts[id];
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
function renderDailyChart(daily) {
|
|
606
|
+
const canvas = document.getElementById('dailyChart');
|
|
607
|
+
if (!canvas || !daily?.length) return;
|
|
608
|
+
destroyChart('daily');
|
|
609
|
+
|
|
610
|
+
const c = getChartColors();
|
|
611
|
+
const defaults = chartDefaults();
|
|
612
|
+
|
|
613
|
+
charts.daily = new Chart(canvas, {
|
|
614
|
+
type: 'bar',
|
|
615
|
+
data: {
|
|
616
|
+
labels: daily.map((d) => shortDate(d.date)),
|
|
617
|
+
datasets: [
|
|
618
|
+
{
|
|
619
|
+
label: 'Daily Cost',
|
|
620
|
+
data: daily.map((d) => d.cost),
|
|
621
|
+
backgroundColor: c.accentDim,
|
|
622
|
+
borderColor: c.accent,
|
|
623
|
+
borderWidth: 1,
|
|
624
|
+
borderRadius: 3,
|
|
625
|
+
},
|
|
626
|
+
],
|
|
627
|
+
},
|
|
628
|
+
options: {
|
|
629
|
+
...defaults,
|
|
630
|
+
interaction: { mode: 'nearest', intersect: true },
|
|
631
|
+
events: [],
|
|
632
|
+
plugins: {
|
|
633
|
+
...defaults.plugins,
|
|
634
|
+
tooltip: {
|
|
635
|
+
...defaults.plugins.tooltip,
|
|
636
|
+
callbacks: { label: (ctx) => `$${ctx.parsed.y.toFixed(2)}` },
|
|
637
|
+
},
|
|
638
|
+
},
|
|
639
|
+
scales: {
|
|
640
|
+
...defaults.scales,
|
|
641
|
+
y: {
|
|
642
|
+
...defaults.scales.y,
|
|
643
|
+
ticks: { ...defaults.scales.y.ticks, callback: (v) => `$${v.toFixed(2)}` },
|
|
644
|
+
},
|
|
645
|
+
},
|
|
646
|
+
},
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
function renderModelChart(models) {
|
|
651
|
+
const canvas = document.getElementById('modelChart');
|
|
652
|
+
if (!canvas || !models?.length) return;
|
|
653
|
+
destroyChart('model');
|
|
654
|
+
|
|
655
|
+
const colors = getChartColors();
|
|
656
|
+
const palette = [colors.chart1, colors.chart2, colors.chart3, colors.chart4, colors.chart5, colors.chart6];
|
|
657
|
+
const defaults = chartDefaults();
|
|
658
|
+
const total = models.reduce((s, m) => s + m.cost, 0);
|
|
659
|
+
|
|
660
|
+
charts.model = new Chart(canvas, {
|
|
661
|
+
type: 'bar',
|
|
662
|
+
data: {
|
|
663
|
+
labels: models.map((m) => shortModel(m.model)),
|
|
664
|
+
datasets: [
|
|
665
|
+
{
|
|
666
|
+
data: models.map((m) => m.cost),
|
|
667
|
+
backgroundColor: models.map((_, i) => palette[i % palette.length]),
|
|
668
|
+
borderWidth: 0,
|
|
669
|
+
borderRadius: 3,
|
|
670
|
+
},
|
|
671
|
+
],
|
|
672
|
+
},
|
|
673
|
+
options: {
|
|
674
|
+
...defaults,
|
|
675
|
+
indexAxis: 'y',
|
|
676
|
+
interaction: { mode: 'nearest', intersect: true },
|
|
677
|
+
events: [],
|
|
678
|
+
plugins: {
|
|
679
|
+
...defaults.plugins,
|
|
680
|
+
tooltip: {
|
|
681
|
+
...defaults.plugins.tooltip,
|
|
682
|
+
callbacks: {
|
|
683
|
+
label: (ctx) => {
|
|
684
|
+
const val = ctx.parsed.x;
|
|
685
|
+
const pct = ((val / total) * 100).toFixed(1);
|
|
686
|
+
return `$${val.toFixed(2)} (${pct}%)`;
|
|
687
|
+
},
|
|
688
|
+
},
|
|
689
|
+
},
|
|
690
|
+
},
|
|
691
|
+
scales: {
|
|
692
|
+
...defaults.scales,
|
|
693
|
+
x: { ...defaults.scales.x, ticks: { ...defaults.scales.x.ticks, callback: (v) => `$${v}` } },
|
|
694
|
+
},
|
|
695
|
+
},
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
function renderCumulativeChart(messages) {
|
|
700
|
+
const canvas = document.getElementById('cumulativeChart');
|
|
701
|
+
if (!canvas || !messages?.length) return;
|
|
702
|
+
destroyChart('cumulative');
|
|
703
|
+
|
|
704
|
+
const c = getChartColors();
|
|
705
|
+
const defaults = chartDefaults();
|
|
706
|
+
|
|
707
|
+
charts.cumulative = new Chart(canvas, {
|
|
708
|
+
type: 'line',
|
|
709
|
+
data: {
|
|
710
|
+
labels: messages.map((_, i) => i + 1),
|
|
711
|
+
datasets: [
|
|
712
|
+
{
|
|
713
|
+
label: 'Cumulative Cost',
|
|
714
|
+
data: messages.map((m) => m.cumulativeCost),
|
|
715
|
+
borderColor: c.accent,
|
|
716
|
+
backgroundColor: c.accentDim,
|
|
717
|
+
fill: true,
|
|
718
|
+
borderWidth: 2,
|
|
719
|
+
pointRadius: messages.length > 50 ? 0 : 3,
|
|
720
|
+
pointBackgroundColor: c.accent,
|
|
721
|
+
tension: 0.2,
|
|
722
|
+
},
|
|
723
|
+
],
|
|
724
|
+
},
|
|
725
|
+
options: {
|
|
726
|
+
...defaults,
|
|
727
|
+
scales: {
|
|
728
|
+
...defaults.scales,
|
|
729
|
+
x: {
|
|
730
|
+
...defaults.scales.x,
|
|
731
|
+
title: { display: true, text: 'Message #', color: c.text, font: { size: 10 } },
|
|
732
|
+
},
|
|
733
|
+
y: {
|
|
734
|
+
...defaults.scales.y,
|
|
735
|
+
ticks: { ...defaults.scales.y.ticks, callback: (v) => `$${v.toFixed(2)}` },
|
|
736
|
+
},
|
|
737
|
+
},
|
|
738
|
+
},
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
function renderTokenBreakdownChart(messages) {
|
|
743
|
+
const canvas = document.getElementById('tokenBreakdownChart');
|
|
744
|
+
if (!canvas || !messages?.length) return;
|
|
745
|
+
destroyChart('tokenBreakdown');
|
|
746
|
+
|
|
747
|
+
const c = getChartColors();
|
|
748
|
+
const defaults = chartDefaults();
|
|
749
|
+
|
|
750
|
+
charts.tokenBreakdown = new Chart(canvas, {
|
|
751
|
+
type: 'bar',
|
|
752
|
+
data: {
|
|
753
|
+
labels: messages.map((_, i) => i + 1),
|
|
754
|
+
datasets: [
|
|
755
|
+
{ label: 'Input', data: messages.map((m) => m.inputTokens), backgroundColor: c.chart1, borderRadius: 2 },
|
|
756
|
+
{ label: 'Output', data: messages.map((m) => m.outputTokens), backgroundColor: c.chart2, borderRadius: 2 },
|
|
757
|
+
{
|
|
758
|
+
label: 'Cache Create',
|
|
759
|
+
data: messages.map((m) => m.cacheCreationTokens),
|
|
760
|
+
backgroundColor: c.chart3,
|
|
761
|
+
borderRadius: 2,
|
|
762
|
+
},
|
|
763
|
+
{
|
|
764
|
+
label: 'Cache Read',
|
|
765
|
+
data: messages.map((m) => m.cacheReadTokens),
|
|
766
|
+
backgroundColor: c.chart4,
|
|
767
|
+
borderRadius: 2,
|
|
768
|
+
},
|
|
769
|
+
],
|
|
770
|
+
},
|
|
771
|
+
options: {
|
|
772
|
+
...defaults,
|
|
773
|
+
plugins: {
|
|
774
|
+
...defaults.plugins,
|
|
775
|
+
legend: {
|
|
776
|
+
display: true,
|
|
777
|
+
position: 'bottom',
|
|
778
|
+
labels: {
|
|
779
|
+
color: c.text,
|
|
780
|
+
font: { size: 10, family: "'IBM Plex Mono', monospace" },
|
|
781
|
+
padding: 8,
|
|
782
|
+
boxWidth: 12,
|
|
783
|
+
boxHeight: 12,
|
|
784
|
+
},
|
|
785
|
+
},
|
|
786
|
+
},
|
|
787
|
+
scales: {
|
|
788
|
+
...defaults.scales,
|
|
789
|
+
x: { ...defaults.scales.x, stacked: true },
|
|
790
|
+
y: {
|
|
791
|
+
...defaults.scales.y,
|
|
792
|
+
stacked: true,
|
|
793
|
+
ticks: { ...defaults.scales.y.ticks, callback: (v) => formatTokens(v) },
|
|
794
|
+
},
|
|
795
|
+
},
|
|
796
|
+
},
|
|
797
|
+
});
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
// biome-ignore lint/correctness/noUnusedVariables: called from theme toggle
|
|
801
|
+
function destroyAllCharts() {
|
|
802
|
+
for (const id of Object.keys(charts)) {
|
|
803
|
+
destroyChart(id);
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
// #endregion
|
|
808
|
+
|
|
809
|
+
// #region THEME
|
|
810
|
+
|
|
811
|
+
function loadTheme() {
|
|
812
|
+
if (localStorage.getItem('theme') === 'light') {
|
|
813
|
+
document.body.classList.add('light');
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
function toggleTheme() {
|
|
818
|
+
const isLight = document.body.classList.toggle('light');
|
|
819
|
+
localStorage.setItem('theme', isLight ? 'light' : 'dark');
|
|
820
|
+
lastRenderHash = {};
|
|
821
|
+
renderCurrentView();
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
// #endregion
|
|
825
|
+
|
|
826
|
+
// #region ROUTER
|
|
827
|
+
|
|
828
|
+
function setActiveNav(view) {
|
|
829
|
+
document.querySelectorAll('.topbar-nav-btn').forEach((btn) => {
|
|
830
|
+
btn.classList.toggle('active', btn.dataset.view === view);
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
function showView(viewId) {
|
|
835
|
+
document.querySelectorAll('.view').forEach((v) => v.classList.remove('active'));
|
|
836
|
+
const el = document.getElementById(viewId);
|
|
837
|
+
if (el) el.classList.add('active');
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
async function navigate(view, params) {
|
|
841
|
+
currentView = view;
|
|
842
|
+
if (params?.project) currentProjectPath = params.project;
|
|
843
|
+
if (params?.projectName) currentProjectName = params.projectName;
|
|
844
|
+
if (params?.session) currentSessionId = params.session;
|
|
845
|
+
|
|
846
|
+
// Clear downstream state
|
|
847
|
+
if (view === 'overview' || view === 'projects') {
|
|
848
|
+
currentProjectPath = null;
|
|
849
|
+
currentProjectName = null;
|
|
850
|
+
currentSessionId = null;
|
|
851
|
+
}
|
|
852
|
+
if (view === 'sessions') {
|
|
853
|
+
currentSessionId = null;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
const navView = view === 'sessions' || view === 'detail' ? 'projects' : view;
|
|
857
|
+
setActiveNav(navView);
|
|
858
|
+
updateUrl();
|
|
859
|
+
await loadAndRender(view);
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
// biome-ignore lint/correctness/noUnusedVariables: called from HTML onclick
|
|
863
|
+
async function navigateToSessions(encodedPath, name) {
|
|
864
|
+
currentProjectPath = encodedPath;
|
|
865
|
+
currentProjectName = name;
|
|
866
|
+
await navigate('sessions', { project: encodedPath, projectName: name });
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
// biome-ignore lint/correctness/noUnusedVariables: called from HTML onclick
|
|
870
|
+
async function navigateToDetail(sessionId) {
|
|
871
|
+
currentSessionId = sessionId;
|
|
872
|
+
await navigate('detail', { session: sessionId });
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
// biome-ignore lint/correctness/noUnusedVariables: called from HTML onclick
|
|
876
|
+
function sortBy(field) {
|
|
877
|
+
if (sortField === field) {
|
|
878
|
+
sortOrder = sortOrder === 'desc' ? 'asc' : 'desc';
|
|
879
|
+
} else {
|
|
880
|
+
sortField = field;
|
|
881
|
+
sortOrder = 'desc';
|
|
882
|
+
}
|
|
883
|
+
lastRenderHash.projects = null;
|
|
884
|
+
lastRenderHash.overview = null;
|
|
885
|
+
updateUrl();
|
|
886
|
+
if (currentView === 'overview') renderOverview();
|
|
887
|
+
else renderProjects();
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
// biome-ignore lint/correctness/noUnusedVariables: called from HTML onchange
|
|
891
|
+
async function onRangeChange(val) {
|
|
892
|
+
dateRange = parseInt(val, 10) || 7;
|
|
893
|
+
saveDateRange(dateRange);
|
|
894
|
+
lastRenderHash = {};
|
|
895
|
+
updateUrl();
|
|
896
|
+
const t = showToast(`Recalculating for ${dateRange} days...`, true);
|
|
897
|
+
await loadAndRender(currentView);
|
|
898
|
+
dismissToast(t);
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
async function refreshData() {
|
|
902
|
+
const btn = document.getElementById('refreshBtn');
|
|
903
|
+
btn.classList.add('loading');
|
|
904
|
+
btn.disabled = true;
|
|
905
|
+
const t = showToast(`Recalculating for ${dateRange} days...`, true);
|
|
906
|
+
try {
|
|
907
|
+
await fetch('/api/refresh', { method: 'POST' });
|
|
908
|
+
clearLocalCache();
|
|
909
|
+
forceRefresh = true;
|
|
910
|
+
lastRenderHash = {};
|
|
911
|
+
await loadAndRender(currentView);
|
|
912
|
+
forceRefresh = false;
|
|
913
|
+
} finally {
|
|
914
|
+
dismissToast(t);
|
|
915
|
+
btn.classList.remove('loading');
|
|
916
|
+
btn.disabled = false;
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
async function loadAndRender(view) {
|
|
921
|
+
const myNav = ++navCounter;
|
|
922
|
+
ensureViewElements();
|
|
923
|
+
showView(`${view}-view`);
|
|
924
|
+
|
|
925
|
+
const viewEl = document.getElementById(`${view}-view`);
|
|
926
|
+
if (viewEl) {
|
|
927
|
+
const hasContent = viewEl.querySelector('.dashboard-content');
|
|
928
|
+
if (!hasContent) {
|
|
929
|
+
viewEl.innerHTML = '<div class="loading-state"><div class="loading-spinner"></div><span>Loading...</span></div>';
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
try {
|
|
934
|
+
switch (view) {
|
|
935
|
+
case 'overview':
|
|
936
|
+
await fetchOverview();
|
|
937
|
+
if (myNav !== navCounter) return;
|
|
938
|
+
renderOverview();
|
|
939
|
+
break;
|
|
940
|
+
case 'projects':
|
|
941
|
+
await fetchProjects();
|
|
942
|
+
if (myNav !== navCounter) return;
|
|
943
|
+
renderProjects();
|
|
944
|
+
break;
|
|
945
|
+
case 'sessions':
|
|
946
|
+
if (currentProjectPath) {
|
|
947
|
+
sessionsData = null;
|
|
948
|
+
renderSessions();
|
|
949
|
+
await fetchSessions(currentProjectPath);
|
|
950
|
+
if (myNav !== navCounter) return;
|
|
951
|
+
renderSessions();
|
|
952
|
+
}
|
|
953
|
+
break;
|
|
954
|
+
case 'detail':
|
|
955
|
+
if (currentSessionId) {
|
|
956
|
+
sessionDetailData = null;
|
|
957
|
+
renderDetail();
|
|
958
|
+
await fetchSessionDetail(currentSessionId);
|
|
959
|
+
if (myNav !== navCounter) return;
|
|
960
|
+
renderDetail();
|
|
961
|
+
}
|
|
962
|
+
break;
|
|
963
|
+
}
|
|
964
|
+
} catch (err) {
|
|
965
|
+
if (myNav !== navCounter) return;
|
|
966
|
+
console.error(`Failed to load ${view}:`, err);
|
|
967
|
+
showToast(`Error: ${err.message}`);
|
|
968
|
+
const viewEl = document.getElementById(`${view}-view`);
|
|
969
|
+
if (viewEl) viewEl.innerHTML = `<div class="loading-state"><span>Failed to load data: ${err.message}</span></div>`;
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
function renderCurrentView() {
|
|
974
|
+
loadAndRender(currentView);
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
function ensureViewElements() {
|
|
978
|
+
const app = document.getElementById('app');
|
|
979
|
+
if (!app) return;
|
|
980
|
+
const views = ['overview', 'projects', 'sessions', 'detail'];
|
|
981
|
+
for (const v of views) {
|
|
982
|
+
if (!document.getElementById(`${v}-view`)) {
|
|
983
|
+
const div = document.createElement('div');
|
|
984
|
+
div.id = `${v}-view`;
|
|
985
|
+
div.className = 'view';
|
|
986
|
+
app.appendChild(div);
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
// Remove initial loading state
|
|
990
|
+
const ls = document.getElementById('loadingState');
|
|
991
|
+
if (ls) ls.remove();
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
// #endregion
|
|
995
|
+
|
|
996
|
+
// #region MODAL
|
|
997
|
+
|
|
998
|
+
function toggleHelpModal() {
|
|
999
|
+
document.getElementById('helpModal').classList.toggle('visible');
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
// #endregion
|
|
1003
|
+
|
|
1004
|
+
// #region KEYBOARD_SHORTCUTS
|
|
1005
|
+
|
|
1006
|
+
let selectedRowIdx = -1;
|
|
1007
|
+
|
|
1008
|
+
function getVisibleRows() {
|
|
1009
|
+
const viewEl = document.getElementById(`${currentView}-view`);
|
|
1010
|
+
if (!viewEl) return [];
|
|
1011
|
+
return Array.from(viewEl.querySelectorAll('tbody tr[data-clickable]'));
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
function selectRow(idx) {
|
|
1015
|
+
const rows = getVisibleRows();
|
|
1016
|
+
rows.forEach((r) => r.classList.remove('kb-selected'));
|
|
1017
|
+
if (idx >= 0 && idx < rows.length) {
|
|
1018
|
+
selectedRowIdx = idx;
|
|
1019
|
+
rows[idx].classList.add('kb-selected');
|
|
1020
|
+
rows[idx].scrollIntoView({ block: 'nearest' });
|
|
1021
|
+
} else {
|
|
1022
|
+
selectedRowIdx = -1;
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
function activateSelectedRow() {
|
|
1027
|
+
const rows = getVisibleRows();
|
|
1028
|
+
if (selectedRowIdx >= 0 && selectedRowIdx < rows.length) {
|
|
1029
|
+
rows[selectedRowIdx].click();
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
function goBack() {
|
|
1034
|
+
if (currentView === 'detail') {
|
|
1035
|
+
navigate('sessions', { project: currentProjectPath, projectName: currentProjectName });
|
|
1036
|
+
} else if (currentView === 'sessions') {
|
|
1037
|
+
navigate('projects');
|
|
1038
|
+
} else if (currentView === 'projects') {
|
|
1039
|
+
navigate('overview');
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
document.addEventListener('keydown', (e) => {
|
|
1044
|
+
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA' || e.target.tagName === 'SELECT') return;
|
|
1045
|
+
|
|
1046
|
+
const anyModal = document.querySelector('.modal-overlay.visible');
|
|
1047
|
+
if (anyModal) {
|
|
1048
|
+
if (e.key === 'Escape') {
|
|
1049
|
+
anyModal.classList.remove('visible');
|
|
1050
|
+
e.preventDefault();
|
|
1051
|
+
}
|
|
1052
|
+
return;
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
if (e.ctrlKey || e.altKey || e.metaKey) return;
|
|
1056
|
+
|
|
1057
|
+
if (e.key === '?' || (e.key === '/' && e.shiftKey)) {
|
|
1058
|
+
e.preventDefault();
|
|
1059
|
+
toggleHelpModal();
|
|
1060
|
+
return;
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
if (e.key === '1') {
|
|
1064
|
+
e.preventDefault();
|
|
1065
|
+
navigate('overview');
|
|
1066
|
+
selectedRowIdx = -1;
|
|
1067
|
+
return;
|
|
1068
|
+
}
|
|
1069
|
+
if (e.key === '2') {
|
|
1070
|
+
e.preventDefault();
|
|
1071
|
+
navigate('projects');
|
|
1072
|
+
selectedRowIdx = -1;
|
|
1073
|
+
return;
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
if (e.key === 'r') {
|
|
1077
|
+
e.preventDefault();
|
|
1078
|
+
refreshData();
|
|
1079
|
+
return;
|
|
1080
|
+
}
|
|
1081
|
+
if (e.key === 't') {
|
|
1082
|
+
e.preventDefault();
|
|
1083
|
+
toggleTheme();
|
|
1084
|
+
return;
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
if (e.key === 'Escape') {
|
|
1088
|
+
e.preventDefault();
|
|
1089
|
+
goBack();
|
|
1090
|
+
selectedRowIdx = -1;
|
|
1091
|
+
return;
|
|
1092
|
+
}
|
|
1093
|
+
if (e.key === 'Backspace') {
|
|
1094
|
+
e.preventDefault();
|
|
1095
|
+
goBack();
|
|
1096
|
+
selectedRowIdx = -1;
|
|
1097
|
+
return;
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
// Table navigation
|
|
1101
|
+
if (e.key === 'j' || e.key === 'ArrowDown') {
|
|
1102
|
+
e.preventDefault();
|
|
1103
|
+
const rows = getVisibleRows();
|
|
1104
|
+
if (rows.length) selectRow(Math.min(selectedRowIdx + 1, rows.length - 1));
|
|
1105
|
+
return;
|
|
1106
|
+
}
|
|
1107
|
+
if (e.key === 'k' || e.key === 'ArrowUp') {
|
|
1108
|
+
e.preventDefault();
|
|
1109
|
+
const rows = getVisibleRows();
|
|
1110
|
+
if (rows.length) selectRow(Math.max(selectedRowIdx - 1, 0));
|
|
1111
|
+
return;
|
|
1112
|
+
}
|
|
1113
|
+
if (e.key === 'Enter') {
|
|
1114
|
+
e.preventDefault();
|
|
1115
|
+
activateSelectedRow();
|
|
1116
|
+
selectedRowIdx = -1;
|
|
1117
|
+
return;
|
|
1118
|
+
}
|
|
1119
|
+
});
|
|
1120
|
+
|
|
1121
|
+
// #endregion
|
|
1122
|
+
|
|
1123
|
+
// #region TOAST
|
|
1124
|
+
|
|
1125
|
+
function showToast(msg, persistent) {
|
|
1126
|
+
const container = document.getElementById('toast');
|
|
1127
|
+
if (!container) return;
|
|
1128
|
+
const el = document.createElement('div');
|
|
1129
|
+
el.className = 'toast';
|
|
1130
|
+
el.textContent = msg;
|
|
1131
|
+
container.appendChild(el);
|
|
1132
|
+
if (!persistent) setTimeout(() => el.remove(), 3000);
|
|
1133
|
+
return el;
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
function dismissToast(el) {
|
|
1137
|
+
if (el) el.remove();
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
// #endregion
|
|
1141
|
+
|
|
1142
|
+
// #region HUB_INTEGRATION
|
|
1143
|
+
|
|
1144
|
+
(async function initHub() {
|
|
1145
|
+
const cfg = await fetch('/hub-config')
|
|
1146
|
+
.then((r) => r.json())
|
|
1147
|
+
.catch(() => ({}));
|
|
1148
|
+
if (!cfg.enabled) return;
|
|
1149
|
+
|
|
1150
|
+
window.__HUB__ = cfg;
|
|
1151
|
+
|
|
1152
|
+
document.addEventListener('keydown', (e) => {
|
|
1153
|
+
if (e.ctrlKey && e.altKey && (e.key === 'ArrowLeft' || e.key === 'ArrowRight')) {
|
|
1154
|
+
e.preventDefault();
|
|
1155
|
+
window.parent?.postMessage({ type: 'hub:keydown', key: e.key }, '*');
|
|
1156
|
+
}
|
|
1157
|
+
if (e.altKey && !e.ctrlKey && !e.shiftKey && !e.metaKey && /^[1-9]$/.test(e.key)) {
|
|
1158
|
+
e.preventDefault();
|
|
1159
|
+
window.parent?.postMessage({ type: 'hub:keydown', key: e.key }, '*');
|
|
1160
|
+
}
|
|
1161
|
+
});
|
|
1162
|
+
})();
|
|
1163
|
+
|
|
1164
|
+
window.hubNavigate = function hubNavigate(app, url) {
|
|
1165
|
+
if (!window.__HUB__?.enabled) return;
|
|
1166
|
+
window.parent?.postMessage({ type: 'hub:navigate', app, url }, '*');
|
|
1167
|
+
};
|
|
1168
|
+
|
|
1169
|
+
// #endregion
|
|
1170
|
+
|
|
1171
|
+
// #region INIT
|
|
1172
|
+
|
|
1173
|
+
loadTheme();
|
|
1174
|
+
|
|
1175
|
+
document.addEventListener('DOMContentLoaded', async () => {
|
|
1176
|
+
const state = getUrlState();
|
|
1177
|
+
dateRange = loadDateRange();
|
|
1178
|
+
document.getElementById('rangeSelect').value = dateRange;
|
|
1179
|
+
|
|
1180
|
+
if (state.sort) sortField = state.sort;
|
|
1181
|
+
if (state.order) sortOrder = state.order;
|
|
1182
|
+
|
|
1183
|
+
if (state.session) {
|
|
1184
|
+
currentSessionId = state.session;
|
|
1185
|
+
currentProjectPath = state.project;
|
|
1186
|
+
currentProjectName = state.projectName;
|
|
1187
|
+
await navigate('detail', { session: state.session });
|
|
1188
|
+
} else if (state.project) {
|
|
1189
|
+
currentProjectPath = state.project;
|
|
1190
|
+
currentProjectName = state.projectName;
|
|
1191
|
+
await navigate('sessions', { project: state.project, projectName: state.projectName });
|
|
1192
|
+
} else if (state.view === 'projects') {
|
|
1193
|
+
await navigate('projects');
|
|
1194
|
+
} else {
|
|
1195
|
+
await navigate('overview');
|
|
1196
|
+
}
|
|
1197
|
+
});
|
|
1198
|
+
|
|
1199
|
+
window.addEventListener('popstate', () => {
|
|
1200
|
+
const state = getUrlState();
|
|
1201
|
+
currentView = state.view || 'overview';
|
|
1202
|
+
currentProjectPath = state.project;
|
|
1203
|
+
currentProjectName = state.projectName;
|
|
1204
|
+
currentSessionId = state.session;
|
|
1205
|
+
lastRenderHash = {};
|
|
1206
|
+
loadAndRender(currentView);
|
|
1207
|
+
});
|
|
1208
|
+
|
|
1209
|
+
if ('serviceWorker' in navigator) {
|
|
1210
|
+
navigator.serviceWorker.register('/sw.js').catch(() => {});
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
// #endregion
|