feat: add automatic event cleanup based on retention setting

Hourly background task deletes events older than configured
retention period (default 90 days). Reads event_retention_days
from settings table. Includes 2 tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-07 17:24:08 -06:00
parent 5908293671
commit 85ecbc22ac
2 changed files with 100 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
"""Automatic event cleanup based on retention settings."""
import asyncio
import logging
from database import get_connection
logger = logging.getLogger(__name__)
async def cleanup_old_events():
"""Delete events older than the configured retention period. Runs hourly."""
while True:
try:
conn = get_connection()
row = conn.execute(
"SELECT value FROM settings WHERE key='event_retention_days'"
).fetchone()
days = int(row["value"]) if row else 90
result = conn.execute(
"DELETE FROM events WHERE created_at < datetime('now', ? || ' days')",
(f"-{days}",),
)
conn.commit()
if result.rowcount > 0:
logger.info(
"Cleaned up %d events older than %d days",
result.rowcount,
days,
)
except Exception:
logger.exception("Cleanup failed")
await asyncio.sleep(3600)
+65
View File
@@ -0,0 +1,65 @@
"""Tests for event retention cleanup."""
import sqlite3
from database import init_db, set_db_path
def test_cleanup_deletes_old_events(db_path):
"""Old events beyond retention should be deleted."""
set_db_path(db_path)
init_db()
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
# Insert an old event (100 days ago)
conn.execute(
"""INSERT INTO events (frigate_event_id, camera, label, score, match_status, created_at)
VALUES ('old-1', 'cam', 'person', 0.9, 'unknown', datetime('now', '-100 days'))"""
)
# Insert a recent event
conn.execute(
"""INSERT INTO events (frigate_event_id, camera, label, score, match_status)
VALUES ('new-1', 'cam', 'person', 0.9, 'unknown')"""
)
conn.commit()
# Simulate cleanup with 90-day retention
result = conn.execute(
"DELETE FROM events WHERE created_at < datetime('now', '-90 days')"
)
conn.commit()
assert result.rowcount == 1
remaining = conn.execute("SELECT COUNT(*) as c FROM events").fetchone()["c"]
assert remaining == 1
conn.close()
def test_cleanup_preserves_recent_events(db_path):
"""Events within retention period should not be deleted."""
set_db_path(db_path)
init_db()
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
# Insert events from 30 and 60 days ago
conn.execute(
"""INSERT INTO events (frigate_event_id, camera, label, score, match_status, created_at)
VALUES ('mid-1', 'cam', 'person', 0.9, 'unknown', datetime('now', '-30 days'))"""
)
conn.execute(
"""INSERT INTO events (frigate_event_id, camera, label, score, match_status, created_at)
VALUES ('mid-2', 'cam', 'car', 0.8, 'known', datetime('now', '-60 days'))"""
)
conn.commit()
result = conn.execute(
"DELETE FROM events WHERE created_at < datetime('now', '-90 days')"
)
conn.commit()
assert result.rowcount == 0
remaining = conn.execute("SELECT COUNT(*) as c FROM events").fetchone()["c"]
assert remaining == 2
conn.close()