Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
699 lines (685 loc) · 101 KB
/
Copy pathapp.js
File metadata and controls
699 lines (685 loc) · 101 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
// Ledger ships with deterministic demo fixtures, but a generated local report can
// inject real, normalized Codex usage through window.__LEDGER_DATA__ before this file.
const DEMO_TASKS = [
{ id:'task-0', name:'Build dashboard shell', project:'Sample App', model:'gpt-5.6-sol', weight:1.7, cache:.78, current:1.25, previous:.94 },
{ id:'task-1', name:'Refine onboarding flow', project:'Sample App', model:'gpt-5.6-terra', weight:1.15, cache:.84, current:1.35, previous:.92 },
{ id:'task-2', name:'Add keyboard navigation', project:'Sample App', model:'gpt-5.6-sol', weight:.82, cache:.76, current:1.2, previous:1 },
{ id:'task-3', name:'Draft a research brief', project:'Research Notebook', model:'gpt-5.6-sol', weight:.72, cache:.7, current:.78, previous:1.08 },
{ id:'task-4', name:'Summarize source material', project:'Research Notebook', model:'gpt-5.6-terra', weight:.92, cache:.66, current:.82, previous:1.12 },
{ id:'task-5', name:'Prepare a weekly digest', project:'Writing Studio', model:'gpt-5.6-luna', weight:.42, cache:.9, current:1.14, previous:1.04 },
{ id:'task-6', name:'Outline a long-form article', project:'Writing Studio', model:'gpt-5.6-sol', weight:1.22, cache:.81, current:1.1, previous:.98 },
{ id:'task-7', name:'Organize project notes', project:'Personal Tools', model:'gpt-5.6-luna', weight:.52, cache:.86, current:.9, previous:1.08 },
{ id:'task-8', name:'Compare model behavior', project:'Personal Tools', model:'gpt-6-astra', weight:1.42, cache:.74, current:1.3, previous:0, activeAfter:'2026-09-11' },
{ id:'task-9', name:'Refactor shared components', project:'Sample App', model:'gpt-5.6-terra', weight:2.75, cache:.61, current:1.7, previous:.64 },
{ id:'task-10', name:'Triage saved references', project:'Research Notebook', model:'gpt-5.6-luna', weight:.36, cache:.93, current:.7, previous:1.05 },
{ id:'task-11', name:'Review release checklist', project:'Personal Tools', model:'gpt-5.6-terra', weight:.58, cache:.82, current:.82, previous:1.1 },
{ id:'task-12', name:'Explore a reasoning problem', project:'Research Notebook', model:'gpt-6-astra', weight:1.1, cache:.58, current:1.15, previous:0, activeAfter:'2026-09-12' }
];
const CREDIT_RATES = {
'gpt-6-astra':{ input:250,cached:25,output:1250 },
'gpt-5.6-sol':{ input:100,cached:10,output:500 },
'gpt-5.6-terra':{ input:50,cached:5,output:300 },
'gpt-5.6-luna':{ input:5,cached:.5,output:30 }
};
const SPEED_BASELINES = {'gpt-6-astra':92,'gpt-5.6-sol':136,'gpt-5.6-terra':172,'gpt-5.6-luna':218};
const MODEL_COLORS = {'gpt-6-astra':'#d97706','gpt-5.6-sol':'#0071e3','gpt-5.6-terra':'#5e5ce6','gpt-5.6-luna':'#248a3d'};
const FALLBACK_MODEL_COLORS = ['#0071e3','#5e5ce6','#248a3d','#d97706','#0a84ff','#bf5af2','#32ade6','#ff375f'];
const DEFAULT_QUOTA = { five:{ used:73, elapsed:56, seconds:8072, source:'demo' }, week:{ used:18, elapsed:28, seconds:558000, source:'demo' } };
const QUOTA_STORAGE_KEY = 'codex-ledger:quota-snapshot:v1';
const EXTERNAL_PAYLOAD = window.__LEDGER_DATA__ && typeof window.__LEDGER_DATA__ === 'object' ? window.__LEDGER_DATA__ : null;
const IS_LIVE_REPORT = Boolean(EXTERNAL_PAYLOAD && Array.isArray(EXTERNAL_PAYLOAD.records));
let quotaSnapshot = IS_LIVE_REPORT && !EXTERNAL_PAYLOAD?.quota ? {
five:{ used:null, elapsed:null, seconds:0, source:'unavailable' },
week:{ used:null, elapsed:null, seconds:0, source:'unavailable' }
} : {
five:{...DEFAULT_QUOTA.five},
week:{...DEFAULT_QUOTA.week}
};
let quotaObservedAt = 0;
let quotaStarted = Date.now();
let quotaRefreshState = 'initial';
function buildDemoRecords(){
let seed=1029;
const random=()=>{seed=(seed*16807)%2147483647;return(seed-1)/2147483646;};
const output=[];
const fixtureStart=new Date('2026-07-20T00:00:00Z');
const fixtureEnd=new Date('2026-09-17T00:00:00Z');
for(let cursor=new Date(fixtureStart);cursor<=fixtureEnd;cursor.setUTCDate(cursor.getUTCDate()+1)){
const date=cursor.toISOString().slice(0,10);
DEMO_TASKS.forEach((task,taskIndex)=>{
if(task.activeAfter&&date<task.activeAfter)return;
const scale=date>='2026-09-11'?task.current:date>='2026-09-04'?task.previous:.78+random()*.44;
if(!scale||random()>Math.min(.82,.31*scale+.24))return;
const turns=Math.max(3,Math.round((3+random()*8)*scale));
const sessionId=`${task.id}-${date}`;
for(let turn=0;turn<turns;turn+=1){
const growthBase=task.id==='task-9'?.23:task.id==='task-12'?.18:.055+.045*random();
const contextGrowth=1+turn*growthBase;
const input=Math.round((22000+random()*78000)*task.weight*(.82+random()*.36)*contextGrowth);
const cacheRate=Math.max(.38,Math.min(.96,task.cache+(random()-.5)*.18));
const cached=Math.round(input*cacheRate);
const outputFactor=task.id==='task-6'?2.2:task.id==='task-12'?1.65:1;
const outputTokens=Math.round((420+random()*3200)*outputFactor*Math.sqrt(task.weight));
const speed=SPEED_BASELINES[task.model]*(.82+random()*.36);
const hour=9+Math.floor(turn/2),minute=turn%2?35:10,time=`${String(hour).padStart(2,'0')}:${minute}`;
output.push({
id:`demo-${date}-${taskIndex}-${turn}`,timestamp:`${date}T${time}:00Z`,date,time,
taskId:sessionId,turnId:`${sessionId}-turn-${turn+1}`,task:task.name,project:task.project,model:task.model,
input,cached,output:outputTokens,reasoning:Math.round(outputTokens*(.28+random()*.22)),
durationSeconds:Math.max(.25,outputTokens/speed),speedOutput:outputTokens,total:input+outputTokens
});
}
});
}
return output;
}
function normalizeRecord(row,index){
const input=Math.max(0,Number(row.input)||0),cached=Math.max(0,Math.min(input,Number(row.cached)||0)),output=Math.max(0,Number(row.output)||0);
const date=String(row.date||'').slice(0,10),time=String(row.time||'00:00').slice(0,5),taskId=String(row.taskId||row.sessionId||`task-${index}`);
return {
id:String(row.id||`record-${index}`),timestamp:String(row.timestamp||`${date}T${time}:00Z`),date,time,
taskId,turnId:String(row.turnId||`${taskId}-turn-${index}`),
task:String(row.task||'Untitled session'),
project:String(row.project||'Unattributed'),
model:String(row.model||'unknown'),
input,cached,output,
reasoning:Math.max(0,Number(row.reasoning)||0),
durationSeconds:Math.max(0,Number(row.durationSeconds)||0),
speedOutput:Math.max(0,Number(row.speedOutput)||0),
total:Math.max(0,Number(row.total)||(input+output))
};
}
const demoRecords=buildDemoRecords();
const records=IS_LIVE_REPORT?EXTERNAL_PAYLOAD.records.map(normalizeRecord).filter(row=>/^\d{4}-\d{2}-\d{2}$/.test(row.date)):demoRecords;
const taskCatalog=[...new Map(records.map(row=>[row.taskId,{id:row.taskId,name:row.task,project:row.project,model:row.model}])).values()];
const PROJECTS=[...new Set(records.map(row=>row.project))].sort((a,b)=>a.localeCompare(b));
const MODELS=[...new Set(records.map(row=>row.model))].sort((a,b)=>a.localeCompare(b));
const localTodayISO=()=>{const now=new Date(),local=new Date(now.getTime()-now.getTimezoneOffset()*60000);return local.toISOString().slice(0,10);};
const anchorDate=IS_LIVE_REPORT?localTodayISO():(records.length?records.reduce((max,row)=>row.date>max?row.date:max,records[0].date):localTodayISO());
const shiftISO=(value,days)=>{const date=new Date(`${value}T00:00:00Z`);date.setUTCDate(date.getUTCDate()+days);return date.toISOString().slice(0,10);};
const initialStart=shiftISO(anchorDate,-6);
const initialEnd=anchorDate;
const state = { range:'7d',project:'all',task:'all',model:'all',start:initialStart,end:initialEnd,search:'',descending:true,page:0,leftMetric:'burn',language:'en',matrixExpanded:false };
let filteredRecords = [];
let previousRecords = [];
let currentMetrics = null;
let previousMetrics = null;
let currentDrivers = new Map();
let lastFocusedElement = null;
let toastTimer = null;
const $ = id => document.getElementById(id);
const COPY = {
en:{vsPrevious:'vs previous period',newLabel:'New',allProjects:'All projects',allTasks:'All tasks',allModels:'All models',dayAverage:n=>`${n}-day selected-period average`,tasksTurns:(tasks,turns)=>`${tasks} tasks / ${turns} turns`,cached:v=>`${v} cached`,perDay:v=>`${v} / Day`,turnCount:n=>`${n} turns`,quotaFresh:'Observed quota snapshot · updated just now',quotaDemo:'Observed quota snapshot · demo data',quotaUnchanged:'Account quota snapshot unchanged · no live quota source returned',quotaUpdated:'Account quota snapshot updated',quotaNote:'Refresh rescans quota snapshots already persisted by Codex; it cannot force an account sync. After a reset, use Codex once, then Refresh again.',modelNote:'Starting a new Codex task to run this skill does use the model selected for that task.',liveBadge:'Live snapshot',demoBadge:'Demo data',noData:'No data for the current filters.',noProjectData:'No project data for the current filters.',adjustFilters:'Adjust the filters to see results.',projects:n=>`${n} projects`,topShare:p=>`Top 3 account for ${p} of current usage.`,showing:(a,b,n)=>`Showing ${a}–${b} / ${n}`,notAvailable:'Unavailable',estimatedRate:(credits,relative)=>`Estimated ${credits} cr · rate weight ${relative}× Luna`,turnsOutput:(turns,output)=>`${turns} turns · ${output} output`,quotaFast:'Short-window pace is high',quotaBody:'The 5-hour window is ahead of elapsed time; weekly pacing remains normal. Quota snapshots are independent of the token filters below.',growthTitle:p=>`${p} usage increased`,growthBody:(change,model)=>`Up ${change}% from the previous period, led by ${model}.`,expensiveTitle:'High-usage task found',expensiveBody:(task,tokens,share)=>`“${task}” used ${tokens} tokens, ${share} of the current period.`,concentrationTitle:m=>`${m} usage is concentrated`,concentrationBody:share=>`This model accounts for ${share} of current-period tokens.`,cacheUp:'Cache efficiency improved',cacheDown:'Cache hit declined',cacheBody:(from,verb,to)=>`Cache Hit ${verb} from ${from} to ${to}.`,rose:'rose',fell:'fell',noAnomaly:'No notable usage anomalies in the current filters.',previousDay:'Previous period',compare:'Compare',invalidDates:'Start date cannot be later than end date',cachedInput:'Cached input',uncachedInput:'Uncached input',output:'Output',quotaIntensity:'Quota intensity',speedRanking:'Generation speed',driverTotal:n=>`Total usage is among the current top ${n}`,driverDuration:v=>`Cumulative generation time: ${v}`,driverCache:v=>`Cache hit is only ${v}`,driverOutput:'Output share is high for the current task set',driverTurn:x=>`Tokens per turn are ${x}× your current median`,driverLongSession:n=>`${n} turns, above the long-session threshold`,driverContextGrowth:x=>`Recent input context is ${x}× the opening-turn baseline`,driverCacheCompare:(value,median)=>`Cache hit is ${value} vs your ${median} median`,driverPeakTurn:(turn,tokens)=>`Turn ${turn} peaked at ${tokens} tokens`,driverNormal:'No notable session driver was triggered',sessionStable:'Stable',sessionGrowing:'Growing',sessionHeavy:'Heavy',sessionLimited:'Limited data',contextInsightTitle:'Context growth detected',contextInsightBody:(count,max)=>`${count} sessions show elevated context growth; the highest reached ${max}×.`,turnRateUp:'Tokens / turn increased',turnRateDown:'Tokens / turn decreased',turnRateBody:(change,current)=>`${change}% vs the previous period · ${current} per turn.`,topCurrent:'Top 5 in the current period.',resetToast:'Reset to last 7 days · all projects and models',exportToast:'Exported dashboard image · PNG',exportSvgFallback:'Exported dashboard image · SVG',exporting:'Exporting…',exportError:'Image export failed · please try again',refreshing:'Refreshing…',refreshData:'Refresh data',refreshDone:n=>`Local refresh complete · ${n} records · no model quota used`,refreshDoneNoQuota:n=>`Local refresh complete · ${n} records · quota snapshot unchanged`,refreshDoneWithQuota:n=>`Local refresh complete · ${n} records · quota snapshot updated`,reduceMotion:'Reduce motion',restoreMotion:'Restore motion',totalTokens:'Total tokens',previous:'Previous',next:'Next',sortTokens:'Total tokens',matrixCompact:'Compact',matrixAll:'Show all',matrixCompactScope:(projects,models)=>`Top ${projects} projects · Top ${models} models`,matrixAllScope:(projects,models)=>`All ${projects} projects · ${models} models`,matrixEmpty:'No model × project usage for the current filters.',noUsage:'No usage'},
zh:{vsPrevious:'较上一周期',newLabel:'新增',allProjects:'所有项目',allTasks:'所有任务',allModels:'所有模型',dayAverage:n=>`所选 ${n} 天日均`,tasksTurns:(tasks,turns)=>`${tasks} 个 Session / ${turns} 轮`,cached:v=>`已缓存 ${v}`,perDay:v=>`${v} / 天`,turnCount:n=>`${n} 轮`,quotaFresh:'额度快照 · 刚刚更新',quotaDemo:'额度快照 · 演示数据',quotaUnchanged:'账户额度快照未变化 · 未返回实时额度来源',quotaUpdated:'账户额度快照已更新',quotaNote:'刷新只能重新读取 Codex 已写入本机的额度快照,不能强制账户侧同步。额度重置后先使用一次 Codex,再点刷新即可读取新的快照。',modelNote:'重新发起 Codex 任务运行此 Skill 时,会使用该任务当前选择的模型。',liveBadge:'实时快照',demoBadge:'演示数据',noData:'当前筛选条件下暂无任务数据。',noProjectData:'当前筛选条件下暂无项目数据。',adjustFilters:'调整筛选条件后再查看。',projects:n=>`${n} 个项目`,topShare:p=>`前 3 项占当前总量 ${p}。`,showing:(a,b,n)=>`显示 ${a}–${b} / ${n}`,notAvailable:'不可用',estimatedRate:(credits,relative)=>`估算 ${credits} cr · 费率权重 ${relative}× Luna`,turnsOutput:(turns,output)=>`${turns} 轮 · ${output} 输出`,quotaFast:'短窗口使用较快',quotaBody:'当前 5h 窗口进度高于时间进度;每周窗口节奏仍正常。额度快照与下方 Token 筛选独立。',growthTitle:p=>`${p} 消耗增长明显`,growthBody:(change,model)=>`较上一周期增加 ${change}%,主要来自 ${model}。`,expensiveTitle:'发现高消耗任务',expensiveBody:(task,tokens,share)=>`“${task}”消耗 ${tokens} Tokens,占当前周期 ${share}。`,concentrationTitle:m=>`${m} 使用集中`,concentrationBody:share=>`该模型占当前周期 Token 的 ${share}。`,cacheUp:'缓存效率改善',cacheDown:'缓存命中下降',cacheBody:(from,verb,to)=>`缓存命中率从 ${from} ${verb}至 ${to}。`,rose:'提升',fell:'下降',noAnomaly:'当前没有明显异常消耗。',previousDay:'上一周期',compare:'对比',invalidDates:'开始日期不能晚于结束日期',cachedInput:'缓存输入',uncachedInput:'非缓存输入',output:'输出',quotaIntensity:'额度消耗强度',speedRanking:'生成速度排名',driverTotal:n=>`总量位于当前任务 Top ${n}`,driverDuration:v=>`累计生成时长 ${v}`,driverCache:v=>`缓存命中率仅 ${v}`,driverOutput:'输出占比位于当前任务高位',driverTurn:x=>`单轮 Token 约为当前中位数 ${x}×`,driverLongSession:n=>`${n} 轮,超过长 Session 阈值`,driverContextGrowth:x=>`最近输入 Context 约为开头基线的 ${x}×`,driverCacheCompare:(value,median)=>`缓存命中 ${value},当前中位数为 ${median}`,driverPeakTurn:(turn,tokens)=>`第 ${turn} 轮达到峰值 ${tokens} Tokens`,driverNormal:'当前 Session 未触发明显消耗特征',sessionStable:'稳定',sessionGrowing:'增长中',sessionHeavy:'偏重',sessionLimited:'数据不足',contextInsightTitle:'发现 Context 增长',contextInsightBody:(count,max)=>`${count} 个 Session 出现明显 Context 增长,最高达到 ${max}×。`,turnRateUp:'单轮 Token 上升',turnRateDown:'单轮 Token 下降',turnRateBody:(change,current)=>`较上一周期变化 ${change}% · 当前每轮 ${current}。`,topCurrent:'当前周期前 5 项。',resetToast:'已恢复近 7 天 · 所有项目与模型',exportToast:'已导出看板图片 · PNG',exportSvgFallback:'已导出看板图片 · SVG',exporting:'导出中…',exportError:'图片导出失败 · 请重试',refreshing:'刷新中…',refreshData:'刷新数据',refreshDone:n=>`本地刷新完成 · ${n} 条记录 · 未消耗模型额度`,refreshDoneNoQuota:n=>`本地刷新完成 · ${n} 条记录 · 额度快照未变化`,refreshDoneWithQuota:n=>`本地刷新完成 · ${n} 条记录 · 额度快照已更新`,reduceMotion:'减少动态效果',restoreMotion:'恢复动态效果',totalTokens:'Token 总量',previous:'上一页',next:'下一页',sortTokens:'Token 总量',matrixCompact:'精简',matrixAll:'全部',matrixCompactScope:(projects,models)=>`Top ${projects} 项目 · Top ${models} 模型`,matrixAllScope:(projects,models)=>`全部 ${projects} 项目 · ${models} 模型`,matrixEmpty:'当前筛选范围内暂无模型 × 项目用量。',noUsage:'无用量'}
};
const t = (key,...args) => { const value=COPY[state.language][key]; return typeof value==='function'?value(...args):value; };
const sum = (rows,key) => rows.reduce((total,row) => total + (Number(row[key]) || 0),0);
const escapeHTML = value => String(value).replace(/[&<>"']/g,char => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[char]));
const shortModel = model => String(model||'unknown').replace('gpt-','');
const truncateMiddle = (value,max=22) => { const text=String(value||''); if(text.length<=max)return text; const tail=Math.max(5,Math.floor(max*.32)),head=Math.max(6,max-tail-1); return `${text.slice(0,head)}…${text.slice(-tail)}`; };
const modelColor = model => MODEL_COLORS[model] || FALLBACK_MODEL_COLORS[Math.abs([...String(model)].reduce((n,c)=>n+c.charCodeAt(0),0))%FALLBACK_MODEL_COLORS.length];
const hasCreditRate = model => Boolean(CREDIT_RATES[model]);
const parseDate = value => new Date(`${value}T00:00:00Z`);
const shiftDate = (value,days) => { const date = parseDate(value); date.setUTCDate(date.getUTCDate()+days); return date.toISOString().slice(0,10); };
function dateRange(start,end) { const dates=[]; for(let cursor=parseDate(start); cursor<=parseDate(end)&&dates.length<366; cursor.setUTCDate(cursor.getUTCDate()+1)) dates.push(cursor.toISOString().slice(0,10)); return dates; }
function formatTokens(value) {
if (!Number.isFinite(value)) return '—';
if (Math.abs(value) < 1000) return Math.round(value).toLocaleString('en-US');
const [divisor,suffix] = Math.abs(value) >= 1e9 ? [1e9,'B'] : Math.abs(value) >= 1e6 ? [1e6,'M'] : [1e3,'K'];
return `${(value/divisor).toFixed(2).replace(/\.0+$|(?<=\.[0-9])0$/,'')}${suffix}`;
}
const formatNumber = value => Number.isFinite(value) ? Math.round(value).toLocaleString('en-US') : '—';
const formatPercent = (value,digits=1) => Number.isFinite(value) ? `${value.toFixed(digits)}%` : '—';
function formatDuration(seconds) { if(!Number.isFinite(seconds)) return '—'; const mins=Math.round(seconds/60); if(mins<60) return `${mins}m`; return `${Math.floor(mins/60)}h ${mins%60}m`; }
function formatCountdown(seconds,long=false) { const safe=Math.max(0,Math.floor(seconds)); if(long){const d=Math.floor(safe/86400),h=Math.floor(safe%86400/3600); return `${d}d ${h}h`;} const h=Math.floor(safe/3600),m=Math.floor(safe%3600/60),s=safe%60; return [h,m,s].map(v=>String(v).padStart(2,'0')).join(':'); }
function changeRate(current,previous) { if(previous===0) return current>0 ? 'new' : null; return (current-previous)/previous; }
function formatChange(change,suffix=t('vsPrevious')) { if(change==='new') return `<span class="change new">${t('newLabel')}</span>`; if(!Number.isFinite(change)) return '<span class="change neutral">—</span>'; const arrow=change>0?'↑':change<0?'↓':'→'; return `<span class="change ${change>0?'up':change<0?'down':'neutral'}">${arrow} ${Math.abs(change*100).toFixed(1)}% ${suffix}</span>`; }
function rangeBounds() {
if(state.range==='today') return [anchorDate,anchorDate];
if(state.range==='30d') return [shiftDate(anchorDate,-29),anchorDate];
if(state.range==='custom') return [state.start,state.end];
return [shiftDate(anchorDate,-6),anchorDate];
}
function calculatePreviousPeriod([start,end]) {
if(start>end) return [start,end];
const days=dateRange(start,end).length;
return [shiftDate(start,-days),shiftDate(start,-1)];
}
function filterUsageData(bounds) {
const [start,end]=bounds;
if(start>end) return [];
return records.filter(row => row.date>=start && row.date<=end &&
(state.project==='all'||row.project===state.project) &&
(state.task==='all'||row.taskId===state.task) &&
(state.model==='all'||row.model===state.model));
}
function creditsForRecord(row) { const rate=CREDIT_RATES[row.model]; if(!rate) return 0; return ((row.input-row.cached)*rate.input+row.cached*rate.cached+row.output*rate.output)/1e6; }
const average = values => values.length ? values.reduce((total,value)=>total+(Number(value)||0),0)/values.length : 0;
const recordEpoch = row => { const parsed=Date.parse(row.timestamp||`${row.date}T${row.time||'00:00'}:00Z`); return Number.isFinite(parsed)?parsed:0; };
function aggregateTaskTurns(rows){
const turns=new Map();
[...rows].sort((a,b)=>recordEpoch(a)-recordEpoch(b)||String(a.id).localeCompare(String(b.id))).forEach(row=>{
const turnId=row.turnId||row.id;
if(!turns.has(turnId))turns.set(turnId,{turnId,input:0,cached:0,output:0,total:0,durationSeconds:0,timestamp:row.timestamp,date:row.date,time:row.time,model:row.model});
const turn=turns.get(turnId);
// A user turn may contain multiple model requests. Max input is the closest
// observable proxy for context size; token totals still sum every request.
turn.input=Math.max(turn.input,row.input||0);
turn.cached=Math.max(turn.cached,row.cached||0);
turn.output+=(row.output||0);
turn.total+=(row.total||0);
turn.durationSeconds+=(row.durationSeconds||0);
if(recordEpoch(row)>=recordEpoch(turn)){turn.timestamp=row.timestamp;turn.date=row.date;turn.time=row.time;turn.model=row.model;}
});
return [...turns.values()].sort((a,b)=>recordEpoch(a)-recordEpoch(b)||String(a.turnId).localeCompare(String(b.turnId))).map((turn,index)=>({...turn,index:index+1}));
}
function aggregateTasks(rows) {
const grouped=new Map();
rows.forEach(row=>{
if(!grouped.has(row.taskId)) grouped.set(row.taskId,{taskId:row.taskId,task:row.task,project:row.project,model:row.model,input:0,cached:0,output:0,reasoning:0,total:0,durationSeconds:0,measuredOutput:0,credits:0,rows:[]});
const item=grouped.get(row.taskId); ['input','cached','output','reasoning','total'].forEach(key=>item[key]+=row[key]); item.credits+=creditsForRecord(row); item.rows.push(row);
if(row.durationSeconds>0){item.durationSeconds+=row.durationSeconds;item.measuredOutput+=(row.speedOutput||row.output);}
});
return [...grouped.values()].map(item=>{
const turns=aggregateTaskTurns(item.rows),windowSize=Math.min(5,turns.length),first=turns.slice(0,windowSize),last=turns.slice(-windowSize);
const firstAvgInput=average(first.map(turn=>turn.input)),lastAvgInput=average(last.map(turn=>turn.input));
const contextGrowth=turns.length>=3&&firstAvgInput>0?lastAvgInput/firstAvgInput:null;
const peakTurn=turns.reduce((peak,turn)=>!peak||turn.total>peak.total?turn:peak,null);
const startedAt=turns[0]?.timestamp||null,endedAt=turns.at(-1)?.timestamp||null,startMs=startedAt?Date.parse(startedAt):NaN,endMs=endedAt?Date.parse(endedAt):NaN;
const spanSeconds=Number.isFinite(startMs)&&Number.isFinite(endMs)?Math.max(0,(endMs-startMs)/1000):0;
return {...item,rows:undefined,turns,count:turns.length,eventCount:item.rows.length,speed:item.durationSeconds?item.measuredOutput/item.durationSeconds:null,cacheRate:item.input?item.cached/item.input:0,tokensPerTurn:turns.length?item.total/turns.length:null,firstAvgInput,lastAvgInput,contextGrowth,peakTurn,startedAt,endedAt,spanSeconds,recentTokensPerTurn:average(last.map(turn=>turn.total))};
});
}
function aggregateModels(rows) {
const grouped=new Map();
rows.forEach(row=>{
if(!grouped.has(row.model)) grouped.set(row.model,{model:row.model,input:0,cached:0,output:0,total:0,durationSeconds:0,measuredOutput:0,credits:0,calls:0,taskIds:new Set(),turnIds:new Set()});
const item=grouped.get(row.model); ['input','cached','output','total'].forEach(key=>item[key]+=row[key]); item.calls+=1; item.credits+=creditsForRecord(row); item.taskIds.add(row.taskId); item.turnIds.add(`${row.taskId}:${row.turnId||row.id}`); item.durationSeconds+=row.durationSeconds; item.measuredOutput+=row.durationSeconds>0?(row.speedOutput||row.output):0;
});
return [...grouped.values()].map(item=>({...item,tasks:item.taskIds.size,turns:item.turnIds.size,speed:item.durationSeconds?item.measuredOutput/item.durationSeconds:null,cacheRate:item.input?item.cached/item.input:0,tokensPerTask:item.taskIds.size?item.total/item.taskIds.size:null,tokensPerTurn:item.turnIds.size?item.total/item.turnIds.size:null}));
}
const percentile = (values,p) => { const clean=values.filter(Number.isFinite); if(!clean.length)return 0; const sorted=[...clean].sort((a,b)=>a-b); return sorted[Math.min(sorted.length-1,Math.floor((sorted.length-1)*p))]; };
function sessionBaselines(tasks){
return {
medianTokensPerTurn:percentile(tasks.map(task=>task.tokensPerTurn),.5),
p90TokensPerTurn:percentile(tasks.map(task=>task.tokensPerTurn),.9),
medianGrowth:percentile(tasks.map(task=>task.contextGrowth).filter(Number.isFinite),.5)||1,
p90Growth:percentile(tasks.map(task=>task.contextGrowth).filter(Number.isFinite),.9)||1,
medianTurns:percentile(tasks.map(task=>task.count),.5),
p90Turns:percentile(tasks.map(task=>task.count),.9),
medianCache:percentile(tasks.map(task=>task.cacheRate).filter(Number.isFinite),.5)
};
}
function classifySession(task,baseline){
if(task.count<3||!Number.isFinite(task.contextGrowth))return 'limited';
const highGrowth=task.contextGrowth>=Math.max(3,baseline.medianGrowth*1.8);
const elevatedGrowth=task.contextGrowth>=Math.max(1.7,baseline.medianGrowth*1.25);
const highTurn=task.tokensPerTurn>=Math.max(baseline.p90TokensPerTurn,baseline.medianTokensPerTurn*2);
const long=task.count>=Math.max(30,baseline.p90Turns||30);
if((highGrowth&&(highTurn||long))||task.contextGrowth>=5)return 'heavy';
if(highGrowth||elevatedGrowth)return 'growing';
if(highTurn||long)return 'heavy';
return 'stable';
}
function calculateMetrics(rows,bounds) {
const tasks=aggregateTasks(rows),baseline=sessionBaselines(tasks); tasks.forEach(task=>task.health=classifySession(task,baseline));
const input=sum(rows,'input'),cached=sum(rows,'cached'),total=sum(rows,'total'),days=bounds[0]<=bounds[1]?dateRange(...bounds).length:0,turns=tasks.reduce((n,task)=>n+task.count,0);
return {rows,tasks,models:aggregateModels(rows),sessionBaseline:baseline,total,input,cached,output:sum(rows,'output'),uncached:input-cached,cacheRate:input?cached/input:null,burnRate:days?total/days:null,tokensPerTask:tasks.length?total/tasks.length:null,tokensPerTurn:turns?total/turns:null,taskCount:tasks.length,turns,calls:rows.length,days};
}
function calculateTaskDrivers(tasks) {
const driverLabel=label=>state.language==='zh'?({
'Large Task':'高消耗任务','Long Session':'长 Session','Low Cache':'低缓存命中','Output Heavy':'输出偏高','High Token / Turn':'单轮 Token 偏高','High Context Growth':'Context 增长高','Peak Turn':'峰值轮次','Normal':'稳定'
}[label]||label):label;
const result=new Map(),baseline=sessionBaselines(tasks),p90Total=percentile(tasks.map(t=>t.total),.9),p90Output=percentile(tasks.map(t=>t.total?t.output/t.total:0),.9);
tasks.forEach(task=>{
const labels=[],reasons=[];
if(task.total>=p90Total&&tasks.length>2){labels.push(driverLabel('Large Task'));reasons.push(t('driverTotal',tasks.length<10?'3':'10%'));}
if(task.count>=Math.max(30,baseline.p90Turns||30)){labels.push(driverLabel('Long Session'));reasons.push(t('driverLongSession',task.count));}
if(Number.isFinite(task.contextGrowth)&&task.contextGrowth>=Math.max(2.5,baseline.medianGrowth*1.5)){labels.push(driverLabel('High Context Growth'));reasons.push(t('driverContextGrowth',task.contextGrowth.toFixed(1)));}
if(task.cacheRate<Math.min(.65,baseline.medianCache*.8)){labels.push(driverLabel('Low Cache'));reasons.push(t('driverCacheCompare',formatPercent(task.cacheRate*100),formatPercent(baseline.medianCache*100)));}
if(task.total&&task.output/task.total>=p90Output&&tasks.length>2){labels.push(driverLabel('Output Heavy'));reasons.push(t('driverOutput'));}
if(task.tokensPerTurn>=Math.max(baseline.p90TokensPerTurn,baseline.medianTokensPerTurn*2)&&tasks.length>2){labels.push(driverLabel('High Token / Turn'));reasons.push(t('driverTurn',(task.tokensPerTurn/Math.max(baseline.medianTokensPerTurn,1)).toFixed(1)));}
if(task.peakTurn&&task.tokensPerTurn&&task.peakTurn.total>=task.tokensPerTurn*2.2){labels.push(driverLabel('Peak Turn'));reasons.push(t('driverPeakTurn',task.peakTurn.index,formatTokens(task.peakTurn.total)));}
result.set(task.taskId,{labels:labels.length?labels.slice(0,4):[driverLabel('Normal')],reasons:reasons.length?reasons.slice(0,4):[t('driverNormal')]});
});
return result;
}
function normalizeQuotaWindow(value,fallback){
const hasValue=value&&typeof value==='object';
const hasUsedField=Boolean(hasValue&&Object.prototype.hasOwnProperty.call(value,'used'));
const numericUsed=Number(value?.used);
const hasUsed=hasUsedField&&value?.used!==null&&value?.used!==undefined&&Number.isFinite(numericUsed);
const hasElapsed=value?.elapsed!==null&&value?.elapsed!==undefined&&Number.isFinite(Number(value.elapsed));
const seconds=Number(value?.seconds),elapsed=hasElapsed?Number(value.elapsed):null;
const resetRaw=value?.resetsAt;
const resetMs=typeof resetRaw==='number'?(resetRaw<1e12?resetRaw*1000:resetRaw):Date.parse(resetRaw||'');
const expired=Number.isFinite(resetMs)&&resetMs<=Date.now();
const stale=expired||value?.stale===true||value?.source==='stale';
const liveSeconds=Number.isFinite(resetMs)?Math.max(0,Math.floor((resetMs-Date.now())/1000)):seconds;
return {
...fallback,
...(value||{}),
used:stale?null:(hasUsed?Math.max(0,Math.min(100,numericUsed)):(hasUsedField?null:(fallback?.used??null))),
seconds:stale?0:(Number.isFinite(liveSeconds)?liveSeconds:(fallback?.seconds??0)),
elapsed:elapsed!==null?Math.max(0,Math.min(100,elapsed)):(fallback?.elapsed??null),
source:stale?'stale':(value?.source||fallback?.source||'unknown'),
stale
};
}
function snapshotTime(value){
const raw=value?.observedAt;
const parsed=typeof raw==='number'?(raw<1e12?raw*1000:raw):Date.parse(raw||'');
return Number.isFinite(parsed)?parsed:0;
}
function readStoredQuota(){
try{
const stored=window.localStorage?.getItem(QUOTA_STORAGE_KEY);
return stored?JSON.parse(stored):null;
}catch(error){ return null; }
}
function clearStoredQuota(){
try{ window.localStorage?.removeItem(QUOTA_STORAGE_KEY); }catch(error){ /* Storage may be unavailable. */ }
}
function persistQuotaSnapshot(value,observedAt){
try{
window.localStorage?.setItem(QUOTA_STORAGE_KEY,JSON.stringify({
observedAt:new Date(observedAt||Date.now()).toISOString(),
source:value.source||'live',
five:value.five,
week:value.week
}));
}catch(error){ /* Private browsing or file storage can be unavailable. */ }
}
function loadQuotaScriptSync(){
try{
const request=new XMLHttpRequest();
request.open('GET',`quota-snapshot.local.js?sync=${Date.now()}`,false);
request.send(null);
const ok=(request.status>=200&&request.status<300)||request.status===0;
if(!ok||!request.responseText)return false;
new Function(request.responseText)();
return applyQuotaSnapshot(window.__CODEX_QUOTA_SNAPSHOT__);
}catch(error){ return false; }
}
function applyQuotaSnapshot(value,options={}){
if(!value||typeof value!=='object'||(!value.five&&!value.week)) return false;
const observedAt=snapshotTime(value)||Date.now();
if(!options.force&"aObservedAt&&observedAt<quotaObservedAt)return false;
const next={
five:normalizeQuotaWindow(value.five,quotaSnapshot.five),
week:normalizeQuotaWindow(value.week,quotaSnapshot.week)
};
const unchanged=quotaObservedAt===observedAt&&next.five.used===quotaSnapshot.five.used&&next.week.used===quotaSnapshot.week.used&&next.five.resetsAt===quotaSnapshot.five.resetsAt&&next.week.resetsAt===quotaSnapshot.week.resetsAt&&next.five.source===quotaSnapshot.five.source&&next.week.source===quotaSnapshot.week.source;
quotaSnapshot=next;
quotaObservedAt=observedAt;
quotaRefreshState='updated';
quotaStarted=Date.now();
if(options.persist!==false){
const bothStaleOrMissing=['five','week'].every(key=>['stale','unavailable'].includes(next[key]?.source)||next[key]?.used===null);
if(bothStaleOrMissing)clearStoredQuota();
else persistQuotaSnapshot({source:value.source,five:next.five,week:next.week},observedAt);
}
return !unchanged;
}
function quotaRemainingSeconds(item){
const resetMs=Date.parse(item?.resetsAt||'');
if(Number.isFinite(resetMs))return Math.floor((resetMs-Date.now())/1000);
return Number(item?.seconds||0)-Math.floor((Date.now()-quotaStarted)/1000);
}
function expireQuotaSnapshotsNow(){
let changed=false;
['five','week'].forEach(key=>{
const item=quotaSnapshot[key],resetMs=Date.parse(item?.resetsAt||'');
if(item&&item.source!=='stale'&&item.source!=='unavailable'&&Number.isFinite(resetMs)&&resetMs<=Date.now()){
item.used=null;item.seconds=0;item.source='stale';item.stale=true;changed=true;
}
});
if(changed)clearStoredQuota();
return changed;
}
function updateQuotaCountdown(prefix,item,long=false){
const zh=state.language==='zh',label=$(prefix+'-reset-label'),countdown=$(prefix+'-countdown');
if(item?.source==='stale'||item?.stale){
label.textContent=zh?'窗口已重置 · ':'Window reset · ';
countdown.textContent=zh?'等待新快照':'awaiting fresh snapshot';
return;
}
if(item?.source==='unavailable'||item?.used===null||item?.used===undefined){
label.textContent=zh?'重置时间 ':'Reset ';
countdown.textContent='—';
return;
}
const remaining=quotaRemainingSeconds(item);
if(remaining<=0){
label.textContent=zh?'重置 ':'Resets ';
countdown.textContent=zh?'现在':'now';
return;
}
label.textContent=zh?'重置倒计时 ':'Resets in ';
countdown.textContent=formatCountdown(remaining,long);
}
async function loadQuotaSidecar(){
const scriptUpdated=await new Promise(resolve=>{
const script=document.createElement('script');
script.async=true;
script.src=`quota-snapshot.local.js?ts=${Date.now()}`;
script.onload=()=>resolve(applyQuotaSnapshot(window.__CODEX_QUOTA_SNAPSHOT__));
script.onerror=()=>resolve(false);
document.head.appendChild(script);
window.setTimeout(()=>resolve(false),3000);
});
if(scriptUpdated)return true;
try{
const response=await fetch(`quota-snapshot.local.json?ts=${Date.now()}`,{cache:'no-store'});
if(!response.ok)return false;
return applyQuotaSnapshot(await response.json());
}catch(error){
return loadQuotaScriptSync();
}
}
function quotaPacingInsight(){
const five=quotaSnapshot.five,week=quotaSnapshot.week,zh=state.language==='zh';
const available=item=>item&&item.used!==null&&item.used!==undefined&&Number.isFinite(Number(item.used));
const stale=item=>item?.source==='stale'||item?.stale===true;
if(stale(five)||stale(week)) return {tone:'neutral',title:zh?'额度窗口已重置':'Quota window reset',body:zh?'Ledger 已隐藏过期百分比。刷新只能读取 Codex 已持久化的快照;先使用一次 Codex,再点击刷新即可载入新的额度快照。':'Ledger hid the expired percentage. Refresh can only read snapshots already persisted by Codex; use Codex once, then click Refresh to load the next quota snapshot.'};
if(!available(five)&&!available(week)) return {tone:'neutral',title:zh?'额度快照不可用':'Quota snapshot unavailable',body:zh?'本地 Token 记录不能推算官方 5 小时或每周额度;等待 Codex 日志中出现可观测的 rate-limit 快照。':'Local token records cannot infer official 5-hour or weekly quota. Ledger will use a recorded rate-limit snapshot when Codex persists one.'};
const delta=item=>available(item)&&Number.isFinite(Number(item.elapsed))?Number(item.used)-Number(item.elapsed):null;
const fiveDelta=delta(five),weekDelta=delta(week);
if((fiveDelta!==null&&fiveDelta>10)||(weekDelta!==null&&weekDelta>10)) return {tone:'warning',title:zh?'当前额度消耗偏快':'Usage pace is ahead',body:zh?'至少一个额度窗口的已使用比例明显高于时间进度;建议关注重置前的剩余额度。':'At least one quota window is materially ahead of elapsed time. Keep an eye on the remaining allowance before reset.'};
if((fiveDelta!==null&&fiveDelta<-10)&&(weekDelta===null||weekDelta<=5)) return {tone:'green',title:zh?'当前额度节奏正常':'Usage pace looks healthy',body:zh?'当前已使用比例没有领先于时间进度,暂未看到明显的额度节奏风险。':'Observed usage is not running ahead of elapsed time, so there is no obvious pacing risk right now.'};
return {tone:'neutral',title:zh?'额度节奏接近时间进度':'Usage pace is near elapsed time',body:zh?'当前额度使用与时间进度大致接近;继续结合剩余额度和重置时间观察。':'Observed quota usage is roughly tracking elapsed time. Use the remaining allowance and reset time as the primary guide.'};
}
function renderQuota(){
const zh=state.language==='zh', five=quotaSnapshot.five, week=quotaSnapshot.week;
const available=item=>item&&item.used!==null&&item.used!==undefined&&Number.isFinite(Number(item.used))&&!['unavailable','stale'].includes(item.source);
const stale=item=>item?.source==='stale'||item?.stale===true;
const update=(prefix,item,windowLabel,long=false)=>{
const meter=$(prefix+'-meter'),usedLabel=$(prefix+'-used-label'),badge=$(prefix+'-snapshot-badge'),estimate=$(prefix+'-estimate-copy');
if(stale(item)){
$(prefix+'-used').textContent='—';if(usedLabel)usedLabel.textContent='';
$(prefix+'-usage-copy').textContent=zh?'窗口已重置 · 旧百分比已隐藏':'Window reset · old percentage hidden';
meter.removeAttribute('aria-valuenow');meter.querySelector('i').style.setProperty('--quota','0');
if(badge)badge.textContent=zh?'快照已过期':'Stale snapshot';
if(estimate)estimate.textContent=zh?'先使用一次 Codex,再点击刷新读取新的额度快照':'Use Codex once, then Refresh to read the next quota snapshot';
updateQuotaCountdown(prefix,item,long);return;
}
if(!available(item)){
$(prefix+'-used').textContent='—';if(usedLabel)usedLabel.textContent='';
$(prefix+'-usage-copy').textContent=zh?'未观测到官方额度快照':'Official quota snapshot not observed';
meter.removeAttribute('aria-valuenow');meter.querySelector('i').style.setProperty('--quota','0');
if(badge)badge.textContent=zh?'窗口快照':'Window snapshot';
if(estimate)estimate.textContent=zh?'等待 Codex 写入可观测额度快照':'Waiting for Codex to persist an observed quota snapshot';
updateQuotaCountdown(prefix,item,long);return;
}
const used=Math.round(Number(item.used)), left=Math.max(0,100-used);
$(prefix+'-used').textContent=String(used);if(usedLabel)usedLabel.textContent=zh?'% 已使用':'% used';
$(prefix+'-usage-copy').textContent=zh?`${left}% 剩余`:`${left}% remaining`;
meter.setAttribute('aria-label',zh?`${windowLabel}已使用 ${used}%`:`${windowLabel} ${used}% used`);meter.setAttribute('aria-valuenow',String(used));meter.querySelector('i').style.setProperty('--quota',String(used/100));
if(badge)badge.textContent=zh?'窗口快照':'Window snapshot';
if(estimate)estimate.textContent=zh?'来自本地可观测窗口,不由 Token 总量反推':'Observed locally; never inferred from token totals';
updateQuotaCountdown(prefix,item,long);
};
update('five',five,zh?'五小时额度':'Five-hour quota');update('week',week,zh?'每周额度':'Weekly quota',true);
const live=five.source==='live'||week.source==='live',hasStale=stale(five)||stale(week);
const source=hasStale?(zh?'额度窗口已重置 · 等待新的 Codex 快照':'Quota window reset · waiting for a fresh Codex snapshot'):(quotaRefreshState==='unchanged'?t('quotaUnchanged'):(live?t('quotaFresh'):(IS_LIVE_REPORT?(zh?'本地用量已载入 · 官方额度快照不可用':'Local usage loaded · official quota snapshot unavailable'):t('quotaDemo'))));
$('quota-source').textContent=source;
$('mode-badge').textContent=(live||IS_LIVE_REPORT)?t('liveBadge'):t('demoBadge');
const pacing=quotaPacingInsight();
const advice=$('.quota-advice');
if(advice){advice.classList.toggle('healthy',pacing.tone==='green');const strong=advice.querySelector('strong'),body=advice.querySelector('p');if(strong)strong.textContent=pacing.title;if(body)body.textContent=pacing.body;}
}
function syncRangeControls(){document.querySelectorAll('[data-range]').forEach(button=>{const active=button.dataset.range===state.range;button.classList.toggle('active',active);button.setAttribute('aria-pressed',String(active));});$('custom-range').classList.toggle('visible',state.range==='custom');}
function applyStaticLanguage(){
const zh=state.language==='zh'; document.documentElement.lang=zh?'zh-CN':'en'; document.title=zh?'Ledger · Codex 用量分析':'Ledger · Codex usage analytics';
const bindings=[
['.nav-item[data-target="overview"] span','Overview','总览'],['.nav-item[data-target="models"] span','Models','模型'],['.nav-item[data-target="sessions"] span','Sessions','Session'],['.nav-item[data-target="attribution"] span','Attribution','归因'],
['.privacy-status>span','Processed locally','仅在本机处理'],['.privacy-note p','No conversations, prompts, or API keys are uploaded.','不上传对话、提示词或 API 密钥。'],['.breadcrumb>span','Workspace','工作空间'],['#breadcrumb-current','Usage overview','用量总览'],['#mode-badge','Demo data','演示数据'],['#export-button span','Export','导出视图'],['#refresh-button span','Refresh data','刷新数据'],
['.refresh-eyebrow','Refresh engine','刷新引擎'],['.refresh-popover>strong','Local parser · No model','本地解析器 · 不调用模型'],['.refresh-popover>p','Refresh re-reads local usage logs and recalculates the dashboard. It does not call a model or use Codex quota.','刷新会重新读取本地用量日志并计算看板,不会调用模型,也不会消耗 Codex 额度。'],['#refresh-quota-note','Refresh can only read quota snapshots already persisted by Codex; it cannot force an account sync.','刷新只能读取 Codex 已写入本机的额度快照,不能强制账户侧同步。'],['#refresh-model-note','Starting a new Codex task to run this skill does use the model selected for that task.','重新发起 Codex 任务运行此 Skill 时,会使用该任务当前选择的模型。'],
['#page-title','Usage, at a glance.','用量,一眼清楚。'],['.hero>div>p','See where your Codex usage goes, from account quota to individual model calls.','从账户额度到每一次模型调用,找到消耗发生的位置和原因。'],['.snapshot-status>span','Static snapshot','静态快照'],
['.quota-five .quota-top span','5-hour window','5 小时窗口'],['.quota-five .quota-top small','Account-wide · Observed window','账户全局 · Observed window'],['.quota-five .quota-bottom>span:first-child','27% remaining','剩余 27%'],['#five-reset-label','Resets in ','重置倒计时 '],['#five-estimate-copy','At this pace, the short window may hit its limit before reset','按当前节奏,短窗口可能在重置前触顶'],
['.quota-week .quota-top span','Weekly window','每周窗口'],['.quota-week .quota-top small','Account-wide · Observed window','账户全局 · Observed window'],['.quota-week .quota-bottom>span:first-child','82% remaining','剩余 82%'],['#week-reset-label','Resets in ','重置倒计时 '],['#week-estimate-copy','Projected to use 64% by the end of this cycle','按当前速度预计本周期使用 64%'],
['.quota-advice strong','Short-window pace is high','短窗口使用较快'],['.quota-advice p','The 5-hour window is ahead of elapsed time; weekly pacing remains normal. Quota snapshots are independent of the token filters below.','当前 5h 使用进度高于时间进度;每周窗口节奏仍正常。额度来自账户快照,与下方 Token 筛选独立。'],
['[data-range="today"]','Today','今天'],['[data-range="7d"]','Last 7 days','近 7 天'],['[data-range="30d"]','Last 30 days','近 30 天'],['[data-range="custom"]','Custom','自定义'],['.custom-range span','to','至'],['#reset-filters','Reset','重置'],['.filter-context small','Local time · Local observations','本地时间 · 本地观测'],
['.kpi-rail>div:nth-child(1)>span','Total Tokens · Observed','Token 总量 · Observed'],['.kpi-rail>div:nth-child(2)>span','Burn Rate · Calculated','消耗速率 · Calculated'],['.kpi-rail>div:nth-child(3)>span','Tokens / Task · Calculated','单 Session Token · Calculated'],['.kpi-rail>div:nth-child(4)>span','Tokens / Turn · Calculated','单轮 Token · Calculated'],['.kpi-rail>div:nth-child(5)>span','Cache Hit · Calculated','缓存命中 · Calculated'],
['#insights-title','Usage insights','使用洞察'],['.insights-panel .section-head p','Generated from transparent rules using the current filters. Up to four insights.','由当前筛选数据按透明规则生成,最多显示 4 条。'],['.insights-panel .scope-label','Calculated · Current period','Calculated · 当前周期'],['#session-health-title','Session health','Session 健康度'],['#session-health-copy','Transparent status from context growth, turns, and tokens / turn.','基于 Context 增长、轮数与单轮 Token 的透明状态判断。'],['#session-health-scope','Calculated · Your baseline','Calculated · 你的历史基线'],['#context-watch-title','Context growth watch','Context 增长观察'],['#context-watch-copy','First 5 vs last 5 turns. Click a session for detail.','对比前 5 轮与后 5 轮;点击 Session 查看明细。'],
['#model-title','Model quota intensity & token usage','模型额度消耗与 Token 使用'],['.model-section-head>div:first-child p','Compare estimated quota intensity or generation speed on the left, observed token volume on the right.','左侧对比估算额度强度或生成速度;右侧对比可观测 Token 总量。'],['.formula-chip span','Intensity metric','强度口径'],['.formula-chip strong','Estimated credits ÷ model turns','估算 Credits ÷ 模型调用次数'],['.speed-panel .rank-title>div>span','Quota & performance','额度与性能'],['[data-left-metric="burn"]','Quota intensity','额度强度'],['[data-left-metric="speed"]','Generation speed','生成速度'],['.volume-panel .rank-title>div>span','Usage','消耗'],['#volume-title','Token volume','总量排名'],
['#intensity-footnote','Quota intensity is shown only when a built-in rate mapping is available','仅在存在内置费率映射时显示额度强度估算'],['#speed-footnote','Generation speed is output token/s; durations are synthetic in this demo','生成速度为输出 token/s;当前持续时间为合成数据'],
['.trend-panel h2','Token timeline','Token 时间线'],['.trend-panel .section-head p','Hover to compare the matching day in the previous period; click to drill down.','悬停查看上一周期对应日;点击日期下钻。'],['#legend-cached','Cached input','缓存输入'],['#legend-uncached','Uncached input','非缓存输入'],['#legend-output','Output','输出'],['.chart-caption span:last-child','Input + output = total','输入 + 输出 = 总量'],
['.composition-panel h2','Token composition','Token 构成'],['.composition-panel .section-head p','Current filter range','当前筛选范围'],['.panel-note','Reasoning tokens are a subset of output and are not counted twice.','推理 Token 是输出的子集,不重复计入总量。'],
['.matrix-panel h2','Model × project','模型 × 项目'],['.matrix-panel .section-head p','Darker cells indicate more concentrated token usage.','颜色越深,Token 消耗越集中。'],['.matrix-panel .scope-label','Current period · All projects','当前时间范围 · 所有项目'],['.outlier-panel h2','Task efficiency map','任务效率分布'],['.outlier-panel .section-head p','Total tokens × cache hit rate; only high-usage outliers are labelled.','总量 × 缓存命中率;只标注高消耗离群任务。'],['.outlier-panel .scope-label','Point area = turns','点面积 = Turns'],
['.projects-panel h2','Project distribution','项目分布'],['.projects-panel .section-head p','Click a project to filter every detail.','点击项目联动全部明细。'],['.expensive-panel h2','Top expensive tasks','高消耗任务'],['.expensive-panel .scope-label','Calculated · Click for drivers','Calculated · 点击查看原因'],['#task-details-title','Task details','任务明细'],['.tasks-panel .section-head p','Open any task to inspect its consumption drivers and turns.','点击任意任务查看消耗特征与逐次调用。'],
['th:nth-child(1)','Task / Project','任务 / 项目'],['th:nth-child(2)','Model','模型'],['th:nth-child(4)','Tokens / Turn','单轮 Token'],['th:nth-child(5)','Context growth','Context 增长'],['th:nth-child(6)','Cache','缓存命中'],['th:nth-child(7)','Health','状态'],['#previous-page','Previous','上一页'],['#next-page','Next','下一页'],
['.page-footer>span','Ledger · Independent community tool','Ledger · 独立社区工具'],['.page-footer div>span','Local records ≠ complete account billing','本地记录 ≠ 账户全量账单']
];
bindings.forEach(([selector,en,cn])=>{const element=document.querySelector(selector);if(element)element.textContent=zh?cn:en;});
const snapshotStatus=document.querySelector('.snapshot-status>span'),refreshStrong=document.querySelector('.refresh-popover>strong'),refreshBody=document.querySelector('.refresh-popover>p');
if(snapshotStatus)snapshotStatus.textContent=!IS_LIVE_REPORT?(zh?'演示快照':'Demo snapshot'):(typeof window.ledgerRefreshAdapter==='function'?(zh?'实时本地数据':'Live local data'):(zh?'静态本地快照':'Static local snapshot'));
if(!IS_LIVE_REPORT){
if(refreshStrong)refreshStrong.textContent=zh?'演示预览':'Demo preview';
if(refreshBody)refreshBody.textContent=zh?'当前页面使用合成演示数据。要读取并刷新你本机的 Codex 数据,请运行 python3 scripts/ledger.py --open。':'This page uses synthetic demo data. To read and refresh your local Codex data, run python3 scripts/ledger.py --open.';
}else if(typeof window.ledgerRefreshAdapter!=='function'){
if(refreshStrong)refreshStrong.textContent=zh?'静态本地快照':'Static local snapshot';
if(refreshBody)refreshBody.textContent=zh?'当前快照不会从浏览器直接重新读取本机日志。请用 --open 或 --serve 启动 Ledger,即可启用实时刷新。':'This static snapshot cannot rescan local logs from the browser. Start Ledger with --open or --serve to enable live refresh.';
}
const ariaBindings=[
['.sidebar','Primary navigation','主导航'],['.brand','Ledger home','Ledger 首页'],['.language-switch','Language','语言'],
['.quota-layout','Account quota snapshot','账户额度快照'],['.quota-five .quota-meter','Five-hour quota 73% used','五小时额度已使用 73%'],['.quota-week .quota-meter','Weekly quota 18% used','每周额度已使用 18%'],
['.filter-dock','Data filters','数据筛选'],['.segmented','Time range','时间范围'],['#start-date','Start date','开始日期'],['#end-date','End date','结束日期'],['.kpi-rail','Current range summary','当前范围摘要'],
['.metric-switch','Ranking metric','排名指标'],['.analytics-grid','Timeline and composition','时间趋势与构成'],['#timeline-chart','Daily stacked token usage chart','按日 Token 消耗堆叠柱状图'],
['.session-grid','Session health and context growth','Session 健康与 Context 增长'],['.diagnostics-grid','Cross-analysis and task outliers','交叉分析与异常任务'],['#outlier-chart','Task token volume and cache-hit scatter plot','任务 Token 总量与缓存命中率散点图'],['.attribution-grid','Usage attribution','消耗归因'],['.tasks-panel','Task details','任务明细']
];
ariaBindings.forEach(([selector,en,cn])=>{const element=document.querySelector(selector);if(element)element.setAttribute('aria-label',zh?cn:en);});
$('task-search').placeholder=zh?'搜索任务或模型':'Search tasks or models'; $('task-search').setAttribute('aria-label',$('task-search').placeholder); $('theme-toggle').setAttribute('aria-label',zh?'切换明暗主题':'Toggle theme'); $('export-button').setAttribute('aria-label',zh?'导出看板图片':'Export dashboard image'); $('export-button').title=zh?'导出看板图片':'Export dashboard image'; $('refresh-button').setAttribute('aria-label',zh?'刷新数据':'Refresh data'); $('refresh-info').setAttribute('aria-label',zh?'刷新如何工作':'How refresh works'); $('drawer-close').setAttribute('aria-label',zh?'关闭详情':'Close details'); $('project-filter').setAttribute('aria-label',zh?'筛选项目':'Filter by project'); $('task-filter').setAttribute('aria-label',zh?'筛选任务':'Filter by task'); $('model-filter').setAttribute('aria-label',zh?'筛选模型':'Filter by model'); $('previous-page').setAttribute('aria-label',zh?'上一页':'Previous page'); $('next-page').setAttribute('aria-label',zh?'下一页':'Next page'); const matrixGroup=document.querySelector('.matrix-view-switch'); if(matrixGroup)matrixGroup.setAttribute('aria-label',zh?'矩阵显示范围':'Matrix view');
$('project-filter').options[0].textContent=t('allProjects'); $('model-filter').options[0].textContent=t('allModels'); $('motion-toggle').textContent=document.body.classList.contains('reduce-motion')?t('restoreMotion'):t('reduceMotion'); renderQuota();
document.querySelectorAll('[data-language]').forEach(button=>{const active=button.dataset.language===state.language;button.classList.toggle('active',active);button.setAttribute('aria-pressed',String(active));});
}
function populateTaskOptions(){const select=$('task-filter'),previous=state.task;select.innerHTML=`<option value="all">${t('allTasks')}</option>`;const visibleIds=new Set(records.filter(row=>(state.project==='all'||row.project===state.project)&&(state.model==='all'||row.model===state.model)).map(row=>row.taskId));taskCatalog.filter(task=>visibleIds.has(task.id)).sort((a,b)=>a.name.localeCompare(b.name)).forEach(task=>select.add(new Option(task.name,task.id)));state.task=[...select.options].some(option=>option.value===previous)?previous:'all';select.value=state.task;}
function renderSummary(){
const totalChange=changeRate(currentMetrics.total,previousMetrics.total),burnChange=changeRate(currentMetrics.burnRate||0,previousMetrics.burnRate||0),taskChange=changeRate(currentMetrics.tokensPerTask||0,previousMetrics.tokensPerTask||0),turnChange=changeRate(currentMetrics.tokensPerTurn||0,previousMetrics.tokensPerTurn||0),cacheChange=changeRate(currentMetrics.cacheRate||0,previousMetrics.cacheRate||0);
$('total-tokens').textContent=formatTokens(currentMetrics.total); $('total-detail').innerHTML=`${formatChange(totalChange)} · ${formatNumber(currentMetrics.total)} tokens`;
$('burn-rate').textContent=currentMetrics.burnRate?t('perDay',formatTokens(currentMetrics.burnRate)):'—'; $('burn-detail').innerHTML=`${formatChange(burnChange)} · ${t('dayAverage',currentMetrics.days)}`;
$('tokens-task').textContent=currentMetrics.tokensPerTask?formatTokens(currentMetrics.tokensPerTask):'—'; $('tokens-task-detail').innerHTML=`${formatChange(taskChange)} · ${t('tasksTurns',currentMetrics.taskCount,currentMetrics.turns)}`;
$('tokens-turn').textContent=currentMetrics.tokensPerTurn?formatTokens(currentMetrics.tokensPerTurn):'—'; $('tokens-turn-detail').innerHTML=`${formatChange(turnChange)} · ${formatNumber(currentMetrics.turns)} ${state.language==='zh'?'轮':'turns'}`;
$('cache-rate').textContent=currentMetrics.cacheRate!==null?formatPercent(currentMetrics.cacheRate*100):'—'; $('cache-detail').innerHTML=`${formatChange(cacheChange)} · ${t('cached',formatTokens(currentMetrics.cached))}`;
const [start,end]=rangeBounds(),task=taskCatalog.find(item=>item.id===state.task);
$('filter-summary').textContent=`${start.replaceAll('-','.')} — ${end.replaceAll('-','.')} · ${state.project==='all'?t('allProjects'):state.project}${state.model==='all'?'':` · ${shortModel(state.model)}`}${task?` · ${task.name}`:''}`;
}
function calculateInsights(){
const eyebrow=state.language==='zh'?{quota:'额度 / 节奏',context:'Session / Context',turn:'单轮效率',growth:'项目增长',task:'高消耗任务',model:'模型集中度',cache:'缓存'}:{quota:'Quota / Pacing',context:'Session / Context',turn:'Per-turn usage',growth:'Project Growth',task:'Expensive Task',model:'Model Concentration',cache:'Cache'};
const insights=[];
const pacing=quotaPacingInsight(); if(quotaSnapshot.five.source!=='unavailable'||quotaSnapshot.week.source!=='unavailable')insights.push({tone:pacing.tone,eyebrow:eyebrow.quota,title:pacing.title,body:pacing.body});
const growthSessions=currentMetrics.tasks.filter(task=>Number.isFinite(task.contextGrowth)&&task.contextGrowth>=2.5).sort((a,b)=>b.contextGrowth-a.contextGrowth);
if(growthSessions.length){insights.push({tone:'orange',eyebrow:eyebrow.context,title:t('contextInsightTitle'),body:t('contextInsightBody',growthSessions.length,growthSessions[0].contextGrowth.toFixed(1))});}
const turnChange=changeRate(currentMetrics.tokensPerTurn||0,previousMetrics.tokensPerTurn||0);
if(Number.isFinite(turnChange)&&Math.abs(turnChange)>.18){const up=turnChange>0;insights.push({tone:up?'blue':'green',eyebrow:eyebrow.turn,title:t(up?'turnRateUp':'turnRateDown'),body:t('turnRateBody',Math.abs(turnChange*100).toFixed(1),formatTokens(currentMetrics.tokensPerTurn))});}
const projects=PROJECTS.map(project=>{const current=sum(filteredRecords.filter(r=>r.project===project),'total'),previous=sum(previousRecords.filter(r=>r.project===project),'total');return{project,current,previous,change:changeRate(current,previous)};}).filter(item=>item.current>0);
const growth=projects.filter(item=>Number.isFinite(item.change)&&item.change>.25).sort((a,b)=>b.change-a.change)[0];
if(growth){const rows=filteredRecords.filter(r=>r.project===growth.project),top=aggregateModels(rows).sort((a,b)=>b.total-a.total)[0];insights.push({tone:'orange',eyebrow:eyebrow.growth,title:t('growthTitle',growth.project),body:t('growthBody',(growth.change*100).toFixed(1),shortModel(top.model))});}
const top=[...currentMetrics.tasks].sort((a,b)=>b.total-a.total)[0];
if(top&¤tMetrics.total&&top.total/currentMetrics.total>.11) insights.push({tone:'blue',eyebrow:eyebrow.task,title:t('expensiveTitle'),body:t('expensiveBody',top.task,formatTokens(top.total),formatPercent(top.total/currentMetrics.total*100))});
const model=[...currentMetrics.models].sort((a,b)=>b.total-a.total)[0];
if(model&¤tMetrics.total&&model.total/currentMetrics.total>.32) insights.push({tone:'neutral',eyebrow:eyebrow.model,title:t('concentrationTitle',shortModel(model.model)),body:t('concentrationBody',formatPercent(model.total/currentMetrics.total*100))});
if(currentMetrics.cacheRate!==null&&previousMetrics.cacheRate!==null&&Math.abs(currentMetrics.cacheRate-previousMetrics.cacheRate)>.035){const up=currentMetrics.cacheRate>previousMetrics.cacheRate;insights.push({tone:up?'green':'neutral',eyebrow:eyebrow.cache,title:t(up?'cacheUp':'cacheDown'),body:t('cacheBody',formatPercent(previousMetrics.cacheRate*100),t(up?'rose':'fell'),formatPercent(currentMetrics.cacheRate*100))});}
return insights.slice(0,4);
}
function renderInsights(){const items=calculateInsights();$('usage-insights').innerHTML=items.length?items.map(item=>`<article class="usage-insight ${item.tone}"><span>${item.eyebrow}</span><strong>${escapeHTML(item.title)}</strong><p>${escapeHTML(item.body)}</p></article>`).join(''):`<div class="empty-state">${t('noAnomaly')}</div>`;}
function healthLabel(status){return t(status==='stable'?'sessionStable':status==='growing'?'sessionGrowing':status==='heavy'?'sessionHeavy':'sessionLimited');}
function renderSessionHealth(){
const tasks=currentMetrics.tasks,baseline=currentMetrics.sessionBaseline||sessionBaselines(tasks),counts={stable:0,growing:0,heavy:0,limited:0};tasks.forEach(task=>counts[task.health||'limited']+=1);
const stats=[['stable',counts.stable],['growing',counts.growing],['heavy',counts.heavy],['limited',counts.limited]];
$('session-health-summary').innerHTML=stats.map(([key,value])=>`<div class="health-stat ${key}"><span>${escapeHTML(healthLabel(key))}</span><strong>${value}</strong></div>`).join('');
const baselineRows=state.language==='zh'?[
['你的单轮中位数',formatTokens(baseline.medianTokensPerTurn)],['你的 Context 增长中位数',`${(baseline.medianGrowth||1).toFixed(1)}×`],['你的 Session 轮数中位数',formatNumber(baseline.medianTurns)]
]:[['Your median tokens / turn',formatTokens(baseline.medianTokensPerTurn)],['Your median context growth',`${(baseline.medianGrowth||1).toFixed(1)}×`],['Your median session turns',formatNumber(baseline.medianTurns)]];
$('session-baseline-summary').innerHTML=baselineRows.map(([label,value])=>`<div class="baseline-row"><span>${label}</span><strong>${value}</strong></div>`).join('');
const watch=[...tasks].filter(task=>Number.isFinite(task.contextGrowth)).sort((a,b)=>b.contextGrowth-a.contextGrowth).slice(0,5),maxGrowth=Math.max(...watch.map(task=>task.contextGrowth),1);
$('context-watch-list').innerHTML=watch.length?watch.map(task=>`<button class="context-watch-item" data-task="${task.taskId}" title="${escapeHTML(task.task)}"><span class="context-watch-copy"><strong>${escapeHTML(task.task)}</strong><small>${escapeHTML(task.project)} · ${escapeHTML(shortModel(task.model))} · ${task.count} ${state.language==='zh'?'轮':'turns'}</small><span class="context-growth-meter"><i style="--growth:${Math.min(1,task.contextGrowth/maxGrowth)}"></i></span></span><span class="context-watch-value"><strong>${task.contextGrowth.toFixed(1)}×</strong><small class="health-chip ${task.health}">${escapeHTML(healthLabel(task.health))}</small></span></button>`).join(''):`<div class="empty-state">${state.language==='zh'?'当前筛选范围暂无足够的 Session 数据。':'Not enough session data in the current filters.'}</div>`;
$('context-watch-list').querySelectorAll('[data-task]').forEach(button=>button.addEventListener('click',()=>openTask(button.dataset.task)));
}
function rankRows(items,type,maxValue,grandTotal,previousMap){
if(!items.length)return `<div class="rank-empty">${t('noData')}</div>`;
return items.map((item,index)=>{
const isBurn=type==='burn',isSpeed=type==='speed'; const value=isBurn?(item.turns?item.credits/item.turns:0):isSpeed?item.speed:item.total; const width=maxValue?value/maxValue:0; const previous=previousMap.get(item.model); const change=changeRate(item.total,previous?previous.total:0); let valueText,detail;
if(isBurn){const rate=CREDIT_RATES[item.model],base=CREDIT_RATES['gpt-5.6-luna'];if(rate&&base){const relative=rate.output/base.output;valueText=`${value.toFixed(2)}<small> cr/turn</small>`;detail=t('estimatedRate',item.credits.toFixed(1),relative.toFixed(1));}else{valueText=`—<small> rate</small>`;detail=state.language==='zh'?'当前模型暂无费率映射':'No rate mapping for this model';}}
else if(isSpeed){valueText=`${item.speed.toFixed(1)}<small> token/s</small>`;detail=t('turnsOutput',item.turns,formatTokens(item.output));}
else {valueText=`${formatTokens(item.total)}<small> tokens</small>`;detail=`${grandTotal?formatPercent(item.total/grandTotal*100):'0%'} · ${formatChange(change,'')}`;}
return `<div class="rank-row"><div class="rank-meta"><span class="rank-index">${String(index+1).padStart(2,'0')}</span><span class="rank-model">${escapeHTML(shortModel(item.model))}</span><strong class="rank-value">${valueText}</strong></div><div class="rank-bar"><i style="--bar:${width};--delay:${index*55}ms"></i></div><div class="rank-detail">${detail}${isBurn||isSpeed?` · ${formatChange(change,'')}`:''}</div></div>`;
}).join('');
}
function renderModels(){
const models=currentMetrics.models,previousMap=new Map(previousMetrics.models.map(item=>[item.model,item])),grandTotal=currentMetrics.total;
const byBurn=[...models].filter(m=>hasCreditRate(m.model)).sort((a,b)=>(b.turns?b.credits/b.turns:0)-(a.turns?a.credits/a.turns:0)),byCredits=[...models].filter(m=>hasCreditRate(m.model)).sort((a,b)=>b.credits-a.credits),bySpeed=[...models].filter(m=>m.speed!==null).sort((a,b)=>b.speed-a.speed),byVolume=[...models].sort((a,b)=>b.total-a.total),byCache=[...models].filter(m=>m.input).sort((a,b)=>b.cacheRate-a.cacheRate);
const labels=state.language==='zh'?['单次额度强度最高','估算额度总量最高','输出生成最快','缓存效率最高']:['Highest per-turn intensity','Highest estimated credits','Fastest generation','Highest cache hit'];
const cards=[[labels[0],byBurn[0],byBurn[0]?`${(byBurn[0].credits/byBurn[0].calls).toFixed(2)} cr/turn`:''],[labels[1],byCredits[0],byCredits[0]?`${byCredits[0].credits.toFixed(1)} cr`:''],[labels[2],bySpeed[0],bySpeed[0]?`${bySpeed[0].speed.toFixed(1)} token/s`:''],[labels[3],byCache[0],byCache[0]?formatPercent(byCache[0].cacheRate*100):'']];
$('model-insights').innerHTML=cards.map(([label,item,detail])=>`<div class="insight-item"><span>${label}</span><strong>${item?escapeHTML(shortModel(item.model)):'—'}<b>${detail}</b></strong></div>`).join('');
const left=state.leftMetric==='burn'?byBurn:bySpeed,maxLeft=Math.max(...left.map(item=>state.leftMetric==='burn'?(item.turns?item.credits/item.turns:0):item.speed),1);
$('left-ranking').innerHTML=rankRows(left,state.leftMetric,maxLeft,grandTotal,previousMap); $('volume-ranking').innerHTML=rankRows(byVolume,'volume',Math.max(...byVolume.map(i=>i.total),1),grandTotal,previousMap); $('left-rank-title').textContent=state.leftMetric==='burn'?t('quotaIntensity'):t('speedRanking');
document.querySelectorAll('[data-left-metric]').forEach(button=>{const active=button.dataset.leftMetric===state.leftMetric;button.classList.toggle('active',active);button.setAttribute('aria-pressed',String(active));});
}
function renderTimeline(){
const bounds=rangeBounds(),previousBounds=calculatePreviousPeriod(bounds),dates=bounds[0]<=bounds[1]?dateRange(...bounds):[],previousDates=previousBounds[0]<=previousBounds[1]?dateRange(...previousBounds):[];
const values=dates.map((date,index)=>{const rows=filteredRecords.filter(r=>r.date===date),input=sum(rows,'input'),output=sum(rows,'output'),cached=sum(rows,'cached'),previous=sum(previousRecords.filter(r=>r.date===previousDates[index]),'total');return{date,cached,uncached:input-cached,output,total:input+output,previous,previousDate:previousDates[index]};});
const max=Math.max(...values.map(v=>v.total),1)*1.12,left=54,right=806,baseline=222,height=185,step=(right-left)/Math.max(values.length,1),barWidth=Math.min(48,step*.58); let svg='';
for(let i=0;i<4;i+=1){const y=baseline-i*height/3;svg+=`<line class="chart-grid" x1="${left}" x2="${right}" y1="${y}" y2="${y}"/><text class="chart-axis" x="42" y="${y+3}" text-anchor="end">${formatTokens(max*i/3)}</text>`;}
if(!filteredRecords.length)svg+=`<text class="chart-axis" x="430" y="126" text-anchor="middle">${t('noData')}</text>`;
values.forEach((value,index)=>{const x=left+step*(index+.5)-barWidth/2;let y=baseline;svg+=`<g class="timeline-group" data-date="${value.date}" tabindex="0" role="button">`;[['cached','#0a84ff'],['uncached','#64d2ff'],['output','#ff9f0a']].forEach(([key,color],stack)=>{const h=value[key]/max*height;y-=h;svg+=`<rect x="${x}" y="${y}" width="${barWidth}" height="${Math.max(0,h)}" fill="${color}" rx="${stack===2?2:0}"/>`;});svg+=`<rect x="${x-5}" y="${baseline-height-8}" width="${barWidth+10}" height="${height+12}" fill="transparent"/></g>`;if(values.length<=8||index%Math.ceil(values.length/7)===0||index===values.length-1)svg+=`<text class="chart-axis" x="${x+barWidth/2}" y="248" text-anchor="middle">${value.date.slice(5).replace('-','/')}</text>`;});
$('timeline-chart').innerHTML=svg;$('timeline-range').textContent=bounds[0]>bounds[1]?t('invalidDates'):`${bounds[0].slice(5)} — ${bounds[1].slice(5)} · ${t('compare')} ${previousBounds[0].slice(5)} — ${previousBounds[1].slice(5)}`;
$('timeline-chart').querySelectorAll('[data-date]').forEach(element=>{const value=values.find(v=>v.date===element.dataset.date);const select=()=>{state.range='custom';state.start=value.date;state.end=value.date;state.page=0;$('start-date').value=value.date;$('end-date').value=value.date;syncRangeControls();render();};element.addEventListener('click',select);element.addEventListener('keydown',e=>{if(e.key==='Enter'||e.key===' '){e.preventDefault();select();}});element.addEventListener('pointermove',event=>{const frame=element.closest('.chart-frame').getBoundingClientRect(),change=changeRate(value.total,value.previous),tooltip=$('timeline-tooltip');tooltip.innerHTML=`${value.date}<br><strong>${formatTokens(value.total)}</strong> tokens<br><span>${t('previousDay')} ${value.previousDate} · ${formatTokens(value.previous)}</span><br>${formatChange(change,'')}`;tooltip.style.display='block';tooltip.style.left=`${Math.min(event.clientX-frame.left+12,frame.width-205)}px`;tooltip.style.top=`${Math.max(0,event.clientY-frame.top-86)}px`;});element.addEventListener('pointerleave',()=>{$('timeline-tooltip').style.display='none';});});
}
function renderComposition(){const {input,cached,output,total}=currentMetrics,parts=[[t('cachedInput'),cached,'#0a84ff'],[t('uncachedInput'),input-cached,'#64d2ff'],[t('output'),output,'#ff9f0a']];$('composition-total').innerHTML=`${formatTokens(total)}<small>tokens</small>`;$('composition-track').innerHTML=parts.map(p=>`<i style="width:${total?p[1]/total*100:0}%;background:${p[2]}"></i>`).join('');$('composition-list').innerHTML=parts.map(p=>`<div class="composition-row"><i style="background:${p[2]}"></i><span>${p[0]}</span><strong>${formatTokens(p[1])}<small>${total?formatPercent(p[1]/total*100):'0%'}</small></strong></div>`).join('');}
function renderHeatmap(){
const projectTotals=new Map(),modelTotals=new Map(),pairTotals=new Map();
filteredRecords.forEach(row=>{
projectTotals.set(row.project,(projectTotals.get(row.project)||0)+row.total);
modelTotals.set(row.model,(modelTotals.get(row.model)||0)+row.total);
const key=`${row.project}\u0000${row.model}`;
pairTotals.set(key,(pairTotals.get(key)||0)+row.total);
});
const allProjects=[...projectTotals.entries()].filter(([,total])=>total>0).sort((a,b)=>b[1]-a[1]).map(([name])=>name);
const allModels=[...modelTotals.entries()].filter(([,total])=>total>0).sort((a,b)=>b[1]-a[1]).map(([name])=>name);
const projects=state.matrixExpanded?allProjects:allProjects.slice(0,6);
const models=state.matrixExpanded?allModels:allModels.slice(0,5);
const container=$('model-project-heatmap'),scroll=$('heatmap-scroll'),compactButton=$('matrix-compact'),allButton=$('matrix-all');
compactButton.textContent=t('matrixCompact');allButton.textContent=t('matrixAll');
compactButton.classList.toggle('active',!state.matrixExpanded);allButton.classList.toggle('active',state.matrixExpanded);
compactButton.setAttribute('aria-pressed',String(!state.matrixExpanded));allButton.setAttribute('aria-pressed',String(state.matrixExpanded));
scroll.classList.toggle('expanded',state.matrixExpanded);
$('matrix-scope').textContent=state.matrixExpanded?t('matrixAllScope',allProjects.length,allModels.length):t('matrixCompactScope',projects.length,models.length);
if(!projects.length||!models.length){container.removeAttribute('style');container.innerHTML=`<div class="matrix-empty">${t('matrixEmpty')}</div>`;return;}
const max=Math.max(...projects.flatMap(project=>models.map(model=>pairTotals.get(`${project}\u0000${model}`)||0)),1);
const labelWidth=state.matrixExpanded?156:142,cellMin=state.matrixExpanded?94:78,minWidth=labelWidth+models.length*cellMin+(models.length*6);
container.style.setProperty('--matrix-cols',models.length);container.style.setProperty('--matrix-label-width',`${labelWidth}px`);container.style.setProperty('--matrix-cell-min',`${cellMin}px`);container.style.minWidth=`${Math.max(520,minWidth)}px`;
let html='<div class="heatmap-corner" aria-hidden="true"></div>';
models.forEach(model=>html+=`<div class="heatmap-label heatmap-column" title="${escapeHTML(model)}">${escapeHTML(truncateMiddle(shortModel(model),18))}</div>`);
projects.forEach(project=>{
html+=`<div class="heatmap-label heatmap-row" title="${escapeHTML(project)}">${escapeHTML(truncateMiddle(project,22))}</div>`;
models.forEach(model=>{
const total=pairTotals.get(`${project}\u0000${model}`)||0,intensity=total?12+total/max*68:0,title=`${project} · ${model} · ${formatNumber(total)} tokens`;
html+=total?`<button class="heat-cell" data-project="${escapeHTML(project)}" data-model="${escapeHTML(model)}" style="background:color-mix(in srgb,var(--blue) ${intensity.toFixed(1)}%,var(--surface-soft))" title="${escapeHTML(title)}" aria-label="${escapeHTML(title)}">${formatTokens(total)}</button>`:`<span class="heat-cell is-empty" title="${escapeHTML(project)} · ${escapeHTML(model)} · ${t('noUsage')}" aria-label="${escapeHTML(project)} · ${escapeHTML(model)} · ${t('noUsage')}"></span>`;
});
});
container.innerHTML=html;
container.querySelectorAll('button[data-project][data-model]').forEach(button=>button.addEventListener('click',()=>{state.project=button.dataset.project;state.model=button.dataset.model;state.task='all';state.page=0;$('project-filter').value=state.project;$('model-filter').value=state.model;populateTaskOptions();render();}));
}
function renderOutliers(tasks){
const max=Math.max(...tasks.map(t=>t.total),1)*1.08,left=44,right=500,top=24,bottom=212,width=right-left,height=bottom-top,minCache=.4,maxCache=1;let svg='';
for(let i=0;i<4;i+=1){const x=left+i*width/3;svg+=`<line class="chart-grid" x1="${x}" x2="${x}" y1="${top}" y2="${bottom}"/><text class="outlier-axis" x="${x}" y="235" text-anchor="middle">${formatTokens(max*i/3)}</text>`;}[.5,.7,.9].forEach(rate=>{const y=bottom-(rate-minCache)/(maxCache-minCache)*height;svg+=`<line class="chart-grid" x1="${left}" x2="${right}" y1="${y}" y2="${y}"/><text class="outlier-axis" x="36" y="${y+3}" text-anchor="end">${Math.round(rate*100)}%</text>`;});
if(!tasks.length)svg+=`<text class="outlier-axis" x="270" y="125" text-anchor="middle">${t('noData')}</text>`;
const threshold=percentile(tasks.map(t=>t.total),.9),labeled=tasks.filter(t=>t.total>=threshold).sort((a,b)=>b.total-a.total).slice(0,3).map(t=>t.taskId);
tasks.forEach(task=>{const x=left+task.total/max*width,y=bottom-Math.max(0,Math.min(1,(task.cacheRate-minCache)/(maxCache-minCache)))*height,radius=Math.min(12,4+Math.sqrt(task.count)*.75);svg+=`<g class="outlier-point" data-task="${task.taskId}" tabindex="0" role="button"><circle style="fill:${modelColor(task.model)}99;stroke:${modelColor(task.model)}" cx="${x}" cy="${y}" r="${radius}"/></g>`;if(labeled.includes(task.taskId)){const placeLeft=x>410,labelX=placeLeft?x-radius-5:x+radius+5,labelY=Math.max(top+10,Math.min(bottom-4,y+3));svg+=`<text class="outlier-label" x="${labelX}" y="${labelY}" text-anchor="${placeLeft?'end':'start'}">${escapeHTML(truncateMiddle(task.task,16))}</text>`;}});$('outlier-chart').innerHTML=svg;
$('outlier-chart').querySelectorAll('[data-task]').forEach(element=>{const task=tasks.find(t=>t.taskId===element.dataset.task),open=()=>openTask(task.taskId);element.addEventListener('click',open);element.addEventListener('keydown',e=>{if(e.key==='Enter'||e.key===' '){e.preventDefault();open();}});element.addEventListener('pointermove',event=>{const frame=$('outlier-chart').closest('.outlier-frame').getBoundingClientRect(),tip=$('outlier-tooltip'),leftPos=Math.min(event.clientX-frame.left+12,frame.width-272),topPos=Math.max(8,Math.min(event.clientY-frame.top-108,frame.height-126));tip.innerHTML=`<div class="task-tooltip-title">${escapeHTML(task.task)}</div><div class="task-tooltip-meta">${escapeHTML(task.project)} · ${escapeHTML(shortModel(task.model))}</div><div class="task-tooltip-grid"><span>Tokens <strong>${formatTokens(task.total)}</strong></span><span>/ turn <strong>${formatTokens(task.tokensPerTurn)}</strong></span><span>Cache <strong>${formatPercent(task.cacheRate*100)}</strong></span><span>Turns <strong>${task.count}</strong></span></div>`;tip.style.display='block';tip.style.left=`${Math.max(8,leftPos)}px`;tip.style.top=`${topPos}px`;});element.addEventListener('pointerleave',()=>{$('outlier-tooltip').style.display='none';});});
}
function renderProjects(){
const previousMap=new Map(PROJECTS.map(project=>[project,sum(previousRecords.filter(r=>r.project===project),'total')]));
const values=PROJECTS
.map(project=>({project,total:sum(filteredRecords.filter(r=>r.project===project),'total')}))
.filter(item=>item.total>0)
.sort((a,b)=>b.total-a.total);
const grand=values.reduce((total,item)=>total+item.total,0);
const formatProjectShare=value=>value>0&&value<.1?'<0.1%':formatPercent(value);
$('project-count').textContent=t('projects',values.length);
$('project-list').innerHTML=values.length?values.map(item=>{
const change=changeRate(item.total,previousMap.get(item.project)||0),share=grand?item.total/grand*100:0;
return `<button class="project-item ${state.project===item.project?'selected':''}" data-project="${escapeHTML(item.project)}" title="${escapeHTML(item.project)}"><div class="project-line"><span class="project-name">${escapeHTML(item.project)}</span><strong class="project-value">${formatTokens(item.total)}</strong></div><div class="project-sub"><span>${formatProjectShare(share)}</span>${formatChange(change,'')}</div><div class="project-meter"><i style="--share:${grand?item.total/grand:0}"></i></div></button>`;
}).join(''):`<div class="empty-state">${t('noProjectData')}</div>`;
$('project-list').querySelectorAll('[data-project]').forEach(button=>button.addEventListener('click',()=>{state.project=state.project===button.dataset.project?'all':button.dataset.project;state.task='all';state.page=0;$('project-filter').value=state.project;populateTaskOptions();render();}));
}
function renderTopTasks(tasks){const top=[...tasks].sort((a,b)=>b.total-a.total).slice(0,5),topThree=sum(top.slice(0,3),'total');$('top-task-summary').textContent=top.length?t('topShare',formatPercent(currentMetrics.total?topThree/currentMetrics.total*100:0)):t('noData');$('top-expensive-tasks').innerHTML=top.length?top.map((task,index)=>`<button class="expensive-item" data-task="${task.taskId}" title="${escapeHTML(task.task)} · ${escapeHTML(task.project)} · ${escapeHTML(task.model)}"><span class="expensive-index">${String(index+1).padStart(2,'0')}</span><span class="expensive-copy"><strong>${escapeHTML(task.task)}</strong><small><span class="expensive-project">${escapeHTML(task.project)}</span><span class="expensive-model">· ${escapeHTML(shortModel(task.model))}</span></small></span><span class="expensive-value"><strong>${formatTokens(task.total)}</strong><small>${formatPercent(currentMetrics.total?task.total/currentMetrics.total*100:0)}</small></span></button>`).join(''):`<div class="empty-state">${t('adjustFilters')}</div>`;$('top-expensive-tasks').querySelectorAll('[data-task]').forEach(button=>button.addEventListener('click',()=>openTask(button.dataset.task)));}
function renderTaskTable(tasks){const query=state.search.trim().toLowerCase(),matching=tasks.filter(task=>`${task.task} ${task.project} ${task.model}`.toLowerCase().includes(query)).sort((a,b)=>state.descending?b.total-a.total:a.total-b.total),pageSize=8,pages=Math.max(1,Math.ceil(matching.length/pageSize));state.page=Math.min(state.page,pages-1);const visible=matching.slice(state.page*pageSize,state.page*pageSize+pageSize);$('task-count').textContent=`/ ${matching.length}`;$('task-table-body').innerHTML=visible.length?visible.map(task=>{const driver=currentDrivers.get(task.taskId)||{labels:['Normal']},growth=Number.isFinite(task.contextGrowth)?`${task.contextGrowth.toFixed(1)}×`:'—';return `<tr><td><button class="task-link" data-task="${task.taskId}" title="${escapeHTML(task.task)}">${escapeHTML(task.task)}</button><span class="task-project" title="${escapeHTML(task.project)}">${escapeHTML(task.project)} · ${t('turnCount',task.count)}</span></td><td><span class="model-tag" title="${escapeHTML(task.model)}">${escapeHTML(shortModel(task.model))}</span></td><td class="numeric-cell"><span class="rank-value">${formatTokens(task.total)}</span></td><td class="numeric-cell">${formatTokens(task.tokensPerTurn)}</td><td class="numeric-cell">${growth}</td><td class="numeric-cell">${formatPercent(task.cacheRate*100,0)}</td><td><span class="health-chip ${task.health}" title="${escapeHTML(driver.labels.join(', '))}">${escapeHTML(healthLabel(task.health))}</span></td></tr>`;}).join(''):`<tr><td colspan="7" class="table-empty">${t('noData')}</td></tr>`;$('table-status').textContent=t('showing',matching.length?state.page*pageSize+1:0,Math.min((state.page+1)*pageSize,matching.length),matching.length);$('page-number').textContent=`${state.page+1} / ${pages}`;$('previous-page').disabled=state.page===0;$('next-page').disabled=state.page>=pages-1;$('task-table-body').querySelectorAll('[data-task]').forEach(button=>button.addEventListener('click',()=>openTask(button.dataset.task)));}
function contextGrowthChart(task){
const turns=task.turns||[]; if(turns.length<2)return `<div class="empty-state">${state.language==='zh'?'至少需要 2 轮数据才能绘制 Context 曲线。':'At least 2 turns are needed for a context curve.'}</div>`;
const width=430,height=132,left=34,right=416,top=12,bottom=105,max=Math.max(...turns.map(turn=>turn.input),1),span=Math.max(1,turns.length-1);
const points=turns.map((turn,index)=>({x:left+(right-left)*(index/span),y:bottom-(turn.input/max)*(bottom-top),turn}));
const line=points.map((point,index)=>`${index?'L':'M'}${point.x.toFixed(1)} ${point.y.toFixed(1)}`).join(' '),area=`M${left} ${bottom} ${line.slice(1)} L${right} ${bottom} Z`,peak=task.peakTurn,peakPoint=peak?points[Math.max(0,Math.min(points.length-1,peak.index-1))]:null;
return `<div class="context-chart"><svg viewBox="0 0 ${width} ${height}" role="img" aria-label="Context input growth by turn"><line x1="${left}" x2="${right}" y1="${bottom}" y2="${bottom}" stroke="var(--hairline)"/><line x1="${left}" x2="${right}" y1="${top}" y2="${top}" stroke="var(--hairline)" stroke-dasharray="3 4"/><path class="context-area" d="${area}"/><path class="context-line" d="${line}"/>${points.map((point,index)=>`<circle class="context-dot ${peakPoint===point?'peak':''}" cx="${point.x}" cy="${point.y}" r="${peakPoint===point?3.5:2.1}"/>`).join('')}<text class="context-axis" x="${left}" y="124">1</text><text class="context-axis" x="${right}" y="124" text-anchor="end">${turns.length}</text><text class="context-axis" x="${left}" y="10">${formatTokens(max)}</text></svg></div>`;
}
function openTask(taskId){
const rows=filteredRecords.filter(r=>r.taskId===taskId),task=aggregateTasks(rows)[0];if(!task)return;task.health=classifySession(task,currentMetrics.sessionBaseline||sessionBaselines(currentMetrics.tasks));lastFocusedElement=document.activeElement;
const zh=state.language==='zh',driver=currentDrivers.get(taskId)||{labels:['Normal'],reasons:[t('driverNormal')]},ui=zh?{detail:'Session 详情',total:'Token 总量',turns:'轮次',perTurn:'单轮 Token',duration:'生成时长',span:'观测跨度',cache:'缓存命中',speed:'输出速度',health:'Session 状态',context:'Context 增长',first:'前 5 轮平均输入',last:'后 5 轮平均输入',growth:'增长倍率',peak:'峰值轮次',driver:'消耗特征',breakdown:'Token 构成',input:'输入',cached:'缓存输入',uncached:'非缓存输入',output:'输出',calls:'逐轮明细',note:hasCreditRate(task.model)?`Estimated Credits:${task.credits.toFixed(1)} cr。仅用于模型费率权重比较,不代表账户额度百分比。`:'Estimated Credits:当前模型无内置费率映射,因此不估算。'}:{detail:'Session detail',total:'Total tokens',turns:'Turns',perTurn:'Tokens / turn',duration:'Generation time',span:'Observed span',cache:'Cache hit',speed:'Output speed',health:'Session health',context:'Context growth',first:'First 5 avg input',last:'Last 5 avg input',growth:'Growth ratio',peak:'Peak turn',driver:'Consumption drivers',breakdown:'Token breakdown',input:'Input',cached:'Cached input',uncached:'Non-cached input',output:'Output',calls:'Turn detail',note:hasCreditRate(task.model)?`Estimated credits: ${task.credits.toFixed(1)} cr. This is only a model-rate weighting and is not an account quota percentage.`:'Estimated credits are unavailable because this model has no built-in rate mapping.'};
const growth=Number.isFinite(task.contextGrowth)?`${task.contextGrowth.toFixed(1)}×`:'—',peak=task.peakTurn?`${zh?'第':'Turn '}${task.peakTurn.index}${zh?' 轮':''} · ${formatTokens(task.peakTurn.total)}`:'—';
$('drawer-content').innerHTML=`<span class="drawer-label">${ui.detail} · Observed / Calculated</span><h2 id="drawer-title">${escapeHTML(task.task)}</h2><p class="drawer-subtitle">${escapeHTML(task.project)} · ${escapeHTML(task.model)} · <span class="health-chip ${task.health}">${escapeHTML(healthLabel(task.health))}</span></p><div class="drawer-metrics"><div class="drawer-metric"><span>${ui.total}</span><strong>${formatTokens(task.total)}</strong></div><div class="drawer-metric"><span>${ui.turns}</span><strong>${task.count}</strong></div><div class="drawer-metric"><span>${ui.perTurn}</span><strong>${formatTokens(task.tokensPerTurn)}</strong></div><div class="drawer-metric"><span>${ui.duration}</span><strong>${formatDuration(task.durationSeconds)}</strong></div><div class="drawer-metric"><span>${ui.cache}</span><strong>${formatPercent(task.cacheRate*100)}</strong></div><div class="drawer-metric"><span>${ui.span}</span><strong>${task.spanSeconds?formatDuration(task.spanSeconds):'—'}</strong></div></div><h3>${ui.context}</h3><div class="context-metrics"><div class="context-metric"><span>${ui.first}</span><strong>${formatTokens(task.firstAvgInput)}</strong></div><div class="context-metric"><span>${ui.last}</span><strong>${formatTokens(task.lastAvgInput)}</strong></div><div class="context-metric"><span>${ui.growth}</span><strong>${growth}</strong></div><div class="context-metric"><span>${ui.peak}</span><strong>${peak}</strong></div><div class="context-metric"><span>${ui.speed}</span><strong>${task.speed?`${task.speed.toFixed(1)} t/s`:'—'}</strong></div><div class="context-metric"><span>${ui.health}</span><strong>${escapeHTML(healthLabel(task.health))}</strong></div></div>${contextGrowthChart(task)}<h3>${ui.driver}</h3><div class="driver-list">${driver.labels.map(label=>`<span class="driver-tag">${escapeHTML(label)}</span>`).join('')}</div><ul class="driver-reasons">${driver.reasons.map(reason=>`<li>${escapeHTML(reason)}</li>`).join('')}</ul><h3>${ui.breakdown}</h3><div class="breakdown-list"><span>${ui.input} <b>${formatTokens(task.input)}</b></span><span>${ui.cached} <b>${formatTokens(task.cached)}</b></span><span>${ui.uncached} <b>${formatTokens(task.input-task.cached)}</b></span><span>${ui.output} <b>${formatTokens(task.output)}</b></span></div><p class="drawer-note">${ui.note}</p><h3>${ui.calls}</h3>${task.turns.slice().reverse().slice(0,30).map(turn=>`<div class="call-row"><span>${turn.date.slice(5)} ${turn.time} · #${turn.index}</span><strong>${formatTokens(turn.total)} tok</strong><em>${formatTokens(turn.input)} in</em></div>`).join('')}`;
$('drawer-backdrop').classList.add('open');document.body.style.overflow='hidden';$('drawer-close').focus();
}
function closeDrawer(){$('drawer-backdrop').classList.remove('open');document.body.style.overflow='';if(lastFocusedElement)lastFocusedElement.focus();}
function showToast(message){clearTimeout(toastTimer);$('toast').textContent=message;$('toast').classList.add('show');toastTimer=setTimeout(()=>$('toast').classList.remove('show'),2200);}
function render(){const bounds=rangeBounds(),previousBounds=calculatePreviousPeriod(bounds);filteredRecords=filterUsageData(bounds);previousRecords=filterUsageData(previousBounds);currentMetrics=calculateMetrics(filteredRecords,bounds);previousMetrics=calculateMetrics(previousRecords,previousBounds);currentDrivers=calculateTaskDrivers(currentMetrics.tasks);renderQuota();renderSummary();renderInsights();renderSessionHealth();renderModels();renderTimeline();renderComposition();renderHeatmap();renderOutliers(currentMetrics.tasks);renderProjects();renderTopTasks(currentMetrics.tasks);renderTaskTable(currentMetrics.tasks);}
PROJECTS.forEach(project=>$('project-filter').add(new Option(project,project)));MODELS.forEach(model=>$('model-filter').add(new Option(shortModel(model),model)));applyStaticLanguage();populateTaskOptions();syncRangeControls();
document.querySelectorAll('[data-range]').forEach(button=>button.addEventListener('click',()=>{state.range=button.dataset.range;state.page=0;syncRangeControls();render();}));
document.querySelectorAll('[data-left-metric]').forEach(button=>button.addEventListener('click',()=>{state.leftMetric=button.dataset.leftMetric;renderModels();}));
$('matrix-compact').addEventListener('click',()=>{if(!state.matrixExpanded)return;state.matrixExpanded=false;renderHeatmap();});
$('matrix-all').addEventListener('click',()=>{if(state.matrixExpanded)return;state.matrixExpanded=true;renderHeatmap();});
$('project-filter').addEventListener('change',event=>{state.project=event.target.value;state.task='all';state.page=0;populateTaskOptions();render();});
$('model-filter').addEventListener('change',event=>{state.model=event.target.value;state.task='all';state.page=0;populateTaskOptions();render();});
$('task-filter').addEventListener('change',event=>{state.task=event.target.value;state.page=0;render();});
$('start-date').addEventListener('change',event=>{state.start=event.target.value;state.page=0;render();});$('end-date').addEventListener('change',event=>{state.end=event.target.value;state.page=0;render();});
$('task-search').addEventListener('input',event=>{state.search=event.target.value;state.page=0;renderTaskTable(currentMetrics.tasks);});
$('sort-tasks').addEventListener('click',()=>{state.descending=!state.descending;$('sort-tasks').textContent=`${t('sortTokens')} ${state.descending?'↓':'↑'}`;renderTaskTable(currentMetrics.tasks);});
$('previous-page').addEventListener('click',()=>{state.page-=1;renderTaskTable(currentMetrics.tasks);});$('next-page').addEventListener('click',()=>{state.page+=1;renderTaskTable(currentMetrics.tasks);});
$('reset-filters').addEventListener('click',()=>{Object.assign(state,{range:'7d',project:'all',task:'all',model:'all',start:initialStart,end:initialEnd,search:'',page:0,matrixExpanded:false});$('project-filter').value='all';$('model-filter').value='all';$('task-search').value='';$('start-date').value=state.start;$('end-date').value=state.end;populateTaskOptions();syncRangeControls();render();showToast(t('resetToast'));});
$('theme-toggle').addEventListener('click',()=>{document.body.classList.toggle('light');$('theme-toggle').setAttribute('aria-pressed',String(document.body.classList.contains('light')));});
$('motion-toggle').addEventListener('click',()=>{document.body.classList.toggle('reduce-motion');$('motion-toggle').textContent=document.body.classList.contains('reduce-motion')?t('restoreMotion'):t('reduceMotion');});
$('drawer-close').addEventListener('click',closeDrawer);$('drawer-backdrop').addEventListener('click',event=>{if(event.target===$('drawer-backdrop'))closeDrawer();});
document.querySelectorAll('[data-target]').forEach(button=>button.addEventListener('click',()=>{const target=$(button.dataset.target);if(target)target.scrollIntoView({behavior:document.body.classList.contains('reduce-motion')?'auto':'smooth',block:'start'});document.querySelectorAll('.nav-item').forEach(item=>{item.classList.toggle('active',item===button);if(item===button)item.setAttribute('aria-current','page');else item.removeAttribute('aria-current');});$('breadcrumb-current').textContent=button.textContent.trim();}));
document.addEventListener('keydown',event=>{if(event.key==='Escape'&&!$('refresh-popover').hidden){$('refresh-popover').hidden=true;$('refresh-info').setAttribute('aria-expanded','false');}if(!$('drawer-backdrop').classList.contains('open'))return;if(event.key==='Escape')closeDrawer();if(event.key==='Tab'){const focusable=[...$('drawer-backdrop').querySelectorAll('button,[href],[tabindex]:not([tabindex="-1"])')].filter(el=>!el.disabled);if(!focusable.length)return;const first=focusable[0],last=focusable[focusable.length-1];if(event.shiftKey&&document.activeElement===first){event.preventDefault();last.focus();}if(!event.shiftKey&&document.activeElement===last){event.preventDefault();first.focus();}}});
function exportDashboardImage(){
const width=1600,height=1180,scale=2,canvas=document.createElement('canvas');canvas.width=width*scale;canvas.height=height*scale;const ctx=canvas.getContext('2d');if(!ctx)throw new Error('Canvas unavailable');ctx.scale(scale,scale);
const dark=!document.body.classList.contains('light'),palette=dark?{bg:'#0b0c0f',surface:'#15171b',raised:'#202329',text:'#f5f5f7',muted:'#a1a1a6',tertiary:'#6e6e73',line:'rgba(255,255,255,.11)',blue:'#64d2ff',blueStrong:'#2997ff',orange:'#ff9f0a',green:'#30d158'}:{bg:'#f5f5f7',surface:'#ffffff',raised:'#f0f0f3',text:'#1d1d1f',muted:'#636366',tertiary:'#8e8e93',line:'rgba(0,0,0,.1)',blue:'#0a84ff',blueStrong:'#0071e3',orange:'#d97706',green:'#248a3d'};
const font='-apple-system,BlinkMacSystemFont,"SF Pro Text","PingFang SC","Helvetica Neue",sans-serif',mono='ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace';ctx.fillStyle=palette.bg;ctx.fillRect(0,0,width,height);
const round=(x,y,w,h,r,fill,stroke)=>{ctx.beginPath();ctx.moveTo(x+r,y);ctx.arcTo(x+w,y,x+w,y+h,r);ctx.arcTo(x+w,y+h,x,y+h,r);ctx.arcTo(x,y+h,x,y,r);ctx.arcTo(x,y,x+w,y,r);ctx.closePath();if(fill){ctx.fillStyle=fill;ctx.fill();}if(stroke){ctx.strokeStyle=stroke;ctx.stroke();}};
const text=(value,x,y,size,color=palette.text,weight=500,face=font)=>{ctx.font=`${weight} ${size}px ${face}`;ctx.fillStyle=color;ctx.fillText(String(value),x,y);};
const fit=(value,maxWidth,size=14,weight=500,face=font)=>{let result=String(value);ctx.font=`${weight} ${size}px ${face}`;while(result.length>1&&ctx.measureText(result).width>maxWidth)result=`${result.slice(0,-2)}…`;return result;};
const label=(value,x,y)=>text(value.toUpperCase(),x,y,11,palette.tertiary,700,mono);
const card=(x,y,w,h)=>round(x,y,w,h,18,palette.surface,palette.line);
text('Ledger',64,68,34,palette.text,700);text(state.language==='zh'?'用量分享视图':'Usage share view',66,96,15,palette.muted,500);round(338,42,194,34,17,dark?'rgba(255,159,10,.15)':'#fff1d6');text('SYNTHETIC DEMO',355,64,10,palette.orange,700,mono);text(new Date().toLocaleString(state.language==='zh'?'zh-CN':'en-US',{year:'numeric',month:'short',day:'numeric',hour:'2-digit',minute:'2-digit'}),width-360,68,13,palette.muted,500,mono);text(`${rangeBounds()[0]} — ${rangeBounds()[1]}`,width-360,94,12,palette.tertiary,500,mono);ctx.fillStyle=palette.blueStrong;ctx.fillRect(64,116,1472,3);
const quotaCards=[[state.language==='zh'?'5 小时窗口':'5-hour window',quotaSnapshot.five,'#ff9f0a'],[state.language==='zh'?'周窗口':'Weekly window',quotaSnapshot.week,palette.blueStrong]],quotaY=144,quotaW=720;
quotaCards.forEach((item,index)=>{const x=64+index*752,available=item[1]?.used!==null&&item[1]?.used!==undefined&&Number.isFinite(Number(item[1].used))&&item[1]?.source!=='stale',used=available?Math.round(Number(item[1].used)):null;card(x,quotaY,quotaW,132);label(item[0],x+24,quotaY+28);text(available?`${used}% used`:'—',x+24,quotaY+75,34,palette.text,700,mono);text(available?`${Math.max(0,100-used)}% remaining`:(item[1]?.source==='stale'?'Awaiting fresh snapshot':'Snapshot unavailable'),x+250,quotaY+74,14,palette.muted,500);round(x+24,quotaY+94,672,10,5,palette.raised);if(available)round(x+24,quotaY+94,672*Math.max(0,Math.min(1,used/100)),10,5,item[2]);});
const metrics=[['Total tokens',formatTokens(currentMetrics.total)],['Burn rate',currentMetrics.burnRate?t('perDay',formatTokens(currentMetrics.burnRate)):'—'],['Tokens / task',formatTokens(currentMetrics.tokensPerTask)],['Cache hit',formatPercent((currentMetrics.cacheRate||0)*100)]],kpiY=302,kpiW=356;
metrics.forEach((item,index)=>{const x=64+index*376;card(x,kpiY,kpiW,104);label(item[0],x+20,kpiY+27);text(item[1],x+20,kpiY+72,26,palette.text,700,mono);});
const models=currentMetrics.models||[],byVolume=[...models].sort((a,b)=>b.total-a.total).slice(0,4),byMetric=[...models].sort((a,b)=>{const av=state.leftMetric==='speed'?(a.speed||0):(a.calls?a.credits/a.calls:0),bv=state.leftMetric==='speed'?(b.speed||0):(b.calls?b.credits/b.calls:0);return bv-av;}).slice(0,4),modelY=438,panelW=720;
const ranking=(x,title,items,metric,accent,formatValue)=>{card(x,modelY,panelW,372);label(title,x+24,modelY+30);items.forEach((item,index)=>{const rowY=modelY+72+index*72,value=metric(item),max=Math.max(...items.map(metric),1),barW=270*value/max;text(`${String(index+1).padStart(2,'0')}`,x+24,rowY+8,12,palette.tertiary,700,mono);text(fit(shortModel(item.model),155,17,650),x+64,rowY+8,17,palette.text,650);text(formatValue(value),x+510,rowY+8,16,palette.text,700,mono);round(x+64,rowY+24,600,9,4,palette.raised);round(x+64,rowY+24,Math.max(4,barW),9,4,accent);});};
const leftMetric=state.leftMetric==='speed'?item=>item.speed||0:item=>item.calls?item.credits/item.calls:0;
const leftFormat=value=>state.leftMetric==='speed'?`${value.toFixed(1)} t/s`:`${value.toFixed(2)} cr/turn`;
ranking(64,state.leftMetric==='speed'?(state.language==='zh'?'生成速度':'Generation speed'):(state.language==='zh'?'额度强度':'Quota intensity'),byMetric,leftMetric,palette.orange,leftFormat);
ranking(816,state.language==='zh'?'Token 总量排名':'Token volume',byVolume,item=>item.total,palette.blueStrong,value=>formatTokens(value));
const tasks=[...(currentMetrics.tasks||[])].sort((a,b)=>b.total-a.total).slice(0,3),taskY=836;card(64,taskY,1472,184);label(state.language==='zh'?'高消耗任务':'Top tasks',88,taskY+30);tasks.forEach((item,index)=>{const x=88+index*480;const y=taskY+76;text(`${String(index+1).padStart(2,'0')}`,x,y,12,palette.tertiary,700,mono);text(fit(item.task,270,18,650),x+42,y,18,palette.text,650);text(`${fit(item.project,220,12,500)} · ${shortModel(item.model)}`,x+42,y+25,12,palette.muted,500);text(formatTokens(item.total),x+42,y+58,20,palette.text,700,mono);});
round(64,1050,1472,82,16,dark?'#111827':'#1d1d1f');text('Ledger',88,1084,19,'#ffffff',700);text('Local-first Codex usage analytics',88,1107,11,'#a7b0c0',500);text('github.com/ctdaniel/codex-ledger',width-520,1096,16,'#64d2ff',700,mono);
return new Promise((resolve,reject)=>canvas.toBlob(blob=>{if(!blob){reject(new Error('PNG encoding failed'));return;}const url=URL.createObjectURL(blob),anchor=document.createElement('a');anchor.href=url;anchor.download=`codex-ledger-share-${new Date().toISOString().slice(0,10)}.png`;anchor.click();setTimeout(()=>URL.revokeObjectURL(url),1000);resolve('png');},'image/png'));
}
$('export-button').addEventListener('click',async()=>{const button=$('export-button'),label=button.querySelector('span'),original=label.textContent;button.disabled=true;label.textContent=t('exporting');try{await exportDashboardImage();showToast(t('exportToast'));}catch(error){showToast(t('exportError'));console.error('Ledger image export failed',error);}finally{button.disabled=false;label.textContent=original;}});
document.querySelectorAll('[data-language]').forEach(button=>button.addEventListener('click',()=>{if(button.dataset.language===state.language)return;state.language=button.dataset.language;closeDrawer();applyStaticLanguage();populateTaskOptions();$('sort-tasks').textContent=`${t('sortTokens')} ${state.descending?'↓':'↑'}`;render();}));
$('refresh-info').addEventListener('click',event=>{event.stopPropagation();const open=$('refresh-popover').hidden;$('refresh-popover').hidden=!open;$('refresh-info').setAttribute('aria-expanded',String(open));});
document.addEventListener('click',event=>{if(!event.target.closest('.refresh-cluster')){$('refresh-popover').hidden=true;$('refresh-info').setAttribute('aria-expanded','false');}});
$('refresh-button').addEventListener('click',async()=>{const button=$('refresh-button'),label=button.querySelector('span');button.classList.add('loading');button.disabled=true;label.textContent=t('refreshing');try{if(typeof window.ledgerRefreshAdapter==='function'){const result=await window.ledgerRefreshAdapter()||{};showToast(state.language==='zh'?`本地刷新完成 · ${result.records??'—'} 条记录`:`Local refresh complete · ${result.records??'—'} records`);if(result.reload!==false){window.setTimeout(()=>window.location.reload(),120);return;}}else if(IS_LIVE_REPORT){showToast(state.language==='zh'?'当前是静态快照 · 请用 --open 或 --serve 启动实时刷新':'Static snapshot · start Ledger with --open or --serve for live refresh');return;}else{showToast(state.language==='zh'?'演示模式 · 运行 python3 scripts/ledger.py --open 读取真实数据':'Demo mode · run python3 scripts/ledger.py --open for live local data');return;}}catch(error){showToast(state.language==='zh'?'刷新失败 · 已保留上次数据':'Refresh failed · kept the last data');console.error('Ledger refresh failed',error);}finally{button.classList.remove('loading');button.disabled=false;label.textContent=t('refreshData');}});
setInterval(()=>{if(expireQuotaSnapshotsNow())renderQuota();else{updateQuotaCountdown('five',quotaSnapshot.five);updateQuotaCountdown('week',quotaSnapshot.week,true);}},1000);
const storedQuota=readStoredQuota();
// Live generated reports are authoritative. Browser storage must never resurrect an
// older quota percentage after the parser has written a stale/unavailable snapshot.
if(EXTERNAL_PAYLOAD?.quota)applyQuotaSnapshot({...EXTERNAL_PAYLOAD.quota,observedAt:EXTERNAL_PAYLOAD.meta?.observedAt||EXTERNAL_PAYLOAD.meta?.generatedAt||new Date().toISOString()},{persist:false,force:true});
loadQuotaScriptSync();
const sidecarQuota=window.__CODEX_QUOTA_SNAPSHOT__;
const initialQuota=IS_LIVE_REPORT?sidecarQuota:(snapshotTime(storedQuota)>=snapshotTime(sidecarQuota)?storedQuota:sidecarQuota);
if(initialQuota && snapshotTime(initialQuota)>=quotaObservedAt)applyQuotaSnapshot(initialQuota,{persist:false});
if(IS_LIVE_REPORT&&!EXTERNAL_PAYLOAD?.quota&&!sidecarQuota)clearStoredQuota();
if(EXTERNAL_PAYLOAD?.meta?.generatedAt){const d=new Date(EXTERNAL_PAYLOAD.meta.generatedAt);if(Number.isFinite(d.getTime()))$('snapshot-time').textContent=d.toLocaleString(state.language==='zh'?'zh-CN':'en-US',{year:'numeric',month:'2-digit',day:'2-digit',hour:'2-digit',minute:'2-digit'});}
render();
if(quotaRefreshState==='initial')window.setTimeout(async()=>{if(await loadQuotaSidecar()){renderQuota();$('snapshot-time').textContent=state.language==='zh'?'刚刚更新':'Updated just now';}},0);