adacam-odc: purge forwarded detections from SQLite after 7 days (DETECTION_KEEP_DAYS)
This commit is contained in:
parent
9903667c71
commit
56a91b7459
1 changed files with 45 additions and 0 deletions
|
|
@ -118,6 +118,7 @@ REDIS_PORT = int(os.environ.get("REDIS_PORT", 6379))
|
|||
GLOBAL_LIMIT = 5000
|
||||
CLEANUP_INTERVAL_HOURS = 1
|
||||
FRAMEKM_MAX_AGE_DAYS = 10 # Delete framekm files older than 10 days
|
||||
DETECTION_KEEP_DAYS = 7 # Keep forwarded detections in SQLite for 7 days, then purge
|
||||
DISK_PRESSURE_PCT = 90 # Aggressive cleanup if disk usage exceeds this %
|
||||
DISK_PRESSURE_BATCH = 50 # Max files to delete per pressure run
|
||||
|
||||
|
|
@ -746,6 +747,46 @@ def fetch_pending_images_for_upload(
|
|||
# =============================================================================
|
||||
|
||||
|
||||
|
||||
def purge_forwarded_detections():
|
||||
"""
|
||||
Delete old forwarded detections from the Bee's SQLite DB.
|
||||
Only purges rows that:
|
||||
1. Have already been forwarded (id <= last_detection_id cursor)
|
||||
2. Are older than DETECTION_KEEP_DAYS
|
||||
Keeps recent data intact. Safe to call after each successful upload cycle.
|
||||
"""
|
||||
keep_ms = DETECTION_KEEP_DAYS * 86400 * 1000
|
||||
cutoff_ms = int(time.time() * 1000) - keep_ms
|
||||
|
||||
with forwarder_state_lock:
|
||||
cursor_id = forwarder_state.get("last_detection_id", 0)
|
||||
|
||||
if cursor_id == 0:
|
||||
return 0
|
||||
|
||||
try:
|
||||
db_path = ODC_DB_PATH
|
||||
if not db_path.exists():
|
||||
return 0
|
||||
conn = sqlite3.connect(str(db_path))
|
||||
conn.row_factory = sqlite3.Row
|
||||
cur = conn.cursor()
|
||||
cur.execute(
|
||||
"DELETE FROM detections WHERE id <= ? AND ts < ?",
|
||||
(cursor_id, cutoff_ms)
|
||||
)
|
||||
deleted = cur.rowcount
|
||||
conn.commit()
|
||||
conn.close()
|
||||
if deleted > 0:
|
||||
fwd_logger.info(f"[purge] Deleted {deleted} forwarded detections older than {DETECTION_KEEP_DAYS}d from SQLite")
|
||||
return deleted
|
||||
except Exception as e:
|
||||
fwd_logger.warning(f"[purge] Detection purge failed: {e}")
|
||||
return 0
|
||||
|
||||
|
||||
def forwarder_process_cycle() -> Tuple[int, int]:
|
||||
"""
|
||||
One processing cycle: forward new detections + upload images.
|
||||
|
|
@ -893,6 +934,10 @@ def forwarder_process_cycle() -> Tuple[int, int]:
|
|||
|
||||
save_forwarder_state()
|
||||
|
||||
# ── 3. Purge old forwarded detections ────────────────────────────────────
|
||||
if det_sent > 0 or img_uploaded > 0:
|
||||
purge_forwarded_detections()
|
||||
|
||||
return det_sent, img_uploaded
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue