| 258 | * Used when incrementally adding new days to the cache. |
| 259 | */ |
| 260 | export function mergeCacheWithNewStats( |
| 261 | existingCache: PersistedStatsCache, |
| 262 | newStats: { |
| 263 | dailyActivity: DailyActivity[] |
| 264 | dailyModelTokens: DailyModelTokens[] |
| 265 | modelUsage: { [modelName: string]: ModelUsage } |
| 266 | sessionStats: SessionStats[] |
| 267 | hourCounts: { [hour: number]: number } |
| 268 | totalSpeculationTimeSavedMs: number |
| 269 | shotDistribution?: { [shotCount: number]: number } |
| 270 | }, |
| 271 | newLastComputedDate: string, |
| 272 | ): PersistedStatsCache { |
| 273 | // Merge daily activity - combine by date |
| 274 | const dailyActivityMap = new Map<string, DailyActivity>() |
| 275 | for (const day of existingCache.dailyActivity) { |
| 276 | dailyActivityMap.set(day.date, { ...day }) |
| 277 | } |
| 278 | for (const day of newStats.dailyActivity) { |
| 279 | const existing = dailyActivityMap.get(day.date) |
| 280 | if (existing) { |
| 281 | existing.messageCount += day.messageCount |
| 282 | existing.sessionCount += day.sessionCount |
| 283 | existing.toolCallCount += day.toolCallCount |
| 284 | } else { |
| 285 | dailyActivityMap.set(day.date, { ...day }) |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | // Merge daily model tokens - combine by date |
| 290 | const dailyModelTokensMap = new Map<string, { [model: string]: number }>() |
| 291 | for (const day of existingCache.dailyModelTokens) { |
| 292 | dailyModelTokensMap.set(day.date, { ...day.tokensByModel }) |
| 293 | } |
| 294 | for (const day of newStats.dailyModelTokens) { |
| 295 | const existing = dailyModelTokensMap.get(day.date) |
| 296 | if (existing) { |
| 297 | for (const [model, tokens] of Object.entries(day.tokensByModel)) { |
| 298 | existing[model] = (existing[model] || 0) + tokens |
| 299 | } |
| 300 | } else { |
| 301 | dailyModelTokensMap.set(day.date, { ...day.tokensByModel }) |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | // Merge model usage |
| 306 | const modelUsage = { ...existingCache.modelUsage } |
| 307 | for (const [model, usage] of Object.entries(newStats.modelUsage)) { |
| 308 | if (modelUsage[model]) { |
| 309 | modelUsage[model] = { |
| 310 | inputTokens: modelUsage[model]!.inputTokens + usage.inputTokens, |
| 311 | outputTokens: modelUsage[model]!.outputTokens + usage.outputTokens, |
| 312 | cacheReadInputTokens: |
| 313 | modelUsage[model]!.cacheReadInputTokens + usage.cacheReadInputTokens, |
| 314 | cacheCreationInputTokens: |
| 315 | modelUsage[model]!.cacheCreationInputTokens + |
| 316 | usage.cacheCreationInputTokens, |
| 317 | webSearchRequests: |