mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-07 09:36:05 -08:00
wf work
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import sys
|
||||
import sqlite3
|
||||
|
||||
# Register NetAlertX directories
|
||||
INSTALL_PATH="/app"
|
||||
@@ -7,6 +8,8 @@ sys.path.extend([f"{INSTALL_PATH}/server"])
|
||||
import conf
|
||||
from logger import mylog, Logger
|
||||
from helper import get_setting_value, timeNowTZ
|
||||
from models.device_instance import DeviceInstance
|
||||
from models.plugin_object_instance import PluginObjectInstance
|
||||
|
||||
# Make sure log level is initialized correctly
|
||||
Logger(get_setting_value('LOG_LEVEL'))
|
||||
@@ -27,22 +30,76 @@ class Action:
|
||||
class UpdateFieldAction(Action):
|
||||
"""Action to update a specific field of an object."""
|
||||
|
||||
def __init__(self, field, value, trigger):
|
||||
def __init__(self, db, field, value, trigger):
|
||||
super().__init__(trigger) # Call the base class constructor
|
||||
self.field = field
|
||||
self.value = value
|
||||
self.db = db
|
||||
|
||||
def execute(self):
|
||||
mylog('verbose', [f"Updating field '{self.field}' to '{self.value}' for event object {self.trigger.object_type}"])
|
||||
mylog('verbose', f"[WF] Updating field '{self.field}' to '{self.value}' for event object {self.trigger.object_type}")
|
||||
|
||||
obj = self.trigger.object
|
||||
|
||||
# convert to dict for easeir handling
|
||||
if isinstance(obj, sqlite3.Row):
|
||||
obj = dict(obj) # Convert Row object to a standard dictionary
|
||||
|
||||
processed = False
|
||||
|
||||
# currently unused
|
||||
if isinstance(obj, dict) and "ObjectGUID" in obj:
|
||||
plugin_instance = PluginObjectInstance(self.trigger.db)
|
||||
mylog('debug', f"[WF] Updating Object '{obj}' ")
|
||||
plugin_instance = PluginObjectInstance(self.db)
|
||||
plugin_instance.updateField(obj["ObjectGUID"], self.field, self.value)
|
||||
processed = True
|
||||
|
||||
elif isinstance(obj, dict) and "devGUID" in obj:
|
||||
device_instance = DeviceInstance(self.trigger.db)
|
||||
mylog('debug', f"[WF] Updating Device '{obj}' ")
|
||||
device_instance = DeviceInstance(self.db)
|
||||
device_instance.updateField(obj["devGUID"], self.field, self.value)
|
||||
processed = True
|
||||
|
||||
if not processed:
|
||||
mylog('none', f"[WF] Could not process action for object: {obj}")
|
||||
|
||||
return obj
|
||||
|
||||
|
||||
class DeleteObjectAction(Action):
|
||||
"""Action to delete an object."""
|
||||
|
||||
def __init__(self, db, trigger):
|
||||
super().__init__(trigger) # Call the base class constructor
|
||||
self.db = db
|
||||
|
||||
def execute(self):
|
||||
mylog('verbose', f"[WF] Deleting event object {self.trigger.object_type}")
|
||||
|
||||
obj = self.trigger.object
|
||||
|
||||
# convert to dict for easeir handling
|
||||
if isinstance(obj, sqlite3.Row):
|
||||
obj = dict(obj) # Convert Row object to a standard dictionary
|
||||
|
||||
processed = False
|
||||
|
||||
# currently unused
|
||||
if isinstance(obj, dict) and "ObjectGUID" in obj:
|
||||
mylog('debug', f"[WF] Updating Object '{obj}' ")
|
||||
plugin_instance = PluginObjectInstance(self.db)
|
||||
plugin_instance.delete(obj["ObjectGUID"])
|
||||
processed = True
|
||||
|
||||
elif isinstance(obj, dict) and "devGUID" in obj:
|
||||
mylog('debug', f"[WF] Updating Device '{obj}' ")
|
||||
device_instance = DeviceInstance(self.db)
|
||||
device_instance.delete(obj["devGUID"])
|
||||
processed = True
|
||||
|
||||
if not processed:
|
||||
mylog('none', f"[WF] Could not process action for object: {obj}")
|
||||
|
||||
return obj
|
||||
|
||||
|
||||
|
||||
@@ -49,20 +49,21 @@ class AppEvent_obj:
|
||||
"ObjectIsArchived": "NEW.devIsArchived",
|
||||
"ObjectPlugin": "'DEVICES'"
|
||||
}
|
||||
},
|
||||
"Plugins_Objects": {
|
||||
"fields": {
|
||||
"ObjectGUID": "NEW.ObjectGUID",
|
||||
"ObjectPrimaryID": "NEW.Plugin",
|
||||
"ObjectSecondaryID": "NEW.Object_PrimaryID",
|
||||
"ObjectForeignKey": "NEW.ForeignKey",
|
||||
"ObjectStatus": "NEW.Status",
|
||||
"ObjectStatusColumn": "'Status'",
|
||||
"ObjectIsNew": "CASE WHEN NEW.Status = 'new' THEN 1 ELSE 0 END",
|
||||
"ObjectIsArchived": "0", # Default value
|
||||
"ObjectPlugin": "NEW.Plugin"
|
||||
}
|
||||
}
|
||||
# ,
|
||||
# "Plugins_Objects": {
|
||||
# "fields": {
|
||||
# "ObjectGUID": "NEW.ObjectGUID",
|
||||
# "ObjectPrimaryID": "NEW.Plugin",
|
||||
# "ObjectSecondaryID": "NEW.Object_PrimaryID",
|
||||
# "ObjectForeignKey": "NEW.ForeignKey",
|
||||
# "ObjectStatus": "NEW.Status",
|
||||
# "ObjectStatusColumn": "'Status'",
|
||||
# "ObjectIsNew": "CASE WHEN NEW.Status = 'new' THEN 1 ELSE 0 END",
|
||||
# "ObjectIsArchived": "0", # Default value
|
||||
# "ObjectPlugin": "NEW.Plugin"
|
||||
# }
|
||||
# }
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -42,6 +42,9 @@ class WorkflowManager:
|
||||
WHERE AppEventProcessed = 0
|
||||
ORDER BY DateTimeCreated ASC
|
||||
""").fetchall()
|
||||
|
||||
mylog('none', [f'[WF] get_new_app_events - new events count: {len(result)}'])
|
||||
|
||||
return result
|
||||
|
||||
def process_event(self, event):
|
||||
@@ -103,14 +106,17 @@ class WorkflowManager:
|
||||
if action["type"] == "update_field":
|
||||
field = action["field"]
|
||||
value = action["value"]
|
||||
action_instance = UpdateFieldAction(field, value, trigger)
|
||||
action_instance = UpdateFieldAction(self.db, field, value, trigger)
|
||||
# indicate if the api has to be updated
|
||||
self.update_api = True
|
||||
|
||||
elif action["type"] == "run_plugin":
|
||||
plugin_name = action["plugin"]
|
||||
params = action["params"]
|
||||
action_instance = RunPluginAction(plugin_name, params, trigger)
|
||||
action_instance = RunPluginAction(self.db, plugin_name, params, trigger)
|
||||
|
||||
elif action["type"] == "delete_device":
|
||||
action_instance = DeleteObjectAction(self.db, trigger)
|
||||
|
||||
# elif action["type"] == "send_notification":
|
||||
# method = action["method"]
|
||||
|
||||
Reference in New Issue
Block a user