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

Function migrateStatsCache

source code/utils/statsCache.ts:109–143  ·  view source on GitHub ↗

* Migrate an older cache to the current schema. * Returns null if the version is unknown or too old to migrate. * * Preserves historical aggregates that would otherwise be lost when * transcript files have already aged out past cleanupPeriodDays. * Pre-migration days may undercount (e.g. v2 lac

(
  parsed: Partial<PersistedStatsCache> & { version: number },
)

Source from the content-addressed store, hash-verified

107 * we accept that rather than drop the history.
108 */
109function migrateStatsCache(
110 parsed: Partial<PersistedStatsCache> & { version: number },
111): PersistedStatsCache | null {
112 if (
113 typeof parsed.version !== 'number' ||
114 parsed.version < MIN_MIGRATABLE_VERSION ||
115 parsed.version > STATS_CACHE_VERSION
116 ) {
117 return null
118 }
119 if (
120 !Array.isArray(parsed.dailyActivity) ||
121 !Array.isArray(parsed.dailyModelTokens) ||
122 typeof parsed.totalSessions !== 'number' ||
123 typeof parsed.totalMessages !== 'number'
124 ) {
125 return null
126 }
127 return {
128 version: STATS_CACHE_VERSION,
129 lastComputedDate: parsed.lastComputedDate ?? null,
130 dailyActivity: parsed.dailyActivity,
131 dailyModelTokens: parsed.dailyModelTokens,
132 modelUsage: parsed.modelUsage ?? {},
133 totalSessions: parsed.totalSessions,
134 totalMessages: parsed.totalMessages,
135 longestSession: parsed.longestSession ?? null,
136 firstSessionDate: parsed.firstSessionDate ?? null,
137 hourCounts: parsed.hourCounts ?? {},
138 totalSpeculationTimeSavedMs: parsed.totalSpeculationTimeSavedMs ?? 0,
139 // Preserve undefined (don't default to {}) so the SHOT_STATS recompute
140 // check in loadStatsCache fires for v1/v2 caches that lacked this field.
141 shotDistribution: parsed.shotDistribution,
142 }
143}
144
145/**
146 * Load the stats cache from disk.

Callers 1

loadStatsCacheFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected