-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
233 lines (181 loc) · 6.3 KB
/
app.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
226
227
228
229
230
231
232
233
"""API routes and request resolvers for a Bottle app."""
from typing import Dict, Any
import os
import sys
from datetime import date
from kedro.framework.session import KedroSession
from bottle import Bottle, run, request, response
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__)))
SRC_PATH = os.path.join(BASE_DIR, "src")
if SRC_PATH not in sys.path:
sys.path.append(SRC_PATH)
from augury import api
from augury import settings
IS_PRODUCTION = settings.ENV == "production"
TIPRESIAS_HOST = (
"http://www.tipresias.net" if IS_PRODUCTION else "http://host.docker.internal:8000"
)
PACKAGE_NAME = "augury"
app = Bottle()
if IS_PRODUCTION:
from rollbar.contrib.bottle import RollbarBottleReporter
rbr = RollbarBottleReporter(
access_token=settings.ROLLBAR_TOKEN,
environment=settings.ENV,
)
app.install(rbr)
def _run_kwargs():
run_kwargs: Dict[str, Any] = {
"port": int(os.getenv("PORT", "8008")),
"reloader": not IS_PRODUCTION,
"host": "0.0.0.0",
"server": "gunicorn",
"accesslog": "-",
"timeout": 1200,
"workers": 1,
}
return run_kwargs
def _unauthorized_response():
response.status = 401
return "Unauthorized"
def _request_is_authorized(http_request) -> bool:
auth_token = http_request.headers.get("Authorization")
if (
IS_PRODUCTION
and auth_token != f"Bearer {os.environ['DATA_SCIENCE_SERVICE_TOKEN']}"
):
return False
return True
@app.route("/predictions")
def predictions():
"""
Generate predictions for the given year and round number.
Params
------
Request with the following URL params:
year_range (str, optional): Year range for which you want prediction data.
Format = yyyy-yyyy.
Default = current year only.
round_number (int, optional): Round number for which you want prediction data.
Default = All rounds for given year.
ml_models (str, optional): Comma-separated list of names of ML model to use
for making predictions.
Default = All available models.
train_models (bool, optional): Whether to train each model
on earlier seasons' data before generating predictions
for a given season/round.
Default = False.
Returns
-------
Response with a body that has a JSON of prediction data.
"""
if not _request_is_authorized(request):
return _unauthorized_response()
this_year = date.today().year
year_range_param = (
f"{this_year}-{this_year + 1}"
if request.query.year_range in [None, ""]
else request.query.year_range
)
year_range = tuple([int(year) for year in year_range_param.split("-")])
round_number = request.query.round_number
round_number = None if round_number in [None, ""] else int(round_number)
ml_models_param = request.query.ml_models
ml_models_param = (
None if ml_models_param in [None, ""] else ml_models_param.split(",")
)
train_models_param = request.query.train_models
train_models = train_models_param.lower() == "true"
with KedroSession.create(
settings.PACKAGE_NAME,
env=settings.ENV,
project_path=settings.BASE_DIR,
extra_params={"round_number": round_number},
):
return api.make_predictions(
year_range,
round_number=round_number,
ml_model_names=ml_models_param,
train=train_models,
)
@app.route("/fixtures")
def fixtures():
"""
Fetch fixture data for the given date range.
Params
------
Request with the following URL params:
start_date (string of form 'yyyy-mm-dd', required): Start of date range
(inclusive) for which you want data.
end_date (string of form 'yyyy-mm-dd', required): End of date range
(inclusive) for which you want data.
Returns
-------
Response with a body that has a JSON of fixture data.
"""
if not _request_is_authorized(request):
return _unauthorized_response()
start_date = request.query.start_date
end_date = request.query.end_date
with KedroSession.create(
settings.PACKAGE_NAME, env=settings.ENV, project_path=settings.BASE_DIR
):
return api.fetch_fixture_data(start_date, end_date)
@app.route("/match_results")
def match_results():
"""
Fetch match results data for the given round.
Params
------
Request with the following URL params:
round_number (int): Fetch data for the given round. If missing, will fetch
all match results for the current year.
Returns
-------
Response with a body that has a JSON of match results data.
"""
if not _request_is_authorized(request):
return _unauthorized_response()
round_number = request.query.round_number
with KedroSession.create(
settings.PACKAGE_NAME, env=settings.ENV, project_path=settings.BASE_DIR
):
return api.fetch_match_results_data(round_number)
@app.route("/matches")
def matches():
"""
Fetch match data for the given date range.
Params
------
Request with the following URL params:
start_date (string of form 'yyyy-mm-dd', required): Start of date range
(inclusive) for which you want data.
end_date (string of form 'yyyy-mm-dd', required): End of date range
(inclusive) for which you want data.
Returns
-------
Response with a body that has a JSON of match data.
"""
if not _request_is_authorized(request):
return _unauthorized_response()
start_date = request.query.start_date
end_date = request.query.end_date
with KedroSession.create(
settings.PACKAGE_NAME, env=settings.ENV, project_path=settings.BASE_DIR
):
return api.fetch_match_data(start_date, end_date)
@app.route("/ml_models")
def ml_models():
"""
Fetch info for all available ML models.
Returns
-------
Response with a body that has a JSON of ML model data.
"""
if not _request_is_authorized(request):
return _unauthorized_response()
with KedroSession.create(
settings.PACKAGE_NAME, env=settings.ENV, project_path=settings.BASE_DIR
):
return api.fetch_ml_model_info()
run(app, **_run_kwargs())