Skip to content

Commit 66742be

Browse files
committed
Improve graph status checks for Qdrant and Neo4j
Refactors the get_graph_status function to try multiple Qdrant URLs, prioritizing localhost, and simplifies Neo4j status checks by directly using the neo4j package instead of a plugin. Also updates the status table output to omit Neo4j node counts for clarity.
1 parent 90def15 commit 66742be

File tree

1 file changed

+63
-59
lines changed

1 file changed

+63
-59
lines changed

scripts/ctx_cli/commands/status.py

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -296,68 +296,73 @@ def get_graph_status(collection_name: str) -> Tuple[bool, Optional[Dict[str, Any
296296
success = False
297297

298298
# Check Qdrant graph collection
299+
# Try localhost first for CLI running on host
300+
qdrant_urls = ["http://localhost:6333"]
301+
configured_qdrant = os.environ.get("QDRANT_URL", "")
302+
if configured_qdrant and configured_qdrant not in qdrant_urls:
303+
qdrant_urls.append(configured_qdrant)
304+
305+
graph_coll = f"{collection_name}_graph" if collection_name else None
306+
307+
for qdrant_url in qdrant_urls:
308+
try:
309+
if graph_coll:
310+
req = Request(
311+
f"{qdrant_url}/collections/{graph_coll}",
312+
headers={"Accept": "application/json"}
313+
)
314+
with urlopen(req, timeout=2) as response:
315+
if response.status == 200:
316+
data = json.loads(response.read().decode('utf-8'))
317+
result = data.get("result", {})
318+
edge_count = result.get("points_count", 0)
319+
320+
graph_info["qdrant_graph"] = {
321+
"collection": graph_coll,
322+
"edge_count": edge_count,
323+
}
324+
graph_info["backend"] = "qdrant"
325+
success = True
326+
break # Success, stop trying other URLs
327+
except Exception:
328+
continue # Try next URL
329+
330+
# Check Neo4j - always try to connect for status
331+
# Use localhost (Docker exposes port) with common default password
299332
try:
300-
qdrant_url = os.environ.get("QDRANT_URL", "http://localhost:6333")
301-
graph_coll = f"{collection_name}_graph" if collection_name else None
333+
from neo4j import GraphDatabase
302334

303-
if graph_coll:
304-
req = Request(
305-
f"{qdrant_url}/collections/{graph_coll}",
306-
headers={"Accept": "application/json"}
307-
)
308-
with urlopen(req, timeout=2) as response:
309-
if response.status == 200:
310-
data = json.loads(response.read().decode('utf-8'))
311-
result = data.get("result", {})
312-
edge_count = result.get("points_count", 0)
335+
uri = "bolt://localhost:7687"
336+
user = os.environ.get("NEO4J_USER", "neo4j")
337+
password = os.environ.get("NEO4J_PASSWORD", "contextengine")
313338

314-
graph_info["qdrant_graph"] = {
315-
"collection": graph_coll,
316-
"edge_count": edge_count,
317-
}
318-
graph_info["backend"] = "qdrant"
319-
success = True
320-
except Exception:
321-
pass # Graph collection doesn't exist or Qdrant not available
339+
driver = GraphDatabase.driver(uri, auth=(user, password))
340+
driver.verify_connectivity()
322341

323-
# Check Neo4j if enabled
324-
neo4j_enabled = os.environ.get("NEO4J_GRAPH", "").lower() in {"1", "true", "yes", "on"}
325-
if neo4j_enabled:
326-
try:
327-
# Try to import and use Neo4j plugin
328-
from plugins.neo4j_graph import Neo4jGraphBackend
329-
330-
backend = Neo4jGraphBackend()
331-
# Quick health check - try a simple query
332-
driver = backend._get_driver()
333-
if driver:
334-
with driver.session() as session:
335-
# Count nodes and relationships
336-
node_result = session.run(
337-
"MATCH (n:Symbol) RETURN count(n) as count"
338-
)
339-
node_count = node_result.single()["count"]
340-
341-
edge_result = session.run(
342-
"MATCH ()-[r]->() RETURN count(r) as count"
343-
)
344-
edge_count = edge_result.single()["count"]
345-
346-
graph_info["neo4j"] = {
347-
"connected": True,
348-
"node_count": node_count,
349-
"edge_count": edge_count,
350-
}
342+
with driver.session() as session:
343+
node_result = session.run("MATCH (n:Symbol) RETURN count(n) as count")
344+
node_count = node_result.single()["count"]
345+
346+
edge_result = session.run("MATCH ()-[r]->() RETURN count(r) as count")
347+
edge_count = edge_result.single()["count"]
351348

352-
if graph_info["backend"] == "qdrant":
353-
graph_info["backend"] = "both"
354-
else:
355-
graph_info["backend"] = "neo4j"
356-
success = True
357-
except ImportError:
358-
graph_info["neo4j"] = {"connected": False, "error": "plugin not installed"}
359-
except Exception as e:
360-
graph_info["neo4j"] = {"connected": False, "error": str(e)[:50]}
349+
graph_info["neo4j"] = {
350+
"connected": True,
351+
"node_count": node_count,
352+
"edge_count": edge_count,
353+
}
354+
355+
if graph_info["backend"] == "qdrant":
356+
graph_info["backend"] = "both"
357+
else:
358+
graph_info["backend"] = "neo4j"
359+
success = True
360+
361+
driver.close()
362+
except ImportError:
363+
pass # neo4j package not installed
364+
except Exception:
365+
pass # Neo4j not available
361366

362367
return success, graph_info if success else None
363368

@@ -552,9 +557,8 @@ def print_status_table(
552557
# Neo4j edges
553558
if "neo4j" in graph_info and graph_info["neo4j"].get("connected"):
554559
neo4j_edges = graph_info["neo4j"].get("edge_count", 0)
555-
neo4j_nodes = graph_info["neo4j"].get("node_count", 0)
556560
if neo4j_edges > 0:
557-
edge_parts.append(f"{neo4j_edges:,} neo4j ({neo4j_nodes:,} nodes)")
561+
edge_parts.append(f"{neo4j_edges:,} neo4j")
558562

559563
if edge_parts:
560564
graph_text = " + ".join(edge_parts)

0 commit comments

Comments
 (0)