main.py 4.01 KB
Newer Older
Kento HASEGAWA's avatar
Kento HASEGAWA committed
1
#!/usr/bin/env python3
Kento HASEGAWA's avatar
Kento HASEGAWA committed
2 3 4 5

import argparse

from flask import abort, Flask, g, jsonify, render_template, request
Kento HASEGAWA's avatar
Kento HASEGAWA committed
6 7
# from gevent import pywsgi, monkey
# from geventwebsocket.handler import WebSocketHandler
Kento HASEGAWA's avatar
Kento HASEGAWA committed
8
# from queue import Queue
Kento HASEGAWA's avatar
Kento HASEGAWA committed
9 10 11 12 13 14 15 16 17 18

import adc2019system

webui = Flask(__name__)

@webui.route('/')
def webui_index():
    # return adc2019system.role.role
    return render_template('index.html')

19 20 21
@webui.route('/viewer')
def webui_viewer():
    return render_template('viewer.html')
Kento HASEGAWA's avatar
Kento HASEGAWA committed
22

23 24
@webui.route('/part/problems')
def webui_part_problems():
Kento HASEGAWA's avatar
Kento HASEGAWA committed
25
    if (adc2019system.role is not None) and (adc2019system.role.type == 'host'):
26 27
        problems = adc2019system.role.get_problems()
        return render_template('part_problems.html', problems=problems)
Kento HASEGAWA's avatar
Kento HASEGAWA committed
28 29 30
    else:
        return abort(404)

31 32
@webui.route('/part/system-summary')
def webui_parte_workers():
33 34 35 36 37 38
    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)

39 40 41 42 43 44 45 46
@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)

47 48 49
# @webui.route('/part/request/<problem_name>')
@webui.route('/part/problem_status/<problem_name>')
def webui_part_problem_status_list(problem_name=None):
50
    if (adc2019system.role is not None) and (adc2019system.role.type == 'host'):
51 52
        problem = adc2019system.role.get_problem(problem_name)
        solutions = reversed(sorted(problem.get_solutions().items(), key=lambda x:x[1].timestamp))
53
        request_status = adc2019system.role.get_request_by_problem(problem_name)
54 55 56 57
        if problem is None:
            return abort(404)
        else:
            return render_template('part_problem_status_list.html', problem=problem, solutions=solutions, status=request_status)
58 59 60
    else:
        return abort(404)

61
@webui.route('/part/problem/<name>')
62
def webui_part_problem_status(name=None):
Kento HASEGAWA's avatar
Kento HASEGAWA committed
63 64 65 66 67

    if name is None:
        return abort(404)

    if (adc2019system.role is not None) and (adc2019system.role.type == 'host'):
68
        problem = adc2019system.role.get_problem(name)
69
        workers = adc2019system.role.get_workers()
70 71 72 73 74 75 76
        if problem is None:
            return abort(404)
        else:
            return render_template('part_problem_status.html', problem=problem, workers=workers)
    else:
        return abort(404)

Kento HASEGAWA's avatar
Kento HASEGAWA committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
@webui.route('/api/<path:cmd>', 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)

Kento HASEGAWA's avatar
Kento HASEGAWA committed
98 99 100
# @webui.route('/ws')
# def webui_ws():
#     pass
Kento HASEGAWA's avatar
Kento HASEGAWA committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

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
Kento HASEGAWA's avatar
Kento HASEGAWA committed
120 121 122
    webui.run(host='0.0.0.0', port=args['port'])
    # server = pywsgi.WSGIServer(("", args['port']), webui, handler_class=WebSocketHandler)
    # server.serve_forever()
Kento HASEGAWA's avatar
Kento HASEGAWA committed
123