Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/ccusage/src/data-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ export function createUniqueHash(data: UsageData): string | null {
* @param filePath - Path to the JSONL file
* @param processLine - Callback function to process each line
*/
async function processJSONLFileByLine(
export async function processJSONLFileByLine(
filePath: string,
processLine: (line: string, lineNumber: number) => void | Promise<void>,
): Promise<void> {
Expand Down
18 changes: 6 additions & 12 deletions apps/ccusage/src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @module debug
*/

import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { Result } from '@praha/byethrow';
import { createFixture } from 'fs-fixture';
Expand All @@ -19,7 +18,7 @@ import {
USAGE_DATA_GLOB_PATTERN,
} from './_consts.ts';
import { PricingFetcher } from './_pricing-fetcher.ts';
import { getClaudePaths, usageDataSchema } from './data-loader.ts';
import { getClaudePaths, processJSONLFileByLine, usageDataSchema } from './data-loader.ts';
import { logger } from './logger.ts';

/**
Expand Down Expand Up @@ -106,27 +105,22 @@ export async function detectMismatches(claudePath?: string): Promise<MismatchSta
};

for (const file of files) {
const content = await readFile(file, 'utf-8');
const lines = content
.trim()
.split('\n')
.filter((line) => line.length > 0);

for (const line of lines) {
// Use streaming to avoid memory issues with large files
await processJSONLFileByLine(file, async (line) => {
const parseParser = Result.try({
try: () => JSON.parse(line) as unknown,
catch: () => new Error('Invalid JSON'),
});

const parseResult = parseParser();
if (Result.isFailure(parseResult)) {
continue;
return;
}

const schemaResult = v.safeParse(usageDataSchema, parseResult.value);

if (!schemaResult.success) {
continue;
return;
}

const data = schemaResult.output;
Expand Down Expand Up @@ -206,7 +200,7 @@ export async function detectMismatches(claudePath?: string): Promise<MismatchSta
(modelStat.avgPercentDiff * (modelStat.total - 1) + percentDiff) / modelStat.total;
stats.modelStats.set(model, modelStat);
}
}
});
}

return stats;
Expand Down