Spaces:
Runtime error
Runtime error
Create infra_graph.py
Browse files- infra_graph.py +41 -0
infra_graph.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# infra_graph.py
|
| 2 |
+
import logging
|
| 3 |
+
import networkx as nx
|
| 4 |
+
from typing import Dict, Optional
|
| 5 |
+
|
| 6 |
+
logger = logging.getLogger(__name__)
|
| 7 |
+
|
| 8 |
+
class InfraGraph:
|
| 9 |
+
def __init__(self, uri: Optional[str] = None, user: Optional[str] = None, password: Optional[str] = None):
|
| 10 |
+
self.neo4j_driver = None
|
| 11 |
+
if uri and user and password:
|
| 12 |
+
try:
|
| 13 |
+
from neo4j import GraphDatabase
|
| 14 |
+
self.neo4j_driver = GraphDatabase.driver(uri, auth=(user, password))
|
| 15 |
+
logger.info("Connected to Neo4j")
|
| 16 |
+
except Exception as e:
|
| 17 |
+
logger.warning(f"Neo4j connection failed, using mock: {e}")
|
| 18 |
+
# Always create a NetworkX graph as fallback
|
| 19 |
+
self.nx_graph = nx.DiGraph()
|
| 20 |
+
|
| 21 |
+
def update_from_state(self, components: Dict):
|
| 22 |
+
"""Populate graph from simulator state."""
|
| 23 |
+
self.nx_graph.clear()
|
| 24 |
+
for cid, props in components.items():
|
| 25 |
+
self.nx_graph.add_node(cid, **props)
|
| 26 |
+
for conn in props.get("connections", []):
|
| 27 |
+
self.nx_graph.add_edge(cid, conn, relation="connects_to")
|
| 28 |
+
if self.neo4j_driver:
|
| 29 |
+
self._update_neo4j(components)
|
| 30 |
+
|
| 31 |
+
def _update_neo4j(self, components):
|
| 32 |
+
# Implementation to sync with Neo4j (Cypher queries)
|
| 33 |
+
pass
|
| 34 |
+
|
| 35 |
+
def get_failing_components(self):
|
| 36 |
+
"""Return list of components with status != 'up'."""
|
| 37 |
+
failing = []
|
| 38 |
+
for node, data in self.nx_graph.nodes(data=True):
|
| 39 |
+
if data.get("status") != "up":
|
| 40 |
+
failing.append(node)
|
| 41 |
+
return failing
|