Skip to content

fix(gain): report total savings percentage in by-command table (#3506) - #3977

Closed
anujsingh-cse wants to merge 1 commit into
rtk-ai:developfrom
anujsingh-cse:fix/gain-total-savings-pct
Closed

anujsingh-cse wants to merge 1 commit into
rtk-ai:developfrom
anujsingh-cse:fix/gain-total-savings-pct

Conversation

@anujsingh-cse

Copy link
Copy Markdown
Contributor

Closes #3506

Problem

rtk gain's By Command table shows a Saved column (a SUM) next to an Avg% column (a AVG of per-command percentages). The two numbers in a row describe disjoint populations, so a reader naturally combines them ("24% off 478.8M") and gets nonsense — the average percentage is dominated by many tiny commands while the sum is dominated by a few huge ones.

Fix

Make the percentage consistent with the sum in the same row by switching the aggregation from AVG(savings_pct) to a total-based percentage:

COALESCE(100.0 * SUM(saved_tokens) / NULLIF(SUM(input_tokens), 0), 0.0)

COALESCE guards the zero-total-input edge case (matching the total_input > 0 guard already used for the summary's overall percentage). The column header is renamed Avg% → Total% to reflect it now answers a different question than a mean-of-percentages would.

Tests

Added test_get_by_command_uses_total_savings_pct, which records a long-tailed pair (a 90%-savings 1M-token command and a 10%-savings 100-token command under the same rtk_cmd) and asserts the reported percentage is the total-based ~89.99% rather than the mean-of-percentages 50%.

cargo fmt --check and cargo clippy --all-targets clean; 28 tracking tests pass.

@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added analytics bug Something isn't working sql labels Sep 11, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟢 Risk low

Summary

Fixes the rtk gain By Command table so the percentage column reflects the same population as the adjacent Saved sum, replacing AVG(savings_pct) with a total-based SUM(saved_tokens)/SUM(input_tokens) calculation and renaming the header from Avg% to Total%. Includes a new test verifying the total-based percentage against a long-tailed command pair.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #3506


Analyzed automatically by wshm · This is an automated analysis, not a human review.

@anujsingh-cse
anujsingh-cse force-pushed the fix/gain-total-savings-pct branch from e3b910b to 82d4ee2 Compare September 12, 2026 07:43
@KuSh KuSh closed this Sep 12, 2026
@KuSh KuSh added the duplicate This issue or pull request already exists label Sep 12, 2026
@KuSh KuSh reopened this Sep 13, 2026
@KuSh

KuSh commented Sep 13, 2026 •

Copy link
Copy Markdown
Collaborator

Closing as a duplicate of #891, which makes the same change in get_by_command, swapping AVG(savings_pct) for a volume weighted rate over SUM(saved_tokens) and SUM(input_tokens), and renames the same column header. #891 was kept because it already has a green CI run, a maintainer approval and a regression test. Your in-memory test DB is a better fixture than the one there, so it would be welcome as a comment on #891, and please reply here if you were fixing something #891 leaves untouched.

@KuSh KuSh closed this Sep 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

analytics bug Something isn't working duplicate This issue or pull request already exists sql

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gain: by-command table puts SUM(saved_tokens) next to AVG(savings_pct), producing rows that can't be read together

2 participants