hourly endpoint fix?

This commit is contained in:
2026-06-26 00:29:30 -04:00
parent 1ee386949c
commit 70701483f5

View File

@@ -11,7 +11,7 @@ import {existsSync} from 'fs';
import {getAIS} from './ais.mjs'; import {getAIS} from './ais.mjs';
import {getADSB, getADSBHistory, getADSBRange, initAircraftDb} from './adsb.mjs'; import {getADSB, getADSBHistory, getADSBRange, initAircraftDb} from './adsb.mjs';
import {getWeatherCondition} from './openweather.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 {dailyWeather, hourlyWeather} from './openmeteo.mjs';
// import {Aurora} from './aurora.mjs'; // import {Aurora} from './aurora.mjs';
@@ -68,32 +68,21 @@ app.get('/api/current', async (req, res) => {
app.get('/api/hourly', async (req, res) => { app.get('/api/hourly', async (req, res) => {
const { fields } = req.query const { fields } = req.query
const now = new Date() const now = new Date()
const plus24 = new Date(now.getTime() + 24 * 3600 * 1000) 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 start = req.query.start ? new Date(req.query.start) : now
const end = req.query.end ? new Date(req.query.end) : plus24 const end = req.query.end ? new Date(req.query.end) : forecastLimit
const coords = await getCoords() const coords = await getCoords()
const historyEnd = new Date(Math.min(now, end)) const [history, forecast24, forecast3rdParty] = await Promise.all([
historyEnd.setMinutes(0, 0, 0) 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([]),
const [historyResult] = await Promise.allSettled([ end.getTime() > forecastLimit.getTime() ? hourlyWeather(coords.latitude, coords.longitude, forecastLimit, end) : Promise.resolve([]),
start < now ? queryHourly(start, historyEnd) : Promise.resolve([])
]) ])
const history = historyResult.status === 'fulfilled' ? historyResult.value : []
const physics = end > now const hourly = getCelestialForecast(coords.latitude, coords.longitude, [...history, ...forecast24, ...forecast3rdParty])
? 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])
res.json(filterArr(hourly, fields)) res.json(filterArr(hourly, fields))
}) })