mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2026-04-02 16:22:20 -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:
@@ -142,17 +142,17 @@ class DeviceInstance:
|
||||
|
||||
objs = PluginObjectInstance().getByField(
|
||||
plugPrefix='NMAP',
|
||||
matchedColumn='Object_PrimaryID',
|
||||
matchedColumn='objectPrimaryId',
|
||||
matchedKey=primary,
|
||||
returnFields=['Object_SecondaryID', 'Watched_Value2']
|
||||
returnFields=['objectSecondaryId', 'watchedValue2']
|
||||
)
|
||||
|
||||
ports = []
|
||||
for o in objs:
|
||||
|
||||
port = int(o.get('Object_SecondaryID') or 0)
|
||||
port = int(o.get('objectSecondaryId') or 0)
|
||||
|
||||
ports.append({"port": port, "service": o.get('Watched_Value2', '')})
|
||||
ports.append({"port": port, "service": o.get('watchedValue2', '')})
|
||||
|
||||
return ports
|
||||
|
||||
@@ -471,31 +471,31 @@ class DeviceInstance:
|
||||
LOWER(d.devParentMAC) AS devParentMAC,
|
||||
|
||||
(SELECT COUNT(*) FROM Sessions
|
||||
WHERE LOWER(ses_MAC) = LOWER(d.devMac) AND (
|
||||
ses_DateTimeConnection >= {period_date_sql} OR
|
||||
ses_DateTimeDisconnection >= {period_date_sql} OR
|
||||
ses_StillConnected = 1
|
||||
WHERE LOWER(sesMac) = LOWER(d.devMac) AND (
|
||||
sesDateTimeConnection >= {period_date_sql} OR
|
||||
sesDateTimeDisconnection >= {period_date_sql} OR
|
||||
sesStillConnected = 1
|
||||
)) AS devSessions,
|
||||
|
||||
(SELECT COUNT(*) FROM Events
|
||||
WHERE LOWER(eve_MAC) = LOWER(d.devMac) AND eve_DateTime >= {period_date_sql}
|
||||
AND eve_EventType NOT IN ('Connected','Disconnected')) AS devEvents,
|
||||
WHERE LOWER(eveMac) = LOWER(d.devMac) AND eveDateTime >= {period_date_sql}
|
||||
AND eveEventType NOT IN ('Connected','Disconnected')) AS devEvents,
|
||||
|
||||
(SELECT COUNT(*) FROM Events
|
||||
WHERE LOWER(eve_MAC) = LOWER(d.devMac) AND eve_DateTime >= {period_date_sql}
|
||||
AND eve_EventType = 'Device Down') AS devDownAlerts,
|
||||
WHERE LOWER(eveMac) = LOWER(d.devMac) AND eveDateTime >= {period_date_sql}
|
||||
AND eveEventType = 'Device Down') AS devDownAlerts,
|
||||
|
||||
(SELECT CAST(MAX(0, SUM(
|
||||
julianday(IFNULL(ses_DateTimeDisconnection,'{now}')) -
|
||||
julianday(CASE WHEN ses_DateTimeConnection < {period_date_sql}
|
||||
THEN {period_date_sql} ELSE ses_DateTimeConnection END)
|
||||
julianday(IFNULL(sesDateTimeDisconnection,'{now}')) -
|
||||
julianday(CASE WHEN sesDateTimeConnection < {period_date_sql}
|
||||
THEN {period_date_sql} ELSE sesDateTimeConnection END)
|
||||
) * 24) AS INT)
|
||||
FROM Sessions
|
||||
WHERE LOWER(ses_MAC) = LOWER(d.devMac)
|
||||
AND ses_DateTimeConnection IS NOT NULL
|
||||
AND (ses_DateTimeDisconnection IS NOT NULL OR ses_StillConnected = 1)
|
||||
AND (ses_DateTimeConnection >= {period_date_sql}
|
||||
OR ses_DateTimeDisconnection >= {period_date_sql} OR ses_StillConnected = 1)
|
||||
WHERE LOWER(sesMac) = LOWER(d.devMac)
|
||||
AND sesDateTimeConnection IS NOT NULL
|
||||
AND (sesDateTimeDisconnection IS NOT NULL OR sesStillConnected = 1)
|
||||
AND (sesDateTimeConnection >= {period_date_sql}
|
||||
OR sesDateTimeDisconnection >= {period_date_sql} OR sesStillConnected = 1)
|
||||
) AS devPresenceHours
|
||||
|
||||
FROM DevicesView d
|
||||
@@ -797,7 +797,7 @@ class DeviceInstance:
|
||||
"""Delete all events for a device."""
|
||||
conn = get_temp_db_connection()
|
||||
cur = conn.cursor()
|
||||
cur.execute("DELETE FROM Events WHERE eve_MAC=?", (mac,))
|
||||
cur.execute("DELETE FROM Events WHERE eveMac=?", (mac,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return {"success": True}
|
||||
|
||||
@@ -21,7 +21,7 @@ class EventInstance:
|
||||
def get_all(self):
|
||||
conn = self._conn()
|
||||
rows = conn.execute(
|
||||
"SELECT * FROM Events ORDER BY eve_DateTime DESC"
|
||||
"SELECT * FROM Events ORDER BY eveDateTime DESC"
|
||||
).fetchall()
|
||||
conn.close()
|
||||
return self._rows_to_list(rows)
|
||||
@@ -31,7 +31,7 @@ class EventInstance:
|
||||
conn = self._conn()
|
||||
rows = conn.execute("""
|
||||
SELECT * FROM Events
|
||||
ORDER BY eve_DateTime DESC
|
||||
ORDER BY eveDateTime DESC
|
||||
LIMIT ?
|
||||
""", (n,)).fetchall()
|
||||
conn.close()
|
||||
@@ -47,8 +47,8 @@ class EventInstance:
|
||||
conn = self._conn()
|
||||
rows = conn.execute("""
|
||||
SELECT * FROM Events
|
||||
WHERE eve_DateTime >= ?
|
||||
ORDER BY eve_DateTime DESC
|
||||
WHERE eveDateTime >= ?
|
||||
ORDER BY eveDateTime DESC
|
||||
""", (since,)).fetchall()
|
||||
conn.close()
|
||||
return self._rows_to_list(rows)
|
||||
@@ -63,8 +63,8 @@ class EventInstance:
|
||||
conn = self._conn()
|
||||
rows = conn.execute("""
|
||||
SELECT * FROM Events
|
||||
WHERE eve_DateTime >= ?
|
||||
ORDER BY eve_DateTime DESC
|
||||
WHERE eveDateTime >= ?
|
||||
ORDER BY eveDateTime DESC
|
||||
""", (since,)).fetchall()
|
||||
conn.close()
|
||||
return self._rows_to_list(rows)
|
||||
@@ -78,8 +78,8 @@ class EventInstance:
|
||||
conn = self._conn()
|
||||
rows = conn.execute("""
|
||||
SELECT * FROM Events
|
||||
WHERE eve_DateTime BETWEEN ? AND ?
|
||||
ORDER BY eve_DateTime DESC
|
||||
WHERE eveDateTime BETWEEN ? AND ?
|
||||
ORDER BY eveDateTime DESC
|
||||
""", (start, end)).fetchall()
|
||||
conn.close()
|
||||
return self._rows_to_list(rows)
|
||||
@@ -89,9 +89,9 @@ class EventInstance:
|
||||
conn = self._conn()
|
||||
conn.execute("""
|
||||
INSERT OR IGNORE INTO Events (
|
||||
eve_MAC, eve_IP, eve_DateTime,
|
||||
eve_EventType, eve_AdditionalInfo,
|
||||
eve_PendingAlertEmail, eve_PairEventRowid
|
||||
eveMac, eveIp, eveDateTime,
|
||||
eveEventType, eveAdditionalInfo,
|
||||
evePendingAlertEmail, evePairEventRowid
|
||||
) VALUES (?,?,?,?,?,?,?)
|
||||
""", (mac, ip, timeNowUTC(), eventType, info,
|
||||
1 if pendingAlert else 0, pairRow))
|
||||
@@ -102,7 +102,7 @@ class EventInstance:
|
||||
def delete_older_than(self, days: int):
|
||||
cutoff = timeNowUTC(as_string=False) - timedelta(days=days)
|
||||
conn = self._conn()
|
||||
result = conn.execute("DELETE FROM Events WHERE eve_DateTime < ?", (cutoff,))
|
||||
result = conn.execute("DELETE FROM Events WHERE eveDateTime < ?", (cutoff,))
|
||||
conn.commit()
|
||||
deleted_count = result.rowcount
|
||||
conn.close()
|
||||
@@ -124,7 +124,7 @@ class EventInstance:
|
||||
cur = conn.cursor()
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT OR IGNORE INTO Events (eve_MAC, eve_IP, eve_DateTime, eve_EventType, eve_AdditionalInfo, eve_PendingAlertEmail)
|
||||
INSERT OR IGNORE INTO Events (eveMac, eveIp, eveDateTime, eveEventType, eveAdditionalInfo, evePendingAlertEmail)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(mac, ip, start_time, event_type, additional_info, pending_alert),
|
||||
@@ -145,10 +145,10 @@ class EventInstance:
|
||||
cur = conn.cursor()
|
||||
|
||||
if mac:
|
||||
sql = "SELECT * FROM Events WHERE eve_MAC=? ORDER BY eve_DateTime DESC"
|
||||
sql = "SELECT * FROM Events WHERE eveMac=? ORDER BY eveDateTime DESC"
|
||||
cur.execute(sql, (mac,))
|
||||
else:
|
||||
sql = "SELECT * FROM Events ORDER BY eve_DateTime DESC"
|
||||
sql = "SELECT * FROM Events ORDER BY eveDateTime DESC"
|
||||
cur.execute(sql)
|
||||
|
||||
rows = cur.fetchall()
|
||||
@@ -163,7 +163,7 @@ class EventInstance:
|
||||
cur = conn.cursor()
|
||||
|
||||
# Use a parameterized query with sqlite date function
|
||||
sql = "DELETE FROM Events WHERE eve_DateTime <= date('now', ?)"
|
||||
sql = "DELETE FROM Events WHERE eveDateTime <= date('now', ?)"
|
||||
cur.execute(sql, [f"-{days} days"])
|
||||
|
||||
conn.commit()
|
||||
@@ -197,19 +197,19 @@ class EventInstance:
|
||||
|
||||
sql = f"""
|
||||
SELECT
|
||||
(SELECT COUNT(*) FROM Events WHERE eve_DateTime >= {period_date_sql}) AS all_events,
|
||||
(SELECT COUNT(*) FROM Events WHERE eveDateTime >= {period_date_sql}) AS all_events,
|
||||
(SELECT COUNT(*) FROM Sessions WHERE
|
||||
ses_DateTimeConnection >= {period_date_sql}
|
||||
OR ses_DateTimeDisconnection >= {period_date_sql}
|
||||
OR ses_StillConnected = 1
|
||||
sesDateTimeConnection >= {period_date_sql}
|
||||
OR sesDateTimeDisconnection >= {period_date_sql}
|
||||
OR sesStillConnected = 1
|
||||
) AS sessions,
|
||||
(SELECT COUNT(*) FROM Sessions WHERE
|
||||
(ses_DateTimeConnection IS NULL AND ses_DateTimeDisconnection >= {period_date_sql})
|
||||
OR (ses_DateTimeDisconnection IS NULL AND ses_StillConnected = 0 AND ses_DateTimeConnection >= {period_date_sql})
|
||||
(sesDateTimeConnection IS NULL AND sesDateTimeDisconnection >= {period_date_sql})
|
||||
OR (sesDateTimeDisconnection IS NULL AND sesStillConnected = 0 AND sesDateTimeConnection >= {period_date_sql})
|
||||
) AS missing,
|
||||
(SELECT COUNT(*) FROM Events WHERE eve_DateTime >= {period_date_sql} AND eve_EventType LIKE 'VOIDED%') AS voided,
|
||||
(SELECT COUNT(*) FROM Events WHERE eve_DateTime >= {period_date_sql} AND eve_EventType LIKE 'New Device') AS new,
|
||||
(SELECT COUNT(*) FROM Events WHERE eve_DateTime >= {period_date_sql} AND eve_EventType LIKE 'Device Down') AS down
|
||||
(SELECT COUNT(*) FROM Events WHERE eveDateTime >= {period_date_sql} AND eveEventType LIKE 'VOIDED%') AS voided,
|
||||
(SELECT COUNT(*) FROM Events WHERE eveDateTime >= {period_date_sql} AND eveEventType LIKE 'New Device') AS new,
|
||||
(SELECT COUNT(*) FROM Events WHERE eveDateTime >= {period_date_sql} AND eveEventType LIKE 'Device Down') AS down
|
||||
"""
|
||||
|
||||
cur.execute(sql)
|
||||
@@ -247,11 +247,11 @@ class EventInstance:
|
||||
conn = self._conn()
|
||||
|
||||
sql = """
|
||||
SELECT eve_MAC, COUNT(*) as event_count
|
||||
SELECT eveMac, COUNT(*) as event_count
|
||||
FROM Events
|
||||
WHERE eve_EventType IN ('Connected','Disconnected','Device Down','Down Reconnected')
|
||||
AND eve_DateTime >= datetime('now', ?)
|
||||
GROUP BY eve_MAC
|
||||
WHERE eveEventType IN ('Connected','Disconnected','Device Down','Down Reconnected')
|
||||
AND eveDateTime >= datetime('now', ?)
|
||||
GROUP BY eveMac
|
||||
HAVING COUNT(*) >= ?
|
||||
"""
|
||||
|
||||
@@ -262,6 +262,6 @@ class EventInstance:
|
||||
conn.close()
|
||||
|
||||
if macs_only:
|
||||
return {row["eve_MAC"] for row in rows}
|
||||
return {row["eveMac"] for row in rows}
|
||||
|
||||
return [dict(row) for row in rows]
|
||||
|
||||
@@ -31,17 +31,17 @@ class NotificationInstance:
|
||||
|
||||
# Create Notifications table if missing
|
||||
self.db.sql.execute("""CREATE TABLE IF NOT EXISTS "Notifications" (
|
||||
"Index" INTEGER,
|
||||
"GUID" TEXT UNIQUE,
|
||||
"DateTimeCreated" TEXT,
|
||||
"DateTimePushed" TEXT,
|
||||
"Status" TEXT,
|
||||
"JSON" TEXT,
|
||||
"Text" TEXT,
|
||||
"HTML" TEXT,
|
||||
"PublishedVia" TEXT,
|
||||
"Extra" TEXT,
|
||||
PRIMARY KEY("Index" AUTOINCREMENT)
|
||||
"index" INTEGER,
|
||||
"guid" TEXT UNIQUE,
|
||||
"dateTimeCreated" TEXT,
|
||||
"dateTimePushed" TEXT,
|
||||
"status" TEXT,
|
||||
"json" TEXT,
|
||||
"text" TEXT,
|
||||
"html" TEXT,
|
||||
"publishedVia" TEXT,
|
||||
"extra" TEXT,
|
||||
PRIMARY KEY("index" AUTOINCREMENT)
|
||||
);
|
||||
""")
|
||||
|
||||
@@ -178,7 +178,7 @@ class NotificationInstance:
|
||||
def upsert(self):
|
||||
self.db.sql.execute(
|
||||
"""
|
||||
INSERT OR REPLACE INTO Notifications (GUID, DateTimeCreated, DateTimePushed, Status, JSON, Text, HTML, PublishedVia, Extra)
|
||||
INSERT OR REPLACE INTO Notifications (guid, dateTimeCreated, dateTimePushed, "status", "json", "text", html, publishedVia, extra)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
@@ -202,7 +202,7 @@ class NotificationInstance:
|
||||
self.db.sql.execute(
|
||||
"""
|
||||
DELETE FROM Notifications
|
||||
WHERE GUID = ?
|
||||
WHERE guid = ?
|
||||
""",
|
||||
(GUID,),
|
||||
)
|
||||
@@ -212,7 +212,7 @@ class NotificationInstance:
|
||||
def getNew(self):
|
||||
self.db.sql.execute("""
|
||||
SELECT * FROM Notifications
|
||||
WHERE Status = "new"
|
||||
WHERE "status" = 'new'
|
||||
""")
|
||||
return self.db.sql.fetchall()
|
||||
|
||||
@@ -221,8 +221,8 @@ class NotificationInstance:
|
||||
# Execute an SQL query to update the status of all notifications
|
||||
self.db.sql.execute("""
|
||||
UPDATE Notifications
|
||||
SET Status = "processed"
|
||||
WHERE Status = "new"
|
||||
SET "status" = 'processed'
|
||||
WHERE "status" = 'new'
|
||||
""")
|
||||
|
||||
self.save()
|
||||
@@ -234,15 +234,15 @@ class NotificationInstance:
|
||||
self.db.sql.execute("""
|
||||
UPDATE Devices SET devLastNotification = ?
|
||||
WHERE devMac IN (
|
||||
SELECT eve_MAC FROM Events
|
||||
WHERE eve_PendingAlertEmail = 1
|
||||
SELECT eveMac FROM Events
|
||||
WHERE evePendingAlertEmail = 1
|
||||
)
|
||||
""", (timeNowUTC(),))
|
||||
|
||||
self.db.sql.execute("""
|
||||
UPDATE Events SET eve_PendingAlertEmail = 0
|
||||
WHERE eve_PendingAlertEmail = 1
|
||||
AND eve_EventType !='Device Down' """)
|
||||
UPDATE Events SET evePendingAlertEmail = 0
|
||||
WHERE evePendingAlertEmail = 1
|
||||
AND eveEventType !='Device Down' """)
|
||||
|
||||
# Clear down events flag after the reporting window passed
|
||||
minutes = int(get_setting_value("NTFPRCS_alert_down_time") or 0)
|
||||
@@ -250,10 +250,10 @@ class NotificationInstance:
|
||||
self.db.sql.execute(
|
||||
"""
|
||||
UPDATE Events
|
||||
SET eve_PendingAlertEmail = 0
|
||||
WHERE eve_PendingAlertEmail = 1
|
||||
AND eve_EventType = 'Device Down'
|
||||
AND eve_DateTime < datetime('now', ?, ?)
|
||||
SET evePendingAlertEmail = 0
|
||||
WHERE evePendingAlertEmail = 1
|
||||
AND eveEventType = 'Device Down'
|
||||
AND eveDateTime < datetime('now', ?, ?)
|
||||
""",
|
||||
(f"-{minutes} minutes", tz_offset),
|
||||
)
|
||||
|
||||
@@ -35,18 +35,18 @@ class PluginObjectInstance:
|
||||
|
||||
def getByGUID(self, ObjectGUID):
|
||||
return self._fetchone(
|
||||
"SELECT * FROM Plugins_Objects WHERE ObjectGUID = ?", (ObjectGUID,)
|
||||
"SELECT * FROM Plugins_Objects WHERE objectGuid = ?", (ObjectGUID,)
|
||||
)
|
||||
|
||||
def exists(self, ObjectGUID):
|
||||
row = self._fetchone("""
|
||||
SELECT COUNT(*) AS count FROM Plugins_Objects WHERE ObjectGUID = ?
|
||||
SELECT COUNT(*) AS count FROM Plugins_Objects WHERE objectGuid = ?
|
||||
""", (ObjectGUID,))
|
||||
return row["count"] > 0 if row else False
|
||||
|
||||
def getByPlugin(self, plugin):
|
||||
return self._fetchall(
|
||||
"SELECT * FROM Plugins_Objects WHERE Plugin = ?", (plugin,)
|
||||
"SELECT * FROM Plugins_Objects WHERE plugin = ?", (plugin,)
|
||||
)
|
||||
|
||||
def getLastNCreatedPerPlugin(self, plugin, entries=1):
|
||||
@@ -54,8 +54,8 @@ class PluginObjectInstance:
|
||||
"""
|
||||
SELECT *
|
||||
FROM Plugins_Objects
|
||||
WHERE Plugin = ?
|
||||
ORDER BY DateTimeCreated DESC
|
||||
WHERE plugin = ?
|
||||
ORDER BY dateTimeCreated DESC
|
||||
LIMIT ?
|
||||
""",
|
||||
(plugin, entries),
|
||||
@@ -63,7 +63,7 @@ class PluginObjectInstance:
|
||||
|
||||
def getByField(self, plugPrefix, matchedColumn, matchedKey, returnFields=None):
|
||||
rows = self._fetchall(
|
||||
f"SELECT * FROM Plugins_Objects WHERE Plugin = ? AND {matchedColumn} = ?",
|
||||
f"SELECT * FROM Plugins_Objects WHERE plugin = ? AND {matchedColumn} = ?",
|
||||
(plugPrefix, matchedKey.lower())
|
||||
)
|
||||
|
||||
@@ -75,12 +75,12 @@ class PluginObjectInstance:
|
||||
def getByPrimary(self, plugin, primary_id):
|
||||
return self._fetchall("""
|
||||
SELECT * FROM Plugins_Objects
|
||||
WHERE Plugin = ? AND Object_PrimaryID = ?
|
||||
WHERE plugin = ? AND objectPrimaryId = ?
|
||||
""", (plugin, primary_id))
|
||||
|
||||
def getByStatus(self, status):
|
||||
return self._fetchall("""
|
||||
SELECT * FROM Plugins_Objects WHERE Status = ?
|
||||
SELECT * FROM Plugins_Objects WHERE "status" = ?
|
||||
""", (status,))
|
||||
|
||||
def updateField(self, ObjectGUID, field, value):
|
||||
@@ -90,7 +90,7 @@ class PluginObjectInstance:
|
||||
raise ValueError(msg)
|
||||
|
||||
self._execute(
|
||||
f"UPDATE Plugins_Objects SET {field}=? WHERE ObjectGUID=?",
|
||||
f"UPDATE Plugins_Objects SET {field}=? WHERE objectGuid=?",
|
||||
(value, ObjectGUID)
|
||||
)
|
||||
|
||||
@@ -100,4 +100,4 @@ class PluginObjectInstance:
|
||||
mylog("none", msg)
|
||||
raise ValueError(msg)
|
||||
|
||||
self._execute("DELETE FROM Plugins_Objects WHERE ObjectGUID=?", (ObjectGUID,))
|
||||
self._execute("DELETE FROM Plugins_Objects WHERE objectGuid=?", (ObjectGUID,))
|
||||
|
||||
Reference in New Issue
Block a user