Add graceful handling for missing db connection
Browse files- agent/backend.py +27 -4
agent/backend.py
CHANGED
|
@@ -10,10 +10,12 @@ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
|
| 10 |
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
| 11 |
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
|
|
|
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
# βββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -21,26 +23,38 @@ supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
|
|
| 21 |
# βββββββββββββββββββββββββββββββββββββββββ
|
| 22 |
|
| 23 |
def get_patient_by_name(name: str):
|
|
|
|
|
|
|
| 24 |
res = supabase.table("patients").select("*").ilike("name", f"%{name}%").execute()
|
| 25 |
return res.data if res.data else {"error": "Patient not found"}
|
| 26 |
|
| 27 |
def get_patients_by_doctor(doctor: str):
|
|
|
|
|
|
|
| 28 |
res = supabase.table("patients").select("*").ilike("doctor", f"%{doctor}%").execute()
|
| 29 |
return res.data if res.data else {"error": "No patients found for this doctor"}
|
| 30 |
|
| 31 |
def get_admitted_patients():
|
|
|
|
|
|
|
| 32 |
res = supabase.table("patients").select("*").eq("status", "admitted").execute()
|
| 33 |
return res.data if res.data else {"message": "No admitted patients currently"}
|
| 34 |
|
| 35 |
def get_appointments_by_date(date: str):
|
|
|
|
|
|
|
| 36 |
res = supabase.table("appointments").select("*").eq("date", date).execute()
|
| 37 |
return res.data if res.data else {"message": f"No appointments on {date}"}
|
| 38 |
|
| 39 |
def get_patient_appointments(patient_name: str):
|
|
|
|
|
|
|
| 40 |
res = supabase.table("appointments").select("*").ilike("patient_name", f"%{patient_name}%").execute()
|
| 41 |
return res.data if res.data else {"error": "No appointments found for this patient"}
|
| 42 |
|
| 43 |
def add_patient(name: str, age: int, gender: str, disease: str, doctor: str, admission_date: str):
|
|
|
|
|
|
|
| 44 |
data = {
|
| 45 |
"name": name,
|
| 46 |
"age": age,
|
|
@@ -54,16 +68,22 @@ def add_patient(name: str, age: int, gender: str, disease: str, doctor: str, adm
|
|
| 54 |
return {"success": True, "patient": res.data[0]} if res.data else {"error": "Failed to add patient"}
|
| 55 |
|
| 56 |
def discharge_patient(patient_id: int):
|
|
|
|
|
|
|
| 57 |
res = supabase.table("patients").update({"status": "discharged"}).eq("id", patient_id).execute()
|
| 58 |
return {"success": True, "message": f"Patient {patient_id} discharged"} if res.data else {"error": "Patient not found"}
|
| 59 |
|
| 60 |
def update_appointment_status(appointment_id: int, status: str):
|
|
|
|
|
|
|
| 61 |
if status not in ["scheduled", "completed", "cancelled"]:
|
| 62 |
return {"error": "Invalid status. Use: scheduled, completed, cancelled"}
|
| 63 |
res = supabase.table("appointments").update({"status": status}).eq("id", appointment_id).execute()
|
| 64 |
return {"success": True, "message": f"Appointment {appointment_id} updated to {status}"} if res.data else {"error": "Appointment not found"}
|
| 65 |
|
| 66 |
def get_disease_statistics():
|
|
|
|
|
|
|
| 67 |
res = supabase.table("patients").select("disease").execute()
|
| 68 |
if not res.data:
|
| 69 |
return {"message": "No data available"}
|
|
@@ -227,6 +247,9 @@ Format responses cleanly with bullet points for multiple records.
|
|
| 227 |
"""
|
| 228 |
|
| 229 |
async def process_agent_chat(user_message: str):
|
|
|
|
|
|
|
|
|
|
| 230 |
headers = {
|
| 231 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 232 |
"Content-Type": "application/json"
|
|
|
|
| 10 |
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
| 11 |
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
|
| 12 |
|
| 13 |
+
supabase = None
|
| 14 |
+
if all([GROQ_API_KEY, SUPABASE_URL, SUPABASE_KEY]):
|
| 15 |
+
try:
|
| 16 |
+
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 17 |
+
except Exception as e:
|
| 18 |
+
print(f"WARNING: Could not initialize Supabase: {e}")
|
| 19 |
|
| 20 |
|
| 21 |
# βββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 23 |
# βββββββββββββββββββββββββββββββββββββββββ
|
| 24 |
|
| 25 |
def get_patient_by_name(name: str):
|
| 26 |
+
if not supabase:
|
| 27 |
+
return {"error": "Database not connected. Check SUPABASE_URL and SUPABASE_KEY."}
|
| 28 |
res = supabase.table("patients").select("*").ilike("name", f"%{name}%").execute()
|
| 29 |
return res.data if res.data else {"error": "Patient not found"}
|
| 30 |
|
| 31 |
def get_patients_by_doctor(doctor: str):
|
| 32 |
+
if not supabase:
|
| 33 |
+
return {"error": "Database not connected. Check SUPABASE_URL and SUPABASE_KEY."}
|
| 34 |
res = supabase.table("patients").select("*").ilike("doctor", f"%{doctor}%").execute()
|
| 35 |
return res.data if res.data else {"error": "No patients found for this doctor"}
|
| 36 |
|
| 37 |
def get_admitted_patients():
|
| 38 |
+
if not supabase:
|
| 39 |
+
return {"error": "Database not connected. Check SUPABASE_URL and SUPABASE_KEY."}
|
| 40 |
res = supabase.table("patients").select("*").eq("status", "admitted").execute()
|
| 41 |
return res.data if res.data else {"message": "No admitted patients currently"}
|
| 42 |
|
| 43 |
def get_appointments_by_date(date: str):
|
| 44 |
+
if not supabase:
|
| 45 |
+
return {"error": "Database not connected. Check SUPABASE_URL and SUPABASE_KEY."}
|
| 46 |
res = supabase.table("appointments").select("*").eq("date", date).execute()
|
| 47 |
return res.data if res.data else {"message": f"No appointments on {date}"}
|
| 48 |
|
| 49 |
def get_patient_appointments(patient_name: str):
|
| 50 |
+
if not supabase:
|
| 51 |
+
return {"error": "Database not connected. Check SUPABASE_URL and SUPABASE_KEY."}
|
| 52 |
res = supabase.table("appointments").select("*").ilike("patient_name", f"%{patient_name}%").execute()
|
| 53 |
return res.data if res.data else {"error": "No appointments found for this patient"}
|
| 54 |
|
| 55 |
def add_patient(name: str, age: int, gender: str, disease: str, doctor: str, admission_date: str):
|
| 56 |
+
if not supabase:
|
| 57 |
+
return {"error": "Database not connected. Check SUPABASE_URL and SUPABASE_KEY."}
|
| 58 |
data = {
|
| 59 |
"name": name,
|
| 60 |
"age": age,
|
|
|
|
| 68 |
return {"success": True, "patient": res.data[0]} if res.data else {"error": "Failed to add patient"}
|
| 69 |
|
| 70 |
def discharge_patient(patient_id: int):
|
| 71 |
+
if not supabase:
|
| 72 |
+
return {"error": "Database not connected. Check SUPABASE_URL and SUPABASE_KEY."}
|
| 73 |
res = supabase.table("patients").update({"status": "discharged"}).eq("id", patient_id).execute()
|
| 74 |
return {"success": True, "message": f"Patient {patient_id} discharged"} if res.data else {"error": "Patient not found"}
|
| 75 |
|
| 76 |
def update_appointment_status(appointment_id: int, status: str):
|
| 77 |
+
if not supabase:
|
| 78 |
+
return {"error": "Database not connected. Check SUPABASE_URL and SUPABASE_KEY."}
|
| 79 |
if status not in ["scheduled", "completed", "cancelled"]:
|
| 80 |
return {"error": "Invalid status. Use: scheduled, completed, cancelled"}
|
| 81 |
res = supabase.table("appointments").update({"status": status}).eq("id", appointment_id).execute()
|
| 82 |
return {"success": True, "message": f"Appointment {appointment_id} updated to {status}"} if res.data else {"error": "Appointment not found"}
|
| 83 |
|
| 84 |
def get_disease_statistics():
|
| 85 |
+
if not supabase:
|
| 86 |
+
return {"error": "Database not connected. Check SUPABASE_URL and SUPABASE_KEY."}
|
| 87 |
res = supabase.table("patients").select("disease").execute()
|
| 88 |
if not res.data:
|
| 89 |
return {"message": "No data available"}
|
|
|
|
| 247 |
"""
|
| 248 |
|
| 249 |
async def process_agent_chat(user_message: str):
|
| 250 |
+
if not GROQ_API_KEY:
|
| 251 |
+
return {"response": "Error: GROQ_API_KEY not configured. Please set it in Space Variables."}
|
| 252 |
+
|
| 253 |
headers = {
|
| 254 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 255 |
"Content-Type": "application/json"
|