MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / processSessionFiles

Function processSessionFiles

source code/utils/stats.ts:119–370  ·  view source on GitHub ↗

* Process session files and extract stats. * Can filter by date range.

(
  sessionFiles: string[],
  options: ProcessOptions = {},
)

Source from the content-addressed store, hash-verified

117 * Can filter by date range.
118 */
119async function processSessionFiles(
120 sessionFiles: string[],
121 options: ProcessOptions = {},
122): Promise<ProcessedStats> {
123 const { fromDate, toDate } = options
124 const fs = getFsImplementation()
125
126 const dailyActivityMap = new Map<string, DailyActivity>()
127 const dailyModelTokensMap = new Map<string, { [modelName: string]: number }>()
128 const sessions: SessionStats[] = []
129 const hourCounts = new Map<number, number>()
130 let totalMessages = 0
131 let totalSpeculationTimeSavedMs = 0
132 const modelUsageAgg: { [modelName: string]: ModelUsage } = {}
133 const shotDistributionMap = feature('SHOT_STATS')
134 ? new Map<number, number>()
135 : undefined
136 // Track parent sessions that already recorded a shot count (dedup across subagents)
137 const sessionsWithShotCount = new Set<string>()
138
139 // Process session files in parallel batches for better performance
140 const BATCH_SIZE = 20
141 for (let i = 0; i < sessionFiles.length; i += BATCH_SIZE) {
142 const batch = sessionFiles.slice(i, i + BATCH_SIZE)
143 const results = await Promise.all(
144 batch.map(async sessionFile => {
145 try {
146 // If we have a fromDate filter, skip files that haven't been modified since then
147 if (fromDate) {
148 let fileSize = 0
149 try {
150 const fileStat = await fs.stat(sessionFile)
151 const fileModifiedDate = toDateString(fileStat.mtime)
152 if (isDateBefore(fileModifiedDate, fromDate)) {
153 return {
154 sessionFile,
155 entries: null,
156 error: null,
157 skipped: true,
158 }
159 }
160 fileSize = fileStat.size
161 } catch {
162 // If we can't stat the file, try to read it anyway
163 }
164 // For large files, peek at the session start date before reading everything.
165 // Sessions that pass the mtime filter but started before fromDate are skipped
166 // (e.g. a month-old session resumed today gets a new mtime write but old start date).
167 if (fileSize > 65536) {
168 const startDate = await readSessionStartDate(sessionFile)
169 if (startDate && isDateBefore(startDate, fromDate)) {
170 return {
171 sessionFile,
172 entries: null,
173 error: null,
174 skipped: true,
175 }
176 }

Callers 2

aggregateClaudeCodeStatsFunction · 0.85

Calls 13

getFsImplementationFunction · 0.85
toDateStringFunction · 0.85
isDateBeforeFunction · 0.85
readSessionStartDateFunction · 0.85
logForDebuggingFunction · 0.85
isTranscriptMessageFunction · 0.85
setMethod · 0.80
entriesMethod · 0.80
errorMessageFunction · 0.70
hasMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected