Spaces:
Sleeping
Sleeping
Update api/routes/patients.py
Browse files- api/routes/patients.py +25 -5
api/routes/patients.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
from fastapi import APIRouter, HTTPException, Depends, Query, status, Body
|
| 4 |
from db.mongo import patients_collection
|
| 5 |
from core.security import get_current_user
|
|
@@ -35,6 +33,26 @@ BASE_DIR = Path(__file__).resolve().parent.parent.parent
|
|
| 35 |
SYNTHEA_DATA_DIR = BASE_DIR / "output" / "fhir"
|
| 36 |
os.makedirs(SYNTHEA_DATA_DIR, exist_ok=True)
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
@router.post("/patients", status_code=status.HTTP_201_CREATED)
|
| 39 |
async def create_patient(
|
| 40 |
patient_data: PatientCreate,
|
|
@@ -349,7 +367,7 @@ async def list_patients(
|
|
| 349 |
skip: int = Query(0, ge=0)
|
| 350 |
):
|
| 351 |
logger.info(f"Listing patients with search: {search}, limit: {limit}, skip: {skip}")
|
| 352 |
-
query = {"source": "synthea"
|
| 353 |
|
| 354 |
if search:
|
| 355 |
query["$or"] = [
|
|
@@ -363,7 +381,6 @@ async def list_patients(
|
|
| 363 |
if min_conditions > 0:
|
| 364 |
query[f"conditions.{min_conditions-1}"] = {"$exists": True}
|
| 365 |
|
| 366 |
-
# Removed $slice to return full arrays for the frontend
|
| 367 |
projection = {
|
| 368 |
"fhir_id": 1,
|
| 369 |
"full_name": 1,
|
|
@@ -374,7 +391,8 @@ async def list_patients(
|
|
| 374 |
"conditions": 1,
|
| 375 |
"medications": 1,
|
| 376 |
"encounters": 1,
|
| 377 |
-
"notes": 1
|
|
|
|
| 378 |
}
|
| 379 |
|
| 380 |
try:
|
|
@@ -394,6 +412,7 @@ async def list_patients(
|
|
| 394 |
"medications": patient.get("medications", []),
|
| 395 |
"encounters": patient.get("encounters", []),
|
| 396 |
"notes": patient.get("notes", []),
|
|
|
|
| 397 |
"age": calculate_age(patient.get("date_of_birth")),
|
| 398 |
"stats": {
|
| 399 |
"notes": len(patient.get("notes", [])),
|
|
@@ -412,6 +431,7 @@ async def list_patients(
|
|
| 412 |
detail=f"Failed to retrieve patients: {str(e)}"
|
| 413 |
)
|
| 414 |
|
|
|
|
| 415 |
@router.get("/patients/{patient_id}", response_model=dict)
|
| 416 |
async def get_patient(patient_id: str):
|
| 417 |
logger.info(f"Retrieving patient: {patient_id}")
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import APIRouter, HTTPException, Depends, Query, status, Body
|
| 2 |
from db.mongo import patients_collection
|
| 3 |
from core.security import get_current_user
|
|
|
|
| 33 |
SYNTHEA_DATA_DIR = BASE_DIR / "output" / "fhir"
|
| 34 |
os.makedirs(SYNTHEA_DATA_DIR, exist_ok=True)
|
| 35 |
|
| 36 |
+
@router.get("/debug/count")
|
| 37 |
+
async def debug_patient_count():
|
| 38 |
+
"""Debug endpoint to verify patient counts"""
|
| 39 |
+
try:
|
| 40 |
+
total = await patients_collection.count_documents({})
|
| 41 |
+
synthea = await patients_collection.count_documents({"source": "synthea"})
|
| 42 |
+
manual = await patients_collection.count_documents({"source": "manual"})
|
| 43 |
+
return {
|
| 44 |
+
"total": total,
|
| 45 |
+
"synthea": synthea,
|
| 46 |
+
"manual": manual,
|
| 47 |
+
"message": f"Found {total} total patients ({synthea} from synthea, {manual} manual)"
|
| 48 |
+
}
|
| 49 |
+
except Exception as e:
|
| 50 |
+
logger.error(f"Error counting patients: {str(e)}")
|
| 51 |
+
raise HTTPException(
|
| 52 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 53 |
+
detail=f"Error counting patients: {str(e)}"
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
@router.post("/patients", status_code=status.HTTP_201_CREATED)
|
| 57 |
async def create_patient(
|
| 58 |
patient_data: PatientCreate,
|
|
|
|
| 367 |
skip: int = Query(0, ge=0)
|
| 368 |
):
|
| 369 |
logger.info(f"Listing patients with search: {search}, limit: {limit}, skip: {skip}")
|
| 370 |
+
query = {} # Removed the default "source": "synthea" filter
|
| 371 |
|
| 372 |
if search:
|
| 373 |
query["$or"] = [
|
|
|
|
| 381 |
if min_conditions > 0:
|
| 382 |
query[f"conditions.{min_conditions-1}"] = {"$exists": True}
|
| 383 |
|
|
|
|
| 384 |
projection = {
|
| 385 |
"fhir_id": 1,
|
| 386 |
"full_name": 1,
|
|
|
|
| 391 |
"conditions": 1,
|
| 392 |
"medications": 1,
|
| 393 |
"encounters": 1,
|
| 394 |
+
"notes": 1,
|
| 395 |
+
"source": 1 # Added source to projection
|
| 396 |
}
|
| 397 |
|
| 398 |
try:
|
|
|
|
| 412 |
"medications": patient.get("medications", []),
|
| 413 |
"encounters": patient.get("encounters", []),
|
| 414 |
"notes": patient.get("notes", []),
|
| 415 |
+
"source": patient.get("source", "unknown"),
|
| 416 |
"age": calculate_age(patient.get("date_of_birth")),
|
| 417 |
"stats": {
|
| 418 |
"notes": len(patient.get("notes", [])),
|
|
|
|
| 431 |
detail=f"Failed to retrieve patients: {str(e)}"
|
| 432 |
)
|
| 433 |
|
| 434 |
+
|
| 435 |
@router.get("/patients/{patient_id}", response_model=dict)
|
| 436 |
async def get_patient(patient_id: str):
|
| 437 |
logger.info(f"Retrieving patient: {patient_id}")
|