d3json: make output more similar to pre-rewrite version

This commit is contained in:
Jan-Philipp Litza 2014-07-31 16:31:14 +02:00
parent 446bc98403
commit 5fba69de7a
2 changed files with 39 additions and 24 deletions

View file

@ -86,4 +86,6 @@ class Node(defaultdict):
else: else:
new_neighbor[key] = val new_neighbor[key] = val
ret["neighbors"].append(new_neighbor) ret["neighbors"].append(new_neighbor)
if "id" not in ret:
ret["id"] = self.id
return ret return ret

View file

@ -25,42 +25,55 @@ class Output:
nodes = [] nodes = []
count = 0 count = 0
for node in set(nodedb.values()): 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 indexes[node.id] = count
count += 1 count += 1
links = [] links = {}
for node in set(nodedb.values()): for node in set(nodedb.values()):
if "neighbors" in node: for neighbor in node.get("neighbors", []):
links.extend( 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], "source": indexes[node.id],
"target": indexes[neighbor["neighbor"].id], "target": indexes[neighbor["neighbor"].id],
"quality": neighbor["metric"], "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)), "id": "-".join((node.id, neighbor["neighbor"].id)),
} for neighbor in node["neighbors"] }
) for client in node.get("clients", []):
if "clients" in node:
for client in node["clients"]:
if not client in indexes: if not client in indexes:
nodes.append({ nodes.append({
"id": client, "id": client,
"flags": {
"client": True,
"online": True,
"gateway": False
}
}) })
indexes[client] = count indexes[client] = count
count += 1 count += 1
links.append({ links[(node.id, client)] = {
"source": indexes[node.id], "source": indexes[node.id],
"target": indexes[client], "target": indexes[client],
"quality": "TT", "quality": "TT",
"type": "client", "type": "client",
"id": "-".join((node.id, client)), "id": "-".join((node.id, client)),
}) }
return { return {
"nodes": nodes, "nodes": nodes,
"links": links, "links": list(links.values()),
"meta": { "meta": {
"timestamp": datetime.utcnow() "timestamp": datetime.utcnow()
.replace(microsecond=0) .replace(microsecond=0)