From 20935966efdae847023680217a4c109648698340 Mon Sep 17 00:00:00 2001 From: Javier Sánchez Parra Date: Thu, 29 Sep 2022 17:21:00 +0200 Subject: WIP Make scopes tree async --- ogcp/static/js/ogcp.js | 12 ++++++++++++ ogcp/templates/macros.html | 7 +++---- ogcp/templates/scopes.html | 2 +- ogcp/templates/tree.html | 2 ++ ogcp/views.py | 42 ++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 ogcp/templates/tree.html diff --git a/ogcp/static/js/ogcp.js b/ogcp/static/js/ogcp.js index 61d828d..d267e5b 100644 --- a/ogcp/static/js/ogcp.js +++ b/ogcp/static/js/ogcp.js @@ -3,6 +3,18 @@ const macs = new Map(); const Interval = 1000; let updateTimeoutId = null; +async function buildScopesTree() { + const resp = await fetch('/ogservers'); + const servers = await resp.json(); + const scopes = document.getElementById("scopes") + + for (const s of servers) { + const resp = await fetch('/scopes-tree?server=' + s); + const htmlText = await resp.text(); + scopes.innerHTML += htmlText; + } +} + async function show_client_mac(pill_id) { const pill = $('#' +pill_id); const ip = pill.html().split('
')[1] diff --git a/ogcp/templates/macros.html b/ogcp/templates/macros.html index c56e4e8..e114692 100644 --- a/ogcp/templates/macros.html +++ b/ogcp/templates/macros.html @@ -1,8 +1,6 @@ -{% macro scopes_tree_collapse(scopes, state='', selection_mode='scopes') -%} +{% macro scopes_tree_collapse(state='', selection_mode='scopes') -%} - + diff --git a/ogcp/templates/scopes.html b/ogcp/templates/scopes.html index d07e7d3..db2b742 100644 --- a/ogcp/templates/scopes.html +++ b/ogcp/templates/scopes.html @@ -12,7 +12,7 @@ {% endblock %} {% block sidebar %} - {{ macros.scopes_tree_collapse(scopes, sidebar_state, 'scopes') }} + {{ macros.scopes_tree_collapse(sidebar_state, 'scopes') }} {% endblock %} {% block commands %} diff --git a/ogcp/templates/tree.html b/ogcp/templates/tree.html new file mode 100644 index 0000000..fc06105 --- /dev/null +++ b/ogcp/templates/tree.html @@ -0,0 +1,2 @@ +{% import "macros.html" as macros %} +{{ macros.scopes_tree_collapse_level(scopes["scope"], '', '', '', 'scopes') }} diff --git a/ogcp/views.py b/ogcp/views.py index 0a65c17..5be8647 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -237,6 +237,27 @@ def get_server_from_ip_port(str_ip_port): raise Exception('Server with address ' + str_ip_port + 'is not configured') +def get_scopes_from_server(server): + r = server.get('/scopes') + server_scope = {} + server_scope['name'] = server.name + server_scope['type'] = "server" + server_scope['server_ip_port'] = (server.ip + ":" + str(server.port)) + server_scope.update(r.json()) + list_scopes = [] + list_scopes.append(server_scope) + scopes = {'scope': list_scopes} + if current_user.scopes: + allowed_scopes = [] + get_allowed_scopes(scopes, allowed_scopes) + scopes = {'scope': allowed_scopes} + r = server.get('/clients') + clients = r.json() + add_state_and_ips(scopes, clients['clients'], set()) + + return scopes + + def get_scopes(ips=set()): list_scopes = [] responses = multi_request('get', '/scopes') @@ -410,11 +431,28 @@ def get_client_mac(): return jsonify(pretty_mac) +@app.route('/ogservers', methods=['GET']) +@login_required +def get_ogservers(): + servers_list = [s.ip + ':' + str(s.port) for s in servers] + resp = jsonify(servers_list) + return resp + + +@app.route('/scopes-tree', methods=['GET']) +@login_required +def scopes_tree(): + params = request.args.to_dict() + server = get_server_from_ip_port(params['server']) + scopes = get_scopes_from_server(server) + return render_template('tree.html', scopes=scopes) + + @app.route('/scopes/') @login_required def scopes(): - scopes, clients = get_scopes() - return render_template('scopes.html', scopes=scopes, clients=clients) + return render_template('scopes.html') + @app.route('/action/poweroff', methods=['GET', 'POST']) @login_required -- cgit v1.2.3-18-g5258