Skip to content

Commit a261378

Browse files
committed
BE: # ---------------------------------------------------------------------------------#
Signed-off-by: jokob-sk <jokob.sk@gmail.com>
1 parent 63810bc commit a261378

File tree

1 file changed

+86
-28
lines changed

1 file changed

+86
-28
lines changed

server/messaging/reporting.py

Lines changed: 86 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def apply_timezone(data, fields):
100100
# ===============================================================================
101101
def get_notifications(db):
102102
"""
103-
Fetch notifications for all configured sections, applying timezone conversions.
103+
Fetch notifications for all configured sections.
104104
105105
Args:
106106
db: Database object with `.sql` for executing queries.
@@ -126,10 +126,38 @@ def get_notifications(db):
126126
AND eve_MAC IN (SELECT devMac FROM Devices WHERE devAlertDown = 0)
127127
""")
128128

129-
sections = get_setting_value("NTFPRCS_INCLUDED_SECTIONS")
129+
sections = get_setting_value("NTFPRCS_INCLUDED_SECTIONS") or []
130130
mylog("verbose", ["[Notification] Included sections: ", sections])
131131

132-
# Define SQL templates per section
132+
# -------------------------
133+
# Helper: condition mapping
134+
# -------------------------
135+
def get_section_condition(section):
136+
"""
137+
Resolve condition setting key with backward compatibility.
138+
"""
139+
# New format
140+
key = f"NTFPRCS_{section}_condition"
141+
value = get_setting_value(key)
142+
143+
if value:
144+
return value
145+
146+
# Legacy keys
147+
legacy_map = {
148+
"new_devices": "NTFPRCS_new_dev_condition",
149+
"events": "NTFPRCS_event_condition",
150+
}
151+
152+
legacy_key = legacy_map.get(section)
153+
if legacy_key:
154+
return get_setting_value(legacy_key)
155+
156+
return ""
157+
158+
# -------------------------
159+
# SQL templates
160+
# -------------------------
133161
sql_templates = {
134162
"new_devices": """
135163
SELECT
@@ -140,10 +168,11 @@ def get_notifications(db):
140168
devName as "Device name",
141169
devComments as Comments
142170
FROM Events_Devices
143-
WHERE eve_PendingAlertEmail = 1 AND eve_EventType = 'New Device' {condition}
171+
WHERE eve_PendingAlertEmail = 1
172+
AND eve_EventType = 'New Device' {condition}
144173
ORDER BY eve_DateTime
145174
""",
146-
"down_devices": f"""
175+
"down_devices": """
147176
SELECT
148177
devName,
149178
eve_MAC,
@@ -154,7 +183,7 @@ def get_notifications(db):
154183
FROM Events_Devices AS down_events
155184
WHERE eve_PendingAlertEmail = 1
156185
AND down_events.eve_EventType = 'Device Down'
157-
AND eve_DateTime < datetime('now', '-{int(get_setting_value("NTFPRCS_alert_down_time") or 0)} minutes')
186+
AND eve_DateTime < datetime('now', '-0 minutes')
158187
AND NOT EXISTS (
159188
SELECT 1
160189
FROM Events AS connected_events
@@ -214,43 +243,72 @@ def get_notifications(db):
214243
"plugins": "🔌 Plugins"
215244
}
216245

217-
final_json = {}
246+
# Sections that support dynamic conditions
247+
sections_with_conditions = {"new_devices", "events"}
218248

219-
# Pre-initialize final_json with all expected keys
249+
# Initialize final structure
220250
final_json = {}
221251
for section in ["new_devices", "down_devices", "down_reconnected", "events", "plugins"]:
222252
final_json[section] = []
223-
final_json[f"{section}_meta"] = {"title": section_titles.get(section, section), "columnNames": []}
253+
final_json[f"{section}_meta"] = {
254+
"title": section_titles.get(section, section),
255+
"columnNames": []
256+
}
257+
258+
condition_builder = create_safe_condition_builder()
259+
260+
# -------------------------
261+
# Main loop
262+
# -------------------------
263+
condition_builder = create_safe_condition_builder()
264+
265+
SECTION_CONDITION_MAP = {
266+
"new_devices": "NTFPRCS_new_dev_condition",
267+
"events": "NTFPRCS_event_condition",
268+
}
269+
270+
sections_with_conditions = set(SECTION_CONDITION_MAP.keys())
224271

225-
# Loop through each included section
226272
for section in sections:
273+
template = sql_templates.get(section)
274+
275+
if not template:
276+
mylog("verbose", ["[Notification] Unknown section: ", section])
277+
continue
278+
279+
safe_condition = ""
280+
parameters = {}
281+
227282
try:
228-
# Build safe condition for sections that support it
229-
condition_builder = create_safe_condition_builder()
230-
condition_setting = get_setting_value(f"NTFPRCS_{section}_condition")
231-
safe_condition, parameters = condition_builder.get_safe_condition_legacy(condition_setting)
232-
sqlQuery = sql_templates.get(section, "").format(condition=safe_condition)
233-
except Exception:
234-
# Fallback if safe condition fails
235-
sqlQuery = sql_templates.get(section, "").format(condition="")
283+
if section in sections_with_conditions:
284+
condition_key = SECTION_CONDITION_MAP.get(section)
285+
condition_setting = get_setting_value(condition_key)
286+
287+
if condition_setting:
288+
safe_condition, parameters = condition_builder.get_safe_condition_legacy(
289+
condition_setting
290+
)
291+
292+
sqlQuery = template.format(condition=safe_condition)
293+
294+
except Exception as e:
295+
mylog("verbose", [f"[Notification] Error building condition for {section}: ", e])
296+
sqlQuery = template.format(condition="")
236297
parameters = {}
237298

238299
mylog("debug", [f"[Notification] {section} SQL query: ", sqlQuery])
239300
mylog("debug", [f"[Notification] {section} parameters: ", parameters])
240301

241-
# Fetch data as JSON
242-
json_obj = db.get_table_as_json(sqlQuery, parameters)
243-
244-
mylog("debug", [f"[Notification] json_obj.json: {json.dumps(json_obj.json)}"])
245-
246-
# Apply timezone conversion
247-
json_obj.json["data"] = apply_timezone_to_json(json_obj, section=section)
302+
try:
303+
json_obj = db.get_table_as_json(sqlQuery, parameters)
304+
except Exception as e:
305+
mylog("minimal", [f"[Notification] DB error in section {section}: ", e])
306+
continue
248307

249-
# Save data and metadata
250-
final_json[section] = json_obj.json["data"]
308+
final_json[section] = json_obj.json.get("data", [])
251309
final_json[f"{section}_meta"] = {
252310
"title": section_titles.get(section, section),
253-
"columnNames": json_obj.columnNames
311+
"columnNames": getattr(json_obj, "columnNames", [])
254312
}
255313

256314
mylog("debug", [f"[Notification] final_json: {json.dumps(final_json)}"])

0 commit comments

Comments
 (0)