Skip to content

feat(billing): highlight matched conditional multipliers in logs - #6561

Open
seefs001 wants to merge 2 commits into
QuantumNous:mainfrom
seefs001:fix/tier-price-log
Open

feat(billing): highlight matched conditional multipliers in logs#6561
seefs001 wants to merge 2 commits into
QuantumNous:mainfrom
seefs001:fix/tier-price-log

Conversation

@seefs001

@seefs001 seefs001 commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

⚠️ 提交说明 / PR Notice

Important

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

📝 变更描述 / Description

(简述:做了什么?为什么这样改能生效?请基于你对代码逻辑的理解来写,避免粘贴未经整理的内容)

对表达式计费的命中条件进行高亮

🚀 变更类型 / Type of change

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

🔗 关联任务 / Related Issue

  • Closes # (如有)

✅ 提交前检查项 / Checklist

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

📸 运行证明 / Proof of Work

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

Summary by CodeRabbit

  • New Features

    • Tiered pricing details now show request-rule conditions, multipliers, and match status.
    • Matching conditional multiplier rows are highlighted with a localized “Matched” badge.
    • Usage logs include structured request-rule information when available.
    • Billing results preserve request-rule traces for more transparent pricing calculations.
  • Bug Fixes

    • Improved pricing breakdown labels and matching behavior across device sizes.
    • Preserved accurate billing results while recording matched and unmatched request rules.

@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Billing expressions now trace request-rule matches through compilation, settlement, usage logs, and the pricing breakdown. The UI uses traced rules when available and marks matched conditional multipliers.

Changes

Matched request rule tracing

Layer / File(s) Summary
Capture request-rule matches and return them
pkg/billingexpr/types.go, pkg/billingexpr/compile.go, pkg/billingexpr/run.go, pkg/billingexpr/settle.go, pkg/billingexpr/billingexpr_test.go
Eligible request-dependent multipliers use internal trace calls. Runtime execution records matched and unmatched rules. Billing results expose the traces. Tests cover floating-point and integer expressions, fallbacks, and reserved identifiers.
Propagate request rules into usage metadata
service/log_info_generate.go, controller/channel_test_internal_test.go
Generated usage metadata includes request-rule traces. Controller tests verify the field.
Correlate and display traced rules
web/src/features/pricing/lib/billing-expr.ts, web/src/features/pricing/components/dynamic-pricing-breakdown.tsx, web/src/features/usage-logs/types.ts, web/src/features/usage-logs/components/dialogs/details-dialog.tsx, pkg/billingexpr/expr.md
Frontend parsing supports traced conditions and match state. Usage-log details pass traces to the pricing breakdown. The UI marks matched multiplier rows and retains expression parsing as a fallback.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant BillingExpressionCompiler
  participant runProgram
  participant ComputeTieredQuotaWithRequest
  participant InjectTieredBillingInfo
  participant DynamicPricingBreakdown
  BillingExpressionCompiler->>runProgram: execute patched request-rule expressions
  runProgram-->>ComputeTieredQuotaWithRequest: return RequestRules
  ComputeTieredQuotaWithRequest->>InjectTieredBillingInfo: provide TieredResult.RequestRules
  InjectTieredBillingInfo-->>DynamicPricingBreakdown: provide other.request_rules
  DynamicPricingBreakdown->>DynamicPricingBreakdown: render matched multiplier rows
Loading

Suggested reviewers: calcium-ion

Poem

A rabbit traces rules in flight,
The matched paths now show their light.
Logs carry each condition through,
Pricing marks the matches too.
Hop by hop, the trace stays true.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 7.69% 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 summarizes the main change: highlighting matched conditional billing multipliers in usage logs.
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/src/features/pricing/components/dynamic-pricing-breakdown.tsx (1)

158-164: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Prefer direct props access in this component.

The change extends props destructuring with matchedRequestRules. Use props.matchedRequestRules instead. This follows the frontend rule to avoid unnecessary component-props destructuring.

