-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneo.py
More file actions
389 lines (338 loc) · 16.5 KB
/
neo.py
File metadata and controls
389 lines (338 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
from neo4j import GraphDatabase
import logging
from typing import Optional, Dict, Any
import os
from dotenv import load_dotenv
import json
class AuraConnection:
def __init__(self):
"""Initialize the Neo4j Aura connection handler"""
# Debug: Read .env file directly
try:
with open('.env', 'r') as f:
print("Raw .env contents:")
print(repr(f.read())) # Using repr() to show hidden characters
except Exception as e:
print(f"Error reading .env: {e}")
load_dotenv()
# Debug: Check if .env file is being found
print(f"Current working directory: {os.getcwd()}")
print(f"Does .env exist?: {os.path.exists('.env')}")
# Connection details should be stored in environment variables
self.uri = os.getenv('NEO4J_URI') # Aura connection URI
self.username = os.getenv('NEO4J_USERNAME') # Usually 'neo4j'
self.password = os.getenv('NEO4J_PASSWORD') # Your Aura instance password
# Debug: Print all environment variables
print("Environment variables:")
print(f"NEO4J_URI: {os.getenv('NEO4J_URI')}")
print(f"NEO4J_USERNAME: {os.getenv('NEO4J_USERNAME')}")
# Initialize logger
self.logger = logging.getLogger(__name__)
# Initialize driver as None
self.driver: Optional[GraphDatabase.driver] = None
def connect(self) -> bool:
"""
Establish connection to Neo4j Aura instance
Returns: bool indicating if connection was successful
"""
try:
# Debug prints
print(f"URI type: {type(self.uri)}")
print(f"URI value: {self.uri}")
print(f"Username: {self.username}")
# Create the driver instance
self.driver = GraphDatabase.driver(
self.uri,
auth=(self.username, self.password),
max_connection_lifetime=3600,
max_connection_pool_size=50,
connection_acquisition_timeout=60
)
# Verify connectivity
self.driver.verify_connectivity()
self.logger.info("Successfully connected to Neo4j Aura instance")
return True
except Exception as e:
self.logger.error(f"Failed to connect to Neo4j Aura: {str(e)}")
return False
def close(self):
"""Close the Neo4j connection"""
if self.driver:
self.driver.close()
self.logger.info("Neo4j Aura connection closed")
def test_connection(self) -> bool:
"""
Test the connection by running a simple query
Returns: bool indicating if test was successful
"""
try:
with self.driver.session() as session:
result = session.run("RETURN 1 AS test")
return result.single()["test"] == 1
except Exception as e:
self.logger.error(f"Connection test failed: {str(e)}")
return False
def execute_query(self, query: str, parameters: dict = None) -> list:
"""
Execute a Cypher query and return the results
Args:
query: Cypher query string
parameters: Optional dictionary of query parameters
Returns:
List of records from the query
"""
try:
with self.driver.session() as session:
result = session.run(query, parameters or {})
return [record.data() for record in result]
except Exception as e:
self.logger.error(f"Query execution failed: {str(e)}")
raise
def import_json_data(self, json_file_path: str) -> bool:
"""
Import SingularityNET meeting data from JSON file into Neo4j
"""
try:
with open(json_file_path, 'r') as file:
data = json.load(file)
# Add this at the start of the method
print("\nFirst meeting structure:")
first_meeting = next(iter(data.values()))[0]
print("Agenda Items:", json.dumps(first_meeting['agendaItems'], indent=2))
with self.driver.session() as session:
# First, clear existing data
session.run("MATCH (n) DETACH DELETE n")
for meeting_id, meetings in data.items():
for meeting in meetings:
# Debug print
print(f"\nProcessing meeting: {meeting_id}")
# Create Meeting node with tags
meeting_query = """
CREATE (m:Meeting {
id: $meeting_id,
workgroup: $workgroup,
workgroup_id: $workgroup_id,
name: $name,
date: $date,
host: $host,
documenter: $documenter,
purpose: $purpose,
topics_covered: $topics_covered,
emotions: $emotions
})
"""
meeting_info = meeting['meetingInfo']
tags = meeting.get('tags', {})
session.run(meeting_query, {
'meeting_id': meeting_id,
'workgroup': meeting['workgroup'],
'workgroup_id': meeting['workgroup_id'],
'name': meeting_info['name'],
'date': meeting_info['date'],
'host': meeting_info['host'],
'documenter': meeting_info['documenter'],
'purpose': meeting_info['purpose'],
'topics_covered': tags.get('topicsCovered', ''),
'emotions': tags.get('emotions', '')
})
# Create Topic nodes and relationships
topics_query = """
MATCH (m:Meeting {id: $meeting_id})
WITH m
UNWIND split($topics, ',') AS topic
WITH m, trim(topic) AS clean_topic
WHERE clean_topic <> ''
MERGE (t:Topic {name: clean_topic})
CREATE (m)-[:COVERS_TOPIC]->(t)
"""
if tags.get('topicsCovered'):
session.run(topics_query, {
'meeting_id': meeting_id,
'topics': tags['topicsCovered']
})
# Create Emotion nodes and relationships
emotions_query = """
MATCH (m:Meeting {id: $meeting_id})
WITH m
UNWIND split($emotions, ',') AS emotion
WITH m, trim(emotion) AS clean_emotion
WHERE clean_emotion <> ''
MERGE (e:Emotion {name: clean_emotion})
CREATE (m)-[:HAS_EMOTION]->(e)
"""
if tags.get('emotions'):
session.run(emotions_query, {
'meeting_id': meeting_id,
'emotions': tags['emotions']
})
# Create Participant nodes and relationships
participants_query = """
MATCH (m:Meeting {id: $meeting_id})
WITH m
UNWIND $participants AS participant
MERGE (p:Person {name: participant})
CREATE (p)-[:ATTENDED]->(m)
"""
participants = [p.strip() for p in meeting_info['peoplePresent'].split(',')]
session.run(participants_query, {
'meeting_id': meeting_id,
'participants': participants
})
# Create Document nodes and relationships
docs_query = """
MATCH (m:Meeting {id: $meeting_id})
WITH m
UNWIND $docs AS doc
CREATE (d:Document {
title: doc.title,
link: doc.link
})
CREATE (m)-[:HAS_DOCUMENT]->(d)
"""
session.run(docs_query, {
'meeting_id': meeting_id,
'docs': meeting_info['workingDocs']
})
# Create Agenda Items and their components
agenda_query = """
MATCH (m:Meeting {id: $meeting_id})
WITH m
UNWIND $agenda_items AS item
CREATE (a:AgendaItem {
status: item.status,
narrative: item.narrative
})
CREATE (m)-[:HAS_AGENDA_ITEM]->(a)
// Create Discussion Points
WITH a, item
UNWIND item.discussionPoints AS point
CREATE (d:DiscussionPoint {text: point})
CREATE (a)-[:INCLUDES_DISCUSSION]->(d)
// Create Decision Items
WITH a, item
UNWIND item.decisionItems AS decision
CREATE (dec:Decision {
decision: decision.decision,
rationale: decision.rationale,
opposing: decision.opposing,
effect: decision.effect
})
CREATE (a)-[:MADE_DECISION]->(dec)
"""
for agenda_item in meeting['agendaItems']:
session.run(agenda_query, {
'meeting_id': meeting_id,
'agenda_items': [{
'status': agenda_item.get('status', ''),
'narrative': agenda_item.get('narrative', ''),
'discussionPoints': agenda_item.get('discussionPoints', []),
'decisionItems': agenda_item.get('decisionItems', [])
}]
})
# Create Action Items with relationships
actions_query = """
MATCH (m:Meeting {id: $meeting_id})
WITH m
UNWIND $actions AS action
CREATE (a:ActionItem {
text: action.text,
status: action.status,
dueDate: action.dueDate
})
CREATE (m)-[:HAS_ACTION]->(a)
WITH a, action
MATCH (p:Person {name: action.assignee})
CREATE (a)-[:ASSIGNED_TO]->(p)
"""
# Flatten action items from all agenda items
all_actions = []
for agenda_item in meeting['agendaItems']:
all_actions.extend([
action for action in agenda_item.get('actionItems', [])
if 'assignee' in action
])
if all_actions:
session.run(actions_query, {
'meeting_id': meeting_id,
'actions': all_actions
})
# Create Decision Items from agenda items
decisions_query = """
MATCH (m:Meeting {id: $meeting_id})
WITH m
UNWIND $decisions AS dec
CREATE (d:Decision {
decision: dec.decision,
rationale: dec.rationale,
opposing: dec.opposing,
effect: dec.effect
})
CREATE (m)-[:MADE_DECISION]->(d)
RETURN count(d) as decisions_created
"""
# Collect all decisions from agenda items with debug
all_decisions = []
for idx, agenda_item in enumerate(meeting['agendaItems']):
decisions = agenda_item.get('decisionItems', [])
print(f"Found {len(decisions)} decisions in agenda item {idx}")
print(f"Decisions: {decisions}")
all_decisions.extend(decisions)
print(f"Total decisions to create: {len(all_decisions)}")
if all_decisions:
result = session.run(decisions_query, {
'meeting_id': meeting_id,
'decisions': all_decisions
})
created = result.single()['decisions_created']
print(f"Created {created} decision nodes")
# Verify decisions were created
verify_query = """
MATCH (m:Meeting {id: $meeting_id})-[:MADE_DECISION]->(d:Decision)
RETURN count(d) as decision_count
"""
result = session.run(verify_query, {'meeting_id': meeting_id})
count = result.single()['decision_count']
print(f"Verified {count} decisions in database for meeting {meeting_id}")
self.logger.info(f"Successfully imported meeting data with decisions")
return True
except Exception as e:
self.logger.error(f"Failed to import JSON data: {str(e)}")
print(f"Error details: {str(e)}")
return False
def main():
# Set up logging
logging.basicConfig(level=logging.INFO)
# Create a .env file with your Aura credentials:
"""
NEO4J_URI=neo4j+s://xxxxxxxx.databases.neo4j.io
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=your-password
"""
# Initialize connection
aura = AuraConnection()
try:
# Connect to Aura
if aura.connect():
# Test the connection
if aura.test_connection():
print("Successfully connected to Neo4j Aura!")
# Example query
result = aura.execute_query(
"MATCH (n) RETURN count(n) as node_count"
)
print(f"Number of nodes in database: {result[0]['node_count']}")
# Import JSON data with explicit path
json_path = os.path.join(os.getcwd(), 'snet-data.json')
if aura.import_json_data(json_path):
print("Successfully imported data from JSON!")
else:
print("Failed to import data from JSON!")
else:
print("Connection test failed!")
else:
print("Failed to connect to Neo4j Aura!")
finally:
# Always close the connection
aura.close()
if __name__ == "__main__":
main()