hourly endpoint fix?
This commit is contained in:
@@ -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))
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user