File size: 4,859 Bytes
1150b5c
 
 
 
 
0e1a6eb
1150b5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff37931
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1150b5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff37931
 
 
 
 
 
 
1150b5c
 
 
 
 
0e1a6eb
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import os
import random
import statistics
from typing import Dict, Optional

from fastapi import BackgroundTasks, Body, FastAPI, Response, UploadFile, status

from schema import *
from telegram_bot import checkup_messages, send_message_to_user

app = FastAPI()
tasks: Dict[str, Optional[Dict]] = {}


class DB:
    def __init__(self):
        self.care_plans = {
            "123": CarePlan(
                purpose="Diabetes",
                start="2021-01-01",
                end="2021-12-31",
                items=[
                    CarePlanItem(
                        name="Insulin",
                        description="Insulin to treat diabetes",
                        quantity=1.0,
                        units="ml",
                        frequency="daily",
                        start="2021-01-01",
                        adherence=0.9,
                    ),
                    CarePlanItem(
                        name="Metformin",
                        description="Metformin to treat diabetes",
                        quantity=1.0,
                        units="tablet",
                        frequency="daily",
                        start="2021-01-01",
                        adherence=0.8,
                    ),
                ],
            )
        }
        self.issues = {
            "123": [
                PatientIssue(
                    title="Patient does not engage in physical activity",
                    reason="Reports feeling tired and fatigued",
                    suggested_actions=[
                        "Discuss reason with physician",
                        "Suggest lighter activities",
                    ],
                ),
                PatientIssue(
                    title="Patient is not taking metformin",
                    reason="Reports feeling nauseous",
                    suggested_actions=[
                        "Discuss reason with physician",
                        "Suggest taking metformin with food",
                    ],
                ),
            ]
        }
        self.adherence_history = {
            "123": AdherenceHistory(
                history=[
                    AdherenceHistoryRecord(date="2021-01-01", adherence=0.9),
                    AdherenceHistoryRecord(date="2021-01-02", adherence=0.85),
                    AdherenceHistoryRecord(date="2021-01-03", adherence=0.87),
                    AdherenceHistoryRecord(date="2021-01-04", adherence=0.79),
                    AdherenceHistoryRecord(date="2021-01-05", adherence=0.77),
                    AdherenceHistoryRecord(date="2021-01-06", adherence=0.8),
                    AdherenceHistoryRecord(date="2021-01-07", adherence=0.75),
                    AdherenceHistoryRecord(date="2021-01-08", adherence=0.78),
                    AdherenceHistoryRecord(date="2021-01-09", adherence=0.80),
                    AdherenceHistoryRecord(date="2021-01-10", adherence=0.85),
                ]
            )
        }


db = DB()


@app.post("/echo")
async def echo(msg: str):
    return msg


@app.post("/trigger-medication-check")
async def trigger_medication_check(background_tasks: BackgroundTasks):
    background_tasks.add_task(
        send_message_to_user,
        chat_id=os.getenv("TELEGRAM_USER_ID", ""),
        message=random.choice(checkup_messages),
    )
    return Response(status_code=status.HTTP_202_ACCEPTED)


@app.get("/{patient_id}/care_plan", response_model=CarePlan)
async def get_patient_care_plan(patient_id: str) -> CarePlan | Response:
    if patient_id not in db.care_plans:
        return Response(status_code=status.HTTP_404_NOT_FOUND)
    return db.care_plans[patient_id]


@app.get("/{patient_id}/adherence_status", response_model=AdherenceStatus)
async def get_patient_adherence_status(patient_id: str) -> AdherenceStatus | Response:
    if patient_id not in db.care_plans:
        return Response(status_code=status.HTTP_404_NOT_FOUND)
    return AdherenceStatus(
        adherence=statistics.mean(
            [item.adherence for item in db.care_plans[patient_id].items]
        )
    )


@app.get("/{patient_id}/adherence_history", response_model=AdherenceHistory)
async def get_patient_adherence_history(patient_id: str) -> Response:
    if patient_id not in db.adherence_history:
        return Response(status_code=status.HTTP_404_NOT_FOUND)
    return db.adherence_history[patient_id]


@app.get("/{patient_id}/issues", response_model=list[PatientIssue])
async def get_patient_issues(patient_id: str) -> list[PatientIssue] | Response:
    if patient_id not in db.issues:
        return Response(status_code=status.HTTP_404_NOT_FOUND)
    return db.issues[patient_id]


@app.post("/log-message-response")
async def log_message_response(msg: dict = Body(...)):
    print(msg)
    return Response(status_code=status.HTTP_202_ACCEPTED)