Duplicate Plugin Objects bug work

This commit is contained in:
Jokob-sk
2023-08-17 08:06:20 +10:00
parent 99e9f5ca68
commit cd9105d0a9

View File

@@ -441,97 +441,113 @@ def combine_plugin_objects(old, new):
def process_plugin_events(db, plugin, pluginsState): def process_plugin_events(db, plugin, pluginsState):
sql = db.sql sql = db.sql
# Access the connection from the DB instance
conn = db.sql_connection
pluginPref = plugin["unique_prefix"] pluginPref = plugin["unique_prefix"]
mylog('debug', ['[Plugins] Processing : ', pluginPref]) mylog('debug', ['[Plugins] Processing : ', pluginPref])
plugObjectsArr = db.get_sql_array ("SELECT * FROM Plugins_Objects where Plugin = '" + str(pluginPref)+"'") try:
plugEventsArr = db.get_sql_array ("SELECT * FROM Plugins_Events where Plugin = '" + str(pluginPref)+"'") # Begin a transaction
with conn:
pluginObjects = [] plugObjectsArr = db.get_sql_array ("SELECT * FROM Plugins_Objects where Plugin = '" + str(pluginPref)+"'")
pluginEvents = [] plugEventsArr = db.get_sql_array ("SELECT * FROM Plugins_Events where Plugin = '" + str(pluginPref)+"'")
for obj in plugObjectsArr: pluginObjects = []
pluginObjects.append(plugin_object_class(plugin, obj)) pluginEvents = []
existingPluginObjectsCount = len(pluginObjects) for obj in plugObjectsArr:
pluginObjects.append(plugin_object_class(plugin, obj))
mylog('debug', ['[Plugins] Existing objects : ', existingPluginObjectsCount]) existingPluginObjectsCount = len(pluginObjects)
mylog('debug', ['[Plugins] New and existing events : ', len(plugEventsArr)])
# set status as new - will be changed later if conditions are fulfilled, e.g. entry found mylog('debug', ['[Plugins] Existing objects : ', existingPluginObjectsCount])
for eve in plugEventsArr: mylog('debug', ['[Plugins] New and existing events : ', len(plugEventsArr)])
tmpObject = plugin_object_class(plugin, eve)
tmpObject.status = "new"
pluginEvents.append(tmpObject)
# set status as new - will be changed later if conditions are fulfilled, e.g. entry found
# Update the status to "exists" for eve in plugEventsArr:
index = 0 tmpObject = plugin_object_class(plugin, eve)
for tmpObjFromEvent in pluginEvents: tmpObject.status = "new"
pluginEvents.append(tmpObject)
# compare hash of the IDs for uniqueness
if any(x.idsHash == tmpObject.idsHash for x in pluginObjects): # Update the status to "exists"
mylog('debug', ['[Plugins] Found existing object'])
pluginEvents[index].status = "exists"
index += 1
# Loop thru events and update the one that exist to determine if watched columns changed
index = 0
for tmpObjFromEvent in pluginEvents:
if tmpObjFromEvent.status == "exists":
# compare hash of the changed watched columns for uniqueness
if any(x.watchedHash != tmpObject.watchedHash for x in pluginObjects):
pluginEvents[index].status = "watched-changed"
else:
pluginEvents[index].status = "watched-not-changed"
index += 1
# Merge existing plugin objects with newly discovered ones and update existing ones with new values
for eveObj in pluginEvents:
if eveObj.status == 'new':
pluginObjects.append(eveObj)
else:
index = 0 index = 0
for plugObj in pluginObjects: for tmpObjFromEvent in pluginEvents:
# find corresponding object for the event and merge
if plugObj.idsHash == eveObj.idsHash:
pluginObjects[index] = combine_plugin_objects(plugObj, eveObj)
# compare hash of the IDs for uniqueness
if any(x.idsHash == tmpObject.idsHash for x in pluginObjects):
mylog('debug', ['[Plugins] Found existing object'])
pluginEvents[index].status = "exists"
index += 1 index += 1
# Update the DB # Loop thru events and update the one that exist to determine if watched columns changed
# ---------------------------- index = 0
# Update the Plugin_Objects for tmpObjFromEvent in pluginEvents:
for plugObj in pluginObjects:
createdTime = plugObj.created if tmpObjFromEvent.status == "exists":
if plugObj.status == 'new': # compare hash of the changed watched columns for uniqueness
if any(x.watchedHash != tmpObject.watchedHash for x in pluginObjects):
createdTime = plugObj.changed pluginEvents[index].status = "watched-changed"
else:
pluginEvents[index].status = "watched-not-changed"
index += 1
sql.execute ("INSERT INTO Plugins_Objects (Plugin, Object_PrimaryID, Object_SecondaryID, DateTimeCreated, DateTimeChanged, Watched_Value1, Watched_Value2, Watched_Value3, Watched_Value4, Status, Extra, UserData, ForeignKey) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", (plugObj.pluginPref, plugObj.primaryId , plugObj.secondaryId , createdTime, plugObj.changed , plugObj.watched1 , plugObj.watched2 , plugObj.watched3 , plugObj.watched4 , plugObj.status , plugObj.extra, plugObj.userData, plugObj.foreignKey )) # Merge existing plugin objects with newly discovered ones and update existing ones with new values
else: for eveObj in pluginEvents:
sql.execute ("UPDATE Plugins_Objects set Plugin = ?, DateTimeChanged = ?, Watched_Value1 = ?, Watched_Value2 = ?, Watched_Value3 = ?, Watched_Value4 = ?, Status = ?, Extra = ?, ForeignKey = ? WHERE \"Index\" = ?", (plugObj.pluginPref, plugObj.changed, plugObj.watched1, plugObj.watched2, plugObj.watched3, plugObj.watched4, plugObj.status, plugObj.extra, plugObj.foreignKey, plugObj.index)) if eveObj.status == 'new':
pluginObjects.append(eveObj)
else:
index = 0
for plugObj in pluginObjects:
# find corresponding object for the event and merge
if plugObj.idsHash == eveObj.idsHash:
pluginObjects[index] = combine_plugin_objects(plugObj, eveObj)
# Update the Plugins_Events with the new statuses index += 1
sql.execute ('DELETE FROM Plugins_Events where Plugin = ?', (pluginPref,))
for plugObj in pluginEvents: # Update the DB
# ----------------------------
# Update the Plugin_Objects
for plugObj in pluginObjects:
createdTime = plugObj.created createdTime = plugObj.created
# use the same datetime for created and changed if a new entry if plugObj.status == 'new':
if plugObj.status == 'new':
createdTime = plugObj.changed createdTime = plugObj.changed
# insert only events if they are to be reported on sql.execute ("INSERT INTO Plugins_Objects (Plugin, Object_PrimaryID, Object_SecondaryID, DateTimeCreated, DateTimeChanged, Watched_Value1, Watched_Value2, Watched_Value3, Watched_Value4, Status, Extra, UserData, ForeignKey) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", (plugObj.pluginPref, plugObj.primaryId , plugObj.secondaryId , createdTime, plugObj.changed , plugObj.watched1 , plugObj.watched2 , plugObj.watched3 , plugObj.watched4 , plugObj.status , plugObj.extra, plugObj.userData, plugObj.foreignKey ))
if plugObj.status in get_plugin_setting_value(plugin, "REPORT_ON"): else:
sql.execute ("UPDATE Plugins_Objects set Plugin = ?, DateTimeChanged = ?, Watched_Value1 = ?, Watched_Value2 = ?, Watched_Value3 = ?, Watched_Value4 = ?, Status = ?, Extra = ?, ForeignKey = ? WHERE \"Index\" = ?", (plugObj.pluginPref, plugObj.changed, plugObj.watched1, plugObj.watched2, plugObj.watched3, plugObj.watched4, plugObj.status, plugObj.extra, plugObj.foreignKey, plugObj.index))
sql.execute ("INSERT INTO Plugins_Events (Plugin, Object_PrimaryID, Object_SecondaryID, DateTimeCreated, DateTimeChanged, Watched_Value1, Watched_Value2, Watched_Value3, Watched_Value4, Status, Extra, UserData, ForeignKey) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", (plugObj.pluginPref, plugObj.primaryId , plugObj.secondaryId , createdTime, plugObj.changed , plugObj.watched1 , plugObj.watched2 , plugObj.watched3 , plugObj.watched4 , plugObj.status , plugObj.extra, plugObj.userData, plugObj.foreignKey )) # Update the Plugins_Events with the new statuses
sql.execute ('DELETE FROM Plugins_Events where Plugin = ?', (pluginPref,))
for plugObj in pluginEvents:
createdTime = plugObj.created
# use the same datetime for created and changed if a new entry
if plugObj.status == 'new':
createdTime = plugObj.changed
# insert only events if they are to be reported on
if plugObj.status in get_plugin_setting_value(plugin, "REPORT_ON"):
sql.execute ("INSERT INTO Plugins_Events (Plugin, Object_PrimaryID, Object_SecondaryID, DateTimeCreated, DateTimeChanged, Watched_Value1, Watched_Value2, Watched_Value3, Watched_Value4, Status, Extra, UserData, ForeignKey) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", (plugObj.pluginPref, plugObj.primaryId , plugObj.secondaryId , createdTime, plugObj.changed , plugObj.watched1 , plugObj.watched2 , plugObj.watched3 , plugObj.watched4 , plugObj.status , plugObj.extra, plugObj.userData, plugObj.foreignKey ))
# Commit the transaction
conn.commit()
except Exception as e:
# Rollback the transaction in case of an error
conn.rollback()
mylog('none', ['[Plugins] SQL transaction error: ', e])
raise e
# Perform database table mapping if enabled for the plugin # Perform database table mapping if enabled for the plugin
if len(pluginEvents) > 0 and "mapped_to_table" in plugin: if len(pluginEvents) > 0 and "mapped_to_table" in plugin: