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

Caching tweaks

parent db38cea0
No related branches found
No related tags found
No related merge requests found
......@@ -46,8 +46,8 @@ def map_embed(mode):
elif mode == "p":
return render_template('predictions.html', agency=agency, config=app.config, embed=True)
@app.route('/ajax')
@cache_for(seconds=5)
def ajax():
""" Handle all async requests (from JS). """
query = request.args.get('query')
......@@ -62,6 +62,7 @@ def ajax():
return render_template(template_name, agency=agency, config=app.config)
except TemplateNotFound:
return abort(404)
@cache_for(hours=1)
def routes():
""" Serve the route configuration (routes, stops) """
from models import Route, RouteStop, Stop
......@@ -69,11 +70,12 @@ def ajax():
.filter(Agency.tag==agency_tag).all()
stops = db.session.query(Stop).options(joinedload(Stop.routes))\
.filter(Stop.routes.any(Route.id.in_([r.id for r in routes]))).all()
return {
return jsonify({
"routes": {r.tag: r.serialize() for r in routes},
"stops": {s.id: s.serialize() for s in stops}
}
})
@cache_for(seconds=5)
def vehicles():
""" Serve the current vehicle locations and arrival predictions. """
from models import Route, VehicleLocation, Prediction
......@@ -105,11 +107,12 @@ def ajax():
Prediction.prediction >= now)\
.group_by(Prediction.id, Prediction.vehicle, Prediction.stop_id)\
.all()
return {
return jsonify({
"locations": {v.vehicle: v.serialize() for v in vehicle_locations},
"predictions": {p.id: p.serialize() for p in predictions}
}
})
@cache_for(seconds=15)
def predictions():
""" Serve arrival predictions only. """
from models import Prediction
......@@ -132,16 +135,16 @@ def ajax():
predictions = predictions\
.group_by(Prediction.id, Prediction.vehicle, Prediction.stop_id)\
.all()
return {
return jsonify({
"predictions": {p.vehicle: p.serialize() for p in predictions},
}
})
if query == "routes":
return jsonify(routes())
return routes()
elif query == "vehicles":
return jsonify(vehicles())
return vehicles()
elif query == "predictions":
return jsonify(predictions())
return predictions()
elif query == "modal":
modal_name = request.args.get('modal_name')
return modal(modal_name)
......
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