mirror of
https://github.com/cwinfo/yggdrasil-map
synced 2024-11-09 16:00:27 +00:00
changes to get it working with y.yakamo.org:3000/current as a source of node info
This commit is contained in:
parent
ee631b927e
commit
3d6523fab3
@ -8,10 +8,10 @@ def position_nodes(nodes, edges):
|
||||
G = pgv.AGraph(strict=True, directed=False, size='10!')
|
||||
|
||||
for n in nodes.values():
|
||||
G.add_node(n.ip, label=n.label, version=n.version)
|
||||
G.add_node(n.coords, label=n.ip, version=n.version)
|
||||
|
||||
for e in edges:
|
||||
G.add_edge(e.a.ip, e.b.ip, len=1.0)
|
||||
G.add_edge(e.a.coords, e.b.coords, len=1.0)
|
||||
|
||||
G.layout(prog='neato', args='-Gepsilon=0.0001 -Gmaxiter=100000')
|
||||
|
||||
@ -53,7 +53,7 @@ def get_graph_json(G):
|
||||
}
|
||||
|
||||
centralities = compute_betweenness(G)
|
||||
db = load_db()
|
||||
#db = load_db()
|
||||
|
||||
for n in G.iternodes():
|
||||
neighbor_ratio = len(G.neighbors(n)) / float(max_neighbors)
|
||||
@ -61,7 +61,7 @@ def get_graph_json(G):
|
||||
centrality = centralities.get(n.name, 0)
|
||||
pcentrality = (centrality + 0.0001) * 500
|
||||
size = (pcentrality ** 0.3 / 500) * 1000 + 1
|
||||
name = db.get(n.name)
|
||||
name = None#db.get(n.name)
|
||||
|
||||
out_data['nodes'].append({
|
||||
'id': n.name,
|
||||
|
@ -3,16 +3,64 @@ from flask import Config
|
||||
from database import NodeDB
|
||||
import graphPlotter
|
||||
|
||||
import urllib, json
|
||||
url = "http://y.yakamo.org:3000/current"
|
||||
|
||||
# nodes indexed by coords
|
||||
class NodeInfo:
|
||||
def __init__(self, ip, coords):
|
||||
self.ip = str(ip)
|
||||
self.label = str(ip) # TODO readable labels
|
||||
self.coords = str(coords)
|
||||
self.version = "unknown"
|
||||
def getCoordList(self):
|
||||
return self.coords.strip("[]").split(" ")
|
||||
def getParent(self):
|
||||
p = self.getCoordList()
|
||||
if len(p) > 0: p = p[:-1]
|
||||
return "[" + " ".join(p).strip() + "]"
|
||||
def getLink(self):
|
||||
c = self.getCoordList()
|
||||
return int(self.getCoordList()[-1].strip() or "0")
|
||||
|
||||
class LinkInfo:
|
||||
def __init__(self, a, b):
|
||||
self.a = a # NodeInfo
|
||||
self.b = b # NodeInfo
|
||||
|
||||
def generate_graph(time_limit=60*60*3):
|
||||
nodes, edges = load_graph_from_db(time_limit)
|
||||
response = urllib.urlopen(url)
|
||||
data = json.loads(response.read())["yggnodes"]
|
||||
|
||||
toAdd = []
|
||||
for ip in data:
|
||||
info = NodeInfo(ip, data[ip][0])
|
||||
toAdd.append(info)
|
||||
|
||||
nodes = dict()
|
||||
def addAncestors(info):
|
||||
parent = NodeInfo("?", info.getParent())
|
||||
nodes[parent.coords] = parent
|
||||
if parent.coords != parent.getParent(): addAncestors(parent)
|
||||
|
||||
for info in toAdd: addAncestors(info)
|
||||
for info in toAdd: nodes[info.coords] = info
|
||||
|
||||
sortedNodes = sorted(nodes.values(), key=(lambda x: x.getLink()))
|
||||
#for node in sortedNodes: print node.ip, node.coords, node.getParent(), node.getLink()
|
||||
|
||||
edges = []
|
||||
for node in sortedNodes:
|
||||
if node.coords == node.getParent: continue
|
||||
edges.append(LinkInfo(node, nodes[node.getParent()]))
|
||||
|
||||
print '%d nodes, %d edges' % (len(nodes), len(edges))
|
||||
|
||||
graph = graphPlotter.position_nodes(nodes, edges)
|
||||
json = graphPlotter.get_graph_json(graph)
|
||||
js = graphPlotter.get_graph_json(graph)
|
||||
|
||||
with open('static/graph.json', 'w') as f:
|
||||
f.write(json)
|
||||
f.write(js)
|
||||
|
||||
|
||||
def load_graph_from_db(time_limit):
|
||||
|
Loading…
Reference in New Issue
Block a user