Shreyass334 commited on
Commit
98bf4ff
Β·
verified Β·
1 Parent(s): 3bbe322

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -26
app.py CHANGED
@@ -189,26 +189,51 @@ def query_database(question, dashboard_mode=False, chart_type=None):
189
  def check_health():
190
  try:
191
  print("Checking API health...")
 
 
192
  response = requests.get(f"{FLASK_API_URL}/health", timeout=10)
193
 
194
  print(f"Health check response status: {response.status_code}")
195
- print(f"Health check response: {response.text}")
 
196
 
197
  if response.status_code == 200:
198
- health_data = response.json()
199
- status = health_data.get('status', 'unknown')
200
- tables = health_data.get('tables', [])
201
- model = health_data.get('model', 'unknown')
202
- data_rows = health_data.get('data_rows', 0)
203
-
204
- health_msg = f"βœ… API Status: {status.upper()}\n"
205
- health_msg += f"πŸ€– Model: {model}\n"
206
- health_msg += f"πŸ“Š Tables: {', '.join(tables) if tables else 'None'}\n"
207
- health_msg += f"πŸ“ˆ Data Rows: {data_rows:,}"
208
-
209
- return health_msg, "success"
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  else:
211
  return f"❌ API returned status {response.status_code}\nResponse: {response.text}", "error"
 
 
 
 
 
 
 
 
 
212
  except Exception as e:
213
  error_msg = f"Health check failed: {str(e)}"
214
  print(f"HEALTH CHECK ERROR: {error_msg}")
@@ -218,26 +243,44 @@ def check_health():
218
  def get_schema():
219
  try:
220
  print("Fetching database schema...")
 
 
221
  response = requests.get(f"{FLASK_API_URL}/tables", timeout=10)
222
 
223
  print(f"Schema response status: {response.status_code}")
224
- print(f"Schema response: {response.text}")
225
 
226
  if response.status_code == 200:
227
- tables_data = response.json()
228
- tables = tables_data.get("tables", [])
229
-
230
- schema_text = "## Database Schema\n\n"
231
- for table in tables:
232
- schema_text += f"### {table['name']}\n"
233
- schema_text += "| Column |\n|--------|\n"
234
- for col in table['columns']:
235
- schema_text += f"| {col} |\n"
236
- schema_text += "\n"
237
-
238
- return schema_text, "success"
 
 
 
 
 
 
 
239
  else:
240
  return f"❌ API returned status {response.status_code}\nResponse: {response.text}", "error"
 
 
 
 
 
 
 
 
 
241
  except Exception as e:
242
  error_msg = f"Failed to fetch schema: {str(e)}"
243
  print(f"SCHEMA FETCH ERROR: {error_msg}")
 
189
  def check_health():
190
  try:
191
  print("Checking API health...")
192
+ print(f"API URL: {FLASK_API_URL}/health")
193
+
194
  response = requests.get(f"{FLASK_API_URL}/health", timeout=10)
195
 
196
  print(f"Health check response status: {response.status_code}")
197
+ print(f"Health check response headers: {dict(response.headers)}")
198
+ print(f"Health check response text: {response.text}")
199
 
200
  if response.status_code == 200:
201
+ try:
202
+ health_data = response.json()
203
+ print(f"Parsed health data: {health_data}")
204
+
205
+ status = health_data.get('status', 'unknown')
206
+ tables = health_data.get('tables', [])
207
+ model = health_data.get('model', 'unknown')
208
+ data_rows = health_data.get('data_rows', 0)
209
+
210
+ # Ensure tables is a list before joining
211
+ if isinstance(tables, list):
212
+ tables_str = ', '.join(tables) if tables else 'None'
213
+ else:
214
+ tables_str = str(tables)
215
+
216
+ health_msg = f"βœ… API Status: {status.upper()}\n"
217
+ health_msg += f"πŸ€– Model: {model}\n"
218
+ health_msg += f"πŸ“Š Tables: {tables_str}\n"
219
+ health_msg += f"πŸ“ˆ Data Rows: {data_rows:,}"
220
+
221
+ return health_msg, "success"
222
+ except json.JSONDecodeError as e:
223
+ error_msg = f"Failed to parse health check response: {str(e)}"
224
+ print(f"JSON PARSE ERROR: {error_msg}")
225
+ return f"❌ {error_msg}", "error"
226
  else:
227
  return f"❌ API returned status {response.status_code}\nResponse: {response.text}", "error"
228
+ except requests.exceptions.ConnectionError as e:
229
+ error_msg = f"Connection to API failed: {str(e)}"
230
+ print(f"CONNECTION ERROR: {error_msg}")
231
+ print(f"Traceback: {traceback.format_exc()}")
232
+ return f"❌ {error_msg}", "error"
233
+ except requests.exceptions.Timeout:
234
+ error_msg = "Health check request timed out"
235
+ print(f"TIMEOUT ERROR: {error_msg}")
236
+ return f"❌ {error_msg}", "error"
237
  except Exception as e:
238
  error_msg = f"Health check failed: {str(e)}"
239
  print(f"HEALTH CHECK ERROR: {error_msg}")
 
243
  def get_schema():
244
  try:
245
  print("Fetching database schema...")
246
+ print(f"API URL: {FLASK_API_URL}/tables")
247
+
248
  response = requests.get(f"{FLASK_API_URL}/tables", timeout=10)
249
 
250
  print(f"Schema response status: {response.status_code}")
251
+ print(f"Schema response text: {response.text}")
252
 
253
  if response.status_code == 200:
254
+ try:
255
+ tables_data = response.json()
256
+ print(f"Parsed tables data: {tables_data}")
257
+
258
+ tables = tables_data.get("tables", [])
259
+
260
+ schema_text = "## Database Schema\n\n"
261
+ for table in tables:
262
+ schema_text += f"### {table.get('name', 'Unknown')}\n"
263
+ schema_text += "| Column |\n|--------|\n"
264
+ for col in table.get('columns', []):
265
+ schema_text += f"| {col} |\n"
266
+ schema_text += "\n"
267
+
268
+ return schema_text, "success"
269
+ except json.JSONDecodeError as e:
270
+ error_msg = f"Failed to parse schema response: {str(e)}"
271
+ print(f"JSON PARSE ERROR: {error_msg}")
272
+ return f"❌ {error_msg}", "error"
273
  else:
274
  return f"❌ API returned status {response.status_code}\nResponse: {response.text}", "error"
275
+ except requests.exceptions.ConnectionError as e:
276
+ error_msg = f"Connection to API failed: {str(e)}"
277
+ print(f"CONNECTION ERROR: {error_msg}")
278
+ print(f"Traceback: {traceback.format_exc()}")
279
+ return f"❌ {error_msg}", "error"
280
+ except requests.exceptions.Timeout:
281
+ error_msg = "Schema request timed out"
282
+ print(f"TIMEOUT ERROR: {error_msg}")
283
+ return f"❌ {error_msg}", "error"
284
  except Exception as e:
285
  error_msg = f"Failed to fetch schema: {str(e)}"
286
  print(f"SCHEMA FETCH ERROR: {error_msg}")