diff --git a/backend/app/routers/health.py b/backend/app/routers/health.py index aeb04c6..3ae2dbd 100644 --- a/backend/app/routers/health.py +++ b/backend/app/routers/health.py @@ -1,35 +1,47 @@ import time from datetime import datetime, timezone -from fastapi import APIRouter +from fastapi import APIRouter, Depends +from sqlalchemy import text +from sqlalchemy.orm import Session +from app.database import get_db from app.schemas import HealthCheck, HealthResponse router = APIRouter(tags=['health']) START_TIME = time.time() -def build_health() -> HealthResponse: +def build_health(db: Session) -> HealthResponse: + db_check: HealthCheck + try: + t0 = time.perf_counter() + db.execute(text("SELECT 1")) + latency_ms = round((time.perf_counter() - t0) * 1000, 2) + db_check = HealthCheck(status='pass', latency_ms=latency_ms, detail='sqlite ok') + except Exception as exc: + db_check = HealthCheck(status='fail', detail=str(exc)) + overall = 'pass' if db_check.status == 'pass' else 'fail' return HealthResponse( - status='pass', + status=overall, service='netmapper-backend', version='0.1.0', time=datetime.now(timezone.utc), uptime_s=round(time.time() - START_TIME, 2), - checks={'db': HealthCheck(status='warn', detail='database not configured yet')}, + checks={'db': db_check}, ) @router.get('/live', response_model=HealthResponse) -def live() -> HealthResponse: - return build_health() +def live(db: Session = Depends(get_db)) -> HealthResponse: + return build_health(db) @router.get('/ready', response_model=HealthResponse) -def ready() -> HealthResponse: - return build_health() +def ready(db: Session = Depends(get_db)) -> HealthResponse: + return build_health(db) @router.get('/health', response_model=HealthResponse) -def health() -> HealthResponse: - return build_health() +def health(db: Session = Depends(get_db)) -> HealthResponse: + return build_health(db)