You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.7 KiB
Python

"""
Health check endpoint
"""
import logging
from datetime import datetime, timezone
from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import text
import redis.asyncio as redis
from app.core.database import get_db
from app.core.config import settings
from app.schemas.financial import HealthCheckResponse
logger = logging.getLogger(__name__)
router = APIRouter()
@router.get(
"/health",
response_model=HealthCheckResponse,
summary="Health check",
description="Check the health status of the API and its dependencies"
)
async def health_check(db: AsyncSession = Depends(get_db)):
"""Health check endpoint"""
# Check database
db_status = "unhealthy"
try:
result = await db.execute(text("SELECT 1"))
if result.scalar():
db_status = "healthy"
except Exception as e:
logger.warning("Database health check failed: %s", e)
# Check Redis cache
cache_status = "unhealthy"
try:
r = redis.from_url(settings.REDIS_URL)
await r.ping()
cache_status = "healthy"
await r.close()
except Exception as e:
logger.warning("Redis health check failed: %s", e)
# Check SEC data availability
sec_available = True # Simplified for now
# Overall status
overall_status = "healthy"
if db_status != "healthy" or cache_status != "healthy":
overall_status = "degraded"
return HealthCheckResponse(
status=overall_status,
version=settings.APP_VERSION,
database=db_status,
cache=cache_status,
sec_data_available=sec_available,
timestamp=datetime.now(timezone.utc)
)