Rewrite general weather alerts filter

This commit is contained in:
2025-03-29 22:42:27 -04:00
parent d93d5bd1df
commit 31b543be65

View File

@ -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'})
node.send([tstormMsg, tornadoWarnMsg])
node.status({ fill: 'green', shape: 'dot', text: 'Alerts Updated' })