File size: 1,863 Bytes
f7b331f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sqlite3
import os
from datetime import datetime

DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'patient_history.db')

def init_db():
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    cursor.execute('''

        CREATE TABLE IF NOT EXISTS predictions (

            id INTEGER PRIMARY KEY AUTOINCREMENT,

            timestamp DATETIME,

            heart_rate REAL,

            spo2 REAL,

            sys_bp REAL,

            dia_bp REAL,

            temp REAL,

            fall_detection TEXT,

            prediction_label TEXT,

            confidence REAL

        )

    ''')
    conn.commit()
    conn.close()

def log_prediction(data, prediction_label, confidence):
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    cursor.execute('''

        INSERT INTO predictions (timestamp, heart_rate, spo2, sys_bp, dia_bp, temp, fall_detection, prediction_label, confidence)

        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)

    ''', (
        datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        data.Heart_Rate,
        data.SpO2_Level,
        data.Systolic_BP,
        data.Diastolic_BP,
        data.Body_Temp,
        data.Fall_Detection,
        prediction_label,
        confidence
    ))
    conn.commit()
    conn.close()

def get_history():
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    cursor.execute('SELECT * FROM predictions ORDER BY timestamp DESC LIMIT 100')
    rows = cursor.fetchall()
    
    # Get column names
    col_names = [description[0] for description in cursor.description]
    
    conn.close()
    
    # Format as list of dicts
    history = []
    for row in rows:
        history.append(dict(zip(col_names, row)))
        
    return history

# Initialize db when this module is loaded
init_db()