|
|
|
|
|
|
| import sqlite3
|
| import os
|
|
|
| def manage_cache_limit(db_path: str, limit: int):
|
| """
|
| Ensures the database stays at or below the 'limit' provided.
|
| Logic:
|
| 1. Check if current count > limit.
|
| 2. Apply Rule 1: Delete old (pre-today) NULL/Blank feedback.
|
| 3. Apply Rule 2: Delete old (pre-today) 'N' feedback.
|
| 4. Apply Rule 3: Delete old (pre-today) all records.
|
| 5. Fallback: Delete absolute oldest until count == limit.
|
| """
|
| if not os.path.exists(db_path):
|
| print(f"[Maintenance] DB Path not found: {db_path}")
|
| return
|
|
|
| conn = sqlite3.connect(db_path)
|
| cur = conn.cursor()
|
|
|
| try:
|
|
|
| cur.execute("SELECT COUNT(*) FROM qa_cache")
|
| count_before = cur.fetchone()[0]
|
|
|
| if count_before <= limit:
|
|
|
| return
|
|
|
| print(f"[Maintenance] Count ({count_before}) exceeds limit ({limit}). Starting pruning logic...")
|
|
|
|
|
| cur.execute("""
|
| DELETE FROM qa_cache
|
| WHERE (feedback_status IS NULL OR feedback_status = '')
|
| AND date(created_at) < date('now')
|
| """)
|
| conn.commit()
|
|
|
|
|
| cur.execute("SELECT COUNT(*) FROM qa_cache")
|
| if cur.fetchone()[0] <= limit:
|
| print(f"[Maintenance] Rule 1 cleared enough records.")
|
| return
|
|
|
|
|
| cur.execute("""
|
| DELETE FROM qa_cache
|
| WHERE feedback_status = 'N'
|
| AND date(created_at) < date('now')
|
| """)
|
| conn.commit()
|
|
|
|
|
| cur.execute("SELECT COUNT(*) FROM qa_cache")
|
| if cur.fetchone()[0] <= limit:
|
| print(f"[Maintenance] Rule 2 cleared enough records.")
|
| return
|
|
|
|
|
| cur.execute("""
|
| DELETE FROM qa_cache
|
| WHERE date(created_at) < date('now')
|
| """)
|
| conn.commit()
|
|
|
|
|
|
|
|
|
| cur.execute("SELECT COUNT(*) FROM qa_cache")
|
| count_now = cur.fetchone()[0]
|
|
|
| if count_now > limit:
|
| to_delete = count_now - limit
|
| print(f"[Maintenance] Rules 1-3 (date-based) were not enough. Forcing deletion of {to_delete} oldest records.")
|
|
|
|
|
| cur.execute(f"""
|
| DELETE FROM qa_cache
|
| WHERE id IN (
|
| SELECT id FROM qa_cache
|
| ORDER BY created_at ASC
|
| LIMIT {to_delete}
|
| )
|
| """)
|
| conn.commit()
|
|
|
| cur.execute("SELECT COUNT(*) FROM qa_cache")
|
| print(f"[Maintenance] Cleanup finished. Final count: {cur.fetchone()[0]}")
|
|
|
| except Exception as e:
|
| print(f"[Maintenance] Error: {e}")
|
| finally:
|
| conn.close() |