From 9ecf4114ba08de48bad79439dadcc79e14646ca4 Mon Sep 17 00:00:00 2001 From: Tatiana Villa Ema Date: Mon, 27 Apr 2026 01:03:43 +0200 Subject: [PATCH] fix: Hoy muestra ultimo dia disponible si no hay datos de hoy --- frontend/js/estadisticas.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/frontend/js/estadisticas.js b/frontend/js/estadisticas.js index 03d2fb2..ababd79 100644 --- a/frontend/js/estadisticas.js +++ b/frontend/js/estadisticas.js @@ -70,9 +70,26 @@ async function loadToday() { if (!response.ok) throw new Error("Error cargando hoy: " + response.status); let data = await response.json(); - if (!data || !data.length) throw new Error("Sin datos de hoy"); + data = data ? data.filter(d => d.ciudad === ciudadActual) : []; + + // Si no hay datos de hoy, buscar el último día disponible + if (!data.length) { + const hace30 = new Date(ahora); + hace30.setDate(hace30.getDate() - 30); + const y2 = hace30.getFullYear(); + const m2 = String(hace30.getMonth() + 1).padStart(2, "0"); + const d2 = String(hace30.getDate()).padStart(2, "0"); + const urlFallback = buildApiUrl({ ciudad: ciudadActual, desde: `${y2}-${m2}-${d2}`, hasta: hoy }); + const r2 = await fetch(urlFallback); + let data2 = await r2.json(); + data2 = data2 ? data2.filter(d => d.ciudad === ciudadActual) : []; + if (!data2.length) throw new Error("Sin datos recientes"); + // Ordenar por fecha y tomar el último día + data2.sort((a, b) => a.dia.localeCompare(b.dia)); + const ultimoDia = data2[data2.length - 1].dia; + data = data2.filter(d => d.dia === ultimoDia); + } - data = data.filter(d => d.ciudad === ciudadActual); renderLastData(data); } catch (err) {