from fastapi import APIRouter, HTTPException import logging from core.redis_client import redis_client from services.ollama_monitor import check_ollama_status from utils.config import config from utils.config_validator import ConfigValidator logger = logging.getLogger(__name__) router = APIRouter() @router.get("/ollama-status") async def get_ollama_status(): """Returns the current status of the Ollama service.""" try: status = check_ollama_status() return status except Exception as e: logger.error(f"Error checking Ollama status: {e}") raise HTTPException(status_code=500, detail=str(e)) @router.get("/health") async def health_check(): """Comprehensive health check for all services""" try: # Check configuration validator = ConfigValidator() config_report = validator.validate_all() # Check Redis redis_healthy = redis_client.is_healthy() if redis_client.get_client() else False # Check Ollama try: ollama_status = check_ollama_status() ollama_healthy = ollama_status.get("running", False) except Exception as e: logger.error(f"Error checking Ollama status: {e}") ollama_healthy = False ollama_status = {"error": str(e)} # Overall health overall_healthy = redis_healthy and ollama_healthy and config_report['valid'] return { "status": "healthy" if overall_healthy else "degraded", "services": { "redis": "healthy" if redis_healthy else "unhealthy", "ollama": "healthy" if ollama_healthy else "unhealthy", "configuration": "valid" if config_report['valid'] else "invalid" }, "details": { "redis_healthy": redis_healthy, "ollama_status": ollama_status, "config_validation": config_report } } except Exception as e: logger.error(f"Health check failed: {e}") raise HTTPException(status_code=500, detail=str(e)) @router.get("/config") async def config_status(): """Return current configuration status""" validator = ConfigValidator() report = validator.validate_all() return { "configuration": { "ollama_host": config.ollama_host, "hf_api_url": config.hf_api_url, "redis_host": config.redis_host, "redis_port": config.redis_port, "use_fallback": config.use_fallback }, "validation": report }