Skip to content

Commit 2fea51a

Browse files
committed
fix(ccusage): sort session reports by cost instead of last activity
Closes ccusage#903
1 parent d3cb6df commit 2fea51a

1 file changed

Lines changed: 31 additions & 18 deletions

File tree

‎apps/ccusage/src/data-loader.ts‎

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { createInterface } from 'node:readline';
2020
import { toArray } from '@antfu/utils';
2121
import { Result } from '@praha/byethrow';
2222
import { groupBy, uniq } from 'es-toolkit'; // TODO: after node20 is deprecated, switch to native Object.groupBy
23+
import { sort } from 'fast-sort';
2324
import { createFixture } from 'fs-fixture';
2425
import { isDirectorySync } from 'path-type';
2526
import { glob } from 'tinyglobby';
@@ -904,7 +905,7 @@ export async function loadDailyUsageData(options?: LoadOptions): Promise<DailyUs
904905
* Loads and aggregates Claude usage data by session
905906
* Groups usage data by project path and session ID based on file structure
906907
* @param options - Optional configuration for loading and filtering data
907-
* @returns Array of session usage summaries sorted by last activity
908+
* @returns Array of session usage summaries sorted by cost (highest first)
908909
*/
909910
export async function loadSessionData(options?: LoadOptions): Promise<SessionUsage[]> {
910911
// Get all Claude paths or use the specific one from options
@@ -1080,7 +1081,17 @@ export async function loadSessionData(options?: LoadOptions): Promise<SessionUsa
10801081
options?.project,
10811082
);
10821083

1083-
return sortByDate(sessionFiltered, (item) => item.lastActivity, options?.order);
1084+
// Sort sessions by cost (highest first by default), as documented
1085+
const sorted = sort(sessionFiltered);
1086+
const order = options?.order ?? 'desc';
1087+
switch (order) {
1088+
case 'asc':
1089+
return sorted.asc((item) => item.totalCost);
1090+
case 'desc':
1091+
return sorted.desc((item) => item.totalCost);
1092+
default:
1093+
unreachable(order);
1094+
}
10841095
}
10851096

10861097
/**
@@ -2820,14 +2831,14 @@ invalid json line
28202831
expect(session?.versions).toEqual(['1.0.0', '1.1.0']); // Sorted and unique
28212832
});
28222833

2823-
it('sorts by last activity descending', async () => {
2834+
it('sorts by cost descending by default', async () => {
28242835
const sessions = [
28252836
{
28262837
sessionId: 'session1',
28272838
data: {
28282839
timestamp: createISOTimestamp('2024-01-15T12:00:00Z'),
28292840
message: { usage: { input_tokens: 100, output_tokens: 50 } },
2830-
costUSD: 0.01,
2841+
costUSD: 0.05,
28312842
},
28322843
},
28332844
{
@@ -2843,7 +2854,7 @@ invalid json line
28432854
data: {
28442855
timestamp: createISOTimestamp('2024-01-31T12:00:00Z'),
28452856
message: { usage: { input_tokens: 100, output_tokens: 50 } },
2846-
costUSD: 0.01,
2857+
costUSD: 0.1,
28472858
},
28482859
},
28492860
];
@@ -2856,21 +2867,21 @@ invalid json line
28562867
},
28572868
});
28582869

2859-
const result = await loadSessionData({ claudePath: fixture.path });
2870+
const result = await loadSessionData({ claudePath: fixture.path, mode: 'display' });
28602871

2861-
expect(result[0]?.sessionId).toBe('session3');
2872+
expect(result[0]?.sessionId).toBe('session3'); // highest cost
28622873
expect(result[1]?.sessionId).toBe('session1');
2863-
expect(result[2]?.sessionId).toBe('session2');
2874+
expect(result[2]?.sessionId).toBe('session2'); // lowest cost
28642875
});
28652876

2866-
it("sorts by last activity ascending when order is 'asc'", async () => {
2877+
it("sorts by cost ascending when order is 'asc'", async () => {
28672878
const sessions = [
28682879
{
28692880
sessionId: 'session1',
28702881
data: {
28712882
timestamp: createISOTimestamp('2024-01-15T12:00:00Z'),
28722883
message: { usage: { input_tokens: 100, output_tokens: 50 } },
2873-
costUSD: 0.01,
2884+
costUSD: 0.05,
28742885
},
28752886
},
28762887
{
@@ -2886,7 +2897,7 @@ invalid json line
28862897
data: {
28872898
timestamp: createISOTimestamp('2024-01-31T12:00:00Z'),
28882899
message: { usage: { input_tokens: 100, output_tokens: 50 } },
2889-
costUSD: 0.01,
2900+
costUSD: 0.1,
28902901
},
28912902
},
28922903
];
@@ -2902,21 +2913,22 @@ invalid json line
29022913
const result = await loadSessionData({
29032914
claudePath: fixture.path,
29042915
order: 'asc',
2916+
mode: 'display',
29052917
});
29062918

2907-
expect(result[0]?.sessionId).toBe('session2'); // oldest first
2919+
expect(result[0]?.sessionId).toBe('session2'); // lowest cost first
29082920
expect(result[1]?.sessionId).toBe('session1');
2909-
expect(result[2]?.sessionId).toBe('session3'); // newest last
2921+
expect(result[2]?.sessionId).toBe('session3'); // highest cost last
29102922
});
29112923

2912-
it("sorts by last activity descending when order is 'desc'", async () => {
2924+
it("sorts by cost descending when order is 'desc'", async () => {
29132925
const sessions = [
29142926
{
29152927
sessionId: 'session1',
29162928
data: {
29172929
timestamp: createISOTimestamp('2024-01-15T12:00:00Z'),
29182930
message: { usage: { input_tokens: 100, output_tokens: 50 } },
2919-
costUSD: 0.01,
2931+
costUSD: 0.05,
29202932
},
29212933
},
29222934
{
@@ -2932,7 +2944,7 @@ invalid json line
29322944
data: {
29332945
timestamp: createISOTimestamp('2024-01-31T12:00:00Z'),
29342946
message: { usage: { input_tokens: 100, output_tokens: 50 } },
2935-
costUSD: 0.01,
2947+
costUSD: 0.1,
29362948
},
29372949
},
29382950
];
@@ -2948,11 +2960,12 @@ invalid json line
29482960
const result = await loadSessionData({
29492961
claudePath: fixture.path,
29502962
order: 'desc',
2963+
mode: 'display',
29512964
});
29522965

2953-
expect(result[0]?.sessionId).toBe('session3'); // newest first (same as default)
2966+
expect(result[0]?.sessionId).toBe('session3'); // highest cost (same as default)
29542967
expect(result[1]?.sessionId).toBe('session1');
2955-
expect(result[2]?.sessionId).toBe('session2'); // oldest last
2968+
expect(result[2]?.sessionId).toBe('session2'); // lowest cost
29562969
});
29572970

29582971
it('filters by date range based on last activity', async () => {

0 commit comments

Comments
 (0)