fix(consumer-prices): write retailer_spread_pct metric in aggregate job (#1941)

buildOverviewSnapshot queries retailer_spread_pct from computed_indices
but aggregate.ts never wrote it, causing the spread column to always be NULL.
This commit is contained in:
Elie Habib
2026-03-20 20:08:20 +04:00
committed by GitHub
parent 455449821b
commit 2b19bf0317

View File

@@ -193,6 +193,17 @@ export async function aggregateBasket(basketSlug: string, marketCode: string) {
await writeComputedIndex(basketId, null, null, 'value_index', valueIndex);
await writeComputedIndex(basketId, null, null, 'coverage_pct', coveragePct);
// Retailer spread: (most expensive basket - cheapest basket) / cheapest × 100
const retailerTotals = new Map<string, number>();
for (const r of rows) {
retailerTotals.set(r.retailerSlug, (retailerTotals.get(r.retailerSlug) ?? 0) + r.price);
}
if (retailerTotals.size >= 2) {
const totals = [...retailerTotals.values()];
const spreadPct = ((Math.max(...totals) - Math.min(...totals)) / Math.min(...totals)) * 100;
await writeComputedIndex(basketId, null, null, 'retailer_spread_pct', Math.round(spreadPct * 10) / 10);
}
// Per-category indices for buildTopCategories snapshot
const byCategory = new Map<string, BasketRow[]>();
for (const r of rows) {