Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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.
|
|
|
|
| 196 |
|
| 197 |
if response.status_code == 200:
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
schema_text
|
| 234 |
-
for
|
| 235 |
-
schema_text += f"
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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}")
|