feat: instrument model

This commit is contained in:
OpenSquared
2026-07-03 00:31:30 +02:00
parent a8ee808937
commit d9e9d30889
4 changed files with 591 additions and 48 deletions

View File

@@ -247,12 +247,21 @@ _SUGGEST_FN = {
# ── Public API ─────────────────────────────────────────────────────────────────
def get_latest_gauges(conn) -> tuple[dict, str]:
"""Retourne (gauges_dict, snapshot_date) depuis macro_regime_history."""
row = conn.execute(
"SELECT gauges_summary_json, timestamp FROM macro_regime_history "
"ORDER BY timestamp DESC LIMIT 1"
).fetchone()
def get_latest_gauges(conn, at_date: Optional[str] = None) -> tuple[dict, str]:
"""Retourne (gauges_dict, snapshot_date) depuis macro_regime_history.
Si at_date fourni, retourne le snapshot le plus proche <= at_date."""
if at_date:
row = conn.execute(
"SELECT gauges_summary_json, timestamp FROM macro_regime_history "
"WHERE timestamp <= ? ORDER BY timestamp DESC LIMIT 1",
(at_date,)
).fetchone()
else:
row = conn.execute(
"SELECT gauges_summary_json, timestamp FROM macro_regime_history "
"ORDER BY timestamp DESC LIMIT 1"
).fetchone()
if row:
r = dict(row)
gauges = json.loads(r.get("gauges_summary_json") or "{}")
@@ -261,10 +270,17 @@ def get_latest_gauges(conn) -> tuple[dict, str]:
return gauges, date
# Fallback : macro_gauge_snapshots
row2 = conn.execute(
"SELECT gauges_json, snapshot_date FROM macro_gauge_snapshots "
"ORDER BY snapshot_date DESC LIMIT 1"
).fetchone()
if at_date:
row2 = conn.execute(
"SELECT gauges_json, snapshot_date FROM macro_gauge_snapshots "
"WHERE snapshot_date <= ? ORDER BY snapshot_date DESC LIMIT 1",
(at_date,)
).fetchone()
else:
row2 = conn.execute(
"SELECT gauges_json, snapshot_date FROM macro_gauge_snapshots "
"ORDER BY snapshot_date DESC LIMIT 1"
).fetchone()
if row2:
return json.loads(row2["gauges_json"] or "{}"), str(row2["snapshot_date"])