magpie-multi-usage 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -48
- package/README.zh-CN.md +46 -69
- package/package.json +1 -1
- package/src/aggregator.mjs +271 -2
- package/src/github-store.mjs +1 -1
- package/src/http-server.mjs +12 -2
- package/src/sync-engine.mjs +26 -5
- package/src/ui/app.css +47 -4
- package/src/ui/app.js +260 -42
- package/src/ui/dashboard-state.mjs +131 -0
- package/src/ui/index.html +2 -1
- package/src/ui/view-model.mjs +165 -3
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure UI request-lifecycle helpers for the dashboard. Keeping the
|
|
3
|
+
* selection/race logic here makes pending replacement, late responses,
|
|
4
|
+
* same-selection last-good retention, and navigation recovery executable in
|
|
5
|
+
* Node without a browser DOM.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { summarySelectionKey } from "./view-model.mjs";
|
|
9
|
+
|
|
10
|
+
const SUMMARY_DEVICE_FIELDS = ["deviceId", "displayName", "status", "isLocal"];
|
|
11
|
+
|
|
12
|
+
export function formatDayInTimeZone(value, timeZone) {
|
|
13
|
+
const date = value instanceof Date ? value : new Date(value);
|
|
14
|
+
if (Number.isNaN(date.getTime())) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
try {
|
|
18
|
+
return new Intl.DateTimeFormat("en-CA", {
|
|
19
|
+
timeZone,
|
|
20
|
+
year: "numeric",
|
|
21
|
+
month: "2-digit",
|
|
22
|
+
day: "2-digit",
|
|
23
|
+
}).format(date);
|
|
24
|
+
} catch {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function summaryChartDay(summary) {
|
|
30
|
+
const trendEnd = summary?.trend?.endDay;
|
|
31
|
+
if (typeof trendEnd === "string" && trendEnd) {
|
|
32
|
+
return trendEnd;
|
|
33
|
+
}
|
|
34
|
+
const boundsEnd = summary?.bounds?.endDay;
|
|
35
|
+
return typeof boundsEnd === "string" && boundsEnd ? boundsEnd : null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* All-history charts have no bounded end day. Finite ranges can be reused only
|
|
40
|
+
* in the server-reported timezone, so a browser in a different host timezone
|
|
41
|
+
* cannot mislabel yesterday's chart as today's.
|
|
42
|
+
*/
|
|
43
|
+
export function isSummaryChartCurrent(summary, now = new Date()) {
|
|
44
|
+
const chartDay = summaryChartDay(summary);
|
|
45
|
+
if (!chartDay) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
const zone = typeof summary?.reportingTimeZone === "string"
|
|
49
|
+
? summary.reportingTimeZone.trim()
|
|
50
|
+
: "";
|
|
51
|
+
if (!zone) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
return formatDayInTimeZone(now, zone) === chartDay;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function selectDeviceChoices(devices = []) {
|
|
58
|
+
return devices
|
|
59
|
+
.filter((device) => device && typeof device.deviceId === "string" && device.deviceId)
|
|
60
|
+
.map((device) => {
|
|
61
|
+
const choice = {};
|
|
62
|
+
for (const field of SUMMARY_DEVICE_FIELDS) {
|
|
63
|
+
choice[field] = device[field];
|
|
64
|
+
}
|
|
65
|
+
choice.displayName = typeof choice.displayName === "string" && choice.displayName
|
|
66
|
+
? choice.displayName
|
|
67
|
+
: choice.deviceId;
|
|
68
|
+
choice.status = typeof choice.status === "string" && choice.status ? choice.status : "empty";
|
|
69
|
+
choice.isLocal = Boolean(choice.isLocal);
|
|
70
|
+
return choice;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function beginSummaryRequest(state, { range, device, gen }) {
|
|
75
|
+
state.requestGen = gen;
|
|
76
|
+
const key = summarySelectionKey(range, device);
|
|
77
|
+
state.pendingSelection = key;
|
|
78
|
+
state.requestError = null;
|
|
79
|
+
|
|
80
|
+
if (state.summary && state.summarySelection !== key) {
|
|
81
|
+
state.summary = null;
|
|
82
|
+
state.summarySelection = null;
|
|
83
|
+
state.chartStale = false;
|
|
84
|
+
return { key, invalidated: true };
|
|
85
|
+
}
|
|
86
|
+
return { key, invalidated: false };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function isCurrentSummaryRequest(state, { gen, range, device }) {
|
|
90
|
+
return state.requestGen === gen
|
|
91
|
+
&& state.range === range
|
|
92
|
+
&& state.device === device
|
|
93
|
+
&& state.pendingSelection === summarySelectionKey(range, device);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function applySummaryResult(state, { gen, range, device, result }) {
|
|
97
|
+
if (!result || !isCurrentSummaryRequest(state, { gen, range, device })) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
state.summary = result;
|
|
101
|
+
state.summarySelection = summarySelectionKey(range, device);
|
|
102
|
+
state.chartStale = false;
|
|
103
|
+
state.requestError = null;
|
|
104
|
+
state.pendingSelection = null;
|
|
105
|
+
state.deviceChoices = selectDeviceChoices(result.devices || []);
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Returns "retained" when the previous chart is still for this exact selection
|
|
111
|
+
* and its chart day is still current, "unavailable" when the chart must be
|
|
112
|
+
* dropped, and "ignored" for a late response from a superseded request.
|
|
113
|
+
*/
|
|
114
|
+
export function failSummaryRequest(state, { gen, range, device, message, now = new Date() }) {
|
|
115
|
+
if (!isCurrentSummaryRequest(state, { gen, range, device })) {
|
|
116
|
+
return "ignored";
|
|
117
|
+
}
|
|
118
|
+
state.pendingSelection = null;
|
|
119
|
+
state.requestError = message;
|
|
120
|
+
const key = summarySelectionKey(range, device);
|
|
121
|
+
|
|
122
|
+
if (state.summary && state.summarySelection === key && isSummaryChartCurrent(state.summary, now)) {
|
|
123
|
+
state.chartStale = true;
|
|
124
|
+
return "retained";
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
state.summary = null;
|
|
128
|
+
state.summarySelection = null;
|
|
129
|
+
state.chartStale = false;
|
|
130
|
+
return "unavailable";
|
|
131
|
+
}
|
package/src/ui/index.html
CHANGED
|
@@ -36,11 +36,12 @@
|
|
|
36
36
|
|
|
37
37
|
<section id="kpi-strip" class="stats" aria-label="Usage overview"></section>
|
|
38
38
|
|
|
39
|
-
<section class="chart" aria-label="
|
|
39
|
+
<section id="trend-section" class="chart" aria-label="Token activity">
|
|
40
40
|
<div class="chart-top">
|
|
41
41
|
<span id="trend-readout" class="chart-readout"></span>
|
|
42
42
|
<span id="trend-peak" class="peak"></span>
|
|
43
43
|
</div>
|
|
44
|
+
<span id="trend-keyboard-hint" class="sr-only"></span>
|
|
44
45
|
<div id="trend-container"></div>
|
|
45
46
|
</section>
|
|
46
47
|
|
package/src/ui/view-model.mjs
CHANGED
|
@@ -56,9 +56,19 @@ export const MESSAGES = Object.freeze({
|
|
|
56
56
|
statusStale: "Cached",
|
|
57
57
|
statusError: "Error",
|
|
58
58
|
statusEmpty: "No data",
|
|
59
|
-
trendHeading: "
|
|
59
|
+
trendHeading: "Token activity",
|
|
60
60
|
trendHint: "Hover a bar for details · light prompt/cache, solid output",
|
|
61
61
|
trendEmpty: "No usage recorded in the selected range.",
|
|
62
|
+
trendUnavailable: "Chart unavailable: no verified data for this selection.",
|
|
63
|
+
trendNoHourly: "Hourly detail is unavailable for the selected devices; showing the daily total.",
|
|
64
|
+
trendPartial: "Some sources are cached or unavailable; bars show verified values only.",
|
|
65
|
+
trendStale: "Showing the last successful data for this selection.",
|
|
66
|
+
trendDailyOnly: "Daily total without hourly detail",
|
|
67
|
+
trendNoVerifiedPoint: "No verified usage recorded in this bucket",
|
|
68
|
+
tokensUnit: "tokens",
|
|
69
|
+
summaryUnavailable: "Summary unavailable for this selection.",
|
|
70
|
+
trendLoading: "Loading usage…",
|
|
71
|
+
trendKeyboardHint: "Use the arrow keys, Home, or End to move between time buckets; press Tab to leave the chart.",
|
|
62
72
|
legendInput: "Input",
|
|
63
73
|
legendOutput: "Output",
|
|
64
74
|
legendCacheRead: "Cache read",
|
|
@@ -158,9 +168,19 @@ export const MESSAGES = Object.freeze({
|
|
|
158
168
|
statusStale: "缓存",
|
|
159
169
|
statusError: "异常",
|
|
160
170
|
statusEmpty: "无数据",
|
|
161
|
-
trendHeading: "
|
|
162
|
-
trendHint: "
|
|
171
|
+
trendHeading: "Token 趋势",
|
|
172
|
+
trendHint: "悬停柱体查看明细 · 浅色为输入/缓存,深色为输出",
|
|
163
173
|
trendEmpty: "所选范围内暂无用量数据。",
|
|
174
|
+
trendUnavailable: "图表不可用:当前选择没有已校验的数据。",
|
|
175
|
+
trendNoHourly: "所选设备没有小时级明细,以下显示当日总量。",
|
|
176
|
+
trendPartial: "部分数据来源为缓存或不可用,图表仅展示已校验的数据。",
|
|
177
|
+
trendStale: "当前显示该选择上次成功获取的数据。",
|
|
178
|
+
trendDailyOnly: "无小时明细的当日合计",
|
|
179
|
+
trendNoVerifiedPoint: "该时间段没有已校验的用量",
|
|
180
|
+
tokensUnit: "Token",
|
|
181
|
+
summaryUnavailable: "当前选择的汇总数据不可用。",
|
|
182
|
+
trendLoading: "正在加载用量…",
|
|
183
|
+
trendKeyboardHint: "使用方向键、Home 或 End 在各时间桶之间移动;按 Tab 离开图表。",
|
|
164
184
|
legendInput: "输入",
|
|
165
185
|
legendOutput: "输出",
|
|
166
186
|
legendCacheRead: "缓存读取",
|
|
@@ -408,3 +428,145 @@ export function buildDailyChartModel(daily = [], { width = 880, height = 224 } =
|
|
|
408
428
|
bars,
|
|
409
429
|
};
|
|
410
430
|
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Format a trend point's compact axis label. Hour buckets use 24-hour labels;
|
|
434
|
+
* day buckets use the backend-provided MM-DD label or the day suffix.
|
|
435
|
+
*/
|
|
436
|
+
export function formatTrendPointLabel(point) {
|
|
437
|
+
if (!point || typeof point !== "object") {
|
|
438
|
+
return "";
|
|
439
|
+
}
|
|
440
|
+
if (Number.isInteger(point.hour) && point.hour >= 0 && point.hour <= 23) {
|
|
441
|
+
return `${String(point.hour).padStart(2, "0")}:00`;
|
|
442
|
+
}
|
|
443
|
+
if (typeof point.label === "string" && point.label) {
|
|
444
|
+
return point.label;
|
|
445
|
+
}
|
|
446
|
+
const day = typeof point.day === "string" ? point.day : "";
|
|
447
|
+
return day.length === 10 ? day.slice(5) : day;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* Format a trend point's full hover title. Hour buckets include the calendar
|
|
452
|
+
* day so repeated daily hours can never be confused.
|
|
453
|
+
*/
|
|
454
|
+
export function formatTrendPointTitle(point) {
|
|
455
|
+
if (!point || typeof point !== "object") {
|
|
456
|
+
return "";
|
|
457
|
+
}
|
|
458
|
+
const day = typeof point.day === "string" ? point.day : "";
|
|
459
|
+
if (Number.isInteger(point.hour) && point.hour >= 0 && point.hour <= 23) {
|
|
460
|
+
return `${day} ${formatTrendPointLabel(point)}`.trim();
|
|
461
|
+
}
|
|
462
|
+
return day;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* Choose a sparse axis-label interval that keeps dense 24/30-slot charts legible.
|
|
467
|
+
*/
|
|
468
|
+
export function trendLabelEvery(count) {
|
|
469
|
+
const n = Number(count) || 0;
|
|
470
|
+
if (n <= 8) {
|
|
471
|
+
return 1;
|
|
472
|
+
}
|
|
473
|
+
return n <= 31 ? Math.ceil(n / 6) : Math.ceil(n / 5);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* Decide whether a sparse axis tick should render. The terminal tick is always
|
|
478
|
+
* kept, but a penultimate regular tick is dropped when it would crowd the last
|
|
479
|
+
* label at narrow widths. With a one-slot interval every label remains visible.
|
|
480
|
+
*/
|
|
481
|
+
export function trendShowLabel(index, count, every) {
|
|
482
|
+
const n = Math.max(0, Math.floor(Number(count) || 0));
|
|
483
|
+
const step = Math.max(1, Math.floor(Number(every) || 1));
|
|
484
|
+
if (!Number.isInteger(index) || index < 0 || index >= n) {
|
|
485
|
+
return false;
|
|
486
|
+
}
|
|
487
|
+
if (index === 0 || index === n - 1) {
|
|
488
|
+
return true;
|
|
489
|
+
}
|
|
490
|
+
if (index % step !== 0) {
|
|
491
|
+
return false;
|
|
492
|
+
}
|
|
493
|
+
return n - 1 - index >= step;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Build the visual axis plan for a trend chart. Today keeps all hourly bars but
|
|
498
|
+
* labels only the 12 even hours (00, 02, … 22), while date buckets keep the
|
|
499
|
+
* existing sparse terminal-tick policy. Hover/keyboard detail is unaffected.
|
|
500
|
+
*/
|
|
501
|
+
export function buildTrendAxisModel(points = [], { bucket = "day", every = 1 } = {}) {
|
|
502
|
+
const n = points.length;
|
|
503
|
+
return points.map((point, index) => {
|
|
504
|
+
if (bucket === "hour") {
|
|
505
|
+
const hour = point?.hour;
|
|
506
|
+
const show = Number.isInteger(hour) && hour >= 0 && hour < 24 && hour % 2 === 0;
|
|
507
|
+
const compactLabel = show ? String(hour).padStart(2, "0") : "";
|
|
508
|
+
return {
|
|
509
|
+
index,
|
|
510
|
+
show,
|
|
511
|
+
fullLabel: show ? `${compactLabel}:00` : "",
|
|
512
|
+
compactLabel,
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
const show = trendShowLabel(index, n, every);
|
|
516
|
+
const label = show ? formatTrendPointLabel(point) : "";
|
|
517
|
+
return {
|
|
518
|
+
index,
|
|
519
|
+
show,
|
|
520
|
+
fullLabel: label,
|
|
521
|
+
compactLabel: label,
|
|
522
|
+
};
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Resolve a roving-tab-stop target for chart keyboard navigation. Returns null
|
|
528
|
+
* for keys the chart does not handle so Tab and other shortcuts keep working.
|
|
529
|
+
*/
|
|
530
|
+
export function trendFocusTarget(currentIndex, key, count) {
|
|
531
|
+
const n = Math.max(0, Math.floor(Number(count) || 0));
|
|
532
|
+
if (n === 0) {
|
|
533
|
+
return null;
|
|
534
|
+
}
|
|
535
|
+
const current = Number.isInteger(currentIndex) && currentIndex >= 0 && currentIndex < n
|
|
536
|
+
? currentIndex
|
|
537
|
+
: 0;
|
|
538
|
+
switch (key) {
|
|
539
|
+
case "ArrowRight":
|
|
540
|
+
case "ArrowDown":
|
|
541
|
+
return Math.min(n - 1, current + 1);
|
|
542
|
+
case "ArrowLeft":
|
|
543
|
+
case "ArrowUp":
|
|
544
|
+
return Math.max(0, current - 1);
|
|
545
|
+
case "Home":
|
|
546
|
+
return 0;
|
|
547
|
+
case "End":
|
|
548
|
+
return n - 1;
|
|
549
|
+
default:
|
|
550
|
+
return null;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Stable key for a range/device selection so last-good data can be reused only
|
|
556
|
+
* for the exact selection that produced it.
|
|
557
|
+
*/
|
|
558
|
+
export function summarySelectionKey(range, device) {
|
|
559
|
+
return `${range}|${device}`;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* True only for a partial chart's completely empty bucket. A bucket with calls
|
|
564
|
+
* or cost but no tokens is verified activity and must keep its real tooltip.
|
|
565
|
+
*/
|
|
566
|
+
export function isUnverifiedEmptyTrendPoint(point, availability = "ok") {
|
|
567
|
+
if (availability !== "partial" || !point || typeof point !== "object") {
|
|
568
|
+
return false;
|
|
569
|
+
}
|
|
570
|
+
return ["totalTokens", "calls", "errors", "cost", "input", "output", "cacheRead", "cacheWrite"]
|
|
571
|
+
.every((key) => !Number(point[key]));
|
|
572
|
+
}
|