From 31b543be651b813a17d4bbcdca57f331283e66cc Mon Sep 17 00:00:00 2001 From: Tony Stork Date: Sat, 29 Mar 2025 22:42:27 -0400 Subject: [PATCH] Rewrite general weather alerts filter --- weather/alerts_filter.js | 81 +++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 43 deletions(-) diff --git a/weather/alerts_filter.js b/weather/alerts_filter.js index 928d3df..be2bbaa 100644 --- a/weather/alerts_filter.js +++ b/weather/alerts_filter.js @@ -1,58 +1,53 @@ -const severeWarningEvents = ["Severe Thunderstorm Warning","Destructive Severe Thunderstorm Warning","Considerable Destructive Severe Thunderstorm Warning"] -const tornadoWarningEvents = ["Tornado Warning","Radar Indicated Tornado Warning","Confirmed Tornado Warning","Tornado Emergency"] +const severeWarningEvents = ["Severe Thunderstorm Warning", "Destructive Severe Thunderstorm Warning", "Considerable Destructive Severe Thunderstorm Warning"] +const tornadoWarningEvents = ["Tornado Warning", "Radar Indicated Tornado Warning", "Confirmed Tornado Warning", "Tornado Emergency"] let alerts = msg.payload -// Filter for Severe Thunderstorm Warnings +// Helper function to safely access nested properties +const getNestedProperty = (obj, path, defaultValue = undefined) => { + return path.reduce((acc, key) => (acc && acc[key] !== undefined ? acc[key] : defaultValue), obj) +} -let ts = alerts.filter(function(alert) { - if (alert.raw.properties && - severeWarningEvents.includes(alert.raw.properties.event)) { - return true - } +// Filter function +const filterAlerts = (alerts, condition) => { + return alerts.filter(alert => { + try { + return condition(alert) + } catch (e) { + node.warn(`Error processing alert: ${e.message}`) + return false + } + }) +} + +// Filtering logic +const ts = filterAlerts(alerts, alert => { + const event = getNestedProperty(alert, ['raw', 'properties', 'event']) + return severeWarningEvents.includes(event) }) -// Filter for Severe Thunderstorm Warnings with tornado detection -// that have a tornado possible parameter - -let tstp = alerts.filter(function(alert) { - if (alert.raw.properties && - severeWarningEvents.includes(alert.raw.properties.event) && - alert.raw.properties.parameters && - alert.raw.properties.parameters.tornadoDetection && - alert.raw.properties.parameters.tornadoDetection.length > 0) { - let tornadoPossible = alert.raw.properties.parameters.tornadoDetection[0] - return tornadoPossible === "POSSIBLE" - } +const tstp = filterAlerts(alerts, alert => { + const event = getNestedProperty(alert, ['raw', 'properties', 'event']) + const tornadoDetection = getNestedProperty(alert, ['raw', 'properties', 'parameters', 'tornadoDetection'], []) + return severeWarningEvents.includes(event) && tornadoDetection.includes("POSSIBLE") }) -// Filter for considerable destructive severe thunderstorm warnings - -let cdst = alerts.filter(function(alert) { - if (alert.raw.properties && - alert.raw.properties.event === "Considerable Destructive Severe Thunderstorm Warning") { - return true - } +const cdst = filterAlerts(alerts, alert => { + const event = getNestedProperty(alert, ['raw', 'properties', 'event']) + return event === "Considerable Destructive Severe Thunderstorm Warning" }) -// Filter for Tornado Warnings - -let tornado = alerts.filter(function(alert) { - if (alert.raw.properties && - tornadoWarningEvents.includes(alert.raw.properties.event)) { - return true - } +const tornado = filterAlerts(alerts, alert => { + const event = getNestedProperty(alert, ['raw', 'properties', 'event']) + return tornadoWarningEvents.includes(event) }) -let confirmed_tornado = alerts.filter(function(alert) { - if (alert.raw.properties && - alert.raw.properties.event === "Confirmed Tornado Warning") { - return true - } +const confirmed_tornado = filterAlerts(alerts, alert => { + const event = getNestedProperty(alert, ['raw', 'properties', 'event']) + return event === "Confirmed Tornado Warning" }) -// If there are any alerts, return them - +// Output messages let tstormMsg = { "payload": { "alerts": ts, @@ -70,5 +65,5 @@ let tornadoWarnMsg = { } } -node.send([tstormMsg,tornadoWarnMsg]) -node.status({fill:'green',shape:'dot',text:'Alerts Updated'}) \ No newline at end of file +node.send([tstormMsg, tornadoWarnMsg]) +node.status({ fill: 'green', shape: 'dot', text: 'Alerts Updated' }) \ No newline at end of file