From 0d13e85639684609cb164e4d88bc23713fd72d8d Mon Sep 17 00:00:00 2001 From: Peter Meier Date: Tue, 5 May 2026 00:06:11 +0200 Subject: [PATCH] fix(strommix): discard SMARD snapshot when aligned timestamp >90 min stale MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When SMARD's load filter (id 410) lags 12+ hours, the aligned-min strategy in fetchSmardSnapshot produces a snapshot stamped with that 12-hour-old timestamp — and the widget surfaces it as the live "Stand HH:MM" header. With Energy-Charts already returning fresher numbers through the CMS path, falling back entirely is the right move. Threshold is 90 min: tight enough to catch obvious lag, loose enough to keep normal 30–60 min SMARD delay as the canonical source. --- src/routes/api/strommix/+server.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/routes/api/strommix/+server.ts b/src/routes/api/strommix/+server.ts index ae40176..661bbaf 100644 --- a/src/routes/api/strommix/+server.ts +++ b/src/routes/api/strommix/+server.ts @@ -127,7 +127,7 @@ async function fetchPriceAtNow( } export const GET: RequestHandler = async ({ fetch }) => { - const [smard, energyCharts, cross, priceDe, priceFr] = await Promise.all([ + const [smardRaw, energyCharts, cross, priceDe, priceFr] = await Promise.all([ fetchSmardSnapshot(fetch), // Energy-Charts via the CMS — used as per-field fallback when SMARD // returns null for a specific filter (notably load 410 when SMARD's @@ -138,6 +138,17 @@ export const GET: RequestHandler = async ({ fetch }) => { fetchPriceAtNow("FR", fetch), ]); + // SMARD aligned-min picks the earliest "latest" across filters. If + // one filter (typically load 410) lags 12+ hours, the snapshot ends + // up labelled with a stale timestamp like "12:15 Uhr morgens" while + // newer Energy-Charts data is right there. Discard the SMARD snapshot + // when it is older than 90 min so the aggregator falls back to + // Energy-Charts entirely. 90 min lets typical 30–60 min SMARD lag + // still win. + const nowSec = Math.floor(Date.now() / 1000); + const smard = + smardRaw && nowSec - smardRaw.measuredAtUnix < 90 * 60 ? smardRaw : null; + const ec = energyCharts.data ?? {}; // Per-field fallback. SMARD value `null` = filter unavailable; we