wu981526092 commited on
Commit
d8722a7
Β·
1 Parent(s): ebce353

Add comprehensive debug logging to sample data loading

Browse files
Files changed (1) hide show
  1. backend/database/__init__.py +58 -11
backend/database/__init__.py CHANGED
@@ -81,35 +81,44 @@ def test_database_connection():
81
 
82
  def add_sample_data_for_hf():
83
  """Add simple sample data for HF Spaces using direct SQL."""
 
 
84
  if not os.getenv("SPACE_ID"):
 
85
  return # Only run on HF Spaces
86
 
87
- print(f"πŸ” HF Spaces environment check:")
88
  print(f" β€’ SPACE_ID: {os.getenv('SPACE_ID')}")
89
  print(f" β€’ Database URL: {DATABASE_URL}")
 
90
 
91
  try:
 
92
  # Use direct connection to avoid session issues
93
  with engine.connect() as conn:
 
 
94
  # Begin transaction for atomic operations
 
95
  trans = conn.begin()
96
 
97
  try:
 
98
  # Check if data already exists
99
  result = conn.execute("SELECT COUNT(*) FROM traces").fetchone()
100
  existing_traces = result[0] if result else 0
 
101
 
102
  result = conn.execute("SELECT COUNT(*) FROM knowledge_graphs").fetchone()
103
  existing_kgs = result[0] if result else 0
104
-
105
- print(f" β€’ Existing traces: {existing_traces}")
106
- print(f" β€’ Existing KGs: {existing_kgs}")
107
 
108
  if existing_traces > 0 or existing_kgs > 0:
109
- print("πŸ“Š Sample data already exists, skipping...")
110
  return
111
 
112
- print("🎯 Adding sample data using direct SQL...")
 
113
 
114
  import json
115
  import uuid
@@ -126,11 +135,16 @@ def add_sample_data_for_hf():
126
  {"role": "user", "content": "Thank you for the quick resolution!", "timestamp": "2024-08-31T10:04:00Z", "sentiment": "satisfied"}
