Skip to content

feat: add tpm breakdown display - #5647

Open
SwillerDawn wants to merge 3 commits into
QuantumNous:mainfrom
NaciTech:feat/tpm-display-breakdown
Open

SwillerDawn wants to merge 3 commits into
QuantumNous:mainfrom
NaciTech:feat/tpm-display-breakdown

Conversation

@SwillerDawn

@SwillerDawn SwillerDawn commented Jun 21, 2026 •

Copy link
Copy Markdown

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

在默认前端的用量日志统计区域,为 TPM 增加缓存读、缓存写、输入、输出 token 的明细展示,并在后端日志统计接口补充对应字段。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

  • Closes # (如有)

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 Issues 与 PRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

(请在此粘贴截图、关键日志或测试报告,以证明变更生效)
image

Summary by CodeRabbit

Release Notes

  • New Features
    • Enhanced usage logs with expanded token consumption metrics: cache read tokens, cache write tokens, input tokens, and output tokens.
    • Updated the usage logs UI to replace the standalone TPM badge with an interactive hover card showing a localized TPM breakdown and cache hit rate.
    • Added new internationalized text for “Cache Hit Rate” across supported languages.

@coderabbitai

coderabbitai Bot commented Jun 21, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0bf8abbb-2c22-4864-84f1-10e543a9414e

📥 Commits

Reviewing files that changed from the base of the PR and between 2ae5725 and cceae6f.

📒 Files selected for processing (2)
  • controller/log.go
  • model/log.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • controller/log.go
  • model/log.go

Walkthrough

Adds cache read/write, input, and output token fields to usage log statistics. The backend computes and returns the breakdown, and the frontend shows it in a TPM hover card with localized labels.

Changes

Token Breakdown Stats End-to-End

