""" Status API routes. Provides system status and health check endpoints: - GET /api/status - Get system status and health information """ from typing import Dict, Any from fastapi import APIRouter, Depends from sqlalchemy.orm import Session from sqlalchemy import text from app.api.deps import get_db_session, require_auth_optional from app.schemas.common import SuccessResponse from app.config import get_settings from app.models.server import Server from app.models.ssh_key import SshKey from app.models.repo import Repo router = APIRouter(prefix="/api/status", tags=["Status"]) @router.get("", response_model=SuccessResponse[Dict[str, Any]]) def get_status( db: Session = Depends(get_db_session), _authenticated: bool = Depends(require_auth_optional) ): """ Get system status and health information. This endpoint provides information about: - Application status and version - Database connectivity and statistics - Counts of servers, SSH keys, and repositories - Storage paths Authentication is optional for this endpoint. Authenticated users may receive additional information. Args: db: Database session (injected) _authenticated: Whether request is authenticated (injected) Returns: SuccessResponse containing system status information Example response: { "code": 0, "data": { "status": "healthy", "version": "1.0.0", "database": { "status": "connected", "servers_count": 2, "ssh_keys_count": 3, "repos_count": 15 }, "storage": { "data_dir": "/path/to/data", "repos_dir": "/path/to/data/repos", "ssh_keys_dir": "/path/to/data/ssh_keys" }, "authenticated": true }, "message": "System status retrieved successfully" } """ settings = get_settings() status_info: Dict[str, Any] = { "status": "healthy", "version": "1.0.0", "authenticated": _authenticated } # Check database connectivity try: # Execute a simple query to verify database connection db.execute(text("SELECT 1")) # Get counts for each model servers_count = db.query(Server).count() ssh_keys_count = db.query(SshKey).count() repos_count = db.query(Repo).count() status_info["database"] = { "status": "connected", "servers_count": servers_count, "ssh_keys_count": ssh_keys_count, "repos_count": repos_count } except Exception as e: status_info["database"] = { "status": "error", "error": str(e) } status_info["status"] = "degraded" # Storage paths (only show to authenticated users) if _authenticated: status_info["storage"] = { "data_dir": str(settings.data_dir), "repos_dir": str(settings.repos_dir), "ssh_keys_dir": str(settings.ssh_keys_dir), "db_path": str(settings.db_path) } return SuccessResponse( code=0, data=status_info, message="System status retrieved successfully" ) @router.get("/health", response_model=SuccessResponse[Dict[str, str]]) def health_check(): """ Simple health check endpoint. This is a lightweight endpoint for load balancers and monitoring systems. It always returns 200 OK when the service is running. Returns: SuccessResponse indicating healthy status Example response: { "code": 0, "data": {"status": "ok"}, "message": "Service is healthy" } """ return SuccessResponse( code=0, data={"status": "ok"}, message="Service is healthy" )