#!/usr/bin/env python3 import argparse from flask import abort, Flask, g, jsonify, render_template, request # from gevent import pywsgi, monkey # from geventwebsocket.handler import WebSocketHandler # from queue import Queue import adc2019system webui = Flask(__name__) @webui.route('/') def webui_index(): # return adc2019system.role.role return render_template('index.html') @webui.route('/viewer') def webui_viewer(): return render_template('viewer.html') @webui.route('/part/problems') def webui_part_problems(): if (adc2019system.role is not None) and (adc2019system.role.type == 'host'): problems = adc2019system.role.get_problems() return render_template('part_problems.html', problems=problems) else: return abort(404) @webui.route('/part/system-summary') def webui_parte_workers(): if (adc2019system.role is not None) and (adc2019system.role.type == 'host'): workers = adc2019system.role.get_workers() return render_template('part_system_summary.html', workers=workers) else: return abort(404) @webui.route('/part/solver-status') def webui_part_solver_status(): if (adc2019system.role is not None) and (adc2019system.role.type == 'host'): workers = adc2019system.role.get_workers() return render_template('part_system_summary.html', workers=workers) else: return abort(404) @webui.route('/part/request/') def webui_part_request_status(problem_name=None): if (adc2019system.role is not None) and (adc2019system.role.type == 'host'): request_status = adc2019system.role.get_request_by_problem(problem_name) return render_template('part_request_status.html', status=request_status) else: return abort(404) @webui.route('/part/problem/') def webui_part_problem_status(name=None): if name is None: return abort(404) if (adc2019system.role is not None) and (adc2019system.role.type == 'host'): problem = adc2019system.role.get_problem(name) workers = adc2019system.role.get_workers() # solutions = reversed(sorted(problem.get_solutions().items(), key=lambda x:x[1].timestamp)) if problem is None: return abort(404) else: # return render_template('part_problem_status.html', problem=problem, workers=workers, solutions=solutions) return render_template('part_problem_status.html', problem=problem, workers=workers) else: return abort(404) @webui.route('/part/problem_solution/') def webui_part_solution_status(name=None): if name is None: return abort(404) if (adc2019system.role is not None) and (adc2019system.role.type == 'host'): problem = adc2019system.role.get_problem(name) solutions = reversed(sorted(problem.get_solutions().items(), key=lambda x:x[1].timestamp)) if problem is None: return abort(404) else: return render_template('part_solution_status.html', problem=problem, solutions=solutions) else: return abort(404) @webui.route('/api/', methods=['GET', 'POST']) def webui_api(cmd=None): if cmd is None: return jsonify({'version': 'v1.0', 'app': 'adc2019system'}) if request.method == 'POST': if request.headers['Content-Type'] != 'application/json': return abort(400) else: params = request.json else: params = None res = adc2019system.call_api(request.method, cmd, params) if res is None: return abort(404) else: return jsonify(res) # @webui.route('/ws') # def webui_ws(): # pass def init_system(args): adc2019system.init(args) if __name__ == "__main__": parser = argparse.ArgumentParser(description='ADC2019 System') parser.add_argument('-p', '--port', default=5000, type=int, action='store', help='Web UI and API port') parser.add_argument('-c', '--config', default=None, type=str, action='store', help='Config file') parser.add_argument('--debug', default=False, action='store_true', help='Debug mode') args = vars(parser.parse_args()) init_system(args) if args["debug"]: webui.debug = True webui.threaded = True webui.run(host='0.0.0.0', port=args['port']) # server = pywsgi.WSGIServer(("", args['port']), webui, handler_class=WebSocketHandler) # server.serve_forever()