Forecast fixes
This commit is contained in:
@@ -102,40 +102,25 @@ app.get('/api/hourly', async (req, res) => {
|
||||
|
||||
app.get('/api/daily', async (req, res) => {
|
||||
const { fields } = req.query
|
||||
const now = new Date()
|
||||
const now = new Date()
|
||||
now.setHours(0, 0, 0, 0)
|
||||
const next = new Date(now); next.setDate(now.getDate() + 1)
|
||||
|
||||
const next = new Date(now); next.setDate(now.getDate() + 1)
|
||||
const start = req.query.start ? new Date(req.query.start) : now
|
||||
const end = req.query.end ? new Date(req.query.end) : next
|
||||
const coords = await getCoords()
|
||||
|
||||
// Does the requested window overlap with today (the 24h forecast window)?
|
||||
const todayStr = now
|
||||
const usePhysics = lastForecast.summary && start <= next && end >= now
|
||||
|
||||
// Past influx history (days before today)
|
||||
const [sensorResult] = await Promise.allSettled([
|
||||
start < now ? queryDaily(start, now) : Promise.resolve([])
|
||||
])
|
||||
const todayStr = now.toISOString().slice(0, 10) // ✅ string for comparison
|
||||
const usePhysics = lastForecast.summary && start <= now && end > now // ✅ only when today is in window
|
||||
const [sensorResult] = await Promise.allSettled([start < now ? queryDaily(start, now) : Promise.resolve([])])
|
||||
const history = sensorResult.status === 'fulfilled' ? sensorResult.value : []
|
||||
|
||||
// Future meteo (days after today)
|
||||
const meteoStart = end > next ? next : null
|
||||
const [meteoResult] = await Promise.allSettled([
|
||||
meteoStart && end > next ? dailyWeather(coords.latitude, coords.longitude, next, end) : Promise.resolve([])
|
||||
])
|
||||
// ✅ meteo starts from whichever is later: next or start
|
||||
const meteoFrom = new Date(Math.max(next.getTime(), start.getTime()))
|
||||
const [meteoResult] = await Promise.allSettled([end > next ? dailyWeather(coords.latitude, coords.longitude, meteoFrom, end) : Promise.resolve([])])
|
||||
const meteo = meteoResult.status === 'fulfilled' ? meteoResult.value : []
|
||||
|
||||
// Splice in physics summary for today, skip any meteo row that duplicates it
|
||||
const todaySlot = usePhysics ? [lastForecast.summary] : []
|
||||
const meteoClean = meteo.filter(r => r.time !== todayStr)
|
||||
|
||||
const daily = getCelestialForecast(
|
||||
coords.latitude, coords.longitude,
|
||||
[...history, ...todaySlot, ...meteoClean]
|
||||
)
|
||||
const meteoClean = meteo.filter(r => String(r.time).slice(0, 10) !== todayStr) // ✅ safe string compare
|
||||
|
||||
const daily = getCelestialForecast(coords.latitude, coords.longitude, [...history, ...todaySlot, ...meteoClean])
|
||||
res.json(filterArr(daily, fields))
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user