Rewrite general weather alerts filter

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

View File

@ -3,56 +3,51 @@ const tornadoWarningEvents = ["Tornado Warning","Radar Indicated Tornado Warning
let alerts = msg.payload 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) { // Filter function
if (alert.raw.properties && const filterAlerts = (alerts, condition) => {
severeWarningEvents.includes(alert.raw.properties.event)) { return alerts.filter(alert => {
return true try {
return condition(alert)
} catch (e) {
node.warn(`Error processing alert: ${e.message}`)
return false
} }
}) })
// 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"
} }
// Filtering logic
const ts = filterAlerts(alerts, alert => {
const event = getNestedProperty(alert, ['raw', 'properties', 'event'])
return severeWarningEvents.includes(event)
}) })
// Filter for considerable destructive severe thunderstorm warnings const tstp = filterAlerts(alerts, alert => {
const event = getNestedProperty(alert, ['raw', 'properties', 'event'])
let cdst = alerts.filter(function(alert) { const tornadoDetection = getNestedProperty(alert, ['raw', 'properties', 'parameters', 'tornadoDetection'], [])
if (alert.raw.properties && return severeWarningEvents.includes(event) && tornadoDetection.includes("POSSIBLE")
alert.raw.properties.event === "Considerable Destructive Severe Thunderstorm Warning") {
return true
}
}) })
// Filter for Tornado Warnings const cdst = filterAlerts(alerts, alert => {
const event = getNestedProperty(alert, ['raw', 'properties', 'event'])
let tornado = alerts.filter(function(alert) { return event === "Considerable Destructive Severe Thunderstorm Warning"
if (alert.raw.properties &&
tornadoWarningEvents.includes(alert.raw.properties.event)) {
return true
}
}) })
let confirmed_tornado = alerts.filter(function(alert) { const tornado = filterAlerts(alerts, alert => {
if (alert.raw.properties && const event = getNestedProperty(alert, ['raw', 'properties', 'event'])
alert.raw.properties.event === "Confirmed Tornado Warning") { return tornadoWarningEvents.includes(event)
return true
}
}) })
// If there are any alerts, return them const confirmed_tornado = filterAlerts(alerts, alert => {
const event = getNestedProperty(alert, ['raw', 'properties', 'event'])
return event === "Confirmed Tornado Warning"
})
// Output messages
let tstormMsg = { let tstormMsg = {
"payload": { "payload": {
"alerts": ts, "alerts": ts,