mirror of
https://github.com/christianvidalwolf-prog/CrazeAnalytix.git
synced 2026-08-03 12:25:22 +02:00
fix: BSR rank line now shows in BSR vs Units Sold chart
BSRRecord.market is stored as short codes ('DE', 'IT', etc.) from the
Excel file, but the chart was comparing against full names ('Amazon DE',
'Amazon IT', etc.) — causing bsrRecordsForMarket to always be empty and
the BSR line to never render. Added normalizeBsrMarket() to map short
codes to full names before filtering. Also removed the debug strip.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
dcf77cad2d
commit
0e1457bd3d
@@ -12,6 +12,12 @@ interface Props {
|
||||
|
||||
const MARKETS = ['Amazon DE', 'Amazon ES', 'Amazon FR', 'Amazon IT', 'Amazon UK'] as const;
|
||||
|
||||
// BSR Excel stores market as short codes ('DE', 'IT', …); normalize to full names
|
||||
const BSR_MARKET_NORMALIZE: Record<string, string> = {
|
||||
'DE': 'Amazon DE', 'ES': 'Amazon ES', 'FR': 'Amazon FR', 'IT': 'Amazon IT', 'UK': 'Amazon UK',
|
||||
};
|
||||
const normalizeBsrMarket = (m: string) => BSR_MARKET_NORMALIZE[m.toUpperCase()] ?? m;
|
||||
|
||||
const MARKET_CONFIG: Record<string, { label: string; color: string; flag: string }> = {
|
||||
'Amazon DE': { label: 'DE', color: '#f37526', flag: '🇩🇪' },
|
||||
'Amazon ES': { label: 'ES', color: '#2acbd6', flag: '🇪🇸' },
|
||||
@@ -55,7 +61,7 @@ export const BSRUnitsCorrelationChart: React.FC<Props> = ({ bsrData, combinedSal
|
||||
|
||||
// Derive available markets from actual data
|
||||
const availableMarkets = useMemo(() => {
|
||||
const bsrMarkets = new Set(bsrData.map(r => r.market));
|
||||
const bsrMarkets = new Set(bsrData.map(r => normalizeBsrMarket(r.market)));
|
||||
const salesMarkets = new Set(combinedSalesData.map(r => r.customer));
|
||||
return MARKETS.filter(m => bsrMarkets.has(m) || salesMarkets.has(m));
|
||||
}, [bsrData, combinedSalesData]);
|
||||
@@ -75,11 +81,11 @@ export const BSRUnitsCorrelationChart: React.FC<Props> = ({ bsrData, combinedSal
|
||||
}, [combinedSalesData]);
|
||||
|
||||
// Per-market chart data: BSR avg + units sum aligned by week
|
||||
const { chartData, pearsonCorrelation, debugInfo } = useMemo(() => {
|
||||
const { chartData, pearsonCorrelation } = useMemo(() => {
|
||||
// BSR: average detailLevelBSR per week (falls back to topLevelBSR if detail is unavailable)
|
||||
// Coerce week to number to guard against string values at runtime
|
||||
const bsrByWeek = new Map<number, { sum: number; count: number }>();
|
||||
const bsrRecordsForMarket = bsrData.filter(r => r.market === resolvedMarket);
|
||||
const bsrRecordsForMarket = bsrData.filter(r => normalizeBsrMarket(r.market) === resolvedMarket);
|
||||
bsrRecordsForMarket
|
||||
.filter(r => r.detailLevelBSR != null || r.topLevelBSR != null)
|
||||
.forEach(r => {
|
||||
@@ -98,14 +104,6 @@ export const BSRUnitsCorrelationChart: React.FC<Props> = ({ bsrData, combinedSal
|
||||
unitsByWeek.set(week, (unitsByWeek.get(week) ?? 0) + r.unitsTotal);
|
||||
});
|
||||
|
||||
const debugInfo = {
|
||||
bsrTotal: bsrData.length,
|
||||
bsrForMarket: bsrRecordsForMarket.length,
|
||||
bsrWithValues: bsrByWeek.size,
|
||||
unitsWeeks: unitsByWeek.size,
|
||||
uniqueMarkets: Array.from(new Set(bsrData.map(r => r.market))).join(', '),
|
||||
};
|
||||
|
||||
const allWeeks = Array.from(
|
||||
new Set([...bsrByWeek.keys(), ...unitsByWeek.keys()])
|
||||
).sort((a, b) => a - b);
|
||||
@@ -127,7 +125,7 @@ export const BSRUnitsCorrelationChart: React.FC<Props> = ({ bsrData, combinedSal
|
||||
paired.map(d => d.units as number)
|
||||
);
|
||||
|
||||
return { chartData, pearsonCorrelation: r, debugInfo };
|
||||
return { chartData, pearsonCorrelation: r };
|
||||
}, [resolvedMarket, bsrData, combinedSalesData, currentYear]);
|
||||
|
||||
const config = MARKET_CONFIG[resolvedMarket] ?? { label: '?', color: '#94a3b8', flag: '🌍' };
|
||||
@@ -336,13 +334,6 @@ export const BSRUnitsCorrelationChart: React.FC<Props> = ({ bsrData, combinedSal
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* Temporary debug strip — remove once BSR line is confirmed working */}
|
||||
<div className="px-3 py-2 rounded-lg bg-slate-900/60 border border-slate-800 text-[10px] font-mono text-slate-500 space-y-0.5">
|
||||
<div>BSR records total: <span className="text-slate-300">{debugInfo.bsrTotal}</span> · for {resolvedMarket}: <span className="text-slate-300">{debugInfo.bsrForMarket}</span> · with values: <span className="text-slate-300">{debugInfo.bsrWithValues} weeks</span></div>
|
||||
<div>Units weeks found: <span className="text-slate-300">{debugInfo.unitsWeeks}</span> · year: <span className="text-slate-300">{currentYear}</span></div>
|
||||
<div>Markets in BSR data: <span className="text-slate-300">{debugInfo.uniqueMarkets || '(none)'}</span></div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user