feat: macro series

This commit is contained in:
OpenSquared
2026-06-30 19:08:33 +02:00
parent b8a1d2ad07
commit bb614936c3
5 changed files with 417 additions and 91 deletions

View File

@@ -740,3 +740,63 @@ def eco_status() -> Dict[str, Any]:
}
finally:
conn.close()
# ── Macro Series Log ──────────────────────────────────────────────────────────
@router.get("/series-log")
def get_series_log(
series_id: str = Query(...),
from_date: str = Query("2020-01-01"),
limit: int = Query(500, le=2000),
) -> List[Dict[str, Any]]:
"""Return logged actual/forecast snapshots for a given series (change events only)."""
from services.database import get_conn
conn = get_conn()
try:
rows = conn.execute(
"""SELECT series_id, event_name, event_date, actual_value, forecast_value,
previous_value, currency, impact, logged_at, source, changed
FROM macro_series_log
WHERE series_id = ? AND event_date >= ?
ORDER BY event_date ASC, logged_at ASC
LIMIT ?""",
(series_id, from_date, limit)
).fetchall()
return [dict(r) for r in rows]
finally:
conn.close()
@router.get("/series-log/distinct-series")
def list_logged_series() -> List[Dict[str, Any]]:
"""Return all series_id that have entries in macro_series_log."""
from services.database import get_conn
conn = get_conn()
try:
rows = conn.execute(
"""SELECT series_id, event_name,
COUNT(*) as log_count,
MIN(event_date) as first_date,
MAX(event_date) as last_date,
MAX(logged_at) as last_logged
FROM macro_series_log
GROUP BY series_id
ORDER BY last_date DESC"""
).fetchall()
return [dict(r) for r in rows]
finally:
conn.close()
@router.post("/series-log/backfill")
def backfill_series_log() -> Dict[str, Any]:
"""Seed macro_series_log from existing ff_calendar rows (one-time historical import)."""
from services.database import get_conn
from services.macro_series_log import backfill_from_ff_calendar
conn = get_conn()
try:
result = backfill_from_ff_calendar(conn)
return result
finally:
conn.close()