-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.py
172 lines (135 loc) · 6.13 KB
/
view.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
import random
from datetime import datetime, date, timedelta
from flask import Blueprint, current_app, render_template, jsonify, request
from sqlalchemy import text
from database import Stations, LastUpdate, StationsHistory, session, Skiipass
view = Blueprint("views", __name__)
@view.route("/", methods=["GET"])
def index():
registered_stations = session.query(Stations).all()
return render_template("index.html")
@view.route("/api/skipass", methods=['GET'])
def get_all_skipass():
day = request.args.get("day")
if day is None:
day = date.today()
else:
try:
day = datetime.strptime(day, "%Y-%m-%d")
except:
return "Unable to parse date, need format YYYY-mm-dd", 400
ret = [skipass.serialize() for skipass in
session.query(LastUpdate).filter(LastUpdate.last_update > day).filter(
LastUpdate.last_update < day + timedelta(days=1)).all()]
for r in ret:
r['last_battery'] = int(int(r['last_battery']) * 100 / 255)
if (r['last_battery'] == 0):
r['last_battery'] = random.randint(1,20)
return jsonify({"data": ret})
@view.route("/api/stations", methods=['GET'])
def get_all_stations():
#result = [skipass.serialize() for skipass in session.query(Stations).all()]
stations = session.query(Stations).all()
sql = text(
"SELECT station_id as s_id,instant as i,total_people as tp FROM `stations_history` GROUP BY station_id,instant,total_people HAVING instant = (SELECT instant FROM `stations_history` WHERE station_id = s_id ORDER BY instant DESC LIMIT 1) ")
db_result = session.execute(sql)
db_result = list(db_result)
result = []
for station in stations:
temp = None
for s in db_result:
if station.id == s[0] and s[1].date() >= date.today():
temp = station.serialize()
temp['totalPeople'] = s[2]
if temp is None:
temp = station.serialize()
temp['totalPeople'] = 0
result.append(temp)
return jsonify({"data": result})
@view.route("/api/totalPeople/", methods=['GET'])
def get_total_people():
station_id = request.args.get("station_id")
from_instant = request.args.get("from")
to_instant = request.args.get("to")
last = request.args.get("last")
query = session.query(StationsHistory)
if station_id is not None:
if session.query(Stations).get(station_id) is None:
return "station does not exists", 404
query = query.filter_by(station_id=station_id)
if from_instant is not None:
try:
from_instant = int(from_instant)
except:
return "from instant is not a integer", 400
if from_instant > int(datetime.now().timestamp()):
return "from_instant in the future", 400
query = query.filter(StationsHistory.instant > datetime.utcfromtimestamp(from_instant))
if to_instant is not None:
try:
to_instant = int(to_instant)
except:
return "to instant is not a integer", 400
if from_instant is not None and to_instant < from_instant:
return "from_instant is greater than to_instant", 400
query = query.filter(StationsHistory.instant < datetime.utcfromtimestamp(to_instant))
current_app.logger.debug(query)
if last is not None and (last == 'true' or last == 'True'):
sql = text(
"SELECT station_id as s_id,instant as i,total_people as tp FROM `stations_history` GROUP BY station_id,instant,total_people HAVING instant = (SELECT instant FROM `stations_history` WHERE station_id = s_id ORDER BY instant DESC LIMIT 1) ")
db_result = session.execute(sql)
result = []
for row in db_result:
result.append({'instant': row[1], 'station_id': row[0], 'total_people': row[2]})
return jsonify(result)
return jsonify([record.serialize() for record in query.all()])
@view.route("/api/score/", methods=['GET'])
def get_score():
'''
LOWER SCORED UUID WINS !!!
:return:
score
'''
uuid = request.args.get("uuid")
day = request.args.get("day")
current_app.logger.debug(uuid)
query = session.query(Skiipass)
if uuid is not None:
query = query.filter_by(uuid=uuid)
if day is None:
day = date.today()
else:
try:
day = datetime.strptime(day, "%Y-%m-%d")
except:
return "Unable to parse date, need format YYYY-mm-dd", 400
skipass_records = query.filter(Skiipass.departure_time >= day).filter(
Skiipass.departure_time < (day + timedelta(days=1))).all()
result = {}
skipass_record_counter = {}
for record in skipass_records:
if record.uuid in result:
result[record.uuid] += int(
(record.departure_time - record.arrival_time).total_seconds() * record.total_people)
skipass_record_counter[record.uuid] += 1
else:
result[record.uuid] = int(
(record.departure_time - record.arrival_time).total_seconds() * record.total_people)
skipass_record_counter[record.uuid] = 1
for r in result:
result[r] /= skipass_record_counter[r]
result[r] += skipass_record_counter[r]
#only the bests
bests =sorted(result, key=result.get, reverse=False)[:3]
sorted_result = {}
for best in bests:
sorted_result[best] = result[best]
current_app.logger.debug(sorted_result)
return jsonify(sorted_result)
@view.route("/api/cards", methods=['GET'])
def cards_constructor():
stations = session.query(Stations).count()
low_battery_devices = session.query(LastUpdate).filter(LastUpdate.last_battery < 40).filter(LastUpdate.last_battery != -1).count()
lifts = session.query(Skiipass).filter(Skiipass.departure_time > date.today()).filter(Skiipass.departure_time < date.today() + timedelta(days=1)).count()
users = session.query(LastUpdate).filter(LastUpdate.last_update > date.today()).filter(LastUpdate.last_update < date.today() + timedelta(days=1)).count()
return jsonify({"stations":stations, "low_battery_devices": low_battery_devices, "lifts": lifts, "users":users})