Skip to content

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

Merged
ryoppippi merged 2 commits into
ccusage:mainfrom
bluzername:fix/session-sort-by-cost
May 11, 2026
Merged

ryoppippi merged 2 commits into
ccusage:mainfrom
bluzername:fix/session-sort-by-cost

Conversation

@bluzername

@bluzername bluzername commented Mar 25, 2026 •

Copy link
Copy Markdown
Contributor

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 spot expensive sessions.
    • Sorting can be reversed to show lowest-cost sessions first.
    • Display-mode session lists now align ordering with the displayed cost values for consistent viewing.

Review Change Stack

@coderabbitai

coderabbitai Bot commented Mar 25, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: abec1a03-72b4-480d-aa0d-8937ef0a6999

📥 Commits

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

📒 Files selected for processing (1)
  • apps/ccusage/src/data-loader.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/ccusage/src/data-loader.ts

📝 Walkthrough

Walkthrough

loadSessionData now sorts aggregated sessions by totalCost (desc by default; use options.order = 'asc' for ascending). Implementation switched to fast-sort and tests/docstring were updated to assert cost-based ordering and use mode: 'display'.

Changes

Session sorting logic

Layer / File(s) Summary
Imports / Setup
apps/ccusage/src/data-loader.ts
Add sort import from fast-sort.
Docstring update
apps/ccusage/src/data-loader.ts
JSDoc changed to state sessions are sorted by cost (totalCost) instead of lastActivity.
Sorting implementation
apps/ccusage/src/data-loader.ts
Replace date-based final ordering with sort(...).desc/asc(s => s.totalCost) using options.order ?? 'desc' and an unreachable(order) default.
Tests — default order
apps/ccusage/src/data-loader.ts
Update default-order test to assert cost-descending ordering, adjust costUSD fixtures, call loadSessionData with mode: 'display'.
Tests — asc order
apps/ccusage/src/data-loader.ts
Update order: 'asc' test to assert cost-ascending ordering and call loadSessionData with mode: 'display'.
Tests — desc order
apps/ccusage/src/data-loader.ts
Update order: 'desc' test to assert cost-descending ordering and call loadSessionData with mode: 'display'.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • ryoppippi/ccusage#613: Related change touching session sorting and CLI order handling; it removes the --order flag and stops passing an order argument into loadSessionData.

Poem

🐰 I hopped through code to change the sort,

Costs leap up first — no last-activity court.
Fixtures adjusted, tests now agree,
Desc by default, asc if you plea.
A cheerful hop for costly chat discovery.

🚥 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 title clearly summarizes the main change: replacing last activity sorting with cost-based sorting in session reports.
Linked Issues check ✅ Passed The PR directly addresses issue #903 by implementing cost-based sorting for sessions (highest first by default) as documented, fulfilling the primary objective.
Out of Scope Changes check ✅ Passed All changes are focused on fixing the sorting logic from lastActivity to totalCost and updating corresponding tests, remaining in scope with 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.

@ryoppippi
ryoppippi merged commit 9f31ebc into ccusage:main May 11, 2026
3 checks passed
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

2 participants