From adda0cd58f910612bec4f8fc65834c750c6067ee Mon Sep 17 00:00:00 2001 From: Christian Vidal Wolf Date: Thu, 26 Feb 2026 10:47:36 +0100 Subject: [PATCH] fix(storage): enforce Ads cached schema invalidation to clear broken numeric mapped columns --- services/experimentAnalysis.ts | 14 ++++++++++++-- services/storage.ts | 16 +++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/services/experimentAnalysis.ts b/services/experimentAnalysis.ts index 86a53a9..07f7438 100644 --- a/services/experimentAnalysis.ts +++ b/services/experimentAnalysis.ts @@ -87,12 +87,22 @@ function aggregateWeeklyMetrics( w.revenue += r.salesTotal || (r as any).sellOut || 0; w.adRevenue += r.salesAds || 0; w.sessions += r.glanceViews || 0; - w.cost += r.cost || 0; + w.cost += Number(r.cost) || 0; w.clicks += r.clicks || 0; w.impressions += r.impressions || 0; } - return Array.from(weeklyMap.values()) + // Debug ad map + console.log(`[aggregateWeeklyMetrics] ASIN match for ${marketplace}:`, asinSet); + const debugArr = Array.from(weeklyMap.values()); + const hasAds = debugArr.some(w => w.cost > 0 || w.adRevenue > 0); + if (!hasAds) { + console.log(`[aggregateWeeklyMetrics WARNING] 0 ad data grouped for mkt ${marketplace}!`, debugArr); + } else { + console.log(`[aggregateWeeklyMetrics SUCCESS] Found ad usage:`, debugArr.filter(w => w.cost > 0)); + } + + return debugArr .sort((a, b) => a.timestamp - b.timestamp) .map(w => ({ ...w, diff --git a/services/storage.ts b/services/storage.ts index b45e5ee..b11d017 100644 --- a/services/storage.ts +++ b/services/storage.ts @@ -7,7 +7,7 @@ const ADS_STORE_NAME = 'adsData'; const DB_VERSION = 2; // Increment this when data processing logic changes (e.g., field mapping changes) // This forces cache invalidation and re-processing of CSV data -const DATA_SCHEMA_VERSION = 2; // Updated for LICENSE column mapping +const DATA_SCHEMA_VERSION = 3; // Updated to clear broken Ads data const initDB = (): Promise => { return new Promise((resolve, reject) => { @@ -115,6 +115,7 @@ export const saveAdsData = async (data: any[]): Promise => { store.put(data, 'currentData'); store.put(new Date().toISOString(), 'lastUpdated'); + store.put(DATA_SCHEMA_VERSION, 'schemaVersion'); transaction.oncomplete = () => resolve(); transaction.onerror = () => reject(transaction.error); @@ -133,8 +134,21 @@ export const loadAdsData = async (): Promise<{ data: any[]; lastUpdated: string const dataReq = store.get('currentData'); const dateReq = store.get('lastUpdated'); + const versionReq = store.get('schemaVersion'); transaction.oncomplete = () => { + const cachedVersion = versionReq.result; + + // If schema version doesn't match, invalidate cache + if (cachedVersion !== DATA_SCHEMA_VERSION) { + console.log('[Storage] Ads Schema version mismatch. Cached:', cachedVersion, 'Current:', DATA_SCHEMA_VERSION); + resolve({ + data: [], + lastUpdated: null + }); + return; + } + resolve({ data: dataReq.result || [], lastUpdated: dateReq.result || null