-
Notifications
You must be signed in to change notification settings - Fork 1
/
endpoints.py
225 lines (163 loc) · 5.98 KB
/
endpoints.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
from flask import Flask
from flask_restful import Resource, Api, reqparse, marshal_with, abort, marshal
import sqlalchemy as db
from sqlalchemy import func
from datetime import date
from sqlalchemy.orm import sessionmaker
from database.db_model import *
from common.name_map import state_map
from os import getenv
import responses
from typing import List, Dict, Optional
app = Flask(__name__)
app.config["RESTFUL_JSON"] = {
"indent": 2,
}
if getenv("FLASK_DEBUG"):
app.config["DEBUG"] = False
api = Api(app)
engine = db.create_engine("sqlite:///moh_data.sqlite")
Session = sessionmaker(bind=engine)
parser = reqparse.RequestParser()
state_parser = reqparse.RequestParser()
healthcare_parser = reqparse.RequestParser()
parser.add_argument("from_date", type=lambda x: date.fromisoformat(x))
parser.add_argument("to_date", type=lambda x: date.fromisoformat(x))
state_parser.add_argument("state_id", choices=state_map.keys(), required=True)
healthcare_parser.add_argument("facility", choices=["hospital", "icu", "quarantine"], required=True)
def standard_query(location):
output = []
args = parser.parse_args()
from_date = args.from_date
to_date = args.to_date
with Session() as s:
if location == "state":
source = State
state_args = state_parser.parse_args()
state_id = state_map[state_args.state_id]
if not state_id:
abort(400)
q = s.query(source)
q = q.filter(source.state == state_id)
elif location == "country":
source = Nation
q = s.query(source)
else:
abort(404)
if from_date:
q = q.filter(source.date >= from_date)
if to_date:
q = q.filter(source.date <= to_date)
for row in q:
output.append(row.as_dict())
return output
def get_states_aggregate(session, model, from_date: Optional[date] = None,
to_date: Optional[date] = None, ignore_keys=["date", "state"]) -> List[Dict[str, int]]:
columns = model.__table__.c
agg_func = []
for column in columns:
if column.key not in ignore_keys:
agg_func.append(func.sum(column).label(column.name))
agg_func = tuple(agg_func)
q = session.query(model.date, *agg_func).group_by(model.date)
if from_date:
q = q.filter(model.date >= from_date)
if to_date:
q = q.filter(model.date <= to_date)
out = []
for row in q.all():
out.append(row._asdict())
return out
class Cases(Resource):
def get(self, location):
output = standard_query(location)
if location == "country":
return marshal(output, responses.cases_country)
else:
return marshal(output, responses.cases_state)
class Deaths(Resource):
@marshal_with(responses.deaths)
def get(self, location):
return standard_query(location)
class VaxRegistration(Resource):
@marshal_with(responses.vax_registration)
def get(self, location):
return standard_query(location)
class Vaccination(Resource):
@marshal_with(responses.vaccination)
def get(self, location):
return standard_query(location)
class Tests(Resource):
@marshal_with(responses.tests)
def get(self):
return standard_query("country")
class Checkins(Resource):
@marshal_with(responses.checkins)
def get(self, location):
return standard_query(location)
class Traces(Resource):
@marshal_with(responses.traces)
def get(self):
return standard_query("country")
class Healthcares(Resource):
def get(self, location, facility):
output = []
args = parser.parse_args()
from_date = args.from_date
to_date = args.to_date
if facility not in ["hospital", "icu", "quarantine"]:
abort(404)
with Session() as s:
if location == "state":
source = State
state_args = state_parser.parse_args()
state_id = state_map[state_args.state_id]
if not state_id:
abort(400)
q = s.query(source)
q = q.filter(source.state == state_id)
if from_date:
q = q.filter(source.date >= from_date)
if to_date:
q = q.filter(source.date <= to_date)
for row in q:
if facility == "hospital":
row = row.facility_hospital
elif facility == "icu":
row = row.facility_icu
else:
row = row.facility_quarantine
if row is not None:
output.append(row.as_dict())
elif location == "country":
if facility == "hospital":
output = get_states_aggregate(s, Hospital, from_date, to_date)
elif facility == "icu":
output = get_states_aggregate(s, ICU, from_date, to_date)
else:
output = get_states_aggregate(s, Quarantine, from_date, to_date)
else:
abort(404)
if facility == "hospital":
return marshal(output, responses.hospital)
elif facility == "icu":
return marshal(output, responses.icu)
elif facility == "quarantine":
return marshal(output, responses.quarantine)
class TimeseriesEndpoint(Resource):
@marshal_with(responses.timeseries)
def get(self):
output = []
args = parser.parse_args()
from_date = args.from_date
to_date = args.to_date
with Session() as s:
source = Timeseries
q = s.query(source)
if from_date:
q = q.filter(source.date >= from_date)
if to_date:
q = q.filter(source.date <= to_date)
for row in q:
output.append(row.as_dict())
return output