Spaces:
Sleeping
Sleeping
| import certifi | |
| from motor.motor_asyncio import AsyncIOMotorClient | |
| from core.config import MONGO_URI | |
| # Create MongoDB client with TLS certificate | |
| client = AsyncIOMotorClient(MONGO_URI, tls=True, tlsCAFile=certifi.where()) | |
| # Access main database | |
| db = client["cps_db"] | |
| # Collections | |
| users_collection = db.users | |
| patients_collection = db.patients | |
| appointments_collection = db.appointments | |
| messages_collection = db.messages | |
| password_reset_codes_collection = db.password_reset_codes | |
| # TxAgent Collections | |
| patient_analysis_results_collection = db.patient_analysis_results | |
| chats_collection = db.chats | |
| clinical_alerts_collection = db.clinical_alerts | |
| notifications_collection = db.notifications | |
| # Create indexes for better duplicate detection | |
| async def create_indexes(): | |
| """Create database indexes for better performance and duplicate detection""" | |
| try: | |
| # Index for EHR patients | |
| await patients_collection.create_index([ | |
| ("ehr_id", 1), | |
| ("ehr_system", 1) | |
| ], unique=True, sparse=True) | |
| # Index for HAPI FHIR patients | |
| await patients_collection.create_index([ | |
| ("fhir_id", 1) | |
| ], unique=True, sparse=True) | |
| # Index for demographics.fhir_id | |
| await patients_collection.create_index([ | |
| ("demographics.fhir_id", 1) | |
| ], unique=True, sparse=True) | |
| # Index for name and date of birth combination | |
| await patients_collection.create_index([ | |
| ("full_name", 1), | |
| ("date_of_birth", 1) | |
| ]) | |
| # Index for national_id | |
| await patients_collection.create_index([ | |
| ("national_id", 1) | |
| ], unique=True, sparse=True) | |
| # Index for source field | |
| await patients_collection.create_index([ | |
| ("source", 1) | |
| ]) | |
| # TxAgent indexes | |
| await patient_analysis_results_collection.create_index([ | |
| ("patient_id", 1), | |
| ("timestamp", -1) | |
| ]) | |
| await chats_collection.create_index([ | |
| ("user_id", 1), | |
| ("timestamp", -1) | |
| ]) | |
| await clinical_alerts_collection.create_index([ | |
| ("patient_id", 1), | |
| ("timestamp", -1) | |
| ]) | |
| await notifications_collection.create_index([ | |
| ("user_id", 1), | |
| ("timestamp", -1) | |
| ]) | |
| print("Database indexes created successfully") | |
| except Exception as e: | |
| print(f"Error creating indexes: {e}") | |
| # Continue without indexes if there's an error | |