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

Function aggregateClaudeCodeStats

source code/utils/stats.ts:642–712  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

640 * Uses a disk cache to avoid reprocessing historical data.
641 */
642export async function aggregateClaudeCodeStats(): Promise<ClaudeCodeStats> {
643 const allSessionFiles = await getAllSessionFiles()
644
645 if (allSessionFiles.length === 0) {
646 return getEmptyStats()
647 }
648
649 // Use lock to prevent race conditions with background cache updates
650 const updatedCache = await withStatsCacheLock(async () => {
651 // Load the cache
652 const cache = await loadStatsCache()
653 const yesterday = getYesterdayDateString()
654
655 // Determine what needs to be processed
656 // - If no cache: process everything up to yesterday, then today separately
657 // - If cache exists: process from day after lastComputedDate to yesterday, then today
658 let result = cache
659
660 if (!cache.lastComputedDate) {
661 // No cache - process all historical data (everything before today)
662 logForDebugging('Stats cache empty, processing all historical data')
663 const historicalStats = await processSessionFiles(allSessionFiles, {
664 toDate: yesterday,
665 })
666
667 if (
668 historicalStats.sessionStats.length > 0 ||
669 historicalStats.dailyActivity.length > 0
670 ) {
671 result = mergeCacheWithNewStats(cache, historicalStats, yesterday)
672 await saveStatsCache(result)
673 }
674 } else if (isDateBefore(cache.lastComputedDate, yesterday)) {
675 // Cache is stale - process new days
676 // Process from day after lastComputedDate to yesterday
677 const nextDay = getNextDay(cache.lastComputedDate)
678 logForDebugging(
679 `Stats cache stale (${cache.lastComputedDate}), processing ${nextDay} to ${yesterday}`,
680 )
681 const newStats = await processSessionFiles(allSessionFiles, {
682 fromDate: nextDay,
683 toDate: yesterday,
684 })
685
686 if (
687 newStats.sessionStats.length > 0 ||
688 newStats.dailyActivity.length > 0
689 ) {
690 result = mergeCacheWithNewStats(cache, newStats, yesterday)
691 await saveStatsCache(result)
692 } else {
693 // No new data, but update lastComputedDate
694 result = { ...cache, lastComputedDate: yesterday }
695 await saveStatsCache(result)
696 }
697 }
698
699 return result

Callers 1

Calls 13

getAllSessionFilesFunction · 0.85
getEmptyStatsFunction · 0.85
withStatsCacheLockFunction · 0.85
loadStatsCacheFunction · 0.85
getYesterdayDateStringFunction · 0.85
logForDebuggingFunction · 0.85
processSessionFilesFunction · 0.85
mergeCacheWithNewStatsFunction · 0.85
saveStatsCacheFunction · 0.85
isDateBeforeFunction · 0.85
getNextDayFunction · 0.85
getTodayDateStringFunction · 0.85

Tested by

no test coverage detected