Skip to content

feat: usage logs add completion ratio#3435

Open
feitianbubu wants to merge 1 commit intoQuantumNous:mainfrom
feitianbubu:pr/53f38708e5c4a1d867827c48243a36c7ca2e6c1c
Open

feat: usage logs add completion ratio#3435
feitianbubu wants to merge 1 commit intoQuantumNous:mainfrom
feitianbubu:pr/53f38708e5c4a1d867827c48243a36c7ca2e6c1c

Conversation

@feitianbubu
Copy link
Contributor

@feitianbubu feitianbubu commented Mar 25, 2026

使用日志, 详情增加补全价格显示
image

Summary by CodeRabbit

  • New Features
    • Enhanced usage log pricing display with completion ratio information. When viewing token-based pricing, completion costs per 1M tokens are now displayed alongside standard pricing metrics.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 25, 2026

Walkthrough

A new completionRatio parameter is threaded through the pricing rendering pipeline. The parameter is passed from getUsageLogDetailSummary() in the usage logs column definitions to the renderModelPriceSimple() function, which forwards it to renderPriceSimpleCore(). When rendering token-based pricing segments, a completion ratio segment is conditionally displayed.

Changes

Cohort / File(s) Summary
Usage Logs Integration
web/src/components/table/usage-logs/UsageLogsColumnDefs.jsx
Updated getUsageLogDetailSummary() to pass other?.completion_ratio to renderModelPriceSimple() for both claude and non-claude model pricing branches.
Render Helper Functions
web/src/helpers/render.jsx
Added completionRatio parameter to renderModelPriceSimple() and renderPriceSimpleCore() function signatures. Implemented conditional rendering of a new "补全 {{price}} / 1M tokens" segment when completionRatio is truthy and output mode is segments with token-based pricing.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested reviewers

  • seefs001

Poem

🐰 A token, a ratio, a segment so new,
Through render pipes flowing, completing the view,
The completion doth show its sweet metric with pride,
With prices and ratios dancing inside! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: usage logs add completion ratio' clearly and specifically describes the main change: adding completion ratio functionality to the usage logs feature.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@web/src/helpers/render.jsx`:
- Around line 1363-1372: Guard the completion price by converting
completionRatio to a numeric value and only render when it's a finite number (so
numeric zero is allowed); specifically, in render.jsx compute a numeric ratio
from completionRatio (e.g., via Number(...) or Number.isFinite check) and
replace the current truthiness check with a check like Number.isFinite(ratio)
before pushing the segment that calls formatCompactDisplayPrice with modelRatio
* 2.0 * ratio, ensuring you use the validated numeric variable rather than
coercing inside the formatting call.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9a9115cc-219c-47cc-9988-eb3ed46f616e

📥 Commits

Reviewing files that changed from the base of the PR and between dbf900a and c6b33cc.

📒 Files selected for processing (2)
  • web/src/components/table/usage-logs/UsageLogsColumnDefs.jsx
  • web/src/helpers/render.jsx

Comment on lines +1363 to +1372
if (completionRatio) {
segments.push({
tone: 'secondary',
text: i18next.t('补全 {{price}} / 1M tokens', {
price: formatCompactDisplayPrice(
modelRatio * 2.0 * Number(completionRatio),
),
}),
});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify completion_ratio production/consumption and finite-value guards.

echo "== completion_ratio producers/consumers =="
rg -n -C3 '\bcompletion_ratio\b|\bcompletionRatio\b'

echo
echo "== inspect ratio sync guard implementation (if present) =="
fd ratio_sync.go --exec sed -n '560,700p' {}

Repository: QuantumNous/new-api

Length of output: 50376


🏁 Script executed:

sed -n '1298,1380p' web/src/helpers/render.jsx | cat -n

Repository: QuantumNous/new-api

Length of output: 3073


🏁 Script executed:

rg -n 'renderPriceSimpleCore\(' web/src/ -A2 | head -50

Repository: QuantumNous/new-api

Length of output: 393


🏁 Script executed:

rg -n 'formatCompactDisplayPrice' web/src/helpers/render.jsx -A5 | head -40

Repository: QuantumNous/new-api

Length of output: 1262


Guard completionRatio before rendering completion price.

Line 1363 uses truthiness and line 1368 coerces with Number(...), which can render invalid prices when completionRatio is undefined, null, zero, or malformed (¥NaN, ¥Infinity, or inconsistent hiding of numeric zero).

💡 Suggested fix
-      if (completionRatio) {
+      const completionRatioValue = Number(completionRatio);
+      const hasCompletionRatio =
+        completionRatio !== undefined &&
+        completionRatio !== null &&
+        Number.isFinite(completionRatioValue) &&
+        completionRatioValue >= 0;
+      if (hasCompletionRatio) {
         segments.push({
           tone: 'secondary',
           text: i18next.t('补全 {{price}} / 1M tokens', {
             price: formatCompactDisplayPrice(
-              modelRatio * 2.0 * Number(completionRatio),
+              modelRatio * 2.0 * completionRatioValue,
             ),
           }),
         });
       }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (completionRatio) {
segments.push({
tone: 'secondary',
text: i18next.t('补全 {{price}} / 1M tokens', {
price: formatCompactDisplayPrice(
modelRatio * 2.0 * Number(completionRatio),
),
}),
});
}
const completionRatioValue = Number(completionRatio);
const hasCompletionRatio =
completionRatio !== undefined &&
completionRatio !== null &&
Number.isFinite(completionRatioValue) &&
completionRatioValue >= 0;
if (hasCompletionRatio) {
segments.push({
tone: 'secondary',
text: i18next.t('补全 {{price}} / 1M tokens', {
price: formatCompactDisplayPrice(
modelRatio * 2.0 * completionRatioValue,
),
}),
});
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@web/src/helpers/render.jsx` around lines 1363 - 1372, Guard the completion
price by converting completionRatio to a numeric value and only render when it's
a finite number (so numeric zero is allowed); specifically, in render.jsx
compute a numeric ratio from completionRatio (e.g., via Number(...) or
Number.isFinite check) and replace the current truthiness check with a check
like Number.isFinite(ratio) before pushing the segment that calls
formatCompactDisplayPrice with modelRatio * 2.0 * ratio, ensuring you use the
validated numeric variable rather than coercing inside the formatting call.

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