fix(panel): format boundary 0.0% and 100.0% percentages as 0% and 100%

This commit is contained in:
fish
2026-11-22 21:51:55 +09:00
parent 743bdfa24d
commit 8b9fe3d92f
11 changed files with 155 additions and 82 deletions
@@ -12,6 +12,7 @@ import { h, patchAttr, patchClasses, patchStyleVar, patchText } from '../../core
import {
formatAgentBadgeText,
formatMetric,
formatPctNumber,
formatSpeedMetric,
formatStepAnomalyInlineSummary,
formatTokens,
@@ -573,7 +574,7 @@ function formatYAxisTick(metric, val, step) {
return step >= 1 ? `${Math.round(val)}s` : `${val.toFixed(1)}s`;
}
if (metric === 'cache') {
return step >= 1 ? `${Math.round(val)}%` : `${val.toFixed(1)}%`;
return step >= 1 ? `${Math.round(val)}%` : formatPctNumber(val, { withSymbol: true });
}
if (metric === 'cost') {
if (step >= 1) return `$${Math.round(val)}`;
@@ -648,7 +649,7 @@ function formatChartMetricValue(metric, val) {
case 'ttft':
return formatMetric(val, 's');
case 'cache':
return `${val.toFixed(1)}%`;
return formatPctNumber(val, { withSymbol: true });
case 'cost':
return formatUsd(val, { precise: true });
default:
@@ -667,7 +668,7 @@ function formatChartMetricCompact(metric, val) {
case 'ttft':
return formatMetric(val, 's');
case 'cache':
return `${val.toFixed(1)}%`;
return formatPctNumber(val, { withSymbol: true });
case 'cost':
return formatUsd(val, { precise: true });
default:
@@ -6,7 +6,7 @@
import { defineComponent } from '../../core/component.js';
import { h, patchAttr, patchClasses, patchStyleVar } from '../../core/domPatch.js';
import { formatTokens } from '../../core/formatters.js';
import { formatPctNumber, formatTokens } from '../../core/formatters.js';
import { t } from '../../i18n/index.js';
const SEGMENT_DEFS = [
@@ -84,7 +84,7 @@ export const ContextSegmentMeter = defineComponent({
slotEl,
'title',
isActive
? `${t(item.def.labelKey)} · ${formatTokens(item.val)} (${item.pct.toFixed(1)}%)`
? `${t(item.def.labelKey)} · ${formatTokens(item.val)} (${formatPctNumber(item.pct, { withSymbol: true })})`
: null
);
}
@@ -13,7 +13,12 @@ import {
patchStyleVar,
patchText,
} from '../../core/domPatch.js';
import { formatCountdown, formatTokens, formatUsd } from '../../core/formatters.js';
import {
formatCountdown,
formatPctNumber,
formatTokens,
formatUsd,
} from '../../core/formatters.js';
import { createSvgIcon } from '../../icons/SvgIcon.js';
import { getEffectiveLocale, t } from '../../i18n/index.js';
@@ -189,8 +194,8 @@ function syncQuotaWindowMeter({
const resetStr = hasData ? formatCountdown(resetTime) : '';
if (hasData && rsvVal > 0) {
const rsvStr = rsvVal.toFixed(1);
const actualStr = actualVal.toFixed(1);
const rsvStr = formatPctNumber(rsvVal);
const actualStr = formatPctNumber(actualVal);
const leases = Math.max(1, Number(leaseCount) || 1);
const tipText =
leases >= 2
@@ -198,13 +203,13 @@ function syncQuotaWindowMeter({
: t('quota.reservedTip', { rsv: rsvStr, actual: actualStr });
patchText(
metaBeforeEl,
t('quota.usedBeforeReserved', { reset: resetStr, used: usedVal.toFixed(1) })
t('quota.usedBeforeReserved', { reset: resetStr, used: formatPctNumber(usedVal) })
);
metaReservedEl.hidden = false;
patchText(metaReservedEl, t('quota.reservedTag', { rsv: rsvStr }));
patchAttr(metaReservedEl, 'title', tipText);
metaAfterEl.hidden = false;
patchText(metaAfterEl, t('quota.usedAfterReserved', { rem: remVal.toFixed(1) }));
patchText(metaAfterEl, t('quota.usedAfterReserved', { rem: formatPctNumber(remVal) }));
patchAttr(trackEl, 'title', tipText);
const actualWidth = +Math.min(100, actualVal).toFixed(2);
const rsvWidth = +Math.min(Math.max(0, 100 - actualWidth), rsvVal).toFixed(2);
@@ -217,8 +222,8 @@ function syncQuotaWindowMeter({
hasData
? t('quota.usedAndLeft', {
reset: resetStr,
used: usedVal.toFixed(1),
rem: remVal.toFixed(1),
used: formatPctNumber(usedVal),
rem: formatPctNumber(remVal),
})
: fallbackMetaText
);
+20
View File
@@ -37,6 +37,26 @@ export function formatCachePct(value) {
return `${num}%`;
}
/**
* 格式化百分比数值文本:常规值保留 1 位小数,将边界值 `0.0` / `100.0` 收口为整数 `0` / `100`。
*
* @param {number|string|null|undefined} value
* @param {{ withSymbol?: boolean }} [options]
* @returns {string}
*/
export function formatPctNumber(value, { withSymbol = false } = {}) {
const num = Number(value);
if (!Number.isFinite(num)) return withSymbol ? '0%' : '0';
const rounded = Math.round(num * 10) / 10;
const str =
Math.abs(rounded) < 1e-9
? '0'
: Math.abs(rounded - 100) < 1e-9
? '100'
: rounded.toFixed(1);
return withSymbol ? `${str}%` : str;
}
export function getSpeedToneClass(tokPerSec) {
return `tone-${classifySpeedTier(tokPerSec)}`;
}
+17 -14
View File
@@ -8,6 +8,7 @@ import {
formatAnomalyKindLabel,
formatCompactDateTime,
formatMetric,
formatPctNumber,
formatSpeedMetric,
formatTokensPrecise,
formatUsd,
@@ -429,10 +430,10 @@ export function buildStepsCsv(steps, { displaySettings = {}, includeBom = true }
const promptTok = inTok + cacheTok;
const cachePct =
st.cacheHitPct != null && Number.isFinite(Number(st.cacheHitPct))
? Number(st.cacheHitPct).toFixed(1)
? formatPctNumber(st.cacheHitPct)
: promptTok > 0
? ((cacheTok / promptTok) * 100).toFixed(1)
: '0.0';
? formatPctNumber((cacheTok / promptTok) * 100)
: '0';
const ctxTok = Number(st.contextTokens || promptTok + outTok);
const anomCount = Number(st.anomaly?.count || 0);
const anomDesc =
@@ -693,7 +694,7 @@ export function buildStepsMarkdownReport(
const agg = summarizeStepsForExport(list, displaySettings);
const totalTok = agg.sumIn + agg.sumCache + agg.sumOut;
const cacheHitPct = (computeCacheHitRatio(agg.sumIn, agg.sumCache) * 100).toFixed(1);
const cacheHitPct = formatPctNumber(computeCacheHitRatio(agg.sumIn, agg.sumCache) * 100);
const avgTps = agg.tpsCnt > 0 ? formatSpeedMetric(agg.tpsSum / agg.tpsCnt, ' tok/s') : '—';
const avgTtft = agg.ttftCnt > 0 ? formatMetric(agg.ttftSum / agg.ttftCnt, 's') : '—';
const heading = title
@@ -734,7 +735,9 @@ export function buildStepsMarkdownReport(
);
for (const m of agg.sortedModels) {
const mTotal = m.inTok + m.cacheTok + m.outTok;
const mCache = `${(computeCacheHitRatio(m.inTok, m.cacheTok) * 100).toFixed(1)}%`;
const mCache = formatPctNumber(computeCacheHitRatio(m.inTok, m.cacheTok) * 100, {
withSymbol: true,
});
const mTps = m.tpsCnt > 0 ? formatSpeedMetric(m.tpsSum / m.tpsCnt, ' tok/s') : '—';
lines.push(
`| ${escapeMdCell(m.label)} | ${m.calls} | ${formatTokensPrecise(mTotal)} | ${mCache} | ${mTps} | ${formatUsd(m.cost, { precise: m.cost > 0 && m.cost < 0.1 })} |`
@@ -835,7 +838,7 @@ export function buildConsumptionCsv(
Number(acc.cacheReadTokens || 0),
Number(acc.outputTokens || 0),
Number(acc.totalTokens || 0),
Number(acc.cacheHitPct || 0).toFixed(1),
formatPctNumber(acc.cacheHitPct || 0),
Number(acc.costUsd || 0).toFixed(4),
`saved_usd=${Number(acc.savedCostUsd || 0).toFixed(4)}`,
]),
@@ -851,7 +854,7 @@ export function buildConsumptionCsv(
Number(m.cacheReadTokens || 0),
Number(m.outputTokens || 0),
Number(m.totalTokens || 0),
Number(m.cacheHitPct || 0).toFixed(1),
formatPctNumber(m.cacheHitPct || 0),
Number(m.costUsd || 0).toFixed(4),
m.avgTokPerSec != null ? `avg_tps=${Number(m.avgTokPerSec).toFixed(1)}` : '',
])
@@ -868,7 +871,7 @@ export function buildConsumptionCsv(
Number(w.cacheReadTokens || 0),
Number(w.responseTokens || 0) + Number(w.thinkingTokens || 0),
Number(w.totalTokens || 0),
Number(w.cacheHitPct || 0).toFixed(1),
formatPctNumber(w.cacheHitPct || 0),
Number(w.costUsd || 0).toFixed(4),
`sessions=${Number(w.sessionCount || 0)}`,
])
@@ -885,7 +888,7 @@ export function buildConsumptionCsv(
Number(c.cacheReadTokens || 0),
Number(c.responseTokens || 0) + Number(c.thinkingTokens || 0),
Number(c.totalTokens || 0),
Number(c.cacheHitPct || 0).toFixed(1),
formatPctNumber(c.cacheHitPct || 0),
Number(c.costUsd || 0).toFixed(4),
c.sourceName || c.appSource || '',
])
@@ -1433,14 +1436,14 @@ export function buildConsumptionMarkdownReport(
'',
`- **Total Tokens**: ${formatTokensPrecise(acc.totalTokens || 0)} (${Number(acc.calls || 0)} calls)`,
`- **Est. Cost**: ${formatUsd(acc.costUsd || 0)} (Cache Saved ${formatUsd(acc.savedCostUsd || 0)})`,
`- **Cache Hit Rate**: ${Number(acc.cacheHitPct || 0).toFixed(1)}% (Input ${formatTokensPrecise(acc.inputTokens || 0)} / Cache ${formatTokensPrecise(acc.cacheReadTokens || 0)} / Output ${formatTokensPrecise(acc.outputTokens || 0)})`,
`- **Cache Hit Rate**: ${formatPctNumber(acc.cacheHitPct || 0, { withSymbol: true })} (Input ${formatTokensPrecise(acc.inputTokens || 0)} / Cache ${formatTokensPrecise(acc.cacheReadTokens || 0)} / Output ${formatTokensPrecise(acc.outputTokens || 0)})`,
]
: [
`# 用量统计报告 · ${rangeLabel}`,
'',
`- **总 Token**: ${formatTokensPrecise(acc.totalTokens || 0)}(共 ${Number(acc.calls || 0)} 次调用)`,
`- **预估费用**: ${formatUsd(acc.costUsd || 0)}(缓存节省 ${formatUsd(acc.savedCostUsd || 0)})`,
`- **缓存命中率**: ${Number(acc.cacheHitPct || 0).toFixed(1)}%(新输入 ${formatTokensPrecise(acc.inputTokens || 0)} / 缓存读取 ${formatTokensPrecise(acc.cacheReadTokens || 0)} / 输出 ${formatTokensPrecise(acc.outputTokens || 0)})`,
`- **缓存命中率**: ${formatPctNumber(acc.cacheHitPct || 0, { withSymbol: true })}(新输入 ${formatTokensPrecise(acc.inputTokens || 0)} / 缓存读取 ${formatTokensPrecise(acc.cacheReadTokens || 0)} / 输出 ${formatTokensPrecise(acc.outputTokens || 0)})`,
];
if (models.length > 0) {
@@ -1455,7 +1458,7 @@ export function buildConsumptionMarkdownReport(
);
for (const m of models) {
lines.push(
`| ${escapeMdCell(m.displayText || m.label || m.shortName)} | ${Number(m.calls || 0)} | ${formatTokensPrecise(m.totalTokens || 0)} | ${Number(m.cacheHitPct || 0).toFixed(1)}% | ${m.avgTokPerSec != null ? formatSpeedMetric(m.avgTokPerSec, ' tok/s') : '—'} | ${formatUsd(m.costUsd || 0)} |`
`| ${escapeMdCell(m.displayText || m.label || m.shortName)} | ${Number(m.calls || 0)} | ${formatTokensPrecise(m.totalTokens || 0)} | ${formatPctNumber(m.cacheHitPct || 0, { withSymbol: true })} | ${m.avgTokPerSec != null ? formatSpeedMetric(m.avgTokPerSec, ' tok/s') : '—'} | ${formatUsd(m.costUsd || 0)} |`
);
}
}
@@ -1472,7 +1475,7 @@ export function buildConsumptionMarkdownReport(
);
for (const w of workspaces) {
lines.push(
`| ${escapeMdCell(w.label || w.workspacePath || (en ? 'Unbound' : '未绑定工作区'))} | ${Number(w.sessionCount || 0)} | ${Number(w.calls || 0)} | ${formatTokensPrecise(w.totalTokens || 0)} | ${Number(w.cacheHitPct || 0).toFixed(1)}% | ${formatUsd(w.costUsd || 0)} |`
`| ${escapeMdCell(w.label || w.workspacePath || (en ? 'Unbound' : '未绑定工作区'))} | ${Number(w.sessionCount || 0)} | ${Number(w.calls || 0)} | ${formatTokensPrecise(w.totalTokens || 0)} | ${formatPctNumber(w.cacheHitPct || 0, { withSymbol: true })} | ${formatUsd(w.costUsd || 0)} |`
);
}
}
@@ -1489,7 +1492,7 @@ export function buildConsumptionMarkdownReport(
);
for (const c of sessions.slice(0, 8)) {
lines.push(
`| ${escapeMdCell(c.title || c.cascadeId)} | ${Number(c.calls || 0)} | ${formatTokensPrecise(c.totalTokens || 0)} | ${Number(c.cacheHitPct || 0).toFixed(1)}% | ${formatUsd(c.costUsd || 0)} |`
`| ${escapeMdCell(c.title || c.cascadeId)} | ${Number(c.calls || 0)} | ${formatTokensPrecise(c.totalTokens || 0)} | ${formatPctNumber(c.cacheHitPct || 0, { withSymbol: true })} | ${formatUsd(c.costUsd || 0)} |`
);
}
}
+6 -3
View File
@@ -8,6 +8,7 @@ import {
formatAnomalyKindLabel,
formatCompactDateTime,
formatMetric,
formatPctNumber,
formatSpeedMetric,
formatTimingDetailSec,
formatTokensPrecise,
@@ -267,10 +268,12 @@ export function buildStepDiagnosticMarkdown(
const promptTok = Number(st.inputTokens || 0) + Number(st.cacheReadTokens || 0);
const cachePct =
st.cacheHitPct != null && Number.isFinite(Number(st.cacheHitPct))
? `${Number(st.cacheHitPct).toFixed(1)}%`
? formatPctNumber(st.cacheHitPct, { withSymbol: true })
: promptTok > 0
? `${((Number(st.cacheReadTokens || 0) / promptTok) * 100).toFixed(1)}%`
: '0.0%';
? formatPctNumber((Number(st.cacheReadTokens || 0) / promptTok) * 100, {
withSymbol: true,
})
: '0%';
const ctxTok = Number(st.contextTokens || promptTok + Number(st.outputTokens || 0));
const ctxDelta =
prevContextTokens != null && ctxTok > 0
+11 -10
View File
@@ -23,6 +23,7 @@ import {
formatCnyApprox,
formatDateSpanBadge,
formatMetric,
formatPctNumber,
formatSpeedMetric,
formatTokensPrecise,
formatUsd,
@@ -813,7 +814,7 @@ export const ConsumptionView = defineComponent({
responseTokens,
outputTokens: responseTokens + thinkingTokens,
tokText: formatTokensPrecise(tok),
pctText: `${pct.toFixed(1)}%`,
pctText: formatPctNumber(pct, { withSymbol: true }),
color,
isOther: false,
};
@@ -850,7 +851,7 @@ export const ConsumptionView = defineComponent({
responseTokens: otherResp,
outputTokens: otherResp + otherThink,
tokText: formatTokensPrecise(otherTok),
pctText: `${otherPct.toFixed(1)}%`,
pctText: formatPctNumber(otherPct, { withSymbol: true }),
color: 'var(--cons-other-seg)',
isOther: true,
});
@@ -1014,7 +1015,7 @@ export const ConsumptionView = defineComponent({
: promptTok > 0 && Number(item.cacheReadTokens || 0) > 0
? +((Number(item.cacheReadTokens || 0) / promptTok) * 100).toFixed(1)
: 0;
const cachePctStr = cachePctVal.toFixed(1);
const cachePctStr = formatPctNumber(cachePctVal);
const effCostPer1M =
!isAllUnpriced && costVal > 0 && total >= 1000 ? costVal / (total / 1_000_000) : null;
const outTok = Number(
@@ -1028,7 +1029,7 @@ export const ConsumptionView = defineComponent({
const barPct = costBase > 0 && costVal > 0 ? Math.min(100, Math.max(1, (costVal / costBase) * 100)) : 0;
patchAttr(refs.pctBadge, 'hidden', false);
patchAttr(refs.pctBadge, 'title', null);
patchText(refs.pctBadge, `${costShare.toFixed(1)}%`);
patchText(refs.pctBadge, formatPctNumber(costShare, { withSymbol: true }));
patchText(refs.totalTokEl, isAllUnpriced ? '' : formatCnyApprox(costVal));
patchText(refs.costEl, costText);
@@ -1103,7 +1104,7 @@ export const ConsumptionView = defineComponent({
callsBase > 0 && callsCount > 0 ? Math.min(100, Math.max(1, (callsCount / callsBase) * 100)) : 0;
patchAttr(refs.pctBadge, 'hidden', false);
patchAttr(refs.pctBadge, 'title', null);
patchText(refs.pctBadge, `${callsShare.toFixed(1)}%`);
patchText(refs.pctBadge, formatPctNumber(callsShare, { withSymbol: true }));
patchText(refs.totalTokEl, formatTokensPrecise(total));
patchText(refs.costEl, t('cons.callsCount', { calls: callsCount }));
@@ -1143,7 +1144,7 @@ export const ConsumptionView = defineComponent({
grandTotalTokens > 0 && total > 0 ? Math.min(1, Math.max(0.008, total / grandTotalTokens)) : 0;
patchAttr(refs.pctBadge, 'hidden', false);
patchAttr(refs.pctBadge, 'title', null);
patchText(refs.pctBadge, `${share.toFixed(1)}%`);
patchText(refs.pctBadge, formatPctNumber(share, { withSymbol: true }));
patchText(refs.totalTokEl, formatTokensPrecise(total));
patchText(refs.costEl, costText);
@@ -1317,7 +1318,7 @@ export const ConsumptionView = defineComponent({
const share = grandTotalTokens > 0 ? +((total / grandTotalTokens) * 100).toFixed(1) : 0;
const scaleRatio =
grandTotalTokens > 0 && total > 0 ? Math.min(1, Math.max(0.01, total / grandTotalTokens)) : 0;
patchText(refs.pctBadge, `${share.toFixed(1)}%`);
patchText(refs.pctBadge, formatPctNumber(share, { withSymbol: true }));
patchText(refs.totalTokEl, formatTokensPrecise(total));
const costVal = Number(item.costUsd || 0);
@@ -1568,7 +1569,7 @@ export const ConsumptionView = defineComponent({
? Math.min(1, Math.max(0.01, total / tokBase))
: 0;
patchText(refs.pctBadge, `${share.toFixed(1)}%`);
patchText(refs.pctBadge, formatPctNumber(share, { withSymbol: true }));
if (sortMode === 'tokens') {
patchText(refs.totalTokEl, costText);
patchText(refs.costEl, formatTokensPrecise(total));
@@ -1638,7 +1639,7 @@ export const ConsumptionView = defineComponent({
if (showFallbackMeta) {
const srcLabel = item.sourceName || item.appSource || '';
const cacheLabel = t('cons.topConvCacheMeta', {
pct: Number(item.cacheHitPct ?? 0).toFixed(1),
pct: formatPctNumber(item.cacheHitPct ?? 0),
});
patchText(refs.fallbackMetaEl, srcLabel ? `${srcLabel} · ${cacheLabel}` : cacheLabel);
}
@@ -1755,7 +1756,7 @@ export const ConsumptionView = defineComponent({
patchText(heroCostUsd, formatUsd(costUsd));
patchText(heroCostCny, formatCnyApprox(costUsd));
patchText(cacheHitPctEl, `${Number(acct.cacheHitPct ?? 0).toFixed(1)}%`);
patchText(cacheHitPctEl, formatPctNumber(acct.cacheHitPct ?? 0, { withSymbol: true }));
patchText(cacheTokensEl, t('cons.cacheHitTokens', { tokens: formatTokensPrecise(cacheTokens) }));
let inPct = 0;
+2 -1
View File
@@ -20,6 +20,7 @@ import {
formatAnomalyTagText,
formatCompactDateTime,
formatMetric,
formatPctNumber,
formatSpeedMetric,
formatTimingDetailSec,
formatTokens,
@@ -755,7 +756,7 @@ export const StepsDetailView = defineComponent({
patchText(inspKpiCache.lbl, t('steps.inspCacheLabel'));
patchText(inspKpiCache.val, formatTokensPrecise(cacheTok));
patchText(inspKpiCache.sub, `${cachePct.toFixed(1)}%`);
patchText(inspKpiCache.sub, formatPctNumber(cachePct, { withSymbol: true }));
patchText(inspKpiOutput.lbl, t('steps.inspOutputLabel'));
patchText(inspKpiOutput.val, formatTokensPrecise(outTok));