Skip to content
Snippets Groups Projects
Commit 7feb517a authored by Anton Sarukhanov's avatar Anton Sarukhanov
Browse files

AJAX: only retrieve the latest relevant predictions, skip stale ones.

parent c30b42bb
No related branches found
No related tags found
No related merge requests found
......@@ -50,12 +50,25 @@ def ajax():
vehicle_locations = db.session.query(VehicleLocation)\
.join(VehicleLocation.route).join(Route.agency)\
.filter(Agency.tag==agency).all()
predictions = db.session.query(Prediction)\
.join(Prediction.route).join(Route.agency)\
.filter(Agency.tag==agency).all()
now = datetime.now()
p_inner = db.session.query(Prediction.vehicle, Prediction.stop_id,
db.func.max(Prediction.api_call_id).label("api_call_id"))\
.group_by(Prediction.vehicle, Prediction.stop_id)\
.subquery()
predictions = db.session.query(Prediction).join(p_inner, db.and_(
p_inner.c.api_call_id == Prediction.api_call_id,
p_inner.c.vehicle == Prediction.vehicle,
p_inner.c.stop_id == Prediction.stop_id
)).filter(
Agency.tag==agency,
Prediction.prediction >= now)\
.group_by(Prediction.id, Prediction.vehicle, Prediction.stop_id)\
.all()
z = {
"locations": {v.vehicle: v.serialize() for v in vehicle_locations},
"predictions": {p.vehicle: p.serialize() for p in predictions}
"predictions": {p.id: p.serialize() for p in predictions}
}
return z
......
......@@ -298,7 +298,7 @@ class Nextbus():
db.session.query(Prediction)\
.filter(
Prediction.route_id.in_(
[r.id for r in routes]
[routes[a,r].id for a,r in routes]
))\
.delete(synchronize_session=False)
db.session.expire_all()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment