File size: 1,945 Bytes
96964a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""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)}")