127
  ]'''
128
 
 
129
  # Generate IDs
130
  trace_id = str(uuid.uuid4())
131
  content_hash = hashlib.sha256(sample_trace_content.encode()).hexdigest()
132
  now = datetime.utcnow()
 
 
 
133
 
 
134
  # Insert trace
135
  conn.execute(
136
  """INSERT INTO traces (trace_id, filename, title, description, content, content_hash,
@@ -143,7 +157,9 @@ def add_sample_data_for_hf():
143
  "processed", '["demo", "customer_service", "multi_agent"]',
144
  '{"scenario": "customer_service", "agents": ["RouterAgent", "OrderAgent", "CompensationAgent", "SupervisorAgent"]}')
145
  )
 
146
 
 
147
  # Insert knowledge graph with correct field names
148
  conn.execute(
149
  """INSERT INTO knowledge_graphs (filename, entity_count, relation_count,
@@ -151,10 +167,16 @@ def add_sample_data_for_hf():
151
  VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
152
  ("demo_kg.json", 5, 4, "completed", trace_id, 0, 1, "demo_run")
153
  )
 
154
 
 
155
  # Get KG ID
156
  kg_result = conn.execute("SELECT id FROM knowledge_graphs WHERE trace_id = ?", (trace_id,))
157
- kg_id = kg_result.fetchone()[0]
 
 
 
 
158
 
159
  # Insert sample entities with correct field names
160
  entities = [
@@ -165,9 +187,11 @@ def add_sample_data_for_hf():
165
  ("issue_1", "issue", "PaymentSystemFailure", '{"severity": "high", "impact": "service_disruption"}')
166
  ]
167
 
 
168
  # Insert entities and store their database IDs
169
  entity_db_ids = {}
170
- for entity_id, entity_type, name, properties in entities:
 
171
  conn.execute(
172
  """INSERT INTO entities (graph_id, entity_id, type, name, properties)
173
  VALUES (?, ?, ?, ?, ?)""",
@@ -175,8 +199,14 @@ def add_sample_data_for_hf():
175
  )
176
  # Get the database ID for this entity
177
  result = conn.execute("SELECT id FROM entities WHERE graph_id = ? AND entity_id = ?", (kg_id, entity_id))
178
- entity_db_ids[entity_id] = result.fetchone()[0]
 
 
 
 
 
179
 
 
180
  # Insert sample relations using database IDs as foreign keys
181
  relations = [
182
  ("rel_1", "agent_1", "routes_to", "agent_2", '{"priority": "high", "success": true}'),
@@ -185,32 +215,48 @@ def add_sample_data_for_hf():
185
  ("rel_4", "agent_4", "resolves", "issue_1", '{"method": "manual_override"}')
186
  ]
187
 
188
- for relation_id, from_entity, relation_type, to_entity, properties in relations:
189
  source_db_id = entity_db_ids[from_entity]
190
  target_db_id = entity_db_ids[to_entity]
 
 
191
  conn.execute(
192
  """INSERT INTO relations (graph_id, relation_id, type, source_id, target_id, properties)
193
  VALUES (?, ?, ?, ?, ?, ?)""",
194
  (kg_id, relation_id, relation_type, source_db_id, target_db_id, properties)
195
  )
 
196
 
 
197
  # Commit transaction
198
  trans.commit()
 
199
 
 
200
  # Verify data
201
  final_traces = conn.execute("SELECT COUNT(*) FROM traces").fetchone()[0]
202
  final_kgs = conn.execute("SELECT COUNT(*) FROM knowledge_graphs").fetchone()[0]
203
  final_entities = conn.execute("SELECT COUNT(*) FROM entities").fetchone()[0]
204
  final_relations = conn.execute("SELECT COUNT(*) FROM relations").fetchone()[0]
205
 
206
- print("βœ… Sample data added successfully!")
 
207
  print(f" β€’ Traces: {final_traces}")
208
  print(f" β€’ Knowledge graphs: {final_kgs}")
209
  print(f" β€’ Entities: {final_entities}")
210
  print(f" β€’ Relations: {final_relations}")
 
 
 
 
 
 
 
211
 
212
  except Exception as e:
 
213
  trans.rollback()
 
214
  raise e
215
 
216
  except Exception as e:
@@ -218,6 +264,7 @@ def add_sample_data_for_hf():
218
  import traceback
219
  print("Full error traceback:")
220
  traceback.print_exc()
 
221
 
222
  __all__ = [
223
  'get_db',
 
81
 
82
  def add_sample_data_for_hf():
83
  """Add simple sample data for HF Spaces using direct SQL."""
84
+ print("πŸš€ add_sample_data_for_hf() started")
85
+
86
  if not os.getenv("SPACE_ID"):
87
+ print("❌ Not in HF Spaces environment, skipping sample data")
88
  return # Only run on HF Spaces
89
 
90
+ print(f"πŸ” HF Spaces environment confirmed:")
91
  print(f" β€’ SPACE_ID: {os.getenv('SPACE_ID')}")
92
  print(f" β€’ Database URL: {DATABASE_URL}")
93
+ print(f" β€’ Engine: {engine}")
94
 
95
  try:
96
+ print("πŸ”§ Creating database connection...")
97
  # Use direct connection to avoid session issues
98
  with engine.connect() as conn:
99
+ print("βœ… Database connection successful")
100
+
101
  # Begin transaction for atomic operations
102
+ print("πŸ”„ Starting transaction...")
103
  trans = conn.begin()
104
 
105
  try:
106
+ print("πŸ” Checking existing data...")
107
  # Check if data already exists
108
  result = conn.execute("SELECT COUNT(*) FROM traces").fetchone()
109
  existing_traces = result[0] if result else 0
110
+ print(f" β€’ Found {existing_traces} existing traces")
111
 
112
  result = conn.execute("SELECT COUNT(*) FROM knowledge_graphs").fetchone()
113
  existing_kgs = result[0] if result else 0
114
+ print(f" β€’ Found {existing_kgs} existing knowledge graphs")
 
 
115
 
116
  if existing_traces > 0 or existing_kgs > 0:
117
+ print("πŸ“Š Sample data already exists, skipping insertion...")
118
  return
119
 
120
+ print("🎯 No existing data found. Adding sample data using direct SQL...")
121
+ print("πŸ“ Preparing sample trace data...")
122
 
123
  import json
124
  import uuid
 
135
  {"role": "user", "content": "Thank you for the quick resolution!", "timestamp": "2024-08-31T10:04:00Z", "sentiment": "satisfied"}
136
  ]'''
137
 
138
+ print("πŸ”‘ Generating unique identifiers...")
139
  # Generate IDs
140
  trace_id = str(uuid.uuid4())
141
  content_hash = hashlib.sha256(sample_trace_content.encode()).hexdigest()
142
  now = datetime.utcnow()
143
+ print(f" β€’ Trace ID: {trace_id}")
144
+ print(f" β€’ Content hash: {content_hash[:16]}...")
145
+ print(f" β€’ Timestamp: {now}")
146
 
147
+ print("πŸ’Ύ Inserting sample trace...")
148
  # Insert trace
