fix(analysis): compute variation for oldest month in 12-month table
The last row of the monthly variation table (oldest month of the rolling
window) always showed only the inflation value: the window started exactly
at that month, so there was no previous month to compare against, the
variation was null and the currency cells rendered '--' (hiding the prices
too).
- Backend: fetch 13 months (12 displayed + 1 base) so the oldest displayed
month gets its previous month; bump cache key to monthly-last-v3.
- Backend: fix external fallback filter comparing a full date against a
month key ('2026-10-14' > '2026-10' is always true), which silently
dropped the most recent month whenever it was missing from the DB.
Now filters by month key only.
- Backend: normalize subtractMonths to the 1st of the target month to avoid
month shifts; skip external quotes without a buy value.
- Frontend: compute variations over the 13 months and render the last 12;
show the current price when a variation is unavailable so no row is blank.
- Add regression tests for the window size and the external fallback.
This commit is contained in:
@@ -35,6 +35,10 @@ const MONTH_NAMES = [
|
||||
"Diciembre",
|
||||
];
|
||||
|
||||
// La ventana trae 12 meses a mostrar + 1 mes "base" al inicio, que se usa para
|
||||
// calcular la variación del mes más antiguo mostrado.
|
||||
const DISPLAY_MONTHS = 12;
|
||||
|
||||
function formatMonth(monthKey: string): string {
|
||||
const [y, m] = monthKey.split("-");
|
||||
return `${MONTH_NAMES[Number(m) - 1]} ${y}`;
|
||||
@@ -107,6 +111,11 @@ function VariationIndicator({
|
||||
<span className="text-xs text-muted-foreground inline-flex items-center gap-1">
|
||||
<ArrowRight className="size-3" />
|
||||
--
|
||||
{current !== null && (
|
||||
<span className="text-[10px] tabular-nums">
|
||||
{formatPrice(current)}
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
@@ -195,51 +204,50 @@ export function MonthlyVariationTable() {
|
||||
return ((currentVal - prevVal) / prevVal) * 100;
|
||||
};
|
||||
|
||||
return sortedMonths
|
||||
.map((month, i) => {
|
||||
const prev = i > 0 ? sortedMonths[i - 1] : null;
|
||||
const current = monthsMap.get(month);
|
||||
const prevData = prev ? monthsMap.get(prev) : null;
|
||||
const allRows = sortedMonths.map((month, i) => {
|
||||
const prev = i > 0 ? sortedMonths[i - 1] : null;
|
||||
const current = monthsMap.get(month);
|
||||
const prevData = prev ? monthsMap.get(prev) : null;
|
||||
|
||||
const beloVar = calcVariation(
|
||||
current?.BELO ?? null,
|
||||
prevData?.BELO ?? null,
|
||||
);
|
||||
const blueVar = calcVariation(
|
||||
current?.BLUE ?? null,
|
||||
prevData?.BLUE ?? null,
|
||||
);
|
||||
const bnaVar = calcVariation(
|
||||
current?.BNA ?? null,
|
||||
prevData?.BNA ?? null,
|
||||
);
|
||||
const beloVar = calcVariation(
|
||||
current?.BELO ?? null,
|
||||
prevData?.BELO ?? null,
|
||||
);
|
||||
const blueVar = calcVariation(
|
||||
current?.BLUE ?? null,
|
||||
prevData?.BLUE ?? null,
|
||||
);
|
||||
const bnaVar = calcVariation(current?.BNA ?? null, prevData?.BNA ?? null);
|
||||
|
||||
let inflation: number | null = null;
|
||||
inflation = inflationMap.get(month) ?? null;
|
||||
let inflation: number | null = null;
|
||||
inflation = inflationMap.get(month) ?? null;
|
||||
|
||||
return {
|
||||
month,
|
||||
label: formatMonth(month),
|
||||
fromDb: (current?.dbTypes.size ?? 0) > 0,
|
||||
BELO: {
|
||||
variation: beloVar,
|
||||
prev: prevData?.BELO ?? null,
|
||||
current: current?.BELO ?? null,
|
||||
},
|
||||
BLUE: {
|
||||
variation: blueVar,
|
||||
prev: prevData?.BLUE ?? null,
|
||||
current: current?.BLUE ?? null,
|
||||
},
|
||||
BNA: {
|
||||
variation: bnaVar,
|
||||
prev: prevData?.BNA ?? null,
|
||||
current: current?.BNA ?? null,
|
||||
},
|
||||
inflation,
|
||||
};
|
||||
})
|
||||
.reverse();
|
||||
return {
|
||||
month,
|
||||
label: formatMonth(month),
|
||||
fromDb: (current?.dbTypes.size ?? 0) > 0,
|
||||
BELO: {
|
||||
variation: beloVar,
|
||||
prev: prevData?.BELO ?? null,
|
||||
current: current?.BELO ?? null,
|
||||
},
|
||||
BLUE: {
|
||||
variation: blueVar,
|
||||
prev: prevData?.BLUE ?? null,
|
||||
current: current?.BLUE ?? null,
|
||||
},
|
||||
BNA: {
|
||||
variation: bnaVar,
|
||||
prev: prevData?.BNA ?? null,
|
||||
current: current?.BNA ?? null,
|
||||
},
|
||||
inflation,
|
||||
};
|
||||
});
|
||||
|
||||
// Descartamos el mes base (el más antiguo) del render: ya cumplió su
|
||||
// función como mes previo para calcular la variación del primero mostrado.
|
||||
return allRows.slice(-DISPLAY_MONTHS).reverse();
|
||||
}, [monthlyData, inflationData]);
|
||||
|
||||
const summary = useMemo(() => {
|
||||
|
||||
Reference in New Issue
Block a user