From 2853e7f319d21e48bd708306b06fd228ca92cb22 Mon Sep 17 00:00:00 2001 From: AWADEME Ronaldo Date: Fri, 18 Sep 2026 19:57:51 +0100 Subject: [PATCH] feat: report uptime_seconds in /health response --- app/main.py | 10 +++++++++- app/schemas.py | 1 + tests/test_api.py | 13 +++++++++++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index c4bdfdc..59a5bd3 100644 --- a/app/main.py +++ b/app/main.py @@ -16,6 +16,8 @@ TASK_NOT_FOUND = "Task not found" +_started_at = time.monotonic() + logging.basicConfig( level=settings.log_level.upper(), format="%(asctime)s %(levelname)s %(name)s %(message)s", @@ -72,6 +74,7 @@ def health(db: Session = Depends(get_db)): status="degraded", database="error", version=settings.app_version, + uptime_seconds=time.monotonic() - _started_at, details=str(exc.__class__.__name__), ) return JSONResponse( @@ -79,7 +82,12 @@ def health(db: Session = Depends(get_db)): content=payload.model_dump(), ) - return HealthRead(status="ok", database="ok", version=settings.app_version) + return HealthRead( + status="ok", + database="ok", + version=settings.app_version, + uptime_seconds=time.monotonic() - _started_at, + ) @app.get("/tasks", response_model=list[TaskRead], tags=["tasks"]) diff --git a/app/schemas.py b/app/schemas.py index a12b0b0..540f748 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -15,6 +15,7 @@ class HealthRead(BaseModel): status: str database: str version: str + uptime_seconds: float details: str | None = None diff --git a/tests/test_api.py b/tests/test_api.py index 8f0747c..4d40233 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -2,11 +2,20 @@ from app.main import app - client = TestClient(app) def test_root(): response = client.get("/") - assert response.status_code == 200 \ No newline at end of file + assert response.status_code == 200 + + +def test_health_reports_uptime(): + response = client.get("/health") + + # 200 (base disponible) ou 503 (base injoignable) sont tous les deux + # des reponses valides du endpoint -- ce test verifie seulement que + # uptime_seconds est toujours present, quel que soit l'etat de la base. + assert response.status_code in (200, 503) + assert response.json()["uptime_seconds"] >= 0 \ No newline at end of file