Layer / File(s) Summary
Backend stat shape and aggregation
model/log.go
Stat gains cache read/write, input, and output token fields; strconv is added; and SumUsedQuota is split into separate quota/RPM/TPM query paths with a new recent-log breakdown calculation.
API response and frontend contract fields
controller/log.go, web/default/src/features/usage-logs/types.ts, web/default/src/features/usage-logs/constants.ts, web/default/src/i18n/locales/*.json
Both log-stat endpoints include the new token fields in JSON output; LogStatistics and DEFAULT_LOG_STATS carry matching fields; and the Cache Hit Rate label is added across locales.
TPM breakdown hover card
web/default/src/features/usage-logs/components/common-logs-stats.tsx
A new hover card renders TPM plus cache read/write, input, output, and cache-hit-rate values, and CommonLogsStats swaps in that component for the plain TPM badge.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • Calcium-Ion

Poem

🐇 Hop hop, the tokens now dance,
Cache and input get their chance.
A hover card twinkles bright and clear,
With cache-hit rates rabbit-cheered!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main feature: adding a TPM breakdown display.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
web/default/src/features/usage-logs/components/common-logs-stats.tsx (1)

67-95: Memoize TPM derived structures to avoid per-render allocations.

tpmStats and tpmItems are recreated on every render; wrapping them in useMemo will prevent unnecessary downstream re-renders and align with coding guidelines.

Proposed refactor
+import { useMemo } from 'react'
import { useQuery } from '`@tanstack/react-query`'
import { getRouteApi } from '`@tanstack/react-router`'
import { useTranslation } from 'react-i18next'
...
 function TpmBreakdownHoverCard(props: { stats: LogStatistics | undefined }) {
   const { t } = useTranslation()
-  const tpmStats = {
-    cacheRead: statNumber(props.stats?.cache_read_tokens),
-    cacheWrite: statNumber(props.stats?.cache_write_tokens),
-    input: statNumber(props.stats?.input_tokens),
-    output: statNumber(props.stats?.output_tokens),
-  }
+  const tpmStats = useMemo(
+    () => ({
+      cacheRead: statNumber(props.stats?.cache_read_tokens),
+      cacheWrite: statNumber(props.stats?.cache_write_tokens),
+      input: statNumber(props.stats?.input_tokens),
+      output: statNumber(props.stats?.output_tokens),
+    }),
+    [
+      props.stats?.cache_read_tokens,
+      props.stats?.cache_write_tokens,
+      props.stats?.input_tokens,
+      props.stats?.output_tokens,
+    ]
+  )
   const tpmTotal = statNumber(props.stats?.tpm)
-  const tpmItems = [
+  const tpmItems = useMemo(() => [
     {
       label: t('Cache Read'),
       value: tpmStats.cacheRead,
       accent: 'bg-emerald-500/70',
     },
     {
       label: t('Cache Write'),
       value: tpmStats.cacheWrite,
       accent: 'bg-amber-500/75',
     },
     {
       label: t('Input'),
       value: tpmStats.input,
       accent: 'bg-sky-500/70',
     },
     {
       label: t('Output'),
       value: tpmStats.output,
       accent: 'bg-violet-500/70',
     },
-  ]
+  ], [t, tpmStats])
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@web/default/src/features/usage-logs/components/common-logs-stats.tsx` around
lines 67 - 95, The tpmStats object and tpmItems array are being recreated on
every render, which causes unnecessary re-renders and memory allocations. Wrap
both the tpmStats object definition and the tpmItems array definition in useMemo
hooks with appropriate dependencies (props.stats and the translation function t)
to ensure they are only recalculated when their dependencies change. This will
prevent unnecessary downstream re-renders and improve performance.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@model/log.go`:
- Around line 610-614: The cacheRead and cacheWrite variables can retain
negative values from the statOtherInt calls, which corrupts downstream stat
calculations and TPM totals. Clamp both cacheRead and cacheWrite to non-negative
values immediately after they are assigned from statOtherInt, and also clamp the
result of the conditional assignment on line 614. Additionally, apply the same
clamping to the similar cache token variable assignments that appear in the
lines also mentioned in the comment (629-635) to ensure all cache token values
are non-negative before they are added to stat fields.

---

Nitpick comments:
In `@web/default/src/features/usage-logs/components/common-logs-stats.tsx`:
- Around line 67-95: The tpmStats object and tpmItems array are being recreated
on every render, which causes unnecessary re-renders and memory allocations.
Wrap both the tpmStats object definition and the tpmItems array definition in
useMemo hooks with appropriate dependencies (props.stats and the translation
function t) to ensure they are only recalculated when their dependencies change.
This will prevent unnecessary downstream re-renders and improve performance.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: fe44a705-23cd-42fb-b2be-478d2547efec

📥 Commits

Reviewing files that changed from the base of the PR and between 0b7ae4e and 6d814f9.

📒 Files selected for processing (5)
  • controller/log.go
  • model/log.go
  • web/default/src/features/usage-logs/components/common-logs-stats.tsx
  • web/default/src/features/usage-logs/constants.ts
  • web/default/src/features/usage-logs/types.ts

Comment thread model/log.go
Comment on lines +610 to +614
cacheRead := statOtherInt(other, "cache_tokens")
cacheWrite := statOtherInt(other, "cache_creation_tokens")
if cacheWrite == 0 {
cacheWrite = statOtherInt(other, "cache_creation_tokens_5m") + statOtherInt(other, "cache_creation_tokens_1h")
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Clamp cache token parts to non-negative before summing TPM.

cacheRead/cacheWrite can remain negative from other, and those values are added directly into stat.* and stat.Tpm. That can undercount or even produce invalid TPM totals.

Proposed fix
  cacheRead := statOtherInt(other, "cache_tokens")
+ if cacheRead < 0 {
+   cacheRead = 0
+ }
  cacheWrite := statOtherInt(other, "cache_creation_tokens")
- if cacheWrite == 0 {
+ if cacheWrite <= 0 {
    cacheWrite = statOtherInt(other, "cache_creation_tokens_5m") + statOtherInt(other, "cache_creation_tokens_1h")
  }
+ if cacheWrite < 0 {
+   cacheWrite = 0
+ }

Also applies to: 629-635

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@model/log.go` around lines 610 - 614, The cacheRead and cacheWrite variables
can retain negative values from the statOtherInt calls, which corrupts
downstream stat calculations and TPM totals. Clamp both cacheRead and cacheWrite
to non-negative values immediately after they are assigned from statOtherInt,
and also clamp the result of the conditional assignment on line 614.
Additionally, apply the same clamping to the similar cache token variable
assignments that appear in the lines also mentioned in the comment (629-635) to
ensure all cache token values are non-negative before they are added to stat
fields.

@Calcium-Ion

Copy link
Copy Markdown
Member

感觉占的空间有点多,和现在的设计有点不符,能优化一下布局就好多了。

@watermelon1024

Copy link
Copy Markdown

这个明细展示功能非常实用!可否请求增加一个换存命中率显示?在缓存读取/写入的栏位后面放一个小字写xx.x%?

@SwillerDawn

Copy link
Copy Markdown
Author

感觉占的空间有点多,和现在的设计有点不符,能优化一下布局就好多了。

屏幕截图 2026-06-30 141340 屏幕截图 2026-06-30 141345 细分页面在鼠标悬浮后显示,目前改成如图布局

@Calcium-Ion
Calcium-Ion force-pushed the main branch 2 times, most recently from 51fdfc5 to 2b6f1df Compare August 30, 2026 15:03

This branch has not been deployed

No deployments
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.

3 participants