Skip to content

Commit f2f7af3

Browse files
pbuchmanclaude
andcommitted
fix: address review feedback from CodeRabbit
- Remove export from parseSinceDate and filterFilesByMtime (internal only) - Guard filterFilesByMtime against empty string and invalid format - Extract isOutsideDateRange helper to deduplicate date skip logic - Replace await import() in test describe blocks with top-level imports Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 970eeeb commit f2f7af3

1 file changed

Lines changed: 22 additions & 38 deletions

File tree

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

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type { LoadedUsageEntry, SessionBlock } from './_session-blocks.ts';
1313
import type { ActivityDate, Bucket, CostMode, ModelName, SortOrder, Version } from './_types.ts';
1414
import { Buffer } from 'node:buffer';
1515
import { createReadStream, createWriteStream } from 'node:fs';
16-
import { readFile, stat } from 'node:fs/promises';
16+
import { readFile, stat, utimes } from 'node:fs/promises';
1717
import path from 'node:path';
1818
import process from 'node:process';
1919
import { createInterface } from 'node:readline';
@@ -719,7 +719,7 @@ export async function globUsageFiles(claudePaths: string[]): Promise<GlobResult[
719719
/**
720720
* Parse a YYYYMMDD string into a Date object (local time, midnight)
721721
*/
722-
export function parseSinceDate(since: string): Date {
722+
function parseSinceDate(since: string): Date {
723723
const y = Number(since.slice(0, 4));
724724
const m = Number(since.slice(4, 6)) - 1;
725725
const d = Number(since.slice(6, 8));
@@ -730,8 +730,8 @@ export function parseSinceDate(since: string): Date {
730730
* Pre-filter files by mtime to skip files that can't contain entries in the requested date range.
731731
* Uses a 1-day buffer before the since date to account for timezone edge cases.
732732
*/
733-
export async function filterFilesByMtime(files: string[], since?: string): Promise<string[]> {
734-
if (since == null) {
733+
async function filterFilesByMtime(files: string[], since?: string): Promise<string[]> {
734+
if (since == null || since.trim() === '' || !/^\d{8}$/.test(since)) {
735735
return files;
736736
}
737737

@@ -751,6 +751,17 @@ export async function filterFilesByMtime(files: string[], since?: string): Promi
751751
return results.filter((f): f is string => f != null);
752752
}
753753

754+
function isOutsideDateRange(date: string, since?: string, until?: string): boolean {
755+
const dateCompact = date.substring(0, 10).replace(/-/g, '');
756+
if (since != null && since !== '' && dateCompact < since) {
757+
return true;
758+
}
759+
if (until != null && until !== '' && dateCompact > until) {
760+
return true;
761+
}
762+
return false;
763+
}
764+
754765
/**
755766
* Date range filter for limiting usage data by date
756767
*/
@@ -850,12 +861,7 @@ export async function loadDailyUsageData(options?: LoadOptions): Promise<DailyUs
850861
// Always use DEFAULT_LOCALE for date grouping to ensure YYYY-MM-DD format
851862
const date = formatDate(data.timestamp, options?.timezone, DEFAULT_LOCALE);
852863

853-
// Early skip: entry outside requested date range (avoids expensive cost calculation)
854-
const dateCompact = date.substring(0, 10).replace(/-/g, '');
855-
if (options?.since != null && dateCompact < options.since) {
856-
return;
857-
}
858-
if (options?.until != null && dateCompact > options.until) {
864+
if (isOutsideDateRange(date, options?.since, options?.until)) {
859865
return;
860866
}
861867

@@ -1028,13 +1034,8 @@ export async function loadSessionData(options?: LoadOptions): Promise<SessionUsa
10281034

10291035
const sessionKey = `${projectPath}/${sessionId}`;
10301036

1031-
// Early skip: entry outside requested date range (avoids expensive cost calculation)
10321037
const entryDate = formatDate(data.timestamp, options?.timezone, DEFAULT_LOCALE);
1033-
const dateCompact = entryDate.substring(0, 10).replace(/-/g, '');
1034-
if (options?.since != null && dateCompact < options.since) {
1035-
return;
1036-
}
1037-
if (options?.until != null && dateCompact > options.until) {
1038+
if (isOutsideDateRange(entryDate, options?.since, options?.until)) {
10381039
return;
10391040
}
10401041

@@ -1467,13 +1468,8 @@ export async function loadSessionBlockData(options?: LoadOptions): Promise<Sessi
14671468
// Mark this combination as processed
14681469
markAsProcessed(uniqueHash, processedHashes);
14691470

1470-
// Early skip: entry outside requested date range (avoids expensive cost calculation)
14711471
const entryDate = formatDate(data.timestamp, options?.timezone, DEFAULT_LOCALE);
1472-
const dateCompact = entryDate.substring(0, 10).replace(/-/g, '');
1473-
if (options?.since != null && dateCompact < options.since) {
1474-
return;
1475-
}
1476-
if (options?.until != null && dateCompact > options.until) {
1472+
if (isOutsideDateRange(entryDate, options?.since, options?.until)) {
14771473
return;
14781474
}
14791475

@@ -4856,10 +4852,7 @@ if (import.meta.vitest != null) {
48564852
});
48574853
});
48584854

4859-
describe('filterFilesByMtime', async () => {
4860-
const { createFixture } = await import('fs-fixture');
4861-
const { utimes } = await import('node:fs/promises');
4862-
4855+
describe('filterFilesByMtime', () => {
48634856
it('returns all files when since is undefined', async () => {
48644857
await using fixture = await createFixture({
48654858
'a.jsonl': 'data',
@@ -5121,10 +5114,7 @@ if (import.meta.vitest != null) {
51215114
});
51225115
});
51235116

5124-
describe('mtime + loadDailyUsageData integration', async () => {
5125-
const { createFixture } = await import('fs-fixture');
5126-
const { utimes } = await import('node:fs/promises');
5127-
5117+
describe('mtime + loadDailyUsageData integration', () => {
51285118
afterEach(() => {
51295119
vi.unstubAllEnvs();
51305120
});
@@ -5283,10 +5273,7 @@ if (import.meta.vitest != null) {
52835273
});
52845274
});
52855275

5286-
describe('loadSessionData early date filtering', async () => {
5287-
const { createFixture } = await import('fs-fixture');
5288-
const { utimes } = await import('node:fs/promises');
5289-
5276+
describe('loadSessionData early date filtering', () => {
52905277
afterEach(() => {
52915278
vi.unstubAllEnvs();
52925279
});
@@ -5378,10 +5365,7 @@ if (import.meta.vitest != null) {
53785365
});
53795366
});
53805367

5381-
describe('loadSessionBlockData early date filtering', async () => {
5382-
const { createFixture } = await import('fs-fixture');
5383-
const { utimes } = await import('node:fs/promises');
5384-
5368+
describe('loadSessionBlockData early date filtering', () => {
53855369
afterEach(() => {
53865370
vi.unstubAllEnvs();
53875371
});

0 commit comments

Comments
 (0)