diff --git a/config.json b/config.json index 115a3a3257b496507128724429e0b5aae4f8a097..223ae63e2fc024cdef0875d88658181b99898a7f 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,6 @@ { "db":{ - "host": "a73434.berlin.ptb.de", + "host": "localhost", "port": 5984 }, "server": { diff --git a/repl/__pycache__/utils.cpython-37.pyc b/repl/__pycache__/utils.cpython-37.pyc index 9dd270656dc36dfef34dc785782cf866ab1e5755..b4804addba1d2a5ee5e15b0cfc3d8306a45c44d0 100644 Binary files a/repl/__pycache__/utils.cpython-37.pyc and b/repl/__pycache__/utils.cpython-37.pyc differ diff --git a/repl/utils.py b/repl/utils.py index 756bef9942474aec92b334b43a547224749cf1df..0a284b6a0e8b3911293ab67fb1212e255dbf1bc7 100644 --- a/repl/utils.py +++ b/repl/utils.py @@ -1,5 +1,7 @@ import json import datetime +import requests +from urllib.parse import urlparse def get_config_dict(): ## sollte vielleicht doch xml-datei werden @@ -21,3 +23,61 @@ def get_current_year(short=False): def get_current_date(short=False): return "{}".format(datetime.datetime.today().date()) + +def get_jobs(server, port): + try: + req = requests.get("http://{}:{}/_scheduler/jobs".format(server, port)) + return req.json().get("jobs", []) + except: + return [] + +def gen_count(): + i = 0 + while True: + yield i + i = i + 1 + +def get_info(url): + p = urlparse(url) + db = p.path.replace("/","") + host = p.hostname.split(".")[0] + return db, host + +def gen_ext_name(d, h): + return "{}@{}".format(d, h) + +def get_nodes_and_edges(jobs, gen, hosts, dbs): + nodes = [] + edges = [] + idx = 0 + for job in jobs: + s = job.get("source") + t = job.get("target") + if s and t: + s_db, s_host = get_info(s) + t_db, t_host = get_info(t) + s_ext_db_name = gen_ext_name(s_db, s_host) + t_ext_db_name = gen_ext_name(t_db, t_host) + + if s_host not in hosts: + hosts[s_host] = gen.__next__() + nodes.append({"id": hosts[s_host], "label": s_host, "group": "server" }) + + if t_host not in hosts: + hosts[t_host] = gen.__next__() + nodes.append({"id": hosts[t_host], "label": t_host, "group": "server"}) + + if s_ext_db_name not in dbs: + dbs[s_ext_db_name] = gen.__next__() + nodes.append({"id": dbs[s_ext_db_name], "label": s_db, "group": "db"}) + + if t_ext_db_name not in dbs: + dbs[t_ext_db_name] = gen.__next__() + nodes.append({"id": dbs[t_ext_db_name], "label": t_db, "group": "db"}) + + + edges.append({"from": dbs[s_ext_db_name] , "to":dbs[t_ext_db_name] , "arrow_type": "to"}) + edges.append({"from": hosts[s_host] , "to":dbs[s_ext_db_name], "arrow_type":"box"}) + edges.append({"from": hosts[t_host] , "to":dbs[t_ext_db_name], "arrow_type":"box"}) + + return nodes, edges \ No newline at end of file diff --git a/server.py b/server.py index ea57f6eba79580c92cea5647c300ad93b09e96a7..17f6ce349ff195c552ca4a51854a5f237066e7ae 100644 --- a/server.py +++ b/server.py @@ -1,6 +1,5 @@ import json import datetime -import requests import re from flask import Flask, request, jsonify, send_from_directory, render_template from flask_cors import CORS @@ -13,11 +12,11 @@ CORS(app) @app.route('/repl/all.html', methods=['GET']) def repl_all(): - req = requests.get("http://{}:{}/_scheduler/jobs".format(config["db"]["host"], config["db"]["port"])) - res = req.json() - + jobs = utils.get_jobs(config["db"]["host"], config["db"]["port"]) + gen = utils.gen_count() + nodes, edges = utils.get_nodes_and_edges(jobs, gen, {}, {}) template = utils.path_file(path=config['templates']['html'], file='all.html') - return render_template(template, jobs=res.get("jobs", [])) + return render_template(template, nodes=nodes, edges=edges) @app.route('/js/<fn>', methods=['get']) def js_folder(fn): diff --git a/templates/html/all.html b/templates/html/all.html index 54a73f6f40ade4d05b22baad745e38086f6605e5..63fa34149f3f579aed649a86b9841372b3b4df49 100644 --- a/templates/html/all.html +++ b/templates/html/all.html @@ -43,21 +43,17 @@ <script type="text/javascript" charset="utf8" src="../js/bootstrap.min.js"></script> <script type="text/javascript" src="https://unpkg.com/vis-network/standalone/umd/vis-network.min.js"></script> <script type="text/javascript" charset="utf8"> - // create an array with nodes var nodes = new vis.DataSet([ - {% for job in jobs %} - {id: {{loop.index}}, label: "{{job.source}}" , group:"switch" }, - {%endfor%} - ]); - - // create an array with edges - var edges = new vis.DataSet([ - {from: 1, to: 3}, - {from: 1, to: 2}, - {from: 2, to: 4}, - {from: 2, to: 5} - ]); - + {% for node in nodes %} + {id: {{node.id}}, label: "{{node.label}}" , group: "{{node.group}}" }, + {%endfor%} + ]); + var edges = new vis.DataSet([ + {% for edge in edges %} + {from: {{edge.from}}, to: {{edge.to}} , arrows:"{{edge.arrow_type}}"}, + {%endfor%} + ]); + // create a network var container = document.getElementById('net'); @@ -72,16 +68,16 @@ smooth: false }, groups: { - switch: { + server: { shape: "box", color: "#FF9900" // orange + }, + db: { + shape: "box" }}} // initialize your network! var network = new vis.Network(container, data, options); - window.addEventListener("load", () => { - draw(); - }); </script> </body> </html>