149
  conn.execute(
150
  """INSERT INTO traces (trace_id, filename, title, description, content, content_hash,
 
157
  "processed", '["demo", "customer_service", "multi_agent"]',
158
  '{"scenario": "customer_service", "agents": ["RouterAgent", "OrderAgent", "CompensationAgent", "SupervisorAgent"]}')
159
  )
160
+ print("βœ… Trace inserted successfully")
161
 
162
+ print("πŸ“Š Inserting knowledge graph...")
163
  # Insert knowledge graph with correct field names
164
  conn.execute(
165
  """INSERT INTO knowledge_graphs (filename, entity_count, relation_count,
 
167
  VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
168
  ("demo_kg.json", 5, 4, "completed", trace_id, 0, 1, "demo_run")
169
  )
170
+ print("βœ… Knowledge graph inserted successfully")
171
 
172
+ print("πŸ” Retrieving knowledge graph ID...")
173
  # Get KG ID
174
  kg_result = conn.execute("SELECT id FROM knowledge_graphs WHERE trace_id = ?", (trace_id,))
175
+ kg_row = kg_result.fetchone()
176
+ if not kg_row:
177
+ raise Exception("Failed to retrieve knowledge graph ID")
178
+ kg_id = kg_row[0]
179
+ print(f" β€’ Knowledge graph ID: {kg_id}")
180
 
181
  # Insert sample entities with correct field names
182
  entities = [
 
187
  ("issue_1", "issue", "PaymentSystemFailure", '{"severity": "high", "impact": "service_disruption"}')
188
  ]
189
 
190
+ print("πŸ‘₯ Inserting sample entities...")
191
  # Insert entities and store their database IDs
192
  entity_db_ids = {}
193
+ for i, (entity_id, entity_type, name, properties) in enumerate(entities, 1):
194
+ print(f" β€’ Inserting entity {i}/5: {name} ({entity_type})")
195
  conn.execute(
196
  """INSERT INTO entities (graph_id, entity_id, type, name, properties)
197
  VALUES (?, ?, ?, ?, ?)""",
 
199
  )
200
  # Get the database ID for this entity
201
  result = conn.execute("SELECT id FROM entities WHERE graph_id = ? AND entity_id = ?", (kg_id, entity_id))
202
+ row = result.fetchone()
203
+ if not row:
204
+ raise Exception(f"Failed to retrieve database ID for entity: {entity_id}")
205
+ entity_db_ids[entity_id] = row[0]
206
+ print(f" β†’ Entity DB ID: {entity_db_ids[entity_id]}")
207
+ print("βœ… All entities inserted successfully")
208
 
209
+ print("πŸ”— Inserting sample relations...")
210
  # Insert sample relations using database IDs as foreign keys
211
  relations = [
212
  ("rel_1", "agent_1", "routes_to", "agent_2", '{"priority": "high", "success": true}'),
 
215
  ("rel_4", "agent_4", "resolves", "issue_1", '{"method": "manual_override"}')
216
  ]
217
 
218
+ for i, (relation_id, from_entity, relation_type, to_entity, properties) in enumerate(relations, 1):
219
  source_db_id = entity_db_ids[from_entity]
220
  target_db_id = entity_db_ids[to_entity]
221
+ print(f" β€’ Inserting relation {i}/4: {from_entity} --{relation_type}--> {to_entity}")
222
+ print(f" β†’ Source DB ID: {source_db_id}, Target DB ID: {target_db_id}")
223
  conn.execute(
224
  """INSERT INTO relations (graph_id, relation_id, type, source_id, target_id, properties)
225
  VALUES (?, ?, ?, ?, ?, ?)""",
226
  (kg_id, relation_id, relation_type, source_db_id, target_db_id, properties)
227
  )
228
+ print("βœ… All relations inserted successfully")
229
 
230
+ print("πŸ’Ύ Committing transaction...")
231
  # Commit transaction
232
  trans.commit()
233
+ print("βœ… Transaction committed successfully")
234
 
235
+ print("πŸ” Verifying final data counts...")
236
  # Verify data
237
  final_traces = conn.execute("SELECT COUNT(*) FROM traces").fetchone()[0]
238
  final_kgs = conn.execute("SELECT COUNT(*) FROM knowledge_graphs").fetchone()[0]
239
  final_entities = conn.execute("SELECT COUNT(*) FROM entities").fetchone()[0]
240
  final_relations = conn.execute("SELECT COUNT(*) FROM relations").fetchone()[0]
241
 
242
+ print("πŸŽ‰ Sample data insertion completed successfully!")
243
+ print(f" πŸ“Š Final counts:")
244
  print(f" β€’ Traces: {final_traces}")
245
  print(f" β€’ Knowledge graphs: {final_kgs}")
246
  print(f" β€’ Entities: {final_entities}")
247
  print(f" β€’ Relations: {final_relations}")
248
+ print(f" 🎯 Expected: 1 trace, 1 KG, 5 entities, 4 relations")
249
+
250
+ # Additional verification
251
+ if final_traces == 1 and final_kgs == 1 and final_entities == 5 and final_relations == 4:
252
+ print("βœ… All counts match expected values!")
253
+ else:
254
+ print("⚠️ Counts don't match expected values")
255
 
256
  except Exception as e:
257
+ print(f"❌ Error during transaction: {e}")
258
  trans.rollback()
259
+ print("πŸ”„ Transaction rolled back")
260
  raise e
261
 
262
  except Exception as e:
 
264
  import traceback
265
  print("Full error traceback:")
266
  traceback.print_exc()
267
+ print("🚨 Sample data loading failed completely")
268
 
269
  __all__ = [
270
  'get_db',