Files
market_intel/apps/api/routes/health.py
T
2026-08-02 09:19:04 +08:00

53 lines
1.5 KiB
Python

from __future__ import annotations
import logging
from fastapi import APIRouter
from packages.config import get_settings
from packages.db import Repository
router = APIRouter(tags=["health"])
log = logging.getLogger("health")
@router.get("/health")
def health() -> dict:
try:
s = get_settings()
except Exception as e: # noqa: BLE001
log.exception("settings load failed: %s", e)
return {
"ok": False,
"service": "market_intel",
"product": "比特骆驼行情采集分析",
"error": f"settings: {e}",
}
repo = Repository(s.db_path)
try:
hb = repo.get_heartbeat()
last_ok = hb.get("last_ok_ts_ms")
lag_ms = None
if last_ok:
import time
lag_ms = max(0, int(time.time() * 1000) - int(last_ok))
return {
"ok": True,
"service": "market_intel",
"product": "比特骆驼行情采集分析",
"collector_lag_ms": lag_ms,
"consecutive_failures": hb.get("consecutive_failures", 0),
"option_quotes": repo.count_option_quotes(),
"index_ticks": repo.count_index_ticks(),
}
except Exception as e: # noqa: BLE001
log.exception("health db failed: %s", e)
return {
"ok": False,
"service": "market_intel",
"product": "比特骆驼行情采集分析",
"error": f"db: {e}",
}
finally:
repo.close()