mirror of
https://github.com/christianvidalwolf-prog/CrazeAnalytix.git
synced 2026-08-03 14:25:23 +02:00
feat: replace Vendor tab with BSR tracker
This commit is contained in:
@@ -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 = (
|
||||
|
||||
Reference in New Issue
Block a user