5
5
@author: Gašper Rugelj
6
6
"""
7
7
8
+ from collections import defaultdict as dd
8
9
from datetime import datetime
9
10
import json
11
+ import logging
10
12
from threading import Lock
11
13
12
14
import flask
18
20
YEARS = ('2017' , '2018' , '2019' , '2020' )
19
21
20
22
@app .route ('/get_results/<year>' )
21
- def result_page (year , * args , ** kwargs ):
23
+ def result_page (year , * args , duration = False , ** kwargs ):
22
24
if lock .acquire (timeout = 10 ):
23
- if (datetime .now () - cache [year ]['ts' ]).seconds < LIMIT :
24
- response = cache [year ]['data' ]
25
- print ( 'Too soon, using cache' )
25
+ if (datetime .now () - cache [duration ][ year ]['ts' ]).seconds < LIMIT :
26
+ response = cache [duration ][ year ]['data' ]
27
+ logging . info ( 'Refresh too soon, using cache' )
26
28
else :
27
- total = results .get_results (year = year , convert_ts = True )
29
+ total = results .get_results (year = year ,
30
+ convert_ts = True ,
31
+ duration = duration )
32
+
28
33
if total is None : # something failed
29
- print ('Failed retrieving data, using cache' )
30
- response = cache [year ]['data' ]
34
+ logging .error ('Failed retrieving data, using cache' )
35
+ response = cache [duration ][year ]['data' ]
36
+
31
37
else :
32
38
data = total .to_dict (orient = 'records' )
33
39
columns = list (total .columns )
34
40
ts = datetime .now ()
35
41
response = json .dumps (dict (data = data ,
36
42
columns = columns ,
37
43
ts = ts .strftime ('%Y-%m-%d %H:%M' )))
38
- response = response .replace ('NaN' , 'null' ).replace ('NaT' , '' )
39
- cache [year ]['data' ] = response
40
- cache [year ]['ts' ] = ts
44
+ response = response .replace ('NaN' , 'null' )
45
+ response = response .replace ('NaT' , '' )
46
+ cache [duration ][year ]['data' ] = response
47
+ cache [duration ][year ]['ts' ] = ts
48
+
41
49
lock .release ()
50
+
42
51
else :
43
- print ("Can't acquire lock" )
44
- response = cache [year ]['data' ]
52
+ logging . warning ("Can't acquire lock, returning cached response " )
53
+ response = cache [duration ][ year ]['data' ]
45
54
return response
46
55
56
+
57
+ @app .route ('/get_duration/<year>' )
58
+ def duration (year , * args , ** kwargs ):
59
+ return result_page (year , duration = True )
60
+
61
+
47
62
@app .route ('/' )
48
63
@app .route ('/<year>' )
49
- def index (year = '2020' ):
64
+ def index (year = '2020' , duration = False ):
50
65
if str (year ) not in YEARS :
51
66
year = '2019'
52
67
name = 'AoC {}' .format (year )
53
- ts = cache [year ]['ts' ]
54
- return flask .render_template ('template.html' , name = name , year = year , ts = ts )
68
+ ts = cache [duration ][year ]['ts' ]
69
+ prefix = 'get_duration/' if duration else 'get_results/'
70
+ return flask .render_template ('template.html' ,
71
+ name = name ,
72
+ year = year ,
73
+ ts = ts ,
74
+ prefix = prefix )
75
+
76
+
77
+ @app .route ('/duration' )
78
+ @app .route ('/duration/<year>' )
79
+ def index_duration (year = '2020' ):
80
+ return index (year , duration = True )
81
+
55
82
56
83
if __name__ == '__main__' :
57
84
lock = Lock ()
58
- cache = { year : dict (ts = datetime .min , data = None ) for year in YEARS }
85
+ cache = dd ( lambda : dd ( lambda : dict (ts = datetime .min , data = None )))
59
86
app .run (port = 5008 , host = '0.0.0.0' , debug = True )
0 commit comments