🤖 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/src/features/pricing/components/dynamic-pricing-breakdown.tsx` around
lines 158 - 164, Update DynamicPricingBreakdown to stop destructuring
matchedRequestRules from its props; access it through props.matchedRequestRules
throughout the component while preserving the existing behavior and other prop
handling.

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 `@pkg/billingexpr/compile.go`:
- Around line 43-49: Define and use one canonical trace-key normalization
contract across pkg/billingexpr/compile.go at the _trace emission in compile and
web/src/features/pricing/lib/billing-expr.ts at the matchedRequestRules
comparison. Update compile to emit the normalized conditional key instead of
conditional.Cond.String(), and update the pricing breakdown logic to normalize
raw ternary condition text with the same contract before matching, ensuring both
sides produce identical stable keys.

---

Nitpick comments:
In `@web/src/features/pricing/components/dynamic-pricing-breakdown.tsx`:
- Around line 158-164: Update DynamicPricingBreakdown to stop destructuring
matchedRequestRules from its props; access it through props.matchedRequestRules
throughout the component while preserving the existing behavior and other prop
handling.
🪄 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 Plus

Run ID: 7caeb5fd-8975-4e06-a301-f5b7970d411d

📥 Commits

Reviewing files that changed from the base of the PR and between aa7d0d3 and 6b9b2ab.

📒 Files selected for processing (11)
  • controller/channel_test_internal_test.go
  • pkg/billingexpr/billingexpr_test.go
  • pkg/billingexpr/compile.go
  • pkg/billingexpr/run.go
  • pkg/billingexpr/settle.go
  • pkg/billingexpr/types.go
  • service/log_info_generate.go
  • web/src/features/pricing/components/dynamic-pricing-breakdown.tsx
  • web/src/features/pricing/lib/billing-expr.ts
  • web/src/features/usage-logs/components/dialogs/details-dialog.tsx
  • web/src/features/usage-logs/types.ts

Comment thread pkg/billingexpr/compile.go

@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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
web/src/features/pricing/components/dynamic-pricing-breakdown.tsx (1)

283-295: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Desktop tier table still uses index-based keys; mobile view was fixed but not desktop.

Line 289 switches the mobile tier list to occurrence-based keys derived from tier content, to avoid key collisions/reconciliation issues for duplicate tier rows. The desktop StaticDataTable at Line 359 still uses getRowKey={(_tier, index) => \tier-${index}`}`, so the same duplicate-tier scenario that justified the mobile fix remains unaddressed there.

Apply the same occurrence-based key strategy to the desktop table for parity.

🔧 Proposed fix
   const mobileTierKeyOccurrences = new Map<string, number>()
+  const desktopTierKeyOccurrences = new Map<string, number>()
   const requestRuleKeyOccurrences = new Map<string, number>()
             data={tiers}
-            getRowKey={(_tier, index) => `tier-${index}`}
+            getRowKey={(tier) =>
+              `tier-${nextOccurrenceKey(JSON.stringify(tier), desktopTierKeyOccurrences)}`
+            }

Verify StaticDataTable invokes getRowKey exactly once per row per render before relying on a stateful occurrence map here.

🤖 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/src/features/pricing/components/dynamic-pricing-breakdown.tsx` around
lines 283 - 295, Update the desktop StaticDataTable getRowKey callback to use
the same tier-content occurrence-based strategy as the mobile tiers.map path,
deriving keys from JSON.stringify(tier) and a desktop-scoped occurrence map
rather than the row index. Ensure the occurrence map is reset appropriately per
render and confirm getRowKey is invoked once per row per render before using
this stateful approach.
🤖 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 `@pkg/billingexpr/expr.md`:
- Around line 125-127: Add the text language identifier to the fenced code block
containing the request-probe expression in expr.md, changing the opening fence
to use text while leaving the example content unchanged.

---

Outside diff comments:
In `@web/src/features/pricing/components/dynamic-pricing-breakdown.tsx`:
- Around line 283-295: Update the desktop StaticDataTable getRowKey callback to
use the same tier-content occurrence-based strategy as the mobile tiers.map
path, deriving keys from JSON.stringify(tier) and a desktop-scoped occurrence
map rather than the row index. Ensure the occurrence map is reset appropriately
per render and confirm getRowKey is invoked once per row per render before using
this stateful approach.
🪄 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 Plus

Run ID: b5cd8e82-b252-483e-92c6-eb96fc1b47e7

📥 Commits

Reviewing files that changed from the base of the PR and between 6b9b2ab and 84919bb.

📒 Files selected for processing (12)
  • controller/channel_test_internal_test.go
  • pkg/billingexpr/billingexpr_test.go
  • pkg/billingexpr/compile.go
  • pkg/billingexpr/expr.md
  • pkg/billingexpr/run.go
  • pkg/billingexpr/settle.go
  • pkg/billingexpr/types.go
  • service/log_info_generate.go
  • web/src/features/pricing/components/dynamic-pricing-breakdown.tsx
  • web/src/features/pricing/lib/billing-expr.ts
  • web/src/features/usage-logs/components/dialogs/details-dialog.tsx
  • web/src/features/usage-logs/types.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • controller/channel_test_internal_test.go
  • web/src/features/usage-logs/components/dialogs/details-dialog.tsx
  • pkg/billingexpr/settle.go

Comment thread pkg/billingexpr/expr.md
Comment on lines +125 to +127
```
<request-probe condition> ? <numeric literal> : 1
```

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.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Add a language identifier to the fenced code block.

The fenced block at Line 125 has no language tag. Static analysis (markdownlint MD040) flags this.

📝 Proposed fix
-```
+```text
 <request-probe condition> ? <numeric literal> : 1
</details>

<!-- suggestion_start -->

<details>
<summary>📝 Committable suggestion</summary>

> ‼️ **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.

```suggestion

🧰 Tools
🪛 markdownlint-cli2 (0.23.1)

[warning] 125-125: Fenced code blocks should have a language specified

(MD040, fenced-code-language)

🤖 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 `@pkg/billingexpr/expr.md` around lines 125 - 127, Add the text language
identifier to the fenced code block containing the request-probe expression in
expr.md, changing the opening fence to use text while leaving the example
content unchanged.

Source: Linters/SAST tools

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