Ali2206 commited on
Commit
e3305a8
·
verified ·
1 Parent(s): 94662cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -1
app.py CHANGED
@@ -12,7 +12,8 @@ from fastapi.responses import StreamingResponse
12
  from fastapi.middleware.cors import CORSMiddleware
13
  from pydantic import BaseModel
14
  import asyncio
15
-
 
16
  from txagent.txagent import TxAgent
17
  from db.mongo import get_mongo_client
18
 
@@ -262,6 +263,40 @@ async def status():
262
  "version": "2.2.0"
263
  }
264
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
265
  @app.post("/chat-stream")
266
  async def chat_stream_endpoint(request: ChatRequest):
267
  async def token_stream():
 
12
  from fastapi.middleware.cors import CORSMiddleware
13
  from pydantic import BaseModel
14
  import asyncio
15
+ from fastapi import Query
16
+ from bson import ObjectId
17
  from txagent.txagent import TxAgent
18
  from db.mongo import get_mongo_client
19
 
 
263
  "version": "2.2.0"
264
  }
265
 
266
+ @app.get("/patients/analysis-results")
267
+ async def get_patient_analysis_results(name: Optional[str] = Query(None)):
268
+ try:
269
+ query = {}
270
+
271
+ # If a name filter is provided, we search the patients collection first
272
+ if name:
273
+ name_regex = re.compile(name, re.IGNORECASE)
274
+ matching_patients = await patients_collection.find({"full_name": name_regex}).to_list(length=None)
275
+ patient_ids = [p["fhir_id"] for p in matching_patients if "fhir_id" in p]
276
+ if not patient_ids:
277
+ return []
278
+ query = {"patient_id": {"$in": patient_ids}}
279
+
280
+ # Find analysis results based on patient_ids (or all if no filter)
281
+ analyses = await analysis_collection.find(query).sort("timestamp", -1).to_list(length=100)
282
+
283
+ # Attach full_name to each analysis result
284
+ enriched_results = []
285
+ for analysis in analyses:
286
+ patient = await patients_collection.find_one({"fhir_id": analysis["patient_id"]})
287
+ if patient:
288
+ analysis["full_name"] = patient.get("full_name", "Unknown")
289
+ analysis["_id"] = str(analysis["_id"])
290
+ enriched_results.append(analysis)
291
+
292
+ return enriched_results
293
+
294
+ except Exception as e:
295
+ logger.error(f"Error fetching analysis results: {e}")
296
+ raise HTTPException(status_code=500, detail="Failed to retrieve analysis results")
297
+
298
+
299
+
300
  @app.post("/chat-stream")
301
  async def chat_stream_endpoint(request: ChatRequest):
302
  async def token_stream():