Spaces:
Paused
Paused
| """ | |
| Monitoring API Endpoints | |
| Production monitoring and metrics endpoints | |
| """ | |
| from fastapi import APIRouter, Depends, HTTPException, status | |
| from typing import Any | |
| from datetime import datetime | |
| from app.services.monitoring_collector import MonitoringCollector | |
| from monitoring.config import MonitoringConfig | |
| router = APIRouter(prefix="/monitoring", tags=["monitoring"]) | |
| # Dependency to get monitoring collector | |
| async def get_monitoring_collector(): | |
| """Get monitoring collector instance""" | |
| collector = MonitoringCollector() | |
| return collector | |
| async def get_system_health( | |
| collector: MonitoringCollector = Depends(get_monitoring_collector) | |
| ): | |
| """Get overall system health status""" | |
| try: | |
| health_status = collector.get_health_status() | |
| return { | |
| "success": True, | |
| "data": health_status, | |
| "timestamp": datetime.utcnow().isoformat() | |
| } | |
| except Exception as e: | |
| raise HTTPException( | |
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | |
| detail=f"Failed to get health status: {str(e)}" | |
| ) | |
| async def get_performance_metrics( | |
| minutes: int = 5, | |
| collector: MonitoringCollector = Depends(get_monitoring_collector) | |
| ): | |
| """Get performance metrics for the last N minutes""" | |
| try: | |
| if minutes > 60: | |
| raise HTTPException( | |
| status_code=status.HTTP_400_BAD_REQUEST, | |
| detail="Maximum time range is 60 minutes" | |
| ) | |
| metrics = collector.get_performance_metrics(minutes) | |
| return { | |
| "success": True, | |
| "data": metrics, | |
| "timestamp": datetime.utcnow().isoformat() | |
| } | |
| except Exception as e: | |
| raise HTTPException( | |
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | |
| detail=f"Failed to get performance metrics: {str(e)}" | |
| ) | |
| async def get_security_metrics( | |
| minutes: int = 5, | |
| collector: MonitoringCollector = Depends(get_monitoring_collector) | |
| ): | |
| """Get security metrics for the last N minutes""" | |
| try: | |
| if minutes > 60: | |
| raise HTTPException( | |
| status_code=status.HTTP_400_BAD_REQUEST, | |
| detail="Maximum time range is 60 minutes" | |
| ) | |
| metrics = collector.get_security_metrics(minutes) | |
| return { | |
| "success": True, | |
| "data": metrics, | |
| "timestamp": datetime.utcnow().isoformat() | |
| } | |
| except Exception as e: | |
| raise HTTPException( | |
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | |
| detail=f"Failed to get security metrics: {str(e)}" | |
| ) | |
| async def get_business_metrics( | |
| minutes: int = 5, | |
| collector: MonitoringCollector = Depends(get_monitoring_collector) | |
| ): | |
| """Get business metrics for the last N minutes""" | |
| try: | |
| if minutes > 60: | |
| raise HTTPException( | |
| status_code=status.HTTP_400_BAD_REQUEST, | |
| detail="Maximum time range is 60 minutes" | |
| ) | |
| metrics = collector.get_business_metrics(minutes) | |
| return { | |
| "success": True, | |
| "data": metrics, | |
| "timestamp": datetime.utcnow().isoformat() | |
| } | |
| except Exception as e: | |
| raise HTTPException( | |
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | |
| detail=f"Failed to get business metrics: {str(e)}" | |
| ) | |
| async def get_dashboard_metrics( | |
| collector: MonitoringCollector = Depends(get_monitoring_collector) | |
| ): | |
| """Get comprehensive dashboard metrics""" | |
| try: | |
| health = collector.get_health_status() | |
| performance = collector.get_performance_metrics(5) | |
| security = collector.get_security_metrics(5) | |
| business = collector.get_business_metrics(5) | |
| return { | |
| "success": True, | |
| "data": { | |
| "health": health, | |
| "performance": performance, | |
| "security": security, | |
| "business": business | |
| }, | |
| "timestamp": datetime.utcnow().isoformat() | |
| } | |
| except Exception as e: | |
| raise HTTPException( | |
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | |
| detail=f"Failed to get dashboard metrics: {str(e)}" | |
| ) | |
| async def get_monitoring_status(): | |
| """Get monitoring system status""" | |
| try: | |
| return { | |
| "success": True, | |
| "data": { | |
| "monitoring_enabled": True, | |
| "environment": MonitoringConfig.ENVIRONMENT, | |
| "api_version": "1.0.0", | |
| "features": { | |
| "performance_monitoring": MonitoringConfig.ENABLE_PERFORMANCE_MONITORING, | |
| "security_monitoring": MonitoringConfig.ENABLE_SECURITY_MONITORING, | |
| "business_metrics": MonitoringConfig.ENABLE_BUSINESS_METRICS | |
| }, | |
| "alert_thresholds": MonitoringConfig.ALERT_THRESHOLDS | |
| }, | |
| "timestamp": datetime.utcnow().isoformat() | |
| } | |
| except Exception as e: | |
| raise HTTPException( | |
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | |
| detail=f"Failed to get monitoring status: {str(e)}" | |
| ) | |