File size: 2,006 Bytes
b383602
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# app/database.py

import os
from motor.motor_asyncio import AsyncIOMotorClient
from dotenv import load_dotenv
import datetime
from typing import List, Dict

# Load environment variables from .env file
load_dotenv()

# --- Database Connection ---
# Get the connection string from the environment variables
MONGODB_URL = os.getenv("MONGODB_CONNECTION_STRING")

if not MONGODB_URL:
    raise ValueError("MONGODB_CONNECTION_STRING not found in environment variables. Please check your .env file.")

# Create a client instance
client = AsyncIOMotorClient(MONGODB_URL)

# Get a handle to the database (it will be created if it doesn't exist)
# The database name 'pneumonia_db' should match the one in your connection string
database = client.pneumonia_db

# Get a handle to the collection (like a table in SQL)
patient_collection = database.get_collection("patient_records")


# --- Database Operations (now async) ---

async def add_patient_record(name: str, age: int, result: str, confidence: float) -> Dict:
    """
    Inserts a new patient record into the MongoDB collection.
    
    Returns the inserted document.
    """
    record_document = {
        "name": name,
        "age": age,
        "prediction_result": result,
        "confidence_score": confidence,
        "timestamp": datetime.datetime.utcnow()
    }
    
    # .insert_one is an async operation, so we must 'await' it
    result = await patient_collection.insert_one(record_document)
    
    # Find the newly created document to return it
    new_record = await patient_collection.find_one({"_id": result.inserted_id})
    return new_record


async def get_all_records() -> List[Dict]:
    """
    Retrieves all patient records, sorted by the most recent timestamp.
    """
    records = []
    # .find() returns a cursor, which we iterate over asynchronously
    cursor = patient_collection.find({}).sort("timestamp", -1) # -1 for descending order
    async for document in cursor:
        records.append(document)
    return records