Skip to content

fix(ccusage): sort session reports by cost instead of last activity#907

Open
bluzername wants to merge 1 commit intoryoppippi:mainfrom
bluzername:fix/session-sort-by-cost
Open

fix(ccusage): sort session reports by cost instead of last activity#907
bluzername wants to merge 1 commit intoryoppippi:mainfrom
bluzername:fix/session-sort-by-cost

Conversation

@bluzername
Copy link
Copy Markdown

@bluzername bluzername commented Mar 25, 2026

Problem

The Session Reports documentation says:

Sessions are sorted by cost (highest first) by default

But actually running pnpx ccusage session shows entries sorted by Last Activity timestamp instead of by Cost. This was confusing because the expensive sessions were not at the top like I was expecting.

What I changed

In apps/ccusage/src/data-loader.ts, the loadSessionData function was using sortByDate on lastActivity field:

// before
return sortByDate(sessionFiltered, (item) => item.lastActivity, options?.order);

Changed to sort by totalCost using fast-sort directly:

// after
const sorted = sort(sessionFiltered);
const order = options?.order ?? 'desc';
switch (order) {
  case 'asc':
    return sorted.asc((item) => item.totalCost);
  case 'desc':
    return sorted.desc((item) => item.totalCost);
  default:
    unreachable(order);
}

Also updated:

  • The JSDoc comment that said "sorted by last activity" to say "sorted by cost"
  • The 3 related tests to use different costUSD values per session so they actually verify sorting by cost and not just any order

Testing

  • All 252 tests pass (127 in data-loader.ts)
  • pnpm run format passes
  • Tests now use costUSD: 0.01, 0.05, 0.10 to verify cost ordering works correct

Closes #903

Summary by CodeRabbit

  • Improvements
    • Sessions are now sorted by total cost (highest to lowest) by default, making it easier to identify expensive sessions. Sorting order can be reversed to display lowest-cost sessions first.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 25, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8783a642-f7ae-47f7-951c-bb0013ee9c00

📥 Commits

Reviewing files that changed from the base of the PR and between d3cb6df and 2fea51a.

📒 Files selected for processing (1)
  • apps/ccusage/src/data-loader.ts

📝 Walkthrough

Walkthrough

Session data sorting behavior has been refactored to prioritize cost instead of last activity. The loadSessionData function now sorts aggregated sessions by totalCost in descending order by default, with an order: 'asc' option to reverse. Sorting implementation migrated from sortByDate() to fast-sort's sort() function with explicit order handling.

Changes

Cohort / File(s) Summary
Session sorting logic
apps/ccusage/src/data-loader.ts
Replaced lastActivity sorting with totalCost-based sorting; migrated to fast-sort library; added explicit order parameter handling with unreachable() guard case; updated docstring; modified test fixtures and assertions to validate cost-based ordering with mode: 'display'.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • ccusage#613: Removes the --order flag from CLI and stops passing order parameter to loadSessionData, directly relating to order handling changes in this PR.

Poem

🐰 Costs now rise to the top with a hop,
No more last-activity shuffle and flop,
Sort by the dollars, the bills that do sting,
To find the expensive chats—that's the thing!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title clearly summarizes the main change: fixing session report sorting from last activity to cost, which is the primary objective of the PR.
Linked Issues check ✅ Passed The PR fully addresses issue #903 by fixing the sorting bug to use totalCost instead of lastActivity, aligning implementation with documentation.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the sorting behavior in loadSessionData to use cost-based ordering as specified in issue #903.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ 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.

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.

Session reports use Last Activity for sort order instead of costs

1 participant