feat: replace Vendor tab with BSR tracker

This commit is contained in:
Christian Vidal Wolf
2026-03-02 15:22:12 +01:00
parent 918da227ee
commit 243c44c7f7
7 changed files with 278 additions and 251 deletions
+55 -1
View File
@@ -1,4 +1,4 @@
import { SalesRecord, AdsRecord, TrafficRecord, CombinedKPIs, FilterState, AggregatedData, LineGrowthMetric, ItemGrowthMetric, SeasonalityPoint, YearlySplitData, PivotRow, YearlyData, TimeSeriesData, ComparisonTimeSeriesPoint, ForecastRecord, MonthlyForecastPoint, ProductForecastData, VendorCSVRow, VendorDailyRow } from '../types';
import { SalesRecord, AdsRecord, TrafficRecord, CombinedKPIs, FilterState, AggregatedData, LineGrowthMetric, ItemGrowthMetric, SeasonalityPoint, YearlySplitData, PivotRow, YearlyData, TimeSeriesData, ComparisonTimeSeriesPoint, ForecastRecord, MonthlyForecastPoint, ProductForecastData, VendorCSVRow, VendorDailyRow, BSRRecord } from '../types';
import * as XLSX from 'xlsx';
import Papa from 'papaparse';
@@ -657,6 +657,60 @@ export const processTrafficExcel = async (fileOrBuffer: File | ArrayBuffer): Pro
}
};
// --- BSR DATA PARSING ---
export const processBSRExcel = async (fileOrBuffer: File | ArrayBuffer): Promise<BSRRecord[]> => {
try {
const arrayBuffer = fileOrBuffer instanceof File
? await fileOrBuffer.arrayBuffer()
: fileOrBuffer;
const workbook = XLSX.read(arrayBuffer, { type: 'array' });
const allData: BSRRecord[] = [];
// Typically BSR is in the first sheet
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];
const jsonData: any[] = XLSX.utils.sheet_to_json(worksheet, { defval: "" });
console.log(`Processing BSR sheet "${sheetName}": ${jsonData.length} rows`);
for (let i = 0; i < jsonData.length; i++) {
const row = jsonData[i];
const weekRaw = getColumnValue(row, ['week', 'woche', 'semana']);
const marketRaw = getColumnValue(row, ['market', 'country', 'marketplace']);
const asinRaw = getColumnValue(row, ['asin']);
const topLevelBSRRaw = getColumnValue(row, ['Mean Weekly Top Level BSR']);
const topLevelNameRaw = getColumnValue(row, ['Top Level Category Name']);
const detailLevelBSRRaw = getColumnValue(row, ['Mean Weekly Detail Level BSR']);
const detailLevelNameRaw = getColumnValue(row, ['Detail Level Category Name']);
const avgRatingRaw = getColumnValue(row, ['Mean Weekly Average Rating']);
if (!weekRaw || !marketRaw || !asinRaw) continue;
const week = parseInt(String(weekRaw).match(/\\d+/)?.[0] || '0', 10);
if (isNaN(week) || week === 0) continue;
allData.push({
week,
market: String(marketRaw).trim(),
asin: String(asinRaw).trim(),
topLevelBSR: parseIntSafe(String(topLevelBSRRaw)),
topLevelName: topLevelNameRaw ? String(topLevelNameRaw).trim() : null,
detailLevelBSR: parseIntSafe(String(detailLevelBSRRaw)),
detailLevelName: detailLevelNameRaw ? String(detailLevelNameRaw).trim() : null,
avgRating: parseFloat(String(avgRatingRaw)) || null,
});
}
console.log(`Total BSR records loaded: ${allData.length}`);
return allData;
} catch (error) {
console.error("Error processing BSR Excel:", error);
throw error;
}
};
// --- DATA MERGING ---
export const mergeSalesAndAdsData = (