clearPluginObjects

added sub to be called during main loop to clear plugins_objects table
This commit is contained in:
Ingo Ratsdorf
2025-09-15 15:54:51 +12:00
parent aee93c0e24
commit 750fb33e1c
2 changed files with 111 additions and 103 deletions

View File

@@ -189,6 +189,7 @@ def main ():
notification.clearPendingEmailFlag() notification.clearPendingEmailFlag()
else: else:
notification.clearPluginObjects()
mylog('verbose', ['[Notification] No changes to report']) mylog('verbose', ['[Notification] No changes to report'])
# Commit SQL # Commit SQL

View File

@@ -1,12 +1,8 @@
import datetime
import os
import _io
import json import json
import sys import sys
import uuid import uuid
import socket import socket
import subprocess import subprocess
import requests
from yattag import indent from yattag import indent
from json2table import convert from json2table import convert
@@ -16,15 +12,20 @@ sys.path.extend([f"{INSTALL_PATH}/server"])
# Register NetAlertX modules # Register NetAlertX modules
import conf import conf
from const import applicationPath, logPath, apiPath, confFileName, reportTemplatesPath from const import applicationPath, logPath, apiPath, reportTemplatesPath
from logger import logResult, mylog from logger import mylog
from helper import generate_mac_links, removeDuplicateNewLines, timeNowTZ, get_file_content, write_file, get_setting_value, get_timezone_offset from helper import generate_mac_links, \
removeDuplicateNewLines, \
timeNowTZ, \
write_file, \
get_setting_value, \
get_timezone_offset
from messaging.in_app import write_notification from messaging.in_app import write_notification
#------------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Notification object handling # Notification object handling
#------------------------------------------------------------------------------- # -----------------------------------------------------------------------------
class NotificationInstance: class NotificationInstance:
def __init__(self, db): def __init__(self, db):
self.db = db self.db = db
@@ -52,7 +53,6 @@ class NotificationInstance:
return JSON, Extra return JSON, Extra
# Create a new DB entry if new notifications available, otherwise skip # Create a new DB entry if new notifications available, otherwise skip
def create(self, JSON, Extra=""): def create(self, JSON, Extra=""):
@@ -78,8 +78,6 @@ class NotificationInstance:
self.Extra = Extra self.Extra = Extra
if self.HasNotifications: if self.HasNotifications:
# if not notiStruc.json['data'] and not notiStruc.text and not notiStruc.html: # if not notiStruc.json['data'] and not notiStruc.text and not notiStruc.html:
# mylog('debug', '[Notification] notiStruc is empty') # mylog('debug', '[Notification] notiStruc is empty')
# else: # else:
@@ -89,7 +87,6 @@ class NotificationInstance:
HTML = "" HTML = ""
template_file_path = reportTemplatesPath + 'report_template.html' template_file_path = reportTemplatesPath + 'report_template.html'
# Open text Template # Open text Template
mylog('verbose', ['[Notification] Open text Template']) mylog('verbose', ['[Notification] Open text Template'])
template_file = open(reportTemplatesPath + 'report_template.txt', 'r') template_file = open(reportTemplatesPath + 'report_template.txt', 'r')
@@ -256,9 +253,7 @@ class NotificationInstance:
self.save() self.save()
# Clear the Pending Email flag from all events and devices
def clearPendingEmailFlag(self): def clearPendingEmailFlag(self):
# Clean Pending Alert Events # Clean Pending Alert Events
@@ -280,7 +275,6 @@ class NotificationInstance:
AND eve_DateTime < datetime('now', '-{get_setting_value('NTFPRCS_alert_down_time')} minutes', '{get_timezone_offset()}') AND eve_DateTime < datetime('now', '-{get_setting_value('NTFPRCS_alert_down_time')} minutes', '{get_timezone_offset()}')
""") """)
# clear plugin events # clear plugin events
self.db.sql.execute("DELETE FROM Plugins_Events") self.db.sql.execute("DELETE FROM Plugins_Events")
@@ -289,17 +283,21 @@ class NotificationInstance:
self.save() self.save()
def clearPluginObjects(self):
# clear plugin events
self.db.sql.execute("DELETE FROM Plugins_Events")
self.save()
def save(self): def save(self):
# Commit changes # Commit changes
self.db.commitDB() self.db.commitDB()
#-------------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Reporting # Reporting
#------------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# ------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
def construct_notifications(JSON, section): def construct_notifications(JSON, section):
jsn = JSON[section] jsn = JSON[section]
@@ -314,14 +312,20 @@ def construct_notifications(JSON, section):
html = '' html = ''
text = '' text = ''
table_attributes = {"style" : "border-collapse: collapse; font-size: 12px; color:#70707", "width" : "100%", "cellspacing" : 0, "cellpadding" : "3px", "bordercolor" : "#C0C0C0", "border":"1"} table_attributes = {
"style": "border-collapse: collapse; font-size: 12px; color:#70707",
"width": "100%",
"cellspacing": 0,
"cellpadding": "3px",
"bordercolor": "#C0C0C0",
"border": "1"
}
headerProps = "width='120px' style='color:white; font-size: 16px;' bgcolor='#64a0d6' " headerProps = "width='120px' style='color:white; font-size: 16px;' bgcolor='#64a0d6' "
thProps = "width='120px' style='color:#F0F0F0' bgcolor='#64a0d6' " thProps = "width='120px' style='color:#F0F0F0' bgcolor='#64a0d6' "
build_direction = "TOP_TO_BOTTOM" build_direction = "TOP_TO_BOTTOM"
text_line = '{}\t{}\n' text_line = '{}\t{}\n'
if len(jsn) > 0: if len(jsn) > 0:
text = tableTitle + "\n---------\n" text = tableTitle + "\n---------\n"
@@ -329,7 +333,13 @@ def construct_notifications(JSON, section):
html = convert({"data": jsn}, build_direction=build_direction, table_attributes=table_attributes) html = convert({"data": jsn}, build_direction=build_direction, table_attributes=table_attributes)
# Cleanup the generated HTML table notification # Cleanup the generated HTML table notification
html = format_table(html, "data", headerProps, tableTitle).replace('<ul>','<ul style="list-style:none;padding-left:0">').replace("<td>null</td>", "<td></td>") html = format_table(html,
"data",
headerProps,
tableTitle).replace('<ul>',
'<ul style="list-style:none;padding-left:0">'
).replace("<td>null</td>",
"<td></td>")
# prepare text-only message # prepare text-only message
for device in jsn: for device in jsn:
@@ -346,7 +356,8 @@ def construct_notifications(JSON, section):
return html, text return html, text
#-------------------------------------------------------------------------------
# -----------------------------------------------------------------------------
def send_api(json_final, mail_text, mail_html): def send_api(json_final, mail_text, mail_html):
mylog('verbose', ['[Send API] Updating notification_* files in ', apiPath]) mylog('verbose', ['[Send API] Updating notification_* files in ', apiPath])
@@ -355,8 +366,7 @@ def send_api(json_final, mail_text, mail_html):
write_file(apiPath + 'notification_json_final.json', json.dumps(json_final)) write_file(apiPath + 'notification_json_final.json', json.dumps(json_final))
# -----------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Replacing table headers # Replacing table headers
def format_table(html, thValue, props, newThValue=''): def format_table(html, thValue, props, newThValue=''):
@@ -364,6 +374,3 @@ def format_table (html, thValue, props, newThValue = ''):
newThValue = thValue newThValue = thValue
return html.replace("<th>"+thValue+"</th>", "<th "+props+" >"+newThValue+"</th>") return html.replace("<th>"+thValue+"</th>", "<th "+props+" >"+newThValue+"</th>")