Commit 51414a36 authored by Kento HASEGAWA's avatar Kento HASEGAWA

Use web socket to update status

parent 63c7cf71
...@@ -17,6 +17,8 @@ import threading ...@@ -17,6 +17,8 @@ import threading
import time import time
from collections import OrderedDict from collections import OrderedDict
from flask import Flask, render_template, request, g from flask import Flask, render_template, request, g
from gevent import pywsgi, monkey
from geventwebsocket.handler import WebSocketHandler
from queue import Queue from queue import Queue
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../../solver') sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../../solver')
...@@ -25,10 +27,12 @@ import BoardStr ...@@ -25,10 +27,12 @@ import BoardStr
import adcclilib import adcclilib
app = Flask(__name__) app = Flask(__name__)
monkey.patch_all()
app_args = {} app_args = {}
questions = None questions = None
clients = None clients = None
current_seed = 1 current_seed = 1
wsq = None
def load_questions(): def load_questions():
global app_args global app_args
...@@ -58,10 +62,10 @@ def load_questions(): ...@@ -58,10 +62,10 @@ def load_questions():
"path": v, "path": v,
"status": "Not solved", "status": "Not solved",
"answer": "", "answer": "",
"queue": Queue(),
"board_size": board_size, "board_size": board_size,
"line_num": line_num, "line_num": line_num,
"last_req": None, "last_req": None,
"solver": OrderedDict(),
"answers": OrderedDict(), "answers": OrderedDict(),
} }
...@@ -79,7 +83,7 @@ def load_questions(): ...@@ -79,7 +83,7 @@ def load_questions():
with open(v, "r") as fp: with open(v, "r") as fp:
_ans_dat = fp.read() _ans_dat = fp.read()
try: try:
questions[_name]["status"] = "Solved" questions[_name]["status"] = "Saved"
questions[_name]["answer"] = _ans_dat questions[_name]["answer"] = _ans_dat
except KeyError as e: except KeyError as e:
pass pass
...@@ -88,9 +92,14 @@ def update_answer_list(qname): ...@@ -88,9 +92,14 @@ def update_answer_list(qname):
global app_args global app_args
global questions global questions
global clients
questions[qname]['solver'] = OrderedDict()
questions[qname]['answers'] = OrderedDict() questions[qname]['answers'] = OrderedDict()
for c in clients['solver']:
questions[qname]['solver'][c[0]] = "-"
_folder_name = os.path.splitext(qname)[0] _folder_name = os.path.splitext(qname)[0]
_answers_path = "{}/{}".format(app_args['out'], _folder_name) _answers_path = "{}/{}".format(app_args['out'], _folder_name)
if os.path.isdir(_answers_path): if os.path.isdir(_answers_path):
...@@ -98,6 +107,8 @@ def update_answer_list(qname): ...@@ -98,6 +107,8 @@ def update_answer_list(qname):
answer_log_file.sort() answer_log_file.sort()
answer_log_file.reverse() answer_log_file.reverse()
is_solved = False
for v2 in answer_log_file: for v2 in answer_log_file:
json_name = os.path.basename(v2) json_name = os.path.basename(v2)
with open(v2, "r") as fp: with open(v2, "r") as fp:
...@@ -107,6 +118,17 @@ def update_answer_list(qname): ...@@ -107,6 +118,17 @@ def update_answer_list(qname):
if questions[qname]['answer'] == answer_log['answer']: if questions[qname]['answer'] == answer_log['answer']:
questions[qname]['best_json'] = json_name questions[qname]['best_json'] = json_name
if (questions[qname]['last_req'] == float(answer_log['req_id'])):
if answer_log['solver'] in questions[qname]['solver']:
if answer_log['solved'] == "True":
questions[qname]['solver'][answer_log['solver']] = "Solved"
is_solved = True
else:
questions[qname]['solver'][answer_log['solver']] = "DNS"
if is_solved > 0:
questions[qname]['status'] = "Solved"
@app.before_request @app.before_request
def before_request(): def before_request():
global app_args global app_args
...@@ -118,10 +140,13 @@ def post(): ...@@ -118,10 +140,13 @@ def post():
global questions global questions
global app_args global app_args
global wsq
_qname = request.form["qname"] _qname = request.form["qname"]
_answer = request.form["answer"] _answer = request.form["solution"]
_client = request.form["client"] _client = request.form["client"]
_req_id = request.form["req_id"]
_is_solved = request.form["solved"]
if "solver" in request.form: if "solver" in request.form:
_solver = request.form["solver"] _solver = request.form["solver"]
...@@ -132,45 +157,58 @@ def post(): ...@@ -132,45 +157,58 @@ def post():
receive_time_str = receive_time.strftime("%Y%m%d%H%M%S") receive_time_str = receive_time.strftime("%Y%m%d%H%M%S")
dat = { dat = {
"req_id": request.form['req_id'], "req_id": _req_id,
"qname": _qname,
"client": _client, "client": _client,
"solver": _solver, "solver": _solver,
"answer": _answer, "answer": _answer,
"cputime": request.form['cputime'], "cputime": request.form['cputime'],
"timestamp": receive_time.strftime("%Y/%m/%d %H:%M:%S") "timestamp": receive_time.strftime("%Y/%m/%d %H:%M:%S"),
"nlcheck": -1,
"solved": _is_solved,
} }
# Create a temporary file to execute "nlcheck.py" if _is_solved == "True":
tmp_path = "{}/{}".format(app_args['out'], "tmp") # Create a temporary file to execute "nlcheck.py"
if not os.path.isdir(tmp_path): tmp_path = "{}/{}".format(app_args['out'], "tmp")
os.makedirs(tmp_path) if not os.path.isdir(tmp_path):
tmp_file_name = "{}-{}.tmp".format(receive_time_str, _client.replace(":", ".")) os.makedirs(tmp_path)
tmp_file_path = "{}/{}".format(tmp_path, tmp_file_name) tmp_file_name = "{}-{}.tmp".format(receive_time_str, _client.replace(":", "."))
with open(tmp_file_path, "w") as fp: tmp_file_path = "{}/{}".format(tmp_path, tmp_file_name)
fp.write(_answer) with open(tmp_file_path, "w") as fp:
probpath = "{}/{}".format(app_args["question"], _qname) fp.write(_answer)
probpath = "{}/{}".format(app_args["question"], _qname)
# Format check
cmd = "/usr/bin/python /home/pi/adc2018/conmgr/adc2018/server/nlcheck.py --input {} --target {}".format(probpath, tmp_file_path) # Format check
print("`{}`".format(cmd)) cmd = "/usr/bin/python /home/pi/adc2018/conmgr/adc2018/server/nlcheck.py --input {} --target {}".format(probpath, tmp_file_path)
p = subprocess.run(cmd.strip().split(" "), stdout=subprocess.PIPE) print("`{}`".format(cmd))
nlcheck = p.stdout.decode().strip() p = subprocess.run(cmd.strip().split(" "), stdout=subprocess.PIPE)
nlcheck = p.stdout.decode().strip()
pattern = r"judge += +\[(.+?), ([0-9.]+)\]"
m = re.match(pattern, nlcheck) pattern = r"judge += +\[(.+?), ([0-9.]+)\]"
if m and (m.group(1) == "True"): m = re.match(pattern, nlcheck)
dat["nlcheck"] = float(m.group(2)) if m and (m.group(1) == "True"):
dat["nlcheck"] = float(m.group(2))
save_path = "{}/{}".format(app_args['out'], os.path.splitext(_qname)[0])
if not os.path.isdir(save_path):
os.makedirs(save_path)
file_name = "{}-{}.json".format(receive_time_str, _solver.replace(":", "."))
save_file_path = "{}/{}".format(save_path, file_name)
with open(save_file_path, "w") as fp:
json.dump(dat, fp, indent=4)
else: else:
dat["nlcheck"] = -1 save_path = "{}/{}".format(app_args['out'], os.path.splitext(_qname)[0])
if not os.path.isdir(save_path):
os.makedirs(save_path)
save_path = "{}/{}".format(app_args['out'], os.path.splitext(_qname)[0]) file_name = "{}-{}.json".format(receive_time_str, _solver.replace(":", "."))
if not os.path.isdir(save_path): save_file_path = "{}/{}".format(save_path, file_name)
os.makedirs(save_path) with open(save_file_path, "w") as fp:
json.dump(dat, fp, indent=4)
file_name = "{}-{}.json".format(receive_time_str, _solver.replace(":", ".")) wsq.put(dat)
save_file_path = "{}/{}".format(save_path, file_name)
with open(save_file_path, "w") as fp:
json.dump(dat, fp, indent=4)
res = {"status": "OK"} res = {"status": "OK"}
...@@ -317,6 +355,7 @@ def save_best_solution(): ...@@ -317,6 +355,7 @@ def save_best_solution():
with open(out_file_path, 'w') as fp: with open(out_file_path, 'w') as fp:
fp.write(questions[qname]['answers'][_best_json]['answer']) fp.write(questions[qname]['answers'][_best_json]['answer'])
questions[_name]["status"] = "Saved"
questions[qname]['best_json'] = _best_json questions[qname]['best_json'] = _best_json
res = {"status": "OK"} res = {"status": "OK"}
...@@ -549,6 +588,22 @@ def question_status(): ...@@ -549,6 +588,22 @@ def question_status():
return render_template("part_question_status.html", qname=qname, qdata=qdata, solvers=clients["solver"]) return render_template("part_question_status.html", qname=qname, qdata=qdata, solvers=clients["solver"])
@app.route('/ws')
def ws():
global wsq
if request.environ.get('wsgi.websocket'):
ws = request.environ['wsgi.websocket']
while True:
if wsq is None:
time.sleep(1)
continue
else:
data = wsq.get()
str_data = json.dumps(data)
ws.send(str_data)
@app.route("/") @app.route("/")
def index(): def index():
...@@ -567,9 +622,7 @@ def init_system(): ...@@ -567,9 +622,7 @@ def init_system():
global app_args global app_args
global questions global questions
global clients global clients
global wsq
if questions is None:
load_questions()
if clients is None: if clients is None:
...@@ -591,6 +644,12 @@ def init_system(): ...@@ -591,6 +644,12 @@ def init_system():
"solver": solver, "solver": solver,
"resolver": resolver "resolver": resolver
} }
if questions is None:
load_questions()
if wsq is None:
wsq = Queue()
if __name__ == "__main__": if __name__ == "__main__":
...@@ -607,5 +666,9 @@ if __name__ == "__main__": ...@@ -607,5 +666,9 @@ if __name__ == "__main__":
if args["debug"]: if args["debug"]:
app.debug = True app.debug = True
app.run(host='0.0.0.0', threaded=True)
app.threaded = True
# app.run(host='0.0.0.0', threaded=True)
server = pywsgi.WSGIServer(("", 5000), app, handler_class=WebSocketHandler)
server.serve_forever()
...@@ -63,7 +63,6 @@ var PynqManager = (function(){ ...@@ -63,7 +63,6 @@ var PynqManager = (function(){
}).done(function(res){ }).done(function(res){
var answer = res; var answer = res;
$("#solving-question-status").text(answer["status"]); $("#solving-question-status").text(answer["status"]);
$("#solved-client").text(answer["answer"]["client"]);
if (after !== null){ if (after !== null){
after(); after();
...@@ -110,6 +109,7 @@ $(function(){ ...@@ -110,6 +109,7 @@ $(function(){
var pynqClients = {} var pynqClients = {}
var pm = null; var pm = null;
var last_req = 0;
var refresh_question_table = function(){ var refresh_question_table = function(){
$.ajax({ $.ajax({
...@@ -179,7 +179,7 @@ $(function(){ ...@@ -179,7 +179,7 @@ $(function(){
$("#adccli-status").text("ログアウト"); $("#adccli-status").text("ログアウト");
}); });
pm.getStatus(); if(pm) pm.getStatus();
}); });
} }
...@@ -195,7 +195,7 @@ $(function(){ ...@@ -195,7 +195,7 @@ $(function(){
$("#client-control-pane").find(".start-button").eq(0).click(function(){ $("#client-control-pane").find(".start-button").eq(0).click(function(){
var qname = $(this).data("qname"); var qname = $(this).data("qname");
pm.sendQuestion(qname, after=refresh_question_table); pm.sendQuestion(qname);
}); });
$("#client-control-pane").find(".stop-button").eq(0).click(function(){ $("#client-control-pane").find(".stop-button").eq(0).click(function(){
pm.sendStop(); pm.sendStop();
...@@ -241,6 +241,19 @@ $(function(){ ...@@ -241,6 +241,19 @@ $(function(){
} }
}).trigger('hashchange'); }).trigger('hashchange');
var ws = new WebSocket("ws://" + location.host + "/ws");
ws.onmessage = function(e){
var recv = JSON.parse(e.data.replace(/\n/g, "\\n"));
console.log(recv);
var hash = location.hash.replace("#", "");
if(hash == recv['qname']){
show_question_status(hash);
}
refresh_question_table();
};
refresh_question_table(); refresh_question_table();
$.ajax({ $.ajax({
...@@ -250,6 +263,10 @@ $(function(){ ...@@ -250,6 +263,10 @@ $(function(){
}).done(function(d){ }).done(function(d){
pynqClients = d; pynqClients = d;
pm = PynqManager(pynqClients); pm = PynqManager(pynqClients);
if(location.hash.replace("#", "") == ""){
pm.getStatus();
}
}); });
}); });
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
 ({{c[2]}})  ({{c[2]}})
{% endif %} {% endif %}
</td> </td>
<td></td> <td>{{qdata.solver[c[0]]}}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</table> </table>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment