feat(panel): add 4-mode LED matrix switcher to usage heatmap

- Replace the static daily hint on >90d usage heatmaps with a Day / Week / Month / Total segmented switcher while keeping the 7xW grid geometry fixed in-place
- Render bottom-up proportional LED bars for weekly, monthly, and cumulative views with aggregated per-model breakdown tooltips and click-to-drill-down
This commit is contained in:
fish
2026-11-24 20:14:32 +09:00
parent 02237692f7
commit b9dc2b960c
9 changed files with 1856 additions and 253 deletions
+771 -125
View File
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -4,11 +4,11 @@
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>用量监控</title> <title>用量监控</title>
<link rel="stylesheet" href="/styles.css?v=1795515995591"> <link rel="stylesheet" href="/styles.css?v=1795518808645">
<script src="/preload.js"></script> <script src="/preload.js"></script>
</head> </head>
<body> <body>
<div id="app-root"></div> <div id="app-root"></div>
<script src="/app.js?v=1795515995591"></script> <script src="/app.js?v=1795518808645"></script>
</body> </body>
</html> </html>
@@ -14,12 +14,13 @@ import {
patchText, patchText,
} from '../../core/domPatch.js'; } from '../../core/domPatch.js';
import { import {
formatDateSpanBadge,
formatTokensPrecise, formatTokensPrecise,
formatTrendSlotFullLabel, formatTrendSlotFullLabel,
formatUsd, formatUsd,
} from '../../core/formatters.js'; } from '../../core/formatters.js';
import { SVG_NS } from '../../icons/SvgIcon.js'; import { SVG_NS } from '../../icons/SvgIcon.js';
import { getEffectiveLocale, t } from '../../i18n/index.js'; import { getEffectiveLocale, isEnglishLocale, t } from '../../i18n/index.js';
import { createConsumptionBreakdownContent } from '../ui/ConsumptionTooltipContent.js'; import { createConsumptionBreakdownContent } from '../ui/ConsumptionTooltipContent.js';
/** 超过该自然日天数(> 90 天)时,趋势图切换为类 GitHub 每日小格子热力图 */ /** 超过该自然日天数(> 90 天)时,趋势图切换为类 GitHub 每日小格子热力图 */
@@ -367,6 +368,332 @@ function buildHeatmapMonthTicks(slots, firstDow, totalWeeks) {
return ticks; return ticks;
} }
const EN_SHORT_MONTHS = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];
function normalizeHeatmapMode(mode) {
return mode === 'week' || mode === 'month' || mode === 'all' ? mode : 'day';
}
function createHeatmapAggAcc(extra = {}) {
return {
tokens: 0,
inputTokens: 0,
cacheReadTokens: 0,
thinkingTokens: 0,
responseTokens: 0,
outputTokens: 0,
costUsd: 0,
calls: 0,
minStartMs: Infinity,
maxEndMs: 0,
firstLabel: '',
lastLabel: '',
modelMap: new Map(),
...extra,
};
}
function mergeSlotIntoHeatmapAcc(acc, slot) {
if (!slot) return;
const tok = safeSlotTokens(slot.tokens);
const inTok = safeSlotTokens(slot.inputTokens);
const cacheTok = safeSlotTokens(slot.cacheReadTokens);
const thinkTok = safeSlotTokens(slot.thinkingTokens);
const respTok = safeSlotTokens(slot.responseTokens);
const outTok =
slot.outputTokens !== undefined && slot.outputTokens !== null
? safeSlotTokens(slot.outputTokens)
: thinkTok + respTok;
acc.tokens += tok;
acc.inputTokens += inTok;
acc.cacheReadTokens += cacheTok;
acc.thinkingTokens += thinkTok;
acc.responseTokens += respTok;
acc.outputTokens += outTok;
acc.costUsd += Math.max(0, Number(slot.costUsd || 0));
acc.calls += Math.max(0, Number(slot.calls || 0));
const sMs = Number(slot.startMs);
const eMs = Number(slot.endMs);
if (Number.isFinite(sMs) && sMs > 0 && sMs < acc.minStartMs) {
acc.minStartMs = sMs;
}
if (Number.isFinite(eMs) && eMs > acc.maxEndMs) {
acc.maxEndMs = eMs;
} else if (Number.isFinite(sMs) && sMs > 0 && sMs + 86_400_000 > acc.maxEndMs) {
acc.maxEndMs = sMs + 86_400_000;
}
const lbl = String(slot.label || '');
if (!acc.firstLabel && lbl) acc.firstLabel = lbl;
if (lbl) acc.lastLabel = lbl;
const srcModels = Array.isArray(slot.mergedModels)
? slot.mergedModels
: Array.isArray(slot.models)
? slot.models
: null;
if (srcModels) {
for (let i = 0; i < srcModels.length; i++) {
const m = srcModels[i];
if (!m) continue;
const key = String(m.displayText || m.label || m.shortName || '');
if (!key) continue;
let cur = acc.modelMap.get(key);
if (!cur) {
cur = {
...m,
calls: 0,
inputTokens: 0,
cacheReadTokens: 0,
thinkingTokens: 0,
responseTokens: 0,
outputTokens: 0,
totalTokens: 0,
costUsd: 0,
};
acc.modelMap.set(key, cur);
}
cur.calls += Math.max(0, Number(m.calls || 0));
cur.inputTokens += safeSlotTokens(m.inputTokens);
cur.cacheReadTokens += safeSlotTokens(m.cacheReadTokens);
cur.thinkingTokens += safeSlotTokens(m.thinkingTokens);
cur.responseTokens += safeSlotTokens(m.responseTokens);
cur.outputTokens += safeSlotTokens(m.outputTokens);
cur.totalTokens += safeSlotTokens(m.totalTokens);
cur.costUsd += Math.max(0, Number(m.costUsd || 0));
}
}
}
function finalizeHeatmapBucket(acc, customTitle, shortLabel, buildSegmentsFn) {
const hasStart = Number.isFinite(acc.minStartMs) && acc.minStartMs < Infinity && acc.minStartMs > 0;
const startMs = hasStart ? acc.minStartMs : 0;
const endMs =
hasStart && acc.maxEndMs > startMs
? Math.max(startMs + 60_000, acc.maxEndMs - 1)
: 0;
let modelSegments = [];
if (typeof buildSegmentsFn === 'function' && acc.modelMap.size > 0) {
const mergedList = Array.from(acc.modelMap.values(), (m) => ({ ...m })).sort(
(a, b) => (b.totalTokens || 0) - (a.totalTokens || 0) || (b.calls || 0) - (a.calls || 0)
);
modelSegments = buildSegmentsFn(mergedList) || [];
}
return {
startMs,
endMs,
label: shortLabel || customTitle,
fullLabel: customTitle,
customTitle,
tokens: acc.tokens,
inputTokens: acc.inputTokens,
cacheReadTokens: acc.cacheReadTokens,
thinkingTokens: acc.thinkingTokens,
responseTokens: acc.responseTokens,
outputTokens: acc.outputTokens,
costUsd: +acc.costUsd.toFixed(4),
calls: acc.calls,
modelSegments,
};
}
function buildHeatmapBarView(slots, firstDow, totalWeeks, mode, buildSegmentsFn) {
if (mode === 'week') {
const weekAccs = Array.from({ length: totalWeeks }, () => createHeatmapAggAcc());
for (let i = 0; i < slots.length; i++) {
const col = Math.floor((firstDow + i) / 7);
if (col >= 0 && col < totalWeeks) {
mergeSlotIntoHeatmapAcc(weekAccs[col], slots[i]);
}
}
let maxTok = 0;
let peakIdx = 0;
let activeWeeks = 0;
const nonZero = [];
const buckets = weekAccs.map((acc, idx) => {
if (acc.tokens > 0) {
activeWeeks++;
nonZero.push(acc.tokens);
}
if (acc.tokens >= maxTok) {
maxTok = acc.tokens;
peakIdx = idx;
}
const hasValidRange = Number.isFinite(acc.minStartMs) && acc.minStartMs < Infinity && acc.minStartMs > 0;
const spanTitle = hasValidRange
? formatDateSpanBadge(acc.minStartMs, Math.max(acc.minStartMs, acc.maxEndMs - 1))
: acc.firstLabel && acc.lastLabel && acc.firstLabel !== acc.lastLabel
? `${acc.firstLabel} ~ ${acc.lastLabel}`
: acc.firstLabel || '--';
const shortLbl =
acc.firstLabel && acc.lastLabel && acc.firstLabel !== acc.lastLabel
? `${acc.firstLabel}~${acc.lastLabel.split('/').pop()}`
: acc.firstLabel || spanTitle;
return finalizeHeatmapBucket(acc, spanTitle, shortLbl, buildSegmentsFn);
});
nonZero.sort((a, b) => a - b);
const thresholds = computeHeatmapThresholds(nonZero, maxTok);
const colSpecs = buckets.map((b, col) => {
const litRows =
b.tokens > 0 && maxTok > 0 ? Math.max(1, Math.min(7, Math.ceil((b.tokens / maxTok) * 7))) : 0;
return {
bucketIdx: col,
litRows,
lv: litRows > 0 ? resolveHeatmapLevel(b.tokens, thresholds) : 0,
isMonthEdge: false,
};
});
const peakB = buckets[peakIdx];
const summaryText =
maxTok > 0 && peakB
? t('chart.trend.hmWeekPeak', {
active: activeWeeks,
total: totalWeeks,
label: peakB.label,
tokens: formatTokensPrecise(maxTok),
})
: t('chart.trend.hmWeekZero', { total: totalWeeks });
return { buckets, colSpecs, summaryText };
}
if (mode === 'month') {
const monthIndexByKey = new Map();
const monthAccs = [];
const colMonthCounts = Array.from({ length: totalWeeks }, () => new Map());
for (let i = 0; i < slots.length; i++) {
const slot = slots[i];
const parts = parseSlotCalendarParts(slot);
const mKey = `${parts.year}-${parts.month}`;
let mIdx = monthIndexByKey.get(mKey);
if (mIdx === undefined) {
mIdx = monthAccs.length;
monthIndexByKey.set(mKey, mIdx);
monthAccs.push(createHeatmapAggAcc({ year: parts.year, month: parts.month }));
}
mergeSlotIntoHeatmapAcc(monthAccs[mIdx], slot);
const col = Math.floor((firstDow + i) / 7);
if (col >= 0 && col < totalWeeks) {
colMonthCounts[col].set(mIdx, (colMonthCounts[col].get(mIdx) || 0) + 1);
}
}
const colMonthIdx = new Array(totalWeeks).fill(0);
for (let col = 0; col < totalWeeks; col++) {
let bestM = col > 0 ? colMonthIdx[col - 1] : 0;
let bestCnt = -1;
for (const [mIdx, cnt] of colMonthCounts[col].entries()) {
if (cnt >= bestCnt) {
bestCnt = cnt;
bestM = mIdx;
}
}
colMonthIdx[col] = bestM;
}
if (monthAccs.length > 0 && totalWeeks >= monthAccs.length) {
colMonthIdx[0] = 0;
colMonthIdx[totalWeeks - 1] = monthAccs.length - 1;
}
let maxTok = 0;
let peakIdx = 0;
let activeMonths = 0;
const nonZero = [];
const en = isEnglishLocale();
const buckets = monthAccs.map((acc, idx) => {
if (acc.tokens > 0) {
activeMonths++;
nonZero.push(acc.tokens);
}
if (acc.tokens >= maxTok) {
maxTok = acc.tokens;
peakIdx = idx;
}
const mNum = acc.month || 1;
const enShort = EN_SHORT_MONTHS[mNum - 1] || `M${mNum}`;
const fullTitle = en ? `${enShort} ${acc.year}` : t('cons.monthFullLabel', { year: acc.year, m: mNum });
const shortLbl = en ? enShort : t('cons.monthLabel', { m: mNum });
return finalizeHeatmapBucket(acc, fullTitle, shortLbl, buildSegmentsFn);
});
nonZero.sort((a, b) => a - b);
const thresholds = computeHeatmapThresholds(nonZero, maxTok);
const colSpecs = colMonthIdx.map((mIdx, col) => {
const b = buckets[mIdx] || { tokens: 0 };
const litRows =
b.tokens > 0 && maxTok > 0 ? Math.max(1, Math.min(7, Math.ceil((b.tokens / maxTok) * 7))) : 0;
return {
bucketIdx: mIdx,
litRows,
lv: litRows > 0 ? resolveHeatmapLevel(b.tokens, thresholds) : 0,
isMonthEdge: col > 0 && mIdx !== colMonthIdx[col - 1],
};
});
const peakB = buckets[peakIdx];
const summaryText =
maxTok > 0 && peakB
? t('chart.trend.hmMonthPeak', {
active: activeMonths,
total: buckets.length,
label: peakB.label,
tokens: formatTokensPrecise(maxTok),
})
: t('chart.trend.hmMonthZero', { total: Math.max(1, buckets.length) });
return { buckets, colSpecs, summaryText };
}
// mode === 'all' (累计总量爬升柱阵)
const runAcc = createHeatmapAggAcc();
const buckets = new Array(totalWeeks);
for (let col = 0; col < totalWeeks; col++) {
for (let row = 0; row < 7; row++) {
const sIdx = col * 7 + row - firstDow;
if (sIdx >= 0 && sIdx < slots.length) {
mergeSlotIntoHeatmapAcc(runAcc, slots[sIdx]);
}
}
const hasEnd = Number.isFinite(runAcc.minStartMs) && runAcc.minStartMs < Infinity && runAcc.maxEndMs > 0;
const endDayMs = hasEnd ? Math.max(runAcc.minStartMs, runAcc.maxEndMs - 1) : 0;
const endDateStr = endDayMs > 0 ? formatDateSpanBadge(endDayMs, endDayMs) : runAcc.lastLabel || '--';
const cumTitle = t('chart.trend.hmCumThrough', { date: endDateStr });
buckets[col] = finalizeHeatmapBucket(runAcc, cumTitle, endDateStr, buildSegmentsFn);
}
const grandTotal = buckets[totalWeeks - 1]?.tokens || 0;
const grandCost = buckets[totalWeeks - 1]?.costUsd || 0;
const colSpecs = buckets.map((b, col) => {
const litRows =
b.tokens > 0 && grandTotal > 0
? Math.max(1, Math.min(7, Math.ceil((b.tokens / grandTotal) * 7)))
: 0;
const ratio = grandTotal > 0 ? b.tokens / grandTotal : 0;
const lv = litRows === 0 ? 0 : ratio > 0.75 ? 4 : ratio > 0.5 ? 3 : ratio > 0.25 ? 2 : 1;
return {
bucketIdx: col,
litRows,
lv,
isMonthEdge: false,
};
});
const summaryText = t('chart.trend.hmCumSummary', {
days: slots.length,
tokens: formatTokensPrecise(grandTotal),
cost: formatUsd(grandCost),
});
return { buckets, colSpecs, summaryText };
}
/** /**
* 创建交互式历史趋势组件(<=90 天为 SVG 平滑曲线图,>90 天自动切换为类 GitHub 每日小格子热力图)。 * 创建交互式历史趋势组件(<=90 天为 SVG 平滑曲线图,>90 天自动切换为类 GitHub 每日小格子热力图)。
* *
@@ -544,10 +871,18 @@ export function createTrendAreaChart({ onSelectSlot = null } = {}) {
let currentSlots = []; let currentSlots = [];
let currentPts = []; let currentPts = [];
let currentIsHeatmap = false; let currentIsHeatmap = false;
let currentHmMode = 'day';
let lastBuildSegmentsFn = null;
let currentHeatmapWeeks = 0; let currentHeatmapWeeks = 0;
let currentHeatmapFirstDow = 0; let currentHeatmapFirstDow = 0;
let currentHeatmapGapPx = 2; let currentHeatmapGapPx = 2;
let currentCellEls = []; let currentCellEls = [];
let currentHmBuckets = [];
let currentHmBucketCells = [];
let currentHmAnchorCells = [];
let currentColBucketIdx = [];
let hoveredHighlightEls = [];
let lastHeatmapSummaryText = '';
let tickElsByIdx = new Map(); let tickElsByIdx = new Map();
let hoveredIdx = -1; let hoveredIdx = -1;
let hoveredCellEl = null; let hoveredCellEl = null;
@@ -713,7 +1048,7 @@ export function createTrendAreaChart({ onSelectSlot = null } = {}) {
const slotTok = safeSlotTokens(slot.tokens); const slotTok = safeSlotTokens(slot.tokens);
const slotCost = Number.isFinite(Number(slot.costUsd)) ? Math.max(0, Number(slot.costUsd)) : 0; const slotCost = Number.isFinite(Number(slot.costUsd)) ? Math.max(0, Number(slot.costUsd)) : 0;
tooltipContent.update({ tooltipContent.update({
title: formatTrendSlotFullLabel(slot) || slot.label || '--', title: slot.customTitle || formatTrendSlotFullLabel(slot) || slot.label || '--',
calls: slot.calls, calls: slot.calls,
totalTokens: slotTok, totalTokens: slotTok,
costText: formatUsd(slotCost, { costText: formatUsd(slotCost, {
@@ -854,11 +1189,14 @@ export function createTrendAreaChart({ onSelectSlot = null } = {}) {
} }
function showHeatmapHoverAtIndex(idx, cellEl, pointer = null) { function showHeatmapHoverAtIndex(idx, cellEl, pointer = null) {
if (!currentIsHeatmap || idx < 0 || idx >= currentSlots.length) { if (!currentIsHeatmap || idx < 0 || idx >= currentHmBuckets.length) {
clearHover(); clearHover();
return; return;
} }
const targetCell = cellEl || currentCellEls[idx] || null; const isBarMode = currentHmMode !== 'day';
const targetCell = isBarMode
? currentHmAnchorCells[idx] || cellEl || null
: cellEl || currentHmAnchorCells[idx] || currentCellEls[idx] || null;
if (!targetCell) { if (!targetCell) {
clearHover(); clearHover();
return; return;
@@ -869,13 +1207,29 @@ export function createTrendAreaChart({ onSelectSlot = null } = {}) {
if (pointer) { if (pointer) {
lastPointer = pointer; lastPointer = pointer;
} }
if (hoveredCellEl && hoveredCellEl !== targetCell) {
patchClasses(hoveredCellEl, { 'is-hovered': false });
}
hoveredCellEl = targetCell;
patchClasses(targetCell, { 'is-hovered': true });
const slot = currentSlots[idx]; if (changedCell || hoveredHighlightEls.length === 0) {
for (let i = 0; i < hoveredHighlightEls.length; i++) {
patchClasses(hoveredHighlightEls[i], {
'is-hovered': false,
'is-bar-hovered': false,
});
}
const nextHighlight =
currentHmBucketCells[idx] && currentHmBucketCells[idx].length > 0
? currentHmBucketCells[idx]
: [targetCell];
for (let i = 0; i < nextHighlight.length; i++) {
patchClasses(nextHighlight[i], {
'is-hovered': !isBarMode,
'is-bar-hovered': isBarMode,
});
}
hoveredHighlightEls = nextHighlight;
hoveredCellEl = targetCell;
}
const slot = currentHmBuckets[idx];
const row = Number(targetCell.getAttribute('data-hm-row') || 0); const row = Number(targetCell.getAttribute('data-hm-row') || 0);
const col = Number(targetCell.getAttribute('data-hm-col') || 0); const col = Number(targetCell.getAttribute('data-hm-col') || 0);
@@ -898,18 +1252,32 @@ export function createTrendAreaChart({ onSelectSlot = null } = {}) {
let fallbackX = let fallbackX =
currentHeatmapWeeks > 0 ? ((col + 0.5) / currentHeatmapWeeks) * wrapW : wrapW / 2; currentHeatmapWeeks > 0 ? ((col + 0.5) / currentHeatmapWeeks) * wrapW : wrapW / 2;
let fallbackY = 20 + row * 14; let fallbackY = 20 + row * 14;
if ( const hasValidCellRect = Boolean(
wrapRect && wrapRect &&
wrapRect.width > 0 && wrapRect.width > 0 &&
cellRect && cellRect &&
cellRect.width > 0 && cellRect.width > 0 &&
cellRect.width < wrapRect.width cellRect.width < wrapRect.width
) { );
if (hasValidCellRect) {
fallbackX = cellRect.left + cellRect.width / 2 - wrapRect.left; fallbackX = cellRect.left + cellRect.width / 2 - wrapRect.left;
fallbackY = cellRect.top + cellRect.height / 2 - wrapRect.top; fallbackY = cellRect.top + cellRect.height / 2 - wrapRect.top;
} }
positionTooltipNearAnchor(fallbackX, fallbackY, pointer || lastPointer); const rawPtr = pointer || lastPointer;
const effectivePtr =
isBarMode &&
rawPtr &&
hasValidCellRect &&
Number.isFinite(Number(rawPtr.clientX)) &&
Number.isFinite(Number(rawPtr.clientY))
? {
clientX: Number(rawPtr.clientX),
clientY: Math.min(Number(rawPtr.clientY), cellRect.top + 2),
}
: rawPtr;
positionTooltipNearAnchor(fallbackX, fallbackY, effectivePtr);
armTooltipTracking(wasHidden); armTooltipTracking(wasHidden);
} }
@@ -931,8 +1299,17 @@ export function createTrendAreaChart({ onSelectSlot = null } = {}) {
if (!currentIsHeatmap) { if (!currentIsHeatmap) {
patchAttr(peakPill, 'hidden', false); patchAttr(peakPill, 'hidden', false);
} }
if (hoveredHighlightEls.length > 0) {
for (let i = 0; i < hoveredHighlightEls.length; i++) {
patchClasses(hoveredHighlightEls[i], {
'is-hovered': false,
'is-bar-hovered': false,
});
}
hoveredHighlightEls = [];
}
if (hoveredCellEl) { if (hoveredCellEl) {
patchClasses(hoveredCellEl, { 'is-hovered': false }); patchClasses(hoveredCellEl, { 'is-hovered': false, 'is-bar-hovered': false });
hoveredCellEl = null; hoveredCellEl = null;
} }
for (const el of tickElsByIdx.values()) { for (const el of tickElsByIdx.values()) {
@@ -989,8 +1366,10 @@ export function createTrendAreaChart({ onSelectSlot = null } = {}) {
const idxAttr = cur.getAttribute?.('data-slot-idx'); const idxAttr = cur.getAttribute?.('data-slot-idx');
if (idxAttr !== null && idxAttr !== undefined && idxAttr !== '') { if (idxAttr !== null && idxAttr !== undefined && idxAttr !== '') {
const idx = Number(idxAttr); const idx = Number(idxAttr);
if (Number.isFinite(idx) && idx >= 0 && idx < currentSlots.length) { if (Number.isFinite(idx) && idx >= 0 && idx < currentHmBuckets.length) {
return { idx, cellEl: cur }; const cellEl =
currentHmMode !== 'day' ? currentHmAnchorCells[idx] || cur : cur;
return { idx, cellEl };
} }
} }
cur = cur.parentNode || null; cur = cur.parentNode || null;
@@ -1005,8 +1384,12 @@ export function createTrendAreaChart({ onSelectSlot = null } = {}) {
const clientX = Number(e?.clientX); const clientX = Number(e?.clientX);
const clientY = Number(e?.clientY); const clientY = Number(e?.clientY);
if (!Number.isFinite(clientX) || !Number.isFinite(clientY)) { if (!Number.isFinite(clientX) || !Number.isFinite(clientY)) {
if (hoveredIdx >= 0 && hoveredIdx < currentSlots.length && currentCellEls[hoveredIdx]) { if (
return { idx: hoveredIdx, cellEl: currentCellEls[hoveredIdx] }; hoveredIdx >= 0 &&
hoveredIdx < currentHmBuckets.length &&
currentHmAnchorCells[hoveredIdx]
) {
return { idx: hoveredIdx, cellEl: currentHmAnchorCells[hoveredIdx] };
} }
return null; return null;
} }
@@ -1017,8 +1400,12 @@ export function createTrendAreaChart({ onSelectSlot = null } = {}) {
const rect = cachedHmGridRect; const rect = cachedHmGridRect;
if (!rect || !(rect.width > 0) || !(rect.height > 0) || currentHeatmapWeeks <= 0) { if (!rect || !(rect.width > 0) || !(rect.height > 0) || currentHeatmapWeeks <= 0) {
cachedHmGridRect = null; cachedHmGridRect = null;
if (hoveredIdx >= 0 && hoveredIdx < currentSlots.length && currentCellEls[hoveredIdx]) { if (
return { idx: hoveredIdx, cellEl: currentCellEls[hoveredIdx] }; hoveredIdx >= 0 &&
hoveredIdx < currentHmBuckets.length &&
currentHmAnchorCells[hoveredIdx]
) {
return { idx: hoveredIdx, cellEl: currentHmAnchorCells[hoveredIdx] };
} }
return null; return null;
} }
@@ -1041,6 +1428,36 @@ export function createTrendAreaChart({ onSelectSlot = null } = {}) {
const cellW = Math.max(1, pitchX - G); const cellW = Math.max(1, pitchX - G);
const cellH = Math.max(1, pitchY - G); const cellH = Math.max(1, pitchY - G);
// 柱阵模式下按列定位分桶,并在列间隙保持原高亮防闪烁
if (currentHmMode !== 'day') {
const approxCol = Math.max(0, Math.min(W - 1, Math.floor((relX + G * 0.5) / pitchX)));
if (
hoveredIdx >= 0 &&
hoveredIdx < currentHmBuckets.length &&
currentColBucketIdx[approxCol] !== hoveredIdx
) {
for (const dc of [-1, 1]) {
const nc = approxCol + dc;
if (nc < 0 || nc >= W || currentColBucketIdx[nc] !== hoveredIdx) continue;
const x0 = nc * pitchX;
const dx = relX < x0 ? x0 - relX : relX > x0 + cellW ? relX - (x0 + cellW) : 0;
if (dx <= G + 0.75 && currentHmAnchorCells[hoveredIdx]) {
return { idx: hoveredIdx, cellEl: currentHmAnchorCells[hoveredIdx] };
}
}
}
const bIdx = currentColBucketIdx[approxCol];
if (
Number.isFinite(bIdx) &&
bIdx >= 0 &&
bIdx < currentHmBuckets.length &&
currentHmAnchorCells[bIdx]
) {
return { idx: bIdx, cellEl: currentHmAnchorCells[bIdx] };
}
return null;
}
function distanceToCell(c, r) { function distanceToCell(c, r) {
const x0 = c * pitchX; const x0 = c * pitchX;
const y0 = r * pitchY; const y0 = r * pitchY;
@@ -1049,7 +1466,7 @@ export function createTrendAreaChart({ onSelectSlot = null } = {}) {
return { chebyshev: Math.max(dx, dy), distSq: dx * dx + dy * dy }; return { chebyshev: Math.max(dx, dy), distSq: dx * dx + dy * dy };
} }
// 指针滑入当前悬停格四周的空隙(gap)时保持原高亮,彻底消除跨格空隙闪烁与边缘微抖 // 指针滑入当前悬停格四周的空隙(gap)时保持原高亮,彻底消除跨格空隙闪烁和边缘微抖
if (hoveredIdx >= 0 && hoveredIdx < currentSlots.length && currentCellEls[hoveredIdx]) { if (hoveredIdx >= 0 && hoveredIdx < currentSlots.length && currentCellEls[hoveredIdx]) {
const curGridPos = currentHeatmapFirstDow + hoveredIdx; const curGridPos = currentHeatmapFirstDow + hoveredIdx;
const curCol = Math.floor(curGridPos / 7); const curCol = Math.floor(curGridPos / 7);
@@ -1096,7 +1513,7 @@ export function createTrendAreaChart({ onSelectSlot = null } = {}) {
} }
function handleHeatmapPointer(e) { function handleHeatmapPointer(e) {
if (!currentIsHeatmap || !currentSlots.length) return; if (!currentIsHeatmap || !currentHmBuckets.length) return;
const hit = resolveHeatmapCellFromPointer(e); const hit = resolveHeatmapCellFromPointer(e);
if (!hit || !Number.isFinite(hit.idx)) { if (!hit || !Number.isFinite(hit.idx)) {
clearHover(); clearHover();
@@ -1148,10 +1565,10 @@ export function createTrendAreaChart({ onSelectSlot = null } = {}) {
function handleHeatmapClick(e) { function handleHeatmapClick(e) {
handleHeatmapPointer(e); handleHeatmapPointer(e);
if (!currentIsHeatmap || !currentSlots.length || typeof onSelectSlot !== 'function') return; if (!currentIsHeatmap || !currentHmBuckets.length || typeof onSelectSlot !== 'function') return;
const hit = resolveHeatmapCellFromPointer(e); const hit = resolveHeatmapCellFromPointer(e);
if (!hit || !Number.isFinite(hit.idx)) return; if (!hit || !Number.isFinite(hit.idx)) return;
const slot = currentSlots[hit.idx]; const slot = currentHmBuckets[hit.idx];
if (slot && Number(slot.startMs) > 0 && Number(slot.endMs) > Number(slot.startMs)) { if (slot && Number(slot.startMs) > 0 && Number(slot.endMs) > Number(slot.startMs)) {
onSelectSlot(slot); onSelectSlot(slot);
} }
@@ -1209,9 +1626,20 @@ export function createTrendAreaChart({ onSelectSlot = null } = {}) {
} }
/** /**
* 根据最新时间分桶序列重绘平滑曲线(<=90天/小时级)或类 GitHub 每日小格子热力图(>90天)。 * 根据最新时间分桶序列重绘平滑曲线(<=90天/小时级)或 7×W 矩阵热力图(>90天,支持日/周/月/全部四档)。
*/ */
function update(series = []) { function update(series = [], opts = {}) {
if (opts && opts.heatmapMode !== undefined) {
const nextHmMode = normalizeHeatmapMode(opts.heatmapMode);
if (nextHmMode !== currentHmMode) {
clearHover();
currentHmMode = nextHmMode;
}
}
if (opts && typeof opts.buildModelSegments === 'function') {
lastBuildSegmentsFn = opts.buildModelSegments;
}
const slots = const slots =
Array.isArray(series) && series.length >= 1 Array.isArray(series) && series.length >= 1
? series ? series
@@ -1272,7 +1700,7 @@ export function createTrendAreaChart({ onSelectSlot = null } = {}) {
patchText(hmMoreEl, t('chart.trend.hmMore')); patchText(hmMoreEl, t('chart.trend.hmMore'));
const sig = const sig =
`${locale}|${useHeatmap ? 'hm' : `line:${viewW}`}|` + `${locale}|${useHeatmap ? `hm:${currentHmMode}` : `line:${viewW}`}|` +
slots slots
.map((s) => { .map((s) => {
const mSig = Array.isArray(s?.modelSegments) const mSig = Array.isArray(s?.modelSegments)
@@ -1283,21 +1711,19 @@ export function createTrendAreaChart({ onSelectSlot = null } = {}) {
) )
.join(',') .join(',')
: ''; : '';
return `${s?.startMs || ''}:${s?.label || ''}:${s?.fullLabel || ''}:${safeSlotTokens(s?.tokens)}:${safeSlotTokens(s?.inputTokens)}:${safeSlotTokens(s?.cacheReadTokens)}:${safeSlotTokens(s?.thinkingTokens)}:${safeSlotTokens(s?.responseTokens)}:${Number(s?.costUsd || 0)}:${Number(s?.calls || 0)}:${mSig}`; const mergedCnt = Array.isArray(s?.mergedModels) ? s.mergedModels.length : 0;
return `${s?.startMs || ''}:${s?.label || ''}:${s?.fullLabel || ''}:${safeSlotTokens(s?.tokens)}:${safeSlotTokens(s?.inputTokens)}:${safeSlotTokens(s?.cacheReadTokens)}:${safeSlotTokens(s?.thinkingTokens)}:${safeSlotTokens(s?.responseTokens)}:${Number(s?.costUsd || 0)}:${Number(s?.calls || 0)}:${mergedCnt}:${mSig}`;
}) })
.join('|'); .join('|');
if (hoveredIdx >= 0 && hoveredIdx < slots.length && !tooltipEl.hasAttribute('hidden')) {
populateTooltipContent(slots[hoveredIdx]);
}
if (useHeatmap) { if (useHeatmap) {
currentPts = []; currentPts = [];
const isBarMode = currentHmMode !== 'day';
hmWeekdaysEl.style.visibility = isBarMode ? 'hidden' : '';
patchClasses(heatmapStage, { 'is-bar-mode': isBarMode });
if (sig !== lastRenderSig) { if (sig !== lastRenderSig) {
lastRenderSig = sig; lastRenderSig = sig;
nonZeroVals.sort((a, b) => a - b);
const thresholds = computeHeatmapThresholds(nonZeroVals, maxTokens);
const firstStartMs = Number(slots[0]?.startMs); const firstStartMs = Number(slots[0]?.startMs);
const firstDow = const firstDow =
Number.isFinite(firstStartMs) && firstStartMs > 0 Number.isFinite(firstStartMs) && firstStartMs > 0
@@ -1306,6 +1732,7 @@ export function createTrendAreaChart({ onSelectSlot = null } = {}) {
const totalCells = firstDow + slots.length; const totalCells = firstDow + slots.length;
const totalWeeks = Math.max(1, Math.ceil(totalCells / 7)); const totalWeeks = Math.max(1, Math.ceil(totalCells / 7));
const gapPx = totalWeeks > 42 ? 1.5 : 2; const gapPx = totalWeeks > 42 ? 1.5 : 2;
const shouldAutoScrollRight = modeChanged || currentHeatmapWeeks !== totalWeeks;
currentHeatmapWeeks = totalWeeks; currentHeatmapWeeks = totalWeeks;
currentHeatmapFirstDow = firstDow; currentHeatmapFirstDow = firstDow;
currentHeatmapGapPx = gapPx; currentHeatmapGapPx = gapPx;
@@ -1332,47 +1759,156 @@ export function createTrendAreaChart({ onSelectSlot = null } = {}) {
hmMonthsEl.removeChild(hmMonthsEl.lastChild); hmMonthsEl.removeChild(hmMonthsEl.lastChild);
} }
// 就地增量复用 7 行 × N 周列的每日小格子,首次批量新增节点使用 DocumentFragment 挂载
currentCellEls = new Array(slots.length);
const prevMountedCount = hmGridEl.children.length; const prevMountedCount = hmGridEl.children.length;
const newCellsFrag =
totalCells > prevMountedCount &&
typeof document !== 'undefined' &&
typeof document.createDocumentFragment === 'function'
? document.createDocumentFragment()
: null;
for (let nodeIdx = 0; nodeIdx < totalCells; nodeIdx++) { if (!isBarMode) {
let cellEl = hmCellPool[nodeIdx]; nonZeroVals.sort((a, b) => a - b);
if (!cellEl) { const thresholds = computeHeatmapThresholds(nonZeroVals, maxTokens);
cellEl = h('span', { className: 'trend-hm-cell' });
hmCellPool.push(cellEl); // 就地增量复用 7 行 × N 周列的每日小格子,首次批量新增节点使用 DocumentFragment 挂载
currentCellEls = new Array(slots.length);
const newCellsFrag =
totalCells > prevMountedCount &&
typeof document !== 'undefined' &&
typeof document.createDocumentFragment === 'function'
? document.createDocumentFragment()
: null;
for (let nodeIdx = 0; nodeIdx < totalCells; nodeIdx++) {
let cellEl = hmCellPool[nodeIdx];
if (!cellEl) {
cellEl = h('span', { className: 'trend-hm-cell' });
hmCellPool.push(cellEl);
}
if (nodeIdx < firstDow) {
patchClasses(cellEl, {
'is-pad': true,
'is-hovered': false,
'is-bar-hovered': false,
'is-month-edge': false,
'lv-0': false,
'lv-1': false,
'lv-2': false,
'lv-3': false,
'lv-4': false,
});
patchAttr(cellEl, 'aria-hidden', 'true');
patchAttr(cellEl, 'role', null);
patchAttr(cellEl, 'data-slot-idx', null);
patchAttr(cellEl, 'data-hm-col', null);
patchAttr(cellEl, 'data-hm-row', null);
} else {
const i = nodeIdx - firstDow;
const slot = slots[i];
const tok = safeSlotTokens(slot?.tokens);
const lv = resolveHeatmapLevel(tok, thresholds);
const col = Math.floor(nodeIdx / 7);
const row = nodeIdx % 7;
patchClasses(cellEl, {
'is-pad': false,
'is-hovered': hoveredIdx === i,
'is-bar-hovered': false,
'is-month-edge': false,
'lv-0': lv === 0,
'lv-1': lv === 1,
'lv-2': lv === 2,
'lv-3': lv === 3,
'lv-4': lv === 4,
});
patchAttr(cellEl, 'aria-hidden', null);
patchAttr(cellEl, 'role', 'gridcell');
patchAttr(cellEl, 'data-slot-idx', String(i));
patchAttr(cellEl, 'data-hm-col', String(col));
patchAttr(cellEl, 'data-hm-row', String(row));
currentCellEls[i] = cellEl;
}
if (nodeIdx < prevMountedCount) {
if (hmGridEl.children[nodeIdx] !== cellEl) {
hmGridEl.insertBefore(cellEl, hmGridEl.children[nodeIdx] || null);
}
} else if (newCellsFrag) {
newCellsFrag.appendChild(cellEl);
} else {
hmGridEl.appendChild(cellEl);
}
} }
if (nodeIdx < firstDow) { if (newCellsFrag) {
patchClasses(cellEl, { hmGridEl.appendChild(newCellsFrag);
'is-pad': true, }
'is-hovered': false, while (hmGridEl.children.length > totalCells) {
'lv-0': false, hmGridEl.removeChild(hmGridEl.lastChild);
'lv-1': false, }
'lv-2': false,
'lv-3': false, currentHmBuckets = slots;
'lv-4': false, currentHmBucketCells = currentCellEls.map((el) => (el ? [el] : []));
}); currentHmAnchorCells = currentCellEls.slice();
patchAttr(cellEl, 'aria-hidden', 'true'); currentColBucketIdx = [];
patchAttr(cellEl, 'role', null); hoveredCellEl =
patchAttr(cellEl, 'data-slot-idx', null); hoveredIdx >= 0 && hoveredIdx < slots.length ? currentCellEls[hoveredIdx] || null : null;
patchAttr(cellEl, 'data-hm-col', null); hoveredHighlightEls = hoveredCellEl ? [hoveredCellEl] : [];
patchAttr(cellEl, 'data-hm-row', null);
} else { const peakSlot = slots[peakIdx];
const i = nodeIdx - firstDow; lastHeatmapSummaryText =
const slot = slots[i]; maxTokens > 0 && peakSlot?.label
const tok = safeSlotTokens(slot?.tokens); ? t('chart.trend.hmActivePeak', {
const lv = resolveHeatmapLevel(tok, thresholds); active: activeDays,
total: slots.length,
label: peakSlot.label,
tokens: formatTokensPrecise(maxTokens),
})
: t('chart.trend.hmActiveZero', { total: slots.length });
} else {
// 周 / 月 / 全部柱阵模式:保持 7 行 × N 周列点阵几何不变,自底向上点亮对应格数
const barView = buildHeatmapBarView(
slots,
firstDow,
totalWeeks,
currentHmMode,
lastBuildSegmentsFn
);
const totalGridCells = totalWeeks * 7;
currentCellEls = [];
currentHmBuckets = barView.buckets;
currentColBucketIdx = barView.colSpecs.map((s) => s.bucketIdx);
const bucketCount = barView.buckets.length;
const bucketLitCells = Array.from({ length: bucketCount }, () => []);
const bucketBottomCells = Array.from({ length: bucketCount }, () => []);
const bucketTopRowCells = Array.from({ length: bucketCount }, () => []);
const newCellsFrag =
totalGridCells > prevMountedCount &&
typeof document !== 'undefined' &&
typeof document.createDocumentFragment === 'function'
? document.createDocumentFragment()
: null;
for (let nodeIdx = 0; nodeIdx < totalGridCells; nodeIdx++) {
let cellEl = hmCellPool[nodeIdx];
if (!cellEl) {
cellEl = h('span', { className: 'trend-hm-cell' });
hmCellPool.push(cellEl);
}
const col = Math.floor(nodeIdx / 7); const col = Math.floor(nodeIdx / 7);
const row = nodeIdx % 7; const row = nodeIdx % 7;
const spec = barView.colSpecs[col] || {
bucketIdx: 0,
litRows: 0,
lv: 0,
isMonthEdge: false,
};
const bIdx = spec.bucketIdx;
const topLitRow = spec.litRows > 0 ? 7 - spec.litRows : 6;
const isLit = spec.litRows > 0 && row >= topLitRow;
const lv = isLit ? spec.lv : 0;
const isBarHovered =
hoveredIdx === bIdx && (isLit || (spec.litRows === 0 && row === 6));
patchClasses(cellEl, { patchClasses(cellEl, {
'is-pad': false, 'is-pad': false,
'is-hovered': hoveredIdx === i, 'is-hovered': false,
'is-bar-hovered': isBarHovered,
'is-month-edge': Boolean(spec.isMonthEdge),
'lv-0': lv === 0, 'lv-0': lv === 0,
'lv-1': lv === 1, 'lv-1': lv === 1,
'lv-2': lv === 2, 'lv-2': lv === 2,
@@ -1381,59 +1917,88 @@ export function createTrendAreaChart({ onSelectSlot = null } = {}) {
}); });
patchAttr(cellEl, 'aria-hidden', null); patchAttr(cellEl, 'aria-hidden', null);
patchAttr(cellEl, 'role', 'gridcell'); patchAttr(cellEl, 'role', 'gridcell');
patchAttr(cellEl, 'data-slot-idx', String(i)); patchAttr(cellEl, 'data-slot-idx', String(bIdx));
patchAttr(cellEl, 'data-hm-col', String(col)); patchAttr(cellEl, 'data-hm-col', String(col));
patchAttr(cellEl, 'data-hm-row', String(row)); patchAttr(cellEl, 'data-hm-row', String(row));
currentCellEls[i] = cellEl;
}
if (nodeIdx < prevMountedCount) {
if (hmGridEl.children[nodeIdx] !== cellEl) {
hmGridEl.insertBefore(cellEl, hmGridEl.children[nodeIdx] || null);
}
} else if (newCellsFrag) {
newCellsFrag.appendChild(cellEl);
} else {
hmGridEl.appendChild(cellEl);
}
}
if (newCellsFrag) {
hmGridEl.appendChild(newCellsFrag);
}
while (hmGridEl.children.length > totalCells) {
hmGridEl.removeChild(hmGridEl.lastChild);
}
hoveredCellEl =
hoveredIdx >= 0 && hoveredIdx < slots.length ? currentCellEls[hoveredIdx] || null : null;
if (typeof requestAnimationFrame === 'function') { if (bIdx >= 0 && bIdx < bucketCount) {
if (isLit) bucketLitCells[bIdx].push(cellEl);
if (row === 6) bucketBottomCells[bIdx].push(cellEl);
if (row === topLitRow) bucketTopRowCells[bIdx].push(cellEl);
}
if (nodeIdx < prevMountedCount) {
if (hmGridEl.children[nodeIdx] !== cellEl) {
hmGridEl.insertBefore(cellEl, hmGridEl.children[nodeIdx] || null);
}
} else if (newCellsFrag) {
newCellsFrag.appendChild(cellEl);
} else {
hmGridEl.appendChild(cellEl);
}
}
if (newCellsFrag) {
hmGridEl.appendChild(newCellsFrag);
}
while (hmGridEl.children.length > totalGridCells) {
hmGridEl.removeChild(hmGridEl.lastChild);
}
currentHmBucketCells = barView.buckets.map((_, bIdx) =>
bucketLitCells[bIdx].length > 0 ? bucketLitCells[bIdx] : bucketBottomCells[bIdx]
);
currentHmAnchorCells = barView.buckets.map((_, bIdx) => {
const tops = bucketTopRowCells[bIdx];
return tops.length > 0 ? tops[Math.floor(tops.length / 2)] : null;
});
hoveredCellEl =
hoveredIdx >= 0 && hoveredIdx < bucketCount
? currentHmAnchorCells[hoveredIdx] || null
: null;
hoveredHighlightEls =
hoveredIdx >= 0 && hoveredIdx < bucketCount
? currentHmBucketCells[hoveredIdx] || []
: [];
lastHeatmapSummaryText = barView.summaryText;
}
if (shouldAutoScrollRight && typeof requestAnimationFrame === 'function') {
requestAnimationFrame(() => { requestAnimationFrame(() => {
if (hmScrollEl.scrollWidth > hmScrollEl.clientWidth) { if (hmScrollEl.scrollWidth > hmScrollEl.clientWidth) {
hmScrollEl.scrollLeft = hmScrollEl.scrollWidth; hmScrollEl.scrollLeft = hmScrollEl.scrollWidth;
} }
}); });
} }
} else if (currentHmMode === 'day') {
const peakSlot = slots[peakIdx];
lastHeatmapSummaryText =
maxTokens > 0 && peakSlot?.label
? t('chart.trend.hmActivePeak', {
active: activeDays,
total: slots.length,
label: peakSlot.label,
tokens: formatTokensPrecise(maxTokens),
})
: t('chart.trend.hmActiveZero', { total: slots.length });
} }
const peakSlot = slots[peakIdx]; patchText(hmSummaryEl, lastHeatmapSummaryText);
const summaryText =
maxTokens > 0 && peakSlot?.label
? t('chart.trend.hmActivePeak', {
active: activeDays,
total: slots.length,
label: peakSlot.label,
tokens: formatTokensPrecise(maxTokens),
})
: t('chart.trend.hmActiveZero', { total: slots.length });
patchText(hmSummaryEl, summaryText);
if (hoveredIdx >= slots.length) { if (hoveredIdx >= currentHmBuckets.length) {
clearHover(); clearHover();
} else if (hoveredIdx >= 0) { } else if (hoveredIdx >= 0) {
showHeatmapHoverAtIndex(hoveredIdx, currentCellEls[hoveredIdx], lastPointer); if (!tooltipEl.hasAttribute('hidden') && currentHmBuckets[hoveredIdx]) {
populateTooltipContent(currentHmBuckets[hoveredIdx]);
}
showHeatmapHoverAtIndex(hoveredIdx, currentHmAnchorCells[hoveredIdx], lastPointer);
} }
return; return;
} }
if (hoveredIdx >= 0 && hoveredIdx < slots.length && !tooltipEl.hasAttribute('hidden')) {
populateTooltipContent(slots[hoveredIdx]);
}
// --- <= 90 天(或整点/分钟级):渲染平滑面积曲线图(1:1 贴边对齐 + 连续插值形变过渡) --- // --- <= 90 天(或整点/分钟级):渲染平滑面积曲线图(1:1 贴边对齐 + 连续插值形变过渡) ---
currentCellEls = []; currentCellEls = [];
const { leftX, rightX, spanX } = resolvePlotBounds(viewW); const { leftX, rightX, spanX } = resolvePlotBounds(viewW);
@@ -1590,5 +2155,21 @@ export function createTrendAreaChart({ onSelectSlot = null } = {}) {
} }
} }
return { el: chartWrap, update, destroy }; function setHeatmapMode(nextMode) {
const norm = normalizeHeatmapMode(nextMode);
if (norm === currentHmMode) return;
clearHover();
currentHmMode = norm;
if (currentSlots.length > 0) {
update(currentSlots);
}
}
return {
el: chartWrap,
update,
setHeatmapMode,
getHeatmapMode: () => currentHmMode,
destroy,
};
} }
+11 -1
View File
@@ -271,7 +271,11 @@ export const I18N_MESSAGES_EN_US = Object.freeze({
'cons.replyAndThink': 'Think {think} · Reply {resp}', 'cons.replyAndThink': 'Think {think} · Reply {resp}',
'cons.trendTitle': 'Usage Trend', 'cons.trendTitle': 'Usage Trend',
'cons.allTime': 'All Time', 'cons.allTime': 'All Time',
'cons.trendHeatmapHint': 'Daily ({days}d) · Hover for details', 'cons.hmModeAria': 'Heatmap view mode',
'cons.hmModeDay': 'Day',
'cons.hmModeWeek': 'Week',
'cons.hmModeMonth': 'Month',
'cons.hmModeAll': 'Total',
'cons.trendPointsHintMinute': '{minutes}m ({points} pts) · Hover for details', 'cons.trendPointsHintMinute': '{minutes}m ({points} pts) · Hover for details',
'cons.trendPointsHintHourly': 'Hourly ({points} pts) · Hover for details', 'cons.trendPointsHintHourly': 'Hourly ({points} pts) · Hover for details',
'cons.trendPointsHintDaily': 'Daily ({points} pts) · Hover for details', 'cons.trendPointsHintDaily': 'Daily ({points} pts) · Hover for details',
@@ -882,6 +886,12 @@ export const I18N_MESSAGES_EN_US = Object.freeze({
'chart.trend.hmMore': 'More', 'chart.trend.hmMore': 'More',
'chart.trend.hmActivePeak': 'Active {active}/{total}d · Peak {label} · {tokens}', 'chart.trend.hmActivePeak': 'Active {active}/{total}d · Peak {label} · {tokens}',
'chart.trend.hmActiveZero': 'Active 0/{total}d', 'chart.trend.hmActiveZero': 'Active 0/{total}d',
'chart.trend.hmWeekPeak': 'Active {active}/{total}w · Peak {label} · {tokens}',
'chart.trend.hmWeekZero': 'Active 0/{total}w',
'chart.trend.hmMonthPeak': 'Active {active}/{total}m · Peak {label} · {tokens}',
'chart.trend.hmMonthZero': 'Active 0/{total}m',
'chart.trend.hmCumSummary': 'Cumulative {days}d · Total {tokens} · {cost}',
'chart.trend.hmCumThrough': 'Cumulative through {date}',
'chart.trend.peakPill': 'Peak {label} · {tokens}', 'chart.trend.peakPill': 'Peak {label} · {tokens}',
// Base UI Controls (CustomDateTimePicker, CustomNumberInput, SegmentedControl) // Base UI Controls (CustomDateTimePicker, CustomNumberInput, SegmentedControl)
+11 -1
View File
@@ -271,7 +271,11 @@ export const I18N_MESSAGES_ZH_CN = Object.freeze({
'cons.replyAndThink': '思考 {think} · 回复 {resp}', 'cons.replyAndThink': '思考 {think} · 回复 {resp}',
'cons.trendTitle': '用量趋势', 'cons.trendTitle': '用量趋势',
'cons.allTime': '全部时间', 'cons.allTime': '全部时间',
'cons.trendHeatmapHint': '按日 ({days}天) · 悬停查看', 'cons.hmModeAria': '热力图查看维度',
'cons.hmModeDay': '日',
'cons.hmModeWeek': '周',
'cons.hmModeMonth': '月',
'cons.hmModeAll': '全部',
'cons.trendPointsHintMinute': '按 {minutes} 分钟 ({points}点) · 悬停查看', 'cons.trendPointsHintMinute': '按 {minutes} 分钟 ({points}点) · 悬停查看',
'cons.trendPointsHintHourly': '按小时 ({points}点) · 悬停查看', 'cons.trendPointsHintHourly': '按小时 ({points}点) · 悬停查看',
'cons.trendPointsHintDaily': '按日 ({points}点) · 悬停查看', 'cons.trendPointsHintDaily': '按日 ({points}点) · 悬停查看',
@@ -882,6 +886,12 @@ export const I18N_MESSAGES_ZH_CN = Object.freeze({
'chart.trend.hmMore': '多', 'chart.trend.hmMore': '多',
'chart.trend.hmActivePeak': '活跃 {active}/{total} 天 · 峰值 {label} · {tokens}', 'chart.trend.hmActivePeak': '活跃 {active}/{total} 天 · 峰值 {label} · {tokens}',
'chart.trend.hmActiveZero': '活跃 0/{total} 天', 'chart.trend.hmActiveZero': '活跃 0/{total} 天',
'chart.trend.hmWeekPeak': '活跃 {active}/{total} 周 · 峰值 {label} · {tokens}',
'chart.trend.hmWeekZero': '活跃 0/{total} 周',
'chart.trend.hmMonthPeak': '活跃 {active}/{total} 月 · 峰值 {label} · {tokens}',
'chart.trend.hmMonthZero': '活跃 0/{total} 月',
'chart.trend.hmCumSummary': '累计 {days} 天 · 总计 {tokens} · {cost}',
'chart.trend.hmCumThrough': '截至 {date} 累计',
'chart.trend.peakPill': '峰值 {label} · {tokens}', 'chart.trend.peakPill': '峰值 {label} · {tokens}',
// Base UI Controls (CustomDateTimePicker, CustomNumberInput, SegmentedControl) // Base UI Controls (CustomDateTimePicker, CustomNumberInput, SegmentedControl)
@@ -352,6 +352,31 @@
} }
/* --- 4. 历史趋势交互式 SVG 平滑曲线图 (Interactive Historical Trend SVG Chart) --- */ /* --- 4. 历史趋势交互式 SVG 平滑曲线图 (Interactive Historical Trend SVG Chart) --- */
.cons-trend-head {
flex-wrap: wrap;
gap: 6px;
}
.cons-trend-head-right {
display: inline-flex;
align-items: center;
margin-left: auto;
}
.cons-trend-hint[hidden],
.cons-hm-mode-switch[hidden] {
display: none !important;
}
.cons-hm-mode-switch.segmented {
gap: 1px;
}
.cons-hm-mode-switch .seg-btn {
padding: 1px 7px;
font-size: var(--fs-xs);
}
.trend-chart-wrap { .trend-chart-wrap {
position: relative; position: relative;
width: 100%; width: 100%;
@@ -801,6 +826,20 @@
z-index: 2; z-index: 2;
} }
.trend-hm-cell.is-bar-hovered {
filter: brightness(1.22);
box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--text-primary) 52%, transparent);
z-index: 2;
}
.trend-hm-cell.is-month-edge:not(.lv-0) {
box-shadow: inset 1px 0 0 color-mix(in srgb, var(--surface-panel) 55%, transparent);
}
.trend-hm-cell.is-month-edge.is-bar-hovered:not(.lv-0) {
box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--text-primary) 52%, transparent);
}
.trend-hm-footer { .trend-hm-footer {
display: flex; display: flex;
align-items: center; align-items: center;
+61 -15
View File
@@ -81,6 +81,16 @@ function getConsumptionTimeRangeOptions() {
]; ];
} }
/** 热力图顶栏支持的四档聚合视图选项 */
function getHeatmapModeOptions() {
return [
{ value: 'day', label: t('cons.hmModeDay') },
{ value: 'week', label: t('cons.hmModeWeek') },
{ value: 'month', label: t('cons.hmModeMonth') },
{ value: 'all', label: t('cons.hmModeAll') },
];
}
function floorToStartOfMinuteMs(ms) { function floorToStartOfMinuteMs(ms) {
const n = Number(ms); const n = Number(ms);
if (!(n > 0)) return 0; if (!(n > 0)) return 0;
@@ -460,14 +470,29 @@ export const ConsumptionView = defineComponent({
}); });
} }
// --- Section 2: 历史趋势 (交互式 SVG 平滑曲线图 + 悬停精确日期/小时浮层 + 点击时段下钻) --- // --- Section 2: 历史趋势 (交互式 SVG 平滑曲线图 / 7×W 四态热力格子图 + 悬停明细浮层 + 点击时段下钻) ---
let heatmapViewMode = 'day';
const trendTitleSpan = h('span', { text: t('cons.trendTitle') }); const trendTitleSpan = h('span', { text: t('cons.trendTitle') });
const trendRangeBadge = h('span', { className: 'cons-badge', text: t('cons.allTime') }); const trendRangeBadge = h('span', { className: 'cons-badge', text: t('cons.allTime') });
const trendGranularityHint = h('span', { const trendGranularityHint = h('span', {
className: 'text-muted text-mono', className: 'cons-trend-hint text-muted text-mono',
style: 'font-size: var(--fs-sm);', style: 'font-size: var(--fs-sm);',
text: t('cons.trendPointsHintHourly', { points: 0 }), text: t('cons.trendPointsHintHourly', { points: 0 }),
}); });
const heatmapModeSwitch = SegmentedControl({
ariaLabel: t('cons.hmModeAria'),
modelValue: heatmapViewMode,
options: getHeatmapModeOptions(),
onUpdateModelValue: (nextMode) => {
if (!['day', 'week', 'month', 'all'].includes(nextMode)) return;
heatmapViewMode = nextMode;
heatmapModeSwitch.setValue(nextMode);
trendChart.setHeatmapMode(nextMode);
},
});
heatmapModeSwitch.el.classList.add('cons-hm-mode-switch');
patchAttr(heatmapModeSwitch.el, 'hidden', true);
const trendChart = createTrendAreaChart({ const trendChart = createTrendAreaChart({
onSelectSlot: (slot) => { onSelectSlot: (slot) => {
if (slot && Number(slot.startMs) > 0 && Number(slot.endMs) > Number(slot.startMs)) { if (slot && Number(slot.startMs) > 0 && Number(slot.endMs) > Number(slot.startMs)) {
@@ -477,12 +502,15 @@ export const ConsumptionView = defineComponent({
}); });
const trendPanel = h('section', { className: 'panel' }, [ const trendPanel = h('section', { className: 'panel' }, [
h('div', { className: 'panel-header' }, [ h('div', { className: 'panel-header cons-trend-head' }, [
h('span', { className: 'panel-title' }, [ h('span', { className: 'panel-title' }, [
trendTitleSpan, trendTitleSpan,
trendRangeBadge, trendRangeBadge,
]), ]),
trendGranularityHint, h('div', { className: 'cons-trend-head-right' }, [
trendGranularityHint,
heatmapModeSwitch.el,
]),
]), ]),
trendChart.el, trendChart.el,
]); ]);
@@ -2204,11 +2232,12 @@ export const ConsumptionView = defineComponent({
(firstTrendSlot?.label || '').includes(':')); (firstTrendSlot?.label || '').includes(':'));
const isHeatmapTrend = const isHeatmapTrend =
!isMinuteTrend && !isHourlyTrend && rawTrendSeries.length > HEATMAP_DAYS_THRESHOLD; !isMinuteTrend && !isHourlyTrend && rawTrendSeries.length > HEATMAP_DAYS_THRESHOLD;
patchText( patchAttr(trendGranularityHint, 'hidden', isHeatmapTrend);
trendGranularityHint, patchAttr(heatmapModeSwitch.el, 'hidden', !isHeatmapTrend);
isHeatmapTrend if (!isHeatmapTrend) {
? t('cons.trendHeatmapHint', { days: rawTrendSeries.length }) patchText(
: isMinuteTrend trendGranularityHint,
isMinuteTrend
? t('cons.trendPointsHintMinute', { ? t('cons.trendPointsHintMinute', {
minutes: Math.max(1, Number(firstTrendSlot?.stepMinutes) || 5), minutes: Math.max(1, Number(firstTrendSlot?.stepMinutes) || 5),
points: rawTrendSeries.length || 0, points: rawTrendSeries.length || 0,
@@ -2216,19 +2245,28 @@ export const ConsumptionView = defineComponent({
: isHourlyTrend : isHourlyTrend
? t('cons.trendPointsHintHourly', { points: rawTrendSeries.length || 0 }) ? t('cons.trendPointsHintHourly', { points: rawTrendSeries.length || 0 })
: t('cons.trendPointsHintDaily', { points: rawTrendSeries.length || 0 }) : t('cons.trendPointsHintDaily', { points: rawTrendSeries.length || 0 })
); );
} else {
heatmapModeSwitch.setValue(heatmapViewMode);
}
const buildTrendModelSegments = (mergedSlotModels) =>
partitionModelSegments(mergedSlotModels, modelColorMap, {
maxMain: 5,
minSharePct: 1.5,
});
const enrichedTrendSeries = rawTrendSeries.map((slot, idx) => { const enrichedTrendSeries = rawTrendSeries.map((slot, idx) => {
const mergedSlotModels = trendMergedByIdx[idx]; const mergedSlotModels = trendMergedByIdx[idx];
if (!mergedSlotModels || mergedSlotModels.length === 0) return slot; if (!mergedSlotModels || mergedSlotModels.length === 0) return slot;
return { return {
...slot, ...slot,
modelSegments: partitionModelSegments(mergedSlotModels, modelColorMap, { mergedModels: mergedSlotModels,
maxMain: 5, modelSegments: buildTrendModelSegments(mergedSlotModels),
minSharePct: 1.5,
}),
}; };
}); });
trendChart.update(enrichedTrendSeries); trendChart.update(enrichedTrendSeries, {
heatmapMode: heatmapViewMode,
buildModelSegments: buildTrendModelSegments,
});
// 3. 同步统一排行卡片(分段切换「模型排行 | 项目排行 | 会话排行」及对应排序控制) // 3. 同步统一排行卡片(分段切换「模型排行 | 项目排行 | 会话排行」及对应排序控制)
const sortedTopConvs = [...topConvsWithModels].sort((a, b) => const sortedTopConvs = [...topConvsWithModels].sort((a, b) =>
@@ -2563,6 +2601,11 @@ export const ConsumptionView = defineComponent({
md: { label: t('cons.exportMd'), title: t('cons.exportMdTip') }, md: { label: t('cons.exportMd'), title: t('cons.exportMdTip') },
}); });
patchText(trendTitleSpan, t('cons.trendTitle')); patchText(trendTitleSpan, t('cons.trendTitle'));
heatmapModeSwitch.setProps({
ariaLabel: t('cons.hmModeAria'),
modelValue: heatmapViewMode,
options: getHeatmapModeOptions(),
});
rankingTabSwitch.setProps({ rankingTabSwitch.setProps({
ariaLabel: t('cons.rankTabAria'), ariaLabel: t('cons.rankTabAria'),
modelValue: activeRankingTab, modelValue: activeRankingTab,
@@ -2607,6 +2650,7 @@ export const ConsumptionView = defineComponent({
unsubI18n(); unsubI18n();
consExportMenu.destroy(); consExportMenu.destroy();
cyclePoolSelect.destroy(); cyclePoolSelect.destroy();
heatmapModeSwitch.destroy();
trendChart.destroy?.(); trendChart.destroy?.();
rankingTabSwitch.destroy(); rankingTabSwitch.destroy();
modelsSortSwitch.destroy(); modelsSortSwitch.destroy();
@@ -2622,6 +2666,8 @@ export const ConsumptionView = defineComponent({
refs: { refs: {
timeFilterSwitch, timeFilterSwitch,
cyclePoolSelect, cyclePoolSelect,
heatmapModeSwitch,
trendChart,
startPicker, startPicker,
endPicker, endPicker,
viewScrollbar, viewScrollbar,
+39
View File
@@ -3151,6 +3151,31 @@ html[data-standalone="true"] .app-shell {
} }
/* --- 4. 历史趋势交互式 SVG 平滑曲线图 (Interactive Historical Trend SVG Chart) --- */ /* --- 4. 历史趋势交互式 SVG 平滑曲线图 (Interactive Historical Trend SVG Chart) --- */
.cons-trend-head {
flex-wrap: wrap;
gap: 6px;
}
.cons-trend-head-right {
display: inline-flex;
align-items: center;
margin-left: auto;
}
.cons-trend-hint[hidden],
.cons-hm-mode-switch[hidden] {
display: none !important;
}
.cons-hm-mode-switch.segmented {
gap: 1px;
}
.cons-hm-mode-switch .seg-btn {
padding: 1px 7px;
font-size: var(--fs-xs);
}
.trend-chart-wrap { .trend-chart-wrap {
position: relative; position: relative;
width: 100%; width: 100%;
@@ -3600,6 +3625,20 @@ html[data-standalone="true"] .app-shell {
z-index: 2; z-index: 2;
} }
.trend-hm-cell.is-bar-hovered {
filter: brightness(1.22);
box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--text-primary) 52%, transparent);
z-index: 2;
}
.trend-hm-cell.is-month-edge:not(.lv-0) {
box-shadow: inset 1px 0 0 color-mix(in srgb, var(--surface-panel) 55%, transparent);
}
.trend-hm-cell.is-month-edge.is-bar-hovered:not(.lv-0) {
box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--text-primary) 52%, transparent);
}
.trend-hm-footer { .trend-hm-footer {
display: flex; display: flex;
align-items: center; align-items: center;
@@ -736,3 +736,235 @@ test('selectConsumptionTimeRange serves prefetched presets with 0 network calls,
); );
chart.destroy(); chart.destroy();
}); });
test('ConsumptionView >90d heatmap replaces static hint with Day/Week/Month/Total 7xW LED matrix switcher, rich tooltips, and drill-down', async (t) => {
const prevLocale = i18nStore.getState().locale;
const prevDoc = globalThis.document;
const prevWin = globalThis.window;
const prevTelState = { ...telemetryStore.getState() };
const prevUiState = { ...uiStore.getState() };
const { document } = createMockDom();
globalThis.document = document;
globalThis.window = {
location: { pathname: '/' },
addEventListener() {},
removeEventListener() {},
requestAnimationFrame: (cb) => setTimeout(cb, 0),
cancelAnimationFrame: (id) => clearTimeout(id),
};
let view = null;
t.after(() => {
stopAsyncTelemetryLoop();
clearConsumptionRangeCache();
if (view) view.destroy();
setI18nLanguage(prevLocale);
telemetryStore.setState(prevTelState, { sync: true });
uiStore.setState(prevUiState, { sync: true });
globalThis.document = prevDoc;
globalThis.window = prevWin;
});
t.mock.method(globalThis, 'fetch', async () => ({
ok: true,
json: async () => ({ scope: 'noop' }),
}));
setI18nLanguage('zh-CN');
uiStore.setState(
{ monitorVisible: true, activeView: 'consumption', settingsOpen: false },
{ sync: true }
);
// Build 105 daily slots (15 exact weeks starting Monday 2026-08-03 local midnight)
const firstMondayMs = new Date(2026, 7, 3, 0, 0, 0, 0).getTime();
const dayMs = 86_400_000;
const pad2 = (n) => String(n).padStart(2, '0');
const trendSlots = Array.from({ length: 105 }, (_, i) => {
const sMs = firstMondayMs + i * dayMs;
const d = new Date(sMs);
const lbl = `${pad2(d.getMonth() + 1)}/${pad2(d.getDate())}`;
const weekIdx = Math.floor(i / 7);
// Week 0 = 0 tokens; Week 1 = 20k/day (140k total); Week 2 = peak 100k/day (700k total); others = 40k/day
const dailyTok =
weekIdx === 0 ? 0 : weekIdx === 1 ? 20_000 : weekIdx === 2 ? 100_000 : 40_000;
return {
index: i,
granularity: 'day',
startMs: sMs,
endMs: sMs + dayMs,
label: lbl,
fullLabel: lbl,
tokens: dailyTok,
inputTokens: Math.floor(dailyTok * 0.25),
cacheReadTokens: Math.floor(dailyTok * 0.5),
thinkingTokens: Math.floor(dailyTok * 0.1),
responseTokens: Math.floor(dailyTok * 0.15),
outputTokens: Math.floor(dailyTok * 0.25),
calls: dailyTok > 0 ? 4 : 0,
costUsd: dailyTok > 0 ? +(dailyTok / 100_000).toFixed(2) : 0,
models:
dailyTok > 0
? [
{
label: 'Claude Opus 4.6',
shortName: 'Opus 4.6',
upstreamName: 'opus-4-6',
calls: 4,
inputTokens: Math.floor(dailyTok * 0.25),
cacheReadTokens: Math.floor(dailyTok * 0.5),
thinkingTokens: Math.floor(dailyTok * 0.1),
responseTokens: Math.floor(dailyTok * 0.15),
outputTokens: Math.floor(dailyTok * 0.25),
totalTokens: dailyTok,
costUsd: +(dailyTok / 100_000).toFixed(2),
},
]
: undefined,
};
});
const makePayload = (rangeKey, slots) => ({
settings: { language: 'zh-CN', modelDisplayMode: 'short' },
modelBreakdown: [],
consumption: {
revision: 1,
selectedRange: {
key: rangeKey,
label: rangeKey === 'all' ? '全部时间' : '近 7 天',
startMs: slots[0].startMs,
endMs: slots[slots.length - 1].endMs - 1,
},
account: {
totalTokens: 4_480_000,
inputTokens: 1_120_000,
cacheReadTokens: 2_240_000,
outputTokens: 1_120_000,
responseTokens: 672_000,
thinkingTokens: 448_000,
calls: 392,
costUsd: 44.8,
savedCostUsd: 12.0,
cacheHitPct: 66.7,
},
trendSeries: slots,
monthlyYear: 2026,
monthlySeries: [],
sourcesBreakdown: [],
},
});
// 1. <=90d (7 days): static hint visible, heatmap switcher hidden
telemetryStore.setState(
{ selectedTimeRange: '7d', telemetry: makePayload('7d', trendSlots.slice(0, 7)) },
{ sync: true }
);
view = ConsumptionView({});
document.body.appendChild(view.el);
await Promise.resolve();
const trendHintEl = view.el.querySelector('.cons-trend-hint');
const hmSwitchEl = view.refs.heatmapModeSwitch.el;
assert.equal(trendHintEl.hasAttribute('hidden'), false);
assert.equal(hmSwitchEl.hasAttribute('hidden'), true);
// 2. >90d (105 days = 15 weeks): static hint hidden, 4-way switcher visible ('日' / '周' / '月' / '全部')
telemetryStore.setState(
{ selectedTimeRange: 'all', telemetry: makePayload('all', trendSlots) },
{ sync: true }
);
await Promise.resolve();
assert.equal(trendHintEl.hasAttribute('hidden'), true, 'Static heatmap hint must be hidden on >90d');
assert.equal(hmSwitchEl.hasAttribute('hidden'), false, '4-mode switcher must be shown on >90d');
const modeBtns = hmSwitchEl.querySelectorAll('.seg-btn');
assert.deepEqual(
modeBtns.map((b) => b.textContent),
['日', '周', '月', '全部']
);
const hmWeekdaysEl = view.el.querySelector('.trend-hm-weekdays');
const hmGridEl = view.el.querySelector('.trend-hm-grid');
const hmSummaryEl = view.el.querySelector('.trend-hm-summary');
const trendTip = view.el.querySelector('.trend-tooltip');
// Default 'day' mode: weekday labels visible, 105 cells
assert.equal(view.refs.trendChart.getHeatmapMode(), 'day');
assert.equal(hmWeekdaysEl.style.visibility, '');
assert.equal(hmGridEl.children.length, 105);
assert.match(hmSummaryEl.textContent || '', /活跃 98\/105 天/);
// 3. Switch to 'week' mode: keeps exact 105-cell (7x15) grid, hides weekday labels in-place, lights bottom-up LED columns
modeBtns[1].dispatchEvent({ type: 'click', stopPropagation() {} });
assert.equal(view.refs.trendChart.getHeatmapMode(), 'week');
assert.equal(hmWeekdaysEl.style.visibility, 'hidden');
assert.equal(hmGridEl.children.length, 105);
assert.match(hmSummaryEl.textContent || '', /活跃 14\/15 周/);
const colCells = (col) =>
[...hmGridEl.children].filter((c) => Number(c.getAttribute('data-hm-col')) === col);
const litRowsInCol = (col) =>
colCells(col)
.filter((c) => !c.classList.contains('lv-0'))
.map((c) => Number(c.getAttribute('data-hm-row')));
// Week 0 has 0 tokens -> 0 lit rows; Week 1 has 140k/700k = 20% -> ceil(1.4) = 2 bottom rows [5, 6]; Week 2 is peak 700k -> all 7 rows [0..6]
assert.deepEqual(litRowsInCol(0), []);
assert.deepEqual(litRowsInCol(1), [5, 6]);
assert.deepEqual(litRowsInCol(2), [0, 1, 2, 3, 4, 5, 6]);
// Hovering top unlit cell (row 0) of Week 1 still highlights Week 1's lit bar [row 5, row 6] and shows aggregated weekly tooltip
hmGridEl.dispatchEvent({ type: 'mouseover', target: colCells(1)[0], clientX: 40, clientY: 20 });
assert.equal(trendTip.hasAttribute('hidden'), false);
assert.equal(trendTip.querySelector('.trend-tip-time')?.textContent, '8月10日 ~ 16日');
assert.match(trendTip.querySelector('.trend-tip-tokens')?.textContent || '', /140\.0k TOKEN/);
assert.equal(trendTip.querySelectorAll('.cons-pop-model-row').length, 1);
assert.equal(colCells(1)[5].classList.contains('is-bar-hovered'), true);
assert.equal(colCells(1)[6].classList.contains('is-bar-hovered'), true);
assert.equal(colCells(1)[0].classList.contains('is-bar-hovered'), false);
// 4. Switch to 'month' mode: columns group by dominant calendar month with month-edge markers and monthly tooltip
modeBtns[2].dispatchEvent({ type: 'click', stopPropagation() {} });
assert.equal(view.refs.trendChart.getHeatmapMode(), 'month');
assert.equal(hmGridEl.children.length, 105);
assert.match(hmSummaryEl.textContent || '', /月 · 峰值/);
assert.ok(
[...hmGridEl.children].some((c) => c.classList.contains('is-month-edge')),
'Month mode should mark boundary columns between adjacent months'
);
hmGridEl.dispatchEvent({ type: 'mouseover', target: colCells(0)[6], clientX: 20, clientY: 60 });
assert.equal(trendTip.querySelector('.trend-tip-time')?.textContent, '2026年 8月');
// 5. Switch to 'all' (cumulative) mode: monotonic bottom-up growth ramp reaching 7 rows on last column
modeBtns[3].dispatchEvent({ type: 'click', stopPropagation() {} });
assert.equal(view.refs.trendChart.getHeatmapMode(), 'all');
assert.equal(hmGridEl.children.length, 105);
let prevLitCount = 0;
for (let col = 0; col < 15; col++) {
const cnt = litRowsInCol(col).length;
assert.ok(cnt >= prevLitCount, `Cumulative column ${col} must be non-decreasing`);
prevLitCount = cnt;
}
assert.equal(prevLitCount, 7, 'Final cumulative column must reach full 7-row height');
hmGridEl.dispatchEvent({ type: 'mouseover', target: colCells(14)[0], clientX: 200, clientY: 20 });
assert.match(trendTip.querySelector('.trend-tip-time')?.textContent || '', /^截至 .+ 累计$/);
assert.match(hmSummaryEl.textContent || '', /^累计 105 天/);
// 6. Locale switch updates switcher labels ('Day' / 'Week' / 'Month' / 'Total') and summary text
setI18nLanguage('en-US');
await Promise.resolve();
assert.deepEqual(
hmSwitchEl.querySelectorAll('.seg-btn').map((b) => b.textContent),
['Day', 'Week', 'Month', 'Total']
);
assert.match(hmSummaryEl.textContent || '', /^Cumulative 105d/);
// 7. Clicking a week bar in 'week' mode drills down into custom time range for that week
setI18nLanguage('zh-CN');
await Promise.resolve();
hmSwitchEl.querySelectorAll('.seg-btn')[1].dispatchEvent({ type: 'click', stopPropagation() {} });
hmGridEl.dispatchEvent({ type: 'click', target: colCells(2)[3], clientX: 60, clientY: 40 });
assert.equal(telemetryStore.getState().selectedTimeRange, 'custom');
assert.equal(telemetryStore.getState().customStartMs, firstMondayMs + 14 * dayMs);
});