mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2026-04-03 08:41:35 -07:00
Refactor event and session column names to camelCase
- Updated test cases to reflect new column names (eve_MAC -> eveMac, eve_DateTime -> eveDateTime, etc.) across various test files. - Modified SQL table definitions in the database cleanup and migration tests to use camelCase naming conventions. - Implemented migration tests to ensure legacy column names are correctly renamed to camelCase equivalents. - Ensured that existing data is preserved during the migration process and that views referencing old column names are dropped before renaming. - Verified that the migration function is idempotent, allowing for safe re-execution without data loss.
This commit is contained in:
@@ -581,8 +581,8 @@ def print_scan_stats(db):
|
||||
row_dict = dict(row)
|
||||
mylog("trace", f" {row_dict}")
|
||||
|
||||
mylog("trace", " ================ Events table content where eve_PendingAlertEmail = 1 ================",)
|
||||
sql.execute("select * from Events where eve_PendingAlertEmail = 1")
|
||||
mylog("trace", " ================ Events table content where evePendingAlertEmail = 1 ================",)
|
||||
sql.execute("select * from Events where evePendingAlertEmail = 1")
|
||||
rows = sql.fetchall()
|
||||
for row in rows:
|
||||
row_dict = dict(row)
|
||||
@@ -611,9 +611,9 @@ def create_new_devices(db):
|
||||
mylog("debug", '[New Devices] Insert "New Device" Events')
|
||||
query_new_device_events = f"""
|
||||
INSERT OR IGNORE INTO Events (
|
||||
eve_MAC, eve_IP, eve_DateTime,
|
||||
eve_EventType, eve_AdditionalInfo,
|
||||
eve_PendingAlertEmail
|
||||
eveMac, eveIp, eveDateTime,
|
||||
eveEventType, eveAdditionalInfo,
|
||||
evePendingAlertEmail
|
||||
)
|
||||
SELECT DISTINCT scanMac, scanLastIP, '{startTime}', 'New Device', scanVendor, 1
|
||||
FROM CurrentScan
|
||||
@@ -630,9 +630,9 @@ def create_new_devices(db):
|
||||
mylog("debug", "[New Devices] Insert Connection into session table")
|
||||
|
||||
sql.execute(f"""INSERT INTO Sessions (
|
||||
ses_MAC, ses_IP, ses_EventTypeConnection, ses_DateTimeConnection,
|
||||
ses_EventTypeDisconnection, ses_DateTimeDisconnection,
|
||||
ses_StillConnected, ses_AdditionalInfo
|
||||
sesMac, sesIp, sesEventTypeConnection, sesDateTimeConnection,
|
||||
sesEventTypeDisconnection, sesDateTimeDisconnection,
|
||||
sesStillConnected, sesAdditionalInfo
|
||||
)
|
||||
SELECT scanMac, scanLastIP, 'Connected', '{startTime}', NULL, NULL, 1, scanVendor
|
||||
FROM CurrentScan
|
||||
@@ -642,7 +642,7 @@ def create_new_devices(db):
|
||||
)
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM Sessions
|
||||
WHERE ses_MAC = scanMac AND ses_StillConnected = 1
|
||||
WHERE sesMac = scanMac AND sesStillConnected = 1
|
||||
)
|
||||
""")
|
||||
|
||||
|
||||
@@ -24,8 +24,8 @@ class NameResolver:
|
||||
|
||||
# Check by MAC
|
||||
sql.execute(f"""
|
||||
SELECT Watched_Value2 FROM Plugins_Objects
|
||||
WHERE Plugin = '{plugin}' AND Object_PrimaryID = '{pMAC}'
|
||||
SELECT watchedValue2 FROM Plugins_Objects
|
||||
WHERE plugin = '{plugin}' AND objectPrimaryId = '{pMAC}'
|
||||
""")
|
||||
result = sql.fetchall()
|
||||
# self.db.commitDB() # Issue #1251: Optimize name resolution lookup
|
||||
@@ -37,8 +37,8 @@ class NameResolver:
|
||||
if get_setting_value('NEWDEV_IP_MATCH_NAME'):
|
||||
|
||||
sql.execute(f"""
|
||||
SELECT Watched_Value2 FROM Plugins_Objects
|
||||
WHERE Plugin = '{plugin}' AND Object_SecondaryID = '{pIP}'
|
||||
SELECT watchedValue2 FROM Plugins_Objects
|
||||
WHERE plugin = '{plugin}' AND objectSecondaryId = '{pIP}'
|
||||
""")
|
||||
result = sql.fetchall()
|
||||
# self.db.commitDB() # Issue #1251: Optimize name resolution lookup
|
||||
|
||||
@@ -120,27 +120,27 @@ def pair_sessions_events(db):
|
||||
|
||||
mylog("debug", "[Pair Session] - 1 Connections / New Devices")
|
||||
sql.execute("""UPDATE Events
|
||||
SET eve_PairEventRowid =
|
||||
SET evePairEventRowid =
|
||||
(SELECT ROWID
|
||||
FROM Events AS EVE2
|
||||
WHERE EVE2.eve_EventType IN ('New Device', 'Connected', 'Down Reconnected',
|
||||
WHERE EVE2.eveEventType IN ('New Device', 'Connected', 'Down Reconnected',
|
||||
'Device Down', 'Disconnected')
|
||||
AND EVE2.eve_MAC = Events.eve_MAC
|
||||
AND EVE2.eve_Datetime > Events.eve_DateTime
|
||||
ORDER BY EVE2.eve_DateTime ASC LIMIT 1)
|
||||
WHERE eve_EventType IN ('New Device', 'Connected', 'Down Reconnected')
|
||||
AND eve_PairEventRowid IS NULL
|
||||
AND EVE2.eveMac = Events.eveMac
|
||||
AND EVE2.eveDateTime > Events.eveDateTime
|
||||
ORDER BY EVE2.eveDateTime ASC LIMIT 1)
|
||||
WHERE eveEventType IN ('New Device', 'Connected', 'Down Reconnected')
|
||||
AND evePairEventRowid IS NULL
|
||||
""")
|
||||
|
||||
# Pair Disconnection / Device Down
|
||||
mylog("debug", "[Pair Session] - 2 Disconnections")
|
||||
sql.execute("""UPDATE Events
|
||||
SET eve_PairEventRowid =
|
||||
SET evePairEventRowid =
|
||||
(SELECT ROWID
|
||||
FROM Events AS EVE2
|
||||
WHERE EVE2.eve_PairEventRowid = Events.ROWID)
|
||||
WHERE eve_EventType IN ('Device Down', 'Disconnected')
|
||||
AND eve_PairEventRowid IS NULL
|
||||
WHERE EVE2.evePairEventRowid = Events.ROWID)
|
||||
WHERE eveEventType IN ('Device Down', 'Disconnected')
|
||||
AND evePairEventRowid IS NULL
|
||||
""")
|
||||
|
||||
mylog("debug", "[Pair Session] Pair session end")
|
||||
@@ -171,9 +171,9 @@ def insert_events(db):
|
||||
|
||||
# Check device down – non-sleeping devices (immediate on first absence)
|
||||
mylog("debug", "[Events] - 1a - Devices down (non-sleeping)")
|
||||
sql.execute(f"""INSERT OR IGNORE INTO Events (eve_MAC, eve_IP, eve_DateTime,
|
||||
eve_EventType, eve_AdditionalInfo,
|
||||
eve_PendingAlertEmail)
|
||||
sql.execute(f"""INSERT OR IGNORE INTO Events (eveMac, eveIp, eveDateTime,
|
||||
eveEventType, eveAdditionalInfo,
|
||||
evePendingAlertEmail)
|
||||
SELECT devMac, devLastIP, '{startTime}', 'Device Down', '', 1
|
||||
FROM DevicesView
|
||||
WHERE devAlertDown != 0
|
||||
@@ -185,9 +185,9 @@ def insert_events(db):
|
||||
|
||||
# Check device down – sleeping devices whose sleep window has expired
|
||||
mylog("debug", "[Events] - 1b - Devices down (sleep expired)")
|
||||
sql.execute(f"""INSERT OR IGNORE INTO Events (eve_MAC, eve_IP, eve_DateTime,
|
||||
eve_EventType, eve_AdditionalInfo,
|
||||
eve_PendingAlertEmail)
|
||||
sql.execute(f"""INSERT OR IGNORE INTO Events (eveMac, eveIp, eveDateTime,
|
||||
eveEventType, eveAdditionalInfo,
|
||||
evePendingAlertEmail)
|
||||
SELECT devMac, devLastIP, '{startTime}', 'Device Down', '', 1
|
||||
FROM DevicesView
|
||||
WHERE devAlertDown != 0
|
||||
@@ -197,33 +197,33 @@ def insert_events(db):
|
||||
AND NOT EXISTS (SELECT 1 FROM CurrentScan
|
||||
WHERE devMac = scanMac)
|
||||
AND NOT EXISTS (SELECT 1 FROM Events
|
||||
WHERE eve_MAC = devMac
|
||||
AND eve_EventType = 'Device Down'
|
||||
AND eve_DateTime >= devLastConnection
|
||||
WHERE eveMac = devMac
|
||||
AND eveEventType = 'Device Down'
|
||||
AND eveDateTime >= devLastConnection
|
||||
) """)
|
||||
|
||||
# Check new Connections or Down Reconnections
|
||||
mylog("debug", "[Events] - 2 - New Connections")
|
||||
sql.execute(f""" INSERT OR IGNORE INTO Events (eve_MAC, eve_IP, eve_DateTime,
|
||||
eve_EventType, eve_AdditionalInfo,
|
||||
eve_PendingAlertEmail)
|
||||
sql.execute(f""" INSERT OR IGNORE INTO Events (eveMac, eveIp, eveDateTime,
|
||||
eveEventType, eveAdditionalInfo,
|
||||
evePendingAlertEmail)
|
||||
SELECT DISTINCT c.scanMac, c.scanLastIP, '{startTime}',
|
||||
CASE
|
||||
WHEN last_event.eve_EventType = 'Device Down' and last_event.eve_PendingAlertEmail = 0 THEN 'Down Reconnected'
|
||||
WHEN last_event.eveEventType = 'Device Down' and last_event.evePendingAlertEmail = 0 THEN 'Down Reconnected'
|
||||
ELSE 'Connected'
|
||||
END,
|
||||
'',
|
||||
1
|
||||
FROM CurrentScan AS c
|
||||
LEFT JOIN LatestEventsPerMAC AS last_event ON c.scanMac = last_event.eve_MAC
|
||||
WHERE last_event.devPresentLastScan = 0 OR last_event.eve_MAC IS NULL
|
||||
LEFT JOIN LatestEventsPerMAC AS last_event ON c.scanMac = last_event.eveMac
|
||||
WHERE last_event.devPresentLastScan = 0 OR last_event.eveMac IS NULL
|
||||
""")
|
||||
|
||||
# Check disconnections
|
||||
mylog("debug", "[Events] - 3 - Disconnections")
|
||||
sql.execute(f"""INSERT OR IGNORE INTO Events (eve_MAC, eve_IP, eve_DateTime,
|
||||
eve_EventType, eve_AdditionalInfo,
|
||||
eve_PendingAlertEmail)
|
||||
sql.execute(f"""INSERT OR IGNORE INTO Events (eveMac, eveIp, eveDateTime,
|
||||
eveEventType, eveAdditionalInfo,
|
||||
evePendingAlertEmail)
|
||||
SELECT devMac, devLastIP, '{startTime}', 'Disconnected', '',
|
||||
devAlertEvents
|
||||
FROM Devices
|
||||
@@ -235,9 +235,9 @@ def insert_events(db):
|
||||
|
||||
# Check IP Changed
|
||||
mylog("debug", "[Events] - 4 - IP Changes")
|
||||
sql.execute(f"""INSERT OR IGNORE INTO Events (eve_MAC, eve_IP, eve_DateTime,
|
||||
eve_EventType, eve_AdditionalInfo,
|
||||
eve_PendingAlertEmail)
|
||||
sql.execute(f"""INSERT OR IGNORE INTO Events (eveMac, eveIp, eveDateTime,
|
||||
eveEventType, eveAdditionalInfo,
|
||||
evePendingAlertEmail)
|
||||
SELECT scanMac, scanLastIP, '{startTime}', 'IP Changed',
|
||||
'Previous IP: '|| devLastIP, devAlertEvents
|
||||
FROM Devices, CurrentScan
|
||||
@@ -279,7 +279,7 @@ def insertOnlineHistory(db):
|
||||
|
||||
# Prepare the insert query using parameterized inputs
|
||||
insert_query = """
|
||||
INSERT INTO Online_History (Scan_Date, Online_Devices, Down_Devices, All_Devices, Archived_Devices, Offline_Devices)
|
||||
INSERT INTO Online_History (scanDate, onlineDevices, downDevices, allDevices, archivedDevices, offlineDevices)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
"""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user