mirror of
https://github.com/netbymatt/ws4kp.git
synced 2026-04-14 15:49:31 -07:00
Enhance extended forecast parsing and error handling
- Add module for expired period filtering - Switch from json() to safeJson() for centralized error handling - Improve nighttime period handling to focus on full days - Fix day/night temperature pairing logic - Add debug logging
This commit is contained in:
30
server/scripts/modules/utils/forecast-utils.mjs
Normal file
30
server/scripts/modules/utils/forecast-utils.mjs
Normal file
@@ -0,0 +1,30 @@
|
||||
// shared utility functions for forecast processing
|
||||
|
||||
/**
|
||||
* Filter out expired periods from forecast data
|
||||
* @param {Array} periods - Array of forecast periods
|
||||
* @param {string} forecastUrl - URL used for logging (optional)
|
||||
* @returns {Array} - Array of active (non-expired) periods
|
||||
*/
|
||||
const filterExpiredPeriods = (periods, forecastUrl = '') => {
|
||||
const now = new Date();
|
||||
|
||||
const { activePeriods, removedPeriods } = periods.reduce((acc, period) => {
|
||||
const endTime = new Date(period.endTime);
|
||||
if (endTime > now) {
|
||||
acc.activePeriods.push(period);
|
||||
} else {
|
||||
acc.removedPeriods.push(period);
|
||||
}
|
||||
return acc;
|
||||
}, { activePeriods: [], removedPeriods: [] });
|
||||
|
||||
if (removedPeriods.length > 0) {
|
||||
const source = forecastUrl ? ` from ${forecastUrl}` : '';
|
||||
console.log(`🚮 Forecast: Removed expired periods${source}: ${removedPeriods.map((p) => `${p.name} (ended ${p.endTime})`).join(', ')}`);
|
||||
}
|
||||
|
||||
return activePeriods;
|
||||
};
|
||||
|
||||
export default filterExpiredPeriods;
|
||||
Reference in New Issue
Block a user