4
0
mirror of https://github.com/cwinfo/yggdrasil-map synced 2025-08-14 15:58:10 +00:00

Pushing nodes to a database and generating graph from it

This commit is contained in:
Vanhala Antti
2014-05-30 17:34:00 +03:00
parent d6b0b97528
commit f51cf2025e
13 changed files with 293 additions and 318 deletions

View File

@@ -1,9 +1,35 @@
import json
from database import NodeDB
from graph import Node, Edge
def insert_graph_data(json_str):
def insert_graph_data(config, json_str):
try:
graph_data = json.loads(json_str)
except ValueError:
return False
nodes = dict()
edges = []
if not 'nodes' in graph_data or not 'edges' in graph_data:
return False
try:
for n in graph_data['nodes']:
node = Node(n['ip'], version=n['version'])
nodes[n['ip']] = node
for e in graph_data['edges']:
edge = Edge(nodes[e['a']], nodes[e['b']])
edges.append(edge)
except TypeError:
return False
print "Received %d nodes and %d links." % (len(nodes), len(edges))
with NodeDB(config) as db:
db.insert_graph(nodes, edges)
return True