mirror of
https://github.com/christianvidalwolf-prog/CrazeAnalytix.git
synced 2026-08-03 17:55:24 +02:00
107 lines
4.0 KiB
JavaScript
107 lines
4.0 KiB
JavaScript
// Script to deeply test the calculation logic
|
|||
|
|
import fs from 'fs';
|
||
|
|
|
||
|
|
const mockExperiment = {
|
||
|
|
id: 'test-1',
|
||
|
|
name: 'Test Exp',
|
||
|
|
type: 'advertising',
|
||
|
|
status: 'active',
|
||
|
|
start_date: '2026-01-01',
|
||
|
|
end_date: '2026-01-14',
|
||
|
|
asins: ['LINE:HAIRCARE'],
|
||
|
|
primary_metric: 'revenue'
|
||
|
|
};
|
||
|
|
|
||
|
|
const mockSalesData = [
|
||
|
|
// Before experiment (Baseline)
|
||
|
|
{ year: 2025, week: 52, asin: 'ASIN1', line: 'HAIRCARE', salesTotal: 500, unitsTotal: 50, cost: 50, glanceViews: 100 },
|
||
|
|
{ year: 2025, week: 52, asin: 'ASIN2', line: 'HAIRCARE', salesTotal: 200, unitsTotal: 20, cost: 20, glanceViews: 50 },
|
||
|
|
|
||
|
|
// During experiment
|
||
|
|
{ year: 2026, week: 1, asin: 'ASIN1', line: 'HAIRCARE', salesTotal: 1000, unitsTotal: 100, cost: 100, glanceViews: 200 },
|
||
|
|
{ year: 2026, week: 2, asin: 'ASIN2', line: 'HAIRCARE', salesTotal: 400, unitsTotal: 40, cost: 40, glanceViews: 100 },
|
||
|
|
|
||
|
|
// Outside date range
|
||
|
|
{ year: 2026, week: 3, asin: 'ASIN1', line: 'HAIRCARE', salesTotal: 100, unitsTotal: 10, cost: 10, glanceViews: 20 },
|
||
|
|
|
||
|
|
// Different product line
|
||
|
|
{ year: 2026, week: 1, asin: 'ASIN3', line: 'SKINCARE', salesTotal: 1000, unitsTotal: 100, cost: 100, glanceViews: 200 }
|
||
|
|
];
|
||
|
|
|
||
|
|
const getExperimentAsins = (experimentAsins, salesData) => {
|
||
|
|
const explicitAsins = new Set();
|
||
|
|
const lines = new Set();
|
||
|
|
|
||
|
|
(experimentAsins || []).forEach(a => {
|
||
|
|
const val = (a || '').trim().toUpperCase();
|
||
|
|
if (val.startsWith('LINE:')) {
|
||
|
|
lines.add(val.substring(5).trim());
|
||
|
|
} else if (val) {
|
||
|
|
explicitAsins.add(val);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
const asinSet = new Set(explicitAsins);
|
||
|
|
if (lines.size > 0 && salesData) {
|
||
|
|
salesData.forEach(r => {
|
||
|
|
const line = (r.line || '').trim().toUpperCase();
|
||
|
|
if (line && lines.has(line)) {
|
||
|
|
if (r.asin) asinSet.add(r.asin.toUpperCase());
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
return asinSet;
|
||
|
|
};
|
||
|
|
|
||
|
|
const parseLocalDate = (dateStr) => {
|
||
|
|
if (!dateStr) return new Date();
|
||
|
|
const [y, m, d] = dateStr.split('T')[0].split('-');
|
||
|
|
return new Date(Number(y), Number(m) - 1, Number(d));
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
const calculate = (experiment, salesData) => {
|
||
|
|
const startDate = parseLocalDate(experiment.start_date);
|
||
|
|
const endDate = experiment.end_date ? parseLocalDate(experiment.end_date) : new Date();
|
||
|
|
|
||
|
|
console.log('Parsed start date:', startDate);
|
||
|
|
console.log('Parsed end date:', endDate);
|
||
|
|
|
||
|
|
const durationMs = endDate.getTime() - startDate.getTime();
|
||
|
|
const baselineStart = new Date(startDate.getTime() - durationMs);
|
||
|
|
const baselineEnd = startDate;
|
||
|
|
|
||
|
|
console.log('Parsed baseline start:', baselineStart);
|
||
|
|
console.log('Parsed baseline end:', baselineEnd);
|
||
|
|
|
||
|
|
const asinSet = getExperimentAsins(experiment.asins, salesData);
|
||
|
|
console.log('Resolved ASINs:', Array.from(asinSet));
|
||
|
|
|
||
|
|
const baselineData = salesData.filter(r => {
|
||
|
|
const recordDate = new Date(r.year, 0, 1 + (r.week - 1) * 7);
|
||
|
|
const inRange = recordDate >= baselineStart && recordDate < baselineEnd;
|
||
|
|
const isAsin = asinSet.has(r.asin.toUpperCase());
|
||
|
|
if (isAsin) console.log(`[Baseline] Week ${r.year}-${r.week} date: ${recordDate} (inRange: ${inRange})`);
|
||
|
|
return isAsin && inRange;
|
||
|
|
});
|
||
|
|
|
||
|
|
const experimentData = salesData.filter(r => {
|
||
|
|
const recordDate = new Date(r.year, 0, 1 + (r.week - 1) * 7);
|
||
|
|
const inRange = recordDate >= startDate && recordDate <= endDate;
|
||
|
|
const isAsin = asinSet.has(r.asin.toUpperCase());
|
||
|
|
if (isAsin) console.log(`[Experiment] Week ${r.year}-${r.week} date: ${recordDate} (inRange: ${inRange})`);
|
||
|
|
return isAsin && inRange;
|
||
|
|
});
|
||
|
|
|
||
|
|
console.log('Baseline records matched:', baselineData.length);
|
||
|
|
console.log('Experiment records matched:', experimentData.length);
|
||
|
|
|
||
|
|
const baseline_units = baselineData.reduce((sum, r) => sum + r.unitsTotal, 0);
|
||
|
|
const experiment_units = experimentData.reduce((sum, r) => sum + r.unitsTotal, 0);
|
||
|
|
|
||
|
|
console.log('Units baseline:', baseline_units, 'Units experiment:', experiment_units);
|
||
|
|
};
|
||
|
|
|
||
|
|
calculate(mockExperiment, mockSalesData);
|