Add database cleanup for Sessions and optimize queries

- Implemented deletion of Sessions older than DAYS_TO_KEEP_EVENTS.
- Added index for Plugins_History to improve query performance.
- Introduced unit tests for Sessions trimming and database analysis.
This commit is contained in:
Jokob @NetAlertX
2026-03-01 06:07:57 +00:00
parent c1adfd35f3
commit ea5585a8ef
3 changed files with 284 additions and 0 deletions

View File

@@ -96,6 +96,15 @@ def cleanup_database(
cursor.execute(sql)
mylog("verbose", [f"[{pluginName}] Events deleted rows: {cursor.rowcount}"])
# -----------------------------------------------------
# Sessions (derived snapshot — trimmed to the same window as Events so the
# two tables stay in sync without introducing a separate setting)
mylog("verbose", f"[{pluginName}] Sessions: Delete all older than {str(DAYS_TO_KEEP_EVENTS)} days (reuses DAYS_TO_KEEP_EVENTS)")
sql = f"""DELETE FROM Sessions WHERE ses_DateTimeConnection <= date('now', '-{str(DAYS_TO_KEEP_EVENTS)} day')"""
mylog("verbose", [f"[{pluginName}] SQL : {sql}"])
cursor.execute(sql)
mylog("verbose", [f"[{pluginName}] Sessions deleted rows: {cursor.rowcount}"])
# -----------------------------------------------------
# Plugins_History
mylog("verbose", f"[{pluginName}] Plugins_History: Trim to {str(PLUGINS_KEEP_HIST)} per Plugin")
@@ -199,9 +208,19 @@ def cleanup_database(
cursor.execute("PRAGMA wal_checkpoint(FULL);")
mylog("verbose", [f"[{pluginName}] WAL checkpoint executed to truncate file."])
# Refresh query-planner statistics after bulk deletes so SQLite chooses
# the right indexes on the next scan cycle (fixes CPU scaling with DB size)
cursor.execute("ANALYZE;")
mylog("verbose", [f"[{pluginName}] ANALYZE completed"])
mylog("verbose", [f"[{pluginName}] Shrink Database"])
cursor.execute("VACUUM;")
# Lightweight incremental ANALYZE at connection close — near-zero cost,
# only re-analyzes tables whose statistics have drifted >25%
cursor.execute("PRAGMA optimize;")
mylog("verbose", [f"[{pluginName}] PRAGMA optimize completed"])
conn.close()