-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.py
113 lines (91 loc) · 2.82 KB
/
web.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
"""Web interface"""
from datetime import datetime, timedelta
from flask import Flask, jsonify, request, g
from werkzeug.exceptions import BadRequest
from flask_cors import CORS
import database
import settings
from utils import get_datestring
from frontend import frontend
# define static_url_path to avoid conflict w/ blueprint static
app = Flask(__name__, static_url_path='/static/core')
app.debug = settings.DEBUG
app.register_blueprint(frontend)
CORS(app)
def connect_db():
"""Connects to the specific database."""
return database.get()
def get_db():
"""Opens a new database connection if there is none yet for the
current application context.
"""
if not hasattr(g, 'sqlite_db'):
g.sqlite_db = connect_db()
return g.sqlite_db
def get_since(default_days_ago=1):
"""Get since from request or provide default date"""
since = request.args.get('since')
if since:
try:
since_date = datetime.fromtimestamp(int(since))
except ValueError:
raise BadRequest()
elif default_days_ago:
since_date = datetime.now() - timedelta(days=default_days_ago)
else:
return None
return get_datestring(since_date)
@app.route('/api')
def api():
"""API root (list last results)"""
table = get_db()['results']
since_date = get_since()
aller = table.find(
table.table.columns.date > since_date,
from_gare=settings.FROM_STATION_CODE,
order_by='-date'
)
retour = table.find(
table.table.columns.date > since_date,
from_gare=settings.TO_STATION_CODE,
order_by='-date'
)
return jsonify(
aller=[r for r in aller],
retour=[r for r in retour],
infos={
'from_station': settings.FROM_STATION_LABEL,
'to_station': settings.TO_STATION_LABEL
}
)
@app.route('/api/aggregate/<frequency>')
def api_aggregate(frequency):
"""Aggregate trains by type on given frequency"""
if frequency == 'hour':
group_by = 'substr(date, 0, 15)'
elif frequency == 'day':
group_by = 'substr(date, 0, 11)'
elif frequency == 'month':
group_by = 'substr(date, 0, 8)'
elif frequency == 'year':
group_by = 'substr(date, 0, 5)'
elif frequency == 'hour_overall':
group_by = 'substr(date, 12, 2)'
elif frequency == 'weekday':
group_by = 'weekday'
else:
raise BadRequest()
since = get_since(default_days_ago=None)
res = get_db().query("""
SELECT
%(group_by)s as date, type, COUNT(*) as count
FROM results
%(since)s
GROUP BY %(group_by)s, type;
""" % {
'group_by': group_by,
'since': "WHERE date > '%s'" % since if since else ''
})
return jsonify([r for r in res])
if __name__ == '__main__':
app.run(host='0.0.0.0')