feat: instrument analysis
This commit is contained in:
@@ -1391,21 +1391,47 @@ Règles : coef intermédiaire ∈ [-5,5] ; coef output en pips, |val| ∈ [30,20
|
||||
def refresh_auto_analyses():
|
||||
"""
|
||||
Re-run _run_auto_analysis for all causal_event_analyses rows with empty actual_json.
|
||||
Fills in real price data so comprehension scores can be computed.
|
||||
Returns detailed per-event debug info.
|
||||
"""
|
||||
try:
|
||||
from services.database import get_conn
|
||||
conn = get_conn()
|
||||
rows = conn.execute("""
|
||||
|
||||
# Also return a DB state summary for all analyses (not just empty ones)
|
||||
all_rows = conn.execute("""
|
||||
SELECT a.id AS cea_id, a.market_event_id, a.template_id,
|
||||
a.actual_json
|
||||
a.actual_json, a.prediction_json,
|
||||
e.name AS event_name, e.start_date
|
||||
FROM causal_event_analyses a
|
||||
WHERE a.actual_json IS NULL OR a.actual_json = '{}' OR a.actual_json = 'null'
|
||||
LEFT JOIN market_events e ON e.id = a.market_event_id
|
||||
ORDER BY a.id DESC
|
||||
""").fetchall()
|
||||
|
||||
db_state = []
|
||||
for r in all_rows:
|
||||
actual = r["actual_json"] or ""
|
||||
pred = r["prediction_json"] or ""
|
||||
db_state.append({
|
||||
"cea_id": r["cea_id"],
|
||||
"event_id": r["market_event_id"],
|
||||
"event_name": r["event_name"],
|
||||
"start_date": r["start_date"],
|
||||
"template_id": r["template_id"],
|
||||
"actual_empty": actual in (None, "", "{}", "null"),
|
||||
"pred_empty": pred in (None, "", "{}", "null"),
|
||||
"actual_keys": list(json.loads(actual).keys()) if actual not in (None, "", "{}", "null") else [],
|
||||
"pred_keys": list(json.loads(pred).keys()) if pred not in (None, "", "{}", "null") else [],
|
||||
})
|
||||
|
||||
rows = [r for r in all_rows if (r["actual_json"] or "") in (None, "", "{}", "null")]
|
||||
print(f"[refresh] {len(rows)} analyses with empty actual_json out of {len(all_rows)} total", flush=True)
|
||||
conn.close()
|
||||
|
||||
details = []
|
||||
refreshed, failed = 0, 0
|
||||
for row in rows:
|
||||
detail: dict = {"cea_id": row["cea_id"], "event_id": row["market_event_id"],
|
||||
"event_name": row["event_name"], "template_id": row["template_id"]}
|
||||
try:
|
||||
conn2 = get_conn()
|
||||
ev_row = conn2.execute(
|
||||
@@ -1413,18 +1439,33 @@ def refresh_auto_analyses():
|
||||
).fetchone()
|
||||
conn2.close()
|
||||
if not ev_row:
|
||||
detail["result"] = "event_not_found"
|
||||
details.append(detail)
|
||||
continue
|
||||
ok = _run_auto_analysis(dict(ev_row), row["template_id"])
|
||||
ev = dict(ev_row)
|
||||
detail["surprise_pct"] = ev.get("surprise_pct")
|
||||
detail["start_date"] = ev.get("start_date")
|
||||
ok = _run_auto_analysis(ev, row["template_id"])
|
||||
detail["result"] = "ok" if ok else "failed"
|
||||
if ok:
|
||||
refreshed += 1
|
||||
else:
|
||||
failed += 1
|
||||
detail["result"] = "run_returned_false"
|
||||
except Exception as _e:
|
||||
logger.warning(f"[refresh_auto_analyses] cea#{row['cea_id']}: {_e}")
|
||||
detail["result"] = f"exception: {_e}"
|
||||
failed += 1
|
||||
details.append(detail)
|
||||
|
||||
logger.info(f"[refresh_auto_analyses] done: {refreshed} refreshed, {failed} failed")
|
||||
return {"refreshed": refreshed, "failed": failed, "total": len(rows)}
|
||||
print(f"[refresh] done: {refreshed} refreshed, {failed} failed", flush=True)
|
||||
return {
|
||||
"refreshed": refreshed,
|
||||
"failed": failed,
|
||||
"total": len(rows),
|
||||
"db_state": db_state,
|
||||
"details": details,
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"[refresh_auto_analyses] {e}")
|
||||
raise HTTPException(500, str(e))
|
||||
|
||||
Reference in New Issue
Block a user