File size: 2,608 Bytes
682caaf
 
 
 
 
 
 
 
 
 
 
76201c6
 
 
 
 
682caaf
b515e8c
 
 
 
 
 
682caaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b515e8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
682caaf
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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