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
// 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
}
})
// 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
let cdst = alerts.filter(function(alert) {
if (alert.raw.properties &&
alert.raw.properties.event === "Considerable Destructive Severe Thunderstorm Warning") {
return true
}
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 Tornado Warnings
let tornado = alerts.filter(function(alert) {
if (alert.raw.properties &&
tornadoWarningEvents.includes(alert.raw.properties.event)) {
return true
}
const cdst = filterAlerts(alerts, alert => {
const event = getNestedProperty(alert, ['raw', 'properties', 'event'])
return event === "Considerable Destructive Severe Thunderstorm Warning"
})
let confirmed_tornado = alerts.filter(function(alert) {
if (alert.raw.properties &&
alert.raw.properties.event === "Confirmed Tornado Warning") {
return true
}
const tornado = filterAlerts(alerts, alert => {
const event = getNestedProperty(alert, ['raw', 'properties', 'event'])
return tornadoWarningEvents.includes(event)
})
// 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 = {
"payload": {
"alerts": ts,