"""Topology API endpoints.""" import logging from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy.orm import Session from app.database import get_db from app.schemas import TopologyResponse from app.services.topology_service import TopologyService logger = logging.getLogger(__name__) router = APIRouter() @router.get("", response_model=TopologyResponse) def get_network_topology( include_offline: bool = Query(False, description="Include offline hosts"), db: Session = Depends(get_db) ): """ Get network topology graph data. Args: include_offline: Whether to include offline hosts db: Database session Returns: Topology data with nodes and edges """ try: topology_service = TopologyService(db) topology = topology_service.generate_topology(include_offline=include_offline) logger.info(f"Generated topology with {len(topology.nodes)} nodes") return topology except Exception as e: logger.error(f"Error generating topology: {e}", exc_info=True) raise HTTPException(status_code=500, detail="Failed to generate topology") @router.get("/neighbors/{host_id}") def get_host_neighbors(host_id: int, db: Session = Depends(get_db)): """ Get neighboring hosts for a specific host. Args: host_id: Host ID db: Database session Returns: List of neighboring hosts """ topology_service = TopologyService(db) neighbors = topology_service.get_host_neighbors(host_id) return { 'host_id': host_id, 'neighbors': [ { 'id': h.id, 'ip_address': h.ip_address, 'hostname': h.hostname, 'status': h.status } for h in neighbors ] }