From 70701483f5e50fdc70443f538d308bef6f3d999c Mon Sep 17 00:00:00 2001 From: ztimson Date: Fri, 26 Jun 2026 00:29:30 -0400 Subject: [PATCH] hourly endpoint fix? --- server/src/server.mjs | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/server/src/server.mjs b/server/src/server.mjs index f645aaf..2031250 100644 --- a/server/src/server.mjs +++ b/server/src/server.mjs @@ -11,7 +11,7 @@ import {existsSync} from 'fs'; import {getAIS} from './ais.mjs'; import {getADSB, getADSBHistory, getADSBRange, initAircraftDb} from './adsb.mjs'; import {getWeatherCondition} from './openweather.mjs'; -import {lastForecast, getForecast, forecastTTL} from './forecast.mjs'; +import {lastForecast, getForecast, forecastTTL, get24HourForecast} from './forecast.mjs'; import {dailyWeather, hourlyWeather} from './openmeteo.mjs'; // import {Aurora} from './aurora.mjs'; @@ -67,33 +67,22 @@ app.get('/api/current', async (req, res) => { app.get('/api/hourly', async (req, res) => { const { fields } = req.query - const now = new Date() - const plus24 = new Date(now.getTime() + 24 * 3600 * 1000) + const now = new Date() + now.setMinutes(0, 0, 0) + const forecastLimit = new Date(now.getTime() + 24 * 60 * 60_000) - const start = req.query.start ? new Date(req.query.start) : new Date(now.setHours(0, 0, 0, 0)) - const end = req.query.end ? new Date(req.query.end) : plus24 + const start = req.query.start ? new Date(req.query.start) : now + const end = req.query.end ? new Date(req.query.end) : forecastLimit const coords = await getCoords() - const historyEnd = new Date(Math.min(now, end)) - historyEnd.setMinutes(0, 0, 0) - - const [historyResult] = await Promise.allSettled([ - start < now ? queryHourly(start, historyEnd) : Promise.resolve([]) + const [history, forecast24, forecast3rdParty] = await Promise.all([ + start.getTime() < now.getTime() ? queryHourly(start, now) : Promise.resolve([]), + end.getTime() > now.getTime() ? getForecast().then(s => s.hours.filter(h => new Date(h.time) <= end)) : Promise.resolve([]), + end.getTime() > forecastLimit.getTime() ? hourlyWeather(coords.latitude, coords.longitude, forecastLimit, end) : Promise.resolve([]), ]) - const history = historyResult.status === 'fulfilled' ? historyResult.value : [] - const physics = end > now - ? lastForecast.slots.filter(s => { const t = new Date(s.time); return t >= now && t <= new Date(Math.min(end, plus24)) }) - : [] - - let meteo = [] - if (end > plus24) { - const [meteoResult] = await Promise.allSettled([hourlyWeather(coords.latitude, coords.longitude, plus24, end)]) - meteo = meteoResult.status === 'fulfilled' ? meteoResult.value : [] - } - - const hourly = getCelestialForecast(coords.latitude, coords.longitude, [...history, ...physics, ...meteo]) + const hourly = getCelestialForecast(coords.latitude, coords.longitude, [...history, ...forecast24, ...forecast3rdParty]) res.json(filterArr(hourly, fields)) })