From 5fba69de7adf5e5d9fda211f0dd972e9153da899 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Litza Date: Thu, 31 Jul 2014 16:31:14 +0200 Subject: [PATCH] d3json: make output more similar to pre-rewrite version --- ffmap/node.py | 2 ++ ffmap/outputs/d3json.py | 61 +++++++++++++++++++++++++---------------- 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/ffmap/node.py b/ffmap/node.py index e2169f2..b89dd19 100644 --- a/ffmap/node.py +++ b/ffmap/node.py @@ -86,4 +86,6 @@ class Node(defaultdict): else: new_neighbor[key] = val ret["neighbors"].append(new_neighbor) + if "id" not in ret: + ret["id"] = self.id return ret diff --git a/ffmap/outputs/d3json.py b/ffmap/outputs/d3json.py index fd5b267..06e82fb 100644 --- a/ffmap/outputs/d3json.py +++ b/ffmap/outputs/d3json.py @@ -25,42 +25,55 @@ class Output: nodes = [] count = 0 for node in set(nodedb.values()): - nodes.append(node.export()) + node_export = node.export() + node_export["flags"] = { + "gateway": "vpn" in node and node["vpn"], + "client": False, + "online": True + } + nodes.append(node_export) indexes[node.id] = count count += 1 - links = [] + links = {} for node in set(nodedb.values()): - if "neighbors" in node: - links.extend( - { + for neighbor in node.get("neighbors", []): + key = (neighbor["neighbor"].id, node.id) + rkey = tuple(reversed(key)) + if rkey in links: + links[rkey]["quality"] += ","+neighbor["metric"] + else: + links[key] = { "source": indexes[node.id], "target": indexes[neighbor["neighbor"].id], "quality": neighbor["metric"], - "type": "vpn" if neighbor["neighbor"]["vpn"] else None, + "type": "vpn" if neighbor["neighbor"]["vpn"] or node["vpn"] else None, "id": "-".join((node.id, neighbor["neighbor"].id)), - } for neighbor in node["neighbors"] - ) - if "clients" in node: - for client in node["clients"]: - if not client in indexes: - nodes.append({ - "id": client, - }) - indexes[client] = count - count += 1 - - links.append({ - "source": indexes[node.id], - "target": indexes[client], - "quality": "TT", - "type": "client", - "id": "-".join((node.id, client)), + } + for client in node.get("clients", []): + if not client in indexes: + nodes.append({ + "id": client, + "flags": { + "client": True, + "online": True, + "gateway": False + } }) + indexes[client] = count + count += 1 + + links[(node.id, client)] = { + "source": indexes[node.id], + "target": indexes[client], + "quality": "TT", + "type": "client", + "id": "-".join((node.id, client)), + } return { "nodes": nodes, - "links": links, + "links": list(links.values()), "meta": { "timestamp": datetime.utcnow() .replace(microsecond=0)