Skip to content

feat: 用户侧用量仪表盘 - #9

Merged
mzrodyu merged 1 commit into
mainfrom
feat/model-pull-button
Sep 10, 2026
Merged

mzrodyu merged 1 commit into
mainfrom
feat/model-pull-button

Conversation

@mzrodyu

@mzrodyu mzrodyu commented Sep 10, 2026

Copy link
Copy Markdown
Owner

背景

账户页只显示余额,从不说明钱花到哪去了 —— 而 RequestLog 和额度流水里其实已经有全部数据,只是没有出口。

改动

新增 GET /api/account/usage按会话用户聚合(管理员在这里看到的也是自己的用量,不是整站的,那是 /api/overview):

  • 今日 / 本月 / 累计消费与请求数、今日成功率
  • 近 N 天按天序列(空缺日补零,不会从序列里消失)
  • 按模型拆分,按消费排名,尾部折叠成「其他」

前端是 KPI 区块 + 两张单系列图:每日消费用(离散日、允许为零,折线会在零值间画出误导性的连续),模型拆分用横向条。时间范围控制同时作用于两张图和模型排名。

配色

消费是单一序列,所以所有标记只用主题蓝 --blue —— 不做值阶梯着色,那会把柱长已经表达的信息再用颜色编码一遍。对比度用校验器实测(非目测):

#007aff  vs #ffffff (浅色卡片)  → 4.02:1  PASS
#007aff  vs #1c1c1e (暗色卡片)  → 4.24:1  PASS
#34c759  vs #ffffff            → 2.22:1  FAIL(因此未用于标记)

渲染验证

在无头浏览器里跑真实组件(注入 30 天真实日志、登录、切换主题、悬停),这抓到了三个类型检查和单测都发现不了的缺陷:

  1. 零值日仍画出 2px 短桩 —— 没有消费的日子显示了标记
  2. 轴上限只取 1/2/5/10 —— 峰值 2.47 却对着 5.00 画,只用了一半高度;补上 1.5/2.5/3/4/6/8,并把轴映射到 88% 为峰值标签留出空间
  3. 悬停提示框定位在绘图区上方,会盖住「每日消费」标题;改为卡片内、刻度带之上

顺带修复

clean:dist 改为失败即报错。之前它静默留下旧产物(已清理过一次 51MB 陈旧 bundle),这次构建时 API 服务正持有文件,同样的静默部分删除再次复现。现在清理不干净会以非零退出,而不是把含有被取代资源的 dist/ 发出去。

验证

  • 新增 TestAccountUsageAggregatesOwnLogsOnly:只统计本人日志(他人那笔 999 不计入)、今日/昨日分桶、成功率、空缺日补零、模型榜受范围约束
  • go test ./... 全绿
  • 4 张渲染截图 + 悬停态人工核对:柱宽上限 24px、零值无标记、标签无碰撞、暗色正常

Balances only ever went down on the account page with no explanation of where
they went, even though RequestLog and the quota ledger already held everything
needed. Adds GET /api/account/usage, scoped to the session user (an admin sees
their own usage here, not the installation's).

The view is a KPI row plus two single-series charts: daily spend as columns and
a per-model breakdown. Costs are one series, so every mark wears the theme
accent (--blue, verified at 4.02:1 light / 4.24:1 dark against the card
surface) - never a value ramp, which would re-encode bar length as colour. The
range control scopes both charts and the model ranking.

Rendering the real component in a headless browser caught three defects that
neither the type checker nor the tests would have:

- a zero-cost day still drew a 2px stub, so a day with no spend showed a mark
- the axis only had 1/2/5/10 steps, so a 2.47 peak was charted against 5.00 and
  used barely half the plot; adds 1.5/2.5/3/4/6/8 and maps the axis to 88% of
  the plot so the peak's label keeps headroom
- the hover tooltip was anchored above the plot, where it covered the block
  heading; it now sits inside the card above the tick band

Also makes clean:dist fail loudly instead of silently leaving old bundles
behind. Removed 51MB of accumulated stale bundles this way once already, and
the same silent partial delete recurred here while the API server held the
files - the build now exits non-zero rather than shipping a dist/ that still
contains superseded assets.

Co-Authored-By: Claude Code <noreply@anthropic.com>
@mzrodyu
mzrodyu merged commit a9b52c6 into main Sep 10, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant