Skip to content

fix(my-usage): include active requests in stats summary#1230

Draft
ROOOO wants to merge 1 commit into
ding113:devfrom
ROOOO:codex/my-usage-active-ledger-stats
Draft

fix(my-usage): include active requests in stats summary#1230
ROOOO wants to merge 1 commit into
ding113:devfrom
ROOOO:codex/my-usage-active-ledger-stats

Conversation

@ROOOO

@ROOOO ROOOO commented May 30, 2026

Copy link
Copy Markdown

Summary

Fixes the getMyStatsSummary function to aggregate statistics from both active message_request rows and usage_ledger rows, ensuring accurate stats during the migration period where both data sources coexist.

Problem

PR #1063 switched getMyStatsSummary to read exclusively from usage_ledger to fix gaps after ledger-only DB reimports. However, this meant active requests that hadn't yet been moved to ledger were excluded from the summary, causing underreported usage statistics during normal operation.

Solution

  1. Dual-query architecture: getMyStatsSummary now queries both message_request and usage_ledger in parallel.
  2. Ledger deduplication: The usage_ledger query uses a NOT EXISTS subquery to exclude ledger rows that have a corresponding active message_request with the same id and key, preventing double-counting.
  3. Merged aggregation: Results from both queries are merged by model and aggregated into a single modelBreakdown via the new mergeStatsSummaryRows helper.
  4. Null safety: Added ?? 0 fallbacks in the output mapping to prevent null from appearing in the final stats.

Related

Changes

Core Changes

  • src/actions/my-usage.ts — Added dual-query getMyStatsSummary with mergeStatsSummaryRows helper and ledger deduplication

Test Updates

  • tests/unit/actions/my-usage-token-aggregation.test.ts — Updated to expect 2 query selections and verify precision across both paths

Testing

Automated Tests

  • Unit tests updated (my-usage-token-aggregation.test.ts)

Validation

bunx vitest run tests/unit/actions/my-usage-token-aggregation.test.ts

Checklist

  • Code follows project conventions
  • Self-review completed
  • Tests pass locally

Description enhanced by Claude AI

@coderabbitai

coderabbitai Bot commented May 30, 2026

Copy link
Copy Markdown

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: d0bfa2c0-dc54-4c2b-9bf3-9e5f596ce056

📥 Commits

Reviewing files that changed from the base of the PR and between ed95b48 and 41e37e9.

📒 Files selected for processing (2)
  • src/actions/my-usage.ts
  • tests/unit/actions/my-usage-token-aggregation.test.ts

📝 Walkthrough

总体概览

PR 将 getMyStatsSummary 的模型汇总计算从单一 usageLedger 源改为并行查询 messageRequestusageLedger 两套数据,通过新增的 StatsSummaryRow 类型与 mergeStatsSummaryRows 函数实现去重合并,并在结果映射阶段应用严格的空值容错处理。测试覆盖扩展至验证两次查询的 SQL 断言。

变更详情

Stats Summary 查询与合并重构

Layer / File(s) 概述
合并数据结构与辅助函数
src/actions/my-usage.ts
定义 StatsSummaryRow 类型与 mergeStatsSummaryRows 函数,按 model 维度合并用户与 key 侧汇总,执行空值兜底与排序。
并行查询与去重逻辑
src/actions/my-usage.ts
getMyStatsSummary 改为 Promise.all 并行查询 messageRequestusageLedger;通过 not exists 排除 messageRequest 中活跃的 requestId,避免迁移期重复。
结果映射与空值容错
src/actions/my-usage.ts
keyModelBreakdownuserModelBreakdown 的字段映射应用严格空值处理,requests 与 token 字段使用 ?? 0,cost 使用 Number(...) ?? 0
测试覆盖双查询 SQL 断言
tests/unit/actions/my-usage-token-aggregation.test.ts
预期 capturedSelections 从 1 增至 2,将 expectNoIntTokenSum 断言扩展为对所有 selection 与全量 tokenFields 的嵌套验证。

评估与相关

🎯 3 (中等复杂度) | ⏱️ ~20 分钟

该 PR 涉及查询逻辑重构与去重策略,需要理解数据源并行处理、去重条件(not exists)与合并聚合的相互作用;测试更新反映了额外查询路径的引入。

  • ding113/claude-code-hub#811:两个 PR 都触及 my-usage 统计路径,通过修改 messageRequestusage_ledger 的聚合逻辑(包括 warmup/账本条件与去重策略),与该 PR 的 getMyStatsSummary 合并与防护策略直接对齐。
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.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
Title check ✅ Passed 标题清晰地概括了主要变更:在统计摘要中包含活跃的请求,与代码变更(从仅使用usageLedger改为并行查询messageRequest和usageLedger)的核心目的相符。
Description check ✅ Passed 描述充分关联于变更集,准确说明了三个主要改动:从活跃message_request行和仅ledger行构建摘要、去重处理、保留token精度检查。
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.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
⚔️ Resolve merge conflicts
  • Resolve merge conflict in branch codex/my-usage-active-ledger-stats

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


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.

@github-actions github-actions Bot added bug Something isn't working area:statistics labels May 30, 2026

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request updates the usage statistics aggregation in getMyStatsSummary to query both messageRequest and usageLedger tables in parallel during a migration period, merging and sorting the results in-memory. It also updates the corresponding unit tests to reflect these changes. There are no review comments, so no additional feedback is provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:statistics bug Something isn't working

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

1 participant