54 lines
1.8 KiB
Python
54 lines
1.8 KiB
Python
|
|
"""API endpoints for collector management and status."""
|
||
|
|
from fastapi import APIRouter, HTTPException
|
||
|
|
|
||
|
|
router = APIRouter(prefix="/collectors", tags=["collectors"])
|
||
|
|
|
||
|
|
|
||
|
|
def _get_manager():
|
||
|
|
"""Get the global CollectorManager instance."""
|
||
|
|
from app.main import collector_manager
|
||
|
|
if collector_manager is None:
|
||
|
|
raise HTTPException(status_code=503, detail="Collector manager not active (simulator mode)")
|
||
|
|
return collector_manager
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/status")
|
||
|
|
async def get_collectors_status():
|
||
|
|
"""Get status of all active collectors."""
|
||
|
|
manager = _get_manager()
|
||
|
|
return {
|
||
|
|
"running": manager.is_running,
|
||
|
|
"collector_count": manager.collector_count,
|
||
|
|
"collectors": manager.get_all_status(),
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/status/{device_id}")
|
||
|
|
async def get_collector_status(device_id: int):
|
||
|
|
"""Get status of a specific collector."""
|
||
|
|
manager = _get_manager()
|
||
|
|
collector = manager.get_collector(device_id)
|
||
|
|
if not collector:
|
||
|
|
raise HTTPException(status_code=404, detail="No collector for this device")
|
||
|
|
return collector.get_status()
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("/{device_id}/restart")
|
||
|
|
async def restart_collector(device_id: int):
|
||
|
|
"""Restart a specific device collector."""
|
||
|
|
manager = _get_manager()
|
||
|
|
success = await manager.restart_collector(device_id)
|
||
|
|
if not success:
|
||
|
|
raise HTTPException(status_code=400, detail="Failed to restart collector")
|
||
|
|
return {"message": f"Collector for device {device_id} restarted"}
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("/{device_id}/stop")
|
||
|
|
async def stop_collector(device_id: int):
|
||
|
|
"""Stop a specific device collector."""
|
||
|
|
manager = _get_manager()
|
||
|
|
success = await manager.stop_collector(device_id)
|
||
|
|
if not success:
|
||
|
|
raise HTTPException(status_code=404, detail="No running collector for this device")
|
||
|
|
return {"message": f"Collector for device {device_id} stopped"}
|