"""One-time backfill of daily_run_log from daemon stderr log history.""" import re import sqlite3 from collections import defaultdict LOG_FILE = "/Users/onerozbey/Library/Logs/borsa-trading-worker/worker_stderr.log" DB_PATH = "/Users/onerozbey/borsa_uygulamasi_runtime/huggingface-space/paper_trading/trading.db" # Parse "Cycle: OK", "Cycle: SKIPPED", etc. from log lines pattern = re.compile(r"^(\d{4}-\d{2}-\d{2}) [\d:,]+ .* Cycle: (\w+)") day_status = {} # date -> best status with open(LOG_FILE) as f: for line in f: m = pattern.match(line) if m: date, status = m.group(1), m.group(2) # Priority: OK > SKIPPED > NO_ELIGIBLE_STOCKS if status == "OK" or date not in day_status: day_status[date] = status print(f"Found {len(day_status)} unique dates with cycles") conn = sqlite3.connect(DB_PATH) cur = conn.cursor() # Ensure table exists cur.execute(""" CREATE TABLE IF NOT EXISTS daily_run_log ( id INTEGER PRIMARY KEY AUTOINCREMENT, date TEXT NOT NULL, market_id TEXT NOT NULL DEFAULT 'bist', status TEXT NOT NULL, trades_executed INTEGER DEFAULT 0, elapsed_sec REAL DEFAULT 0, equity REAL, cash REAL, model_safe INTEGER, cycle_detail TEXT, created_at TEXT DEFAULT (datetime('now')) ) """) # Check existing entries existing = set(r[0] for r in cur.execute("SELECT DISTINCT date FROM daily_run_log").fetchall()) print(f"Already have {len(existing)} dates in run_log") inserted = 0 for date in sorted(day_status): if date not in existing: status = day_status[date] cur.execute( "INSERT INTO daily_run_log (date, market_id, status) VALUES (?, 'bist', ?)", (date, status) ) inserted += 1 conn.commit() conn.close() print(f"Backfilled {inserted} run log entries") print(f"Total run dates: {len(day_status)}")