Spaces:
Paused
Paused
Create database.py
Browse files- database.py +87 -0
database.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from neo4j import GraphDatabase, exceptions
|
| 3 |
+
|
| 4 |
+
# Connection secrets retrieved securely from Environment Variables (Assignment Req 5.3)
|
| 5 |
+
COGNODB_URI = os.getenv("COGNODB_URI", "bolt+s://your-instance.databases.cognodb.cloud")
|
| 6 |
+
COGNODB_USER = os.getenv("COGNODB_USER", "cognodb")
|
| 7 |
+
COGNODB_PASSWORD = os.getenv("COGNODB_PASSWORD", "your-password")
|
| 8 |
+
|
| 9 |
+
class GraphDatabaseManager:
|
| 10 |
+
"""Manages CognoDB connectivity and parameterised Cypher operations."""
|
| 11 |
+
def __init__(self):
|
| 12 |
+
self.driver = None
|
| 13 |
+
self._connect()
|
| 14 |
+
|
| 15 |
+
def _connect(self):
|
| 16 |
+
try:
|
| 17 |
+
self.driver = GraphDatabase.driver(
|
| 18 |
+
COGNODB_URI,
|
| 19 |
+
auth=(COGNODB_USER, COGNODB_PASSWORD)
|
| 20 |
+
)
|
| 21 |
+
self.driver.verify_connectivity()
|
| 22 |
+
print("Successfully connected to CognoDB Cloud!")
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"[Warning] CognoDB Cloud Unreachable: {e}")
|
| 25 |
+
self.driver = None
|
| 26 |
+
|
| 27 |
+
def is_connected(self) -> bool:
|
| 28 |
+
"""Verifies if live DB connection is available."""
|
| 29 |
+
if not self.driver:
|
| 30 |
+
return False
|
| 31 |
+
try:
|
| 32 |
+
self.driver.verify_connectivity()
|
| 33 |
+
return True
|
| 34 |
+
except Exception:
|
| 35 |
+
return False
|
| 36 |
+
|
| 37 |
+
def close(self):
|
| 38 |
+
if self.driver:
|
| 39 |
+
self.driver.close()
|
| 40 |
+
|
| 41 |
+
def query_graph_neighborhood(self, search_term: str):
|
| 42 |
+
"""
|
| 43 |
+
Executes parameterised Cypher with a 2-hop traversal:
|
| 44 |
+
Hop 1: (:Article)-[:REQUIRES_PREREQUISITE]->(:Article)
|
| 45 |
+
Hop 2: (:Article)-[:BELONGS_TO]->(:Category)
|
| 46 |
+
Also retrieves resolved issues and mentioned systems.
|
| 47 |
+
"""
|
| 48 |
+
if not self.is_connected():
|
| 49 |
+
return None, "Database unreachable. System operating in General Fallback Mode."
|
| 50 |
+
|
| 51 |
+
# Parameterised Cypher Query ($term) - Strictly no string concatenation (Req 5.1)
|
| 52 |
+
cypher_query = """
|
| 53 |
+
MATCH (a:Article)
|
| 54 |
+
WHERE toLower(a.title) CONTAINS toLower($term)
|
| 55 |
+
OR toLower(a.content) CONTAINS toLower($term)
|
| 56 |
+
|
| 57 |
+
// Hop 1 & 2: Get category, resolved issues, systems, and prerequisite articles with their categories
|
| 58 |
+
OPTIONAL MATCH (a)-[:BELONGS_TO]->(c:Category)
|
| 59 |
+
OPTIONAL MATCH (a)-[:RESOLVES]->(i:Issue)
|
| 60 |
+
OPTIONAL MATCH (a)-[:MENTIONS]->(sys:System)
|
| 61 |
+
OPTIONAL MATCH (a)-[:REQUIRES_PREREQUISITE]->(prereq:Article)-[:BELONGS_TO]->(pCat:Category)
|
| 62 |
+
|
| 63 |
+
RETURN a.id AS articleId,
|
| 64 |
+
a.title AS title,
|
| 65 |
+
a.content AS content,
|
| 66 |
+
c.name AS category,
|
| 67 |
+
i.code AS errorCode,
|
| 68 |
+
i.description AS issueDetail,
|
| 69 |
+
sys.name AS affectedSystem,
|
| 70 |
+
collect(DISTINCT {
|
| 71 |
+
title: prereq.title,
|
| 72 |
+
category: pCat.name
|
| 73 |
+
}) AS prerequisites
|
| 74 |
+
LIMIT 3
|
| 75 |
+
"""
|
| 76 |
+
try:
|
| 77 |
+
with self.driver.session() as session:
|
| 78 |
+
result = session.run(cypher_query, term=search_term)
|
| 79 |
+
records = [record.data() for record in result]
|
| 80 |
+
return records, None
|
| 81 |
+
except exceptions.Neo4jError as err:
|
| 82 |
+
return None, f"Cypher Query Error: {str(err)}"
|
| 83 |
+
except Exception as e:
|
| 84 |
+
return None, f"Unexpected DB Error: {str(e)}"
|
| 85 |
+
|
| 86 |
+
# Singleton database manager instance
|
| 87 |
+
db = GraphDatabaseManager()
|