Read feed replay history from runtime db

This commit is contained in:
2026-03-16 02:26:24 +08:00
parent a41cd705b4
commit 3a5558b576
2 changed files with 97 additions and 2 deletions

View File

@@ -136,6 +136,90 @@ class RuntimeDb:
),
)
def get_recent_feed_events(
self,
*,
limit: int = 200,
event_types: Optional[Iterable[str]] = None,
) -> list[Dict[str, Any]]:
"""Return recent persisted feed events in newest-first order."""
event_types = tuple(event_types or ())
sql = """
SELECT payload_json
FROM events
"""
params: list[Any] = []
if event_types:
placeholders = ",".join("?" for _ in event_types)
sql += f" WHERE event_type IN ({placeholders})"
params.extend(event_types)
sql += " ORDER BY timestamp DESC LIMIT ?"
params.append(max(1, int(limit)))
with self._connect() as conn:
rows = conn.execute(sql, params).fetchall()
items: list[Dict[str, Any]] = []
for row in rows:
try:
payload = json.loads(row["payload_json"]) if row["payload_json"] else {}
except json.JSONDecodeError:
payload = {}
if payload:
items.append(payload)
return items
def get_last_day_feed_events(
self,
*,
current_date: Optional[str] = None,
limit: int = 200,
event_types: Optional[Iterable[str]] = None,
) -> list[Dict[str, Any]]:
"""Return latest trading day events in newest-first order for replay."""
event_types = tuple(event_types or ())
target_date = str(current_date or "").strip() or None
with self._connect() as conn:
if not target_date:
row = conn.execute(
"""
SELECT run_date
FROM events
WHERE run_date IS NOT NULL AND TRIM(run_date) != ''
ORDER BY run_date DESC
LIMIT 1
"""
).fetchone()
target_date = row["run_date"] if row else None
if not target_date:
return []
sql = """
SELECT payload_json
FROM events
WHERE run_date = ?
"""
params: list[Any] = [target_date]
if event_types:
placeholders = ",".join("?" for _ in event_types)
sql += f" AND event_type IN ({placeholders})"
params.extend(event_types)
sql += " ORDER BY timestamp DESC LIMIT ?"
params.append(max(1, int(limit)))
rows = conn.execute(sql, params).fetchall()
items: list[Dict[str, Any]] = []
for row in rows:
try:
payload = json.loads(row["payload_json"]) if row["payload_json"] else {}
except json.JSONDecodeError:
payload = {}
if payload:
items.append(payload)
return items
def upsert_trade(self, trade: Dict[str, Any]):
payload = dict(trade or {})
if not payload: