50 lines
1.8 KiB
Python
50 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
if str(ROOT) not in sys.path:
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
from src.api.trading_runtime import get_trading_runtime_incidents
|
|
|
|
|
|
def main(env=None) -> int:
|
|
env = os.environ if env is None else env
|
|
limit = int(env.get("RUNTIME_INCIDENT_LIMIT", "50"))
|
|
refresh = _get_bool(env, "RUNTIME_INCIDENT_REFRESH", True)
|
|
output_json = _get_bool(env, "RUNTIME_INCIDENT_OUTPUT_JSON", False)
|
|
payload = get_trading_runtime_incidents(limit=limit, refresh=refresh)
|
|
if output_json:
|
|
print(json.dumps(payload, ensure_ascii=False, sort_keys=True, default=str))
|
|
else:
|
|
print(f"Runtime incident journal status: {payload.get('status', 'unknown')}")
|
|
print(f"Persisted: {payload.get('persisted')}")
|
|
print(f"Incidents: {payload.get('incident_count', 0)}")
|
|
print(f"Severity counts: {json.dumps(payload.get('severity_counts') or {}, sort_keys=True)}")
|
|
print(f"Recommended actions: {', '.join(payload.get('recommended_actions') or []) or 'none'}")
|
|
latest = payload.get("latest_incident") or {}
|
|
if latest:
|
|
print(
|
|
"Latest: "
|
|
f"{latest.get('severity')} {latest.get('source')} "
|
|
f"{latest.get('reason')} @ {latest.get('last_seen_ts')}"
|
|
)
|
|
if payload.get("warnings"):
|
|
print(f"Warnings: {', '.join(payload.get('warnings') or [])}")
|
|
return 0 if payload.get("status") != "unavailable" else 2
|
|
|
|
|
|
def _get_bool(env, key: str, default: bool) -> bool:
|
|
value = env.get(key)
|
|
if value is None or value == "":
|
|
return default
|
|
return str(value).strip().lower() in {"1", "true", "yes", "on"}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|