Spaces:
Running on Zero
Running on Zero
Upload community_contributions/chrys/db.py with huggingface_hub
Browse files
community_contributions/chrys/db.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""SQLite audit log for ARIA runs (90-day retention)."""
|
| 2 |
+
import os
|
| 3 |
+
import sqlite3
|
| 4 |
+
from datetime import datetime, timezone
|
| 5 |
+
from typing import List
|
| 6 |
+
|
| 7 |
+
from models import DecisionRecord
|
| 8 |
+
|
| 9 |
+
DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
|
| 10 |
+
DB_PATH = os.path.join(DATA_DIR, "aria_audit.db")
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def _init_db():
|
| 14 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
| 15 |
+
conn = sqlite3.connect(DB_PATH)
|
| 16 |
+
conn.execute("""
|
| 17 |
+
CREATE TABLE IF NOT EXISTS run_log (
|
| 18 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 19 |
+
ts_utc TEXT NOT NULL,
|
| 20 |
+
asset TEXT NOT NULL,
|
| 21 |
+
tech_score INTEGER,
|
| 22 |
+
sentiment TEXT,
|
| 23 |
+
final_score REAL,
|
| 24 |
+
decision TEXT NOT NULL,
|
| 25 |
+
skip_reason TEXT,
|
| 26 |
+
created_at TEXT NOT NULL
|
| 27 |
+
)
|
| 28 |
+
""")
|
| 29 |
+
conn.commit()
|
| 30 |
+
conn.close()
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def log_decisions(records: List[DecisionRecord]) -> None:
|
| 34 |
+
_init_db()
|
| 35 |
+
now = datetime.now(timezone.utc).isoformat()
|
| 36 |
+
conn = sqlite3.connect(DB_PATH)
|
| 37 |
+
for r in records:
|
| 38 |
+
conn.execute(
|
| 39 |
+
"""INSERT INTO run_log (ts_utc, asset, tech_score, sentiment, final_score, decision, skip_reason, created_at)
|
| 40 |
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
|
| 41 |
+
(now, r.asset, r.tech_score, r.sentiment, r.final_score, r.decision, r.skip_reason or "", now),
|
| 42 |
+
)
|
| 43 |
+
conn.commit()
|
| 44 |
+
conn.close()
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def purge_older_than_days(days: int = 90) -> int:
|
| 48 |
+
_init_db()
|
| 49 |
+
conn = sqlite3.connect(DB_PATH)
|
| 50 |
+
cutoff = datetime.now(timezone.utc).replace(tzinfo=None).timestamp() - (days * 86400)
|
| 51 |
+
cutoff_str = datetime.utcfromtimestamp(cutoff).isoformat() + "Z"
|
| 52 |
+
cur = conn.execute("DELETE FROM run_log WHERE created_at < ?", (cutoff_str,))
|
| 53 |
+
deleted = cur.rowcount
|
| 54 |
+
conn.commit()
|
| 55 |
+
conn.close()
|
| 56 |
+
return deleted
|