Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use flask.jsonify for json responses #725

Merged
merged 1 commit into from
Jan 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions locust/static/locust.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ var responseTimeChart = new LocustLineChart($(".charts-container"), "Response Ti
var usersChart = new LocustLineChart($(".charts-container"), "Number of Users", ["Users"], "users");

function updateStats() {
$.get('/stats/requests', function (data) {
report = JSON.parse(data);
$.get('/stats/requests', function (report) {
$("#total_rps").html(Math.round(report.total_rps*100)/100);
//$("#fail_ratio").html(Math.round(report.fail_ratio*10000)/100);
$("#fail_ratio").html(Math.round(report.fail_ratio*100));
Expand Down
24 changes: 9 additions & 15 deletions locust/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from time import time

import six
from flask import Flask, make_response, render_template, request
from flask import Flask, make_response, jsonify, render_template, request
from gevent import pywsgi

from locust import __version__ as version
Expand Down Expand Up @@ -59,16 +59,12 @@ def swarm():
locust_count = int(request.form["locust_count"])
hatch_rate = float(request.form["hatch_rate"])
runners.locust_runner.start_hatching(locust_count, hatch_rate)
response = make_response(json.dumps({'success':True, 'message': 'Swarming started'}))
response.headers["Content-type"] = "application/json"
return response
return jsonify({'success': True, 'message': 'Swarming started'})

@app.route('/stop')
def stop():
runners.locust_runner.stop()
response = make_response(json.dumps({'success':True, 'message': 'Test stopped'}))
response.headers["Content-type"] = "application/json"
return response
return jsonify({'success':True, 'message': 'Test stopped'})

@app.route("/stats/reset")
def reset_stats():
Expand Down Expand Up @@ -135,22 +131,20 @@ def request_stats():
report["state"] = runners.locust_runner.state
report["user_count"] = runners.locust_runner.user_count

return json.dumps(report)
return jsonify(report)

@app.route("/exceptions")
def exceptions():
response = make_response(json.dumps({
return jsonify({
'exceptions': [
{
"count": row["count"],
"msg": row["msg"],
"traceback": row["traceback"],
"count": row["count"],
"msg": row["msg"],
"traceback": row["traceback"],
"nodes" : ", ".join(row["nodes"])
} for row in six.itervalues(runners.locust_runner.exceptions)
]
}))
response.headers["Content-type"] = "application/json"
return response
})

@app.route("/exceptions/csv")
def exceptions_csv():
Expand Down