5
0
mirror of https://github.com/cwinfo/yggdrasil-map synced 2024-11-09 23:00:26 +00:00
yggdrasil-map/web/graphData.py

61 lines
1.6 KiB
Python
Raw Normal View History

2014-05-28 16:48:46 +00:00
import json
from database import NodeDB
from graph import Node, Edge
2015-07-26 17:08:51 +00:00
import traceback
2015-11-21 12:03:19 +00:00
import time
2014-05-28 16:48:46 +00:00
2015-11-21 12:03:19 +00:00
def insert_graph_data(config, data, mail, ip, version):
2015-11-21 11:47:21 +00:00
try:
2015-11-21 12:03:19 +00:00
graph_data = json.loads(data)
2015-11-21 11:47:21 +00:00
except ValueError:
return 'Invalid JSON'
2014-05-28 16:48:46 +00:00
2015-11-21 13:23:21 +00:00
log = '[%s] ip: %s, version: %d, mail: %r, nodes: %d, edges: %d' % (
time.strftime('%Y-%m-%d %H:%M:%S'), ip,
2015-11-21 12:03:19 +00:00
version, mail, len(graph_data['nodes']), len(graph_data['edges']))
with open(config['LOG'], 'a') as f:
f.write(log + '\n')
if mail == 'your@email.here':
return 'Please change email address in config.'
if version != 2:
return 'You are using outdated version of sendGraph script. Get new version from https://github.com/zielmicha/fc00.org/blob/master/scripts/sendGraph.py'
2015-11-21 11:47:21 +00:00
nodes = dict()
edges = []
2015-11-21 11:47:21 +00:00
try:
for n in graph_data['nodes']:
2015-11-21 12:03:19 +00:00
try:
2015-11-21 11:47:21 +00:00
node = Node(n['ip'], version=n['version'])
nodes[n['ip']] = node
except Exception:
pass
2015-11-21 11:47:21 +00:00
for e in graph_data['edges']:
2015-11-21 12:03:19 +00:00
try:
2015-11-21 11:47:21 +00:00
edge = Edge(nodes[e['a']], nodes[e['b']])
edges.append(edge)
except Exception:
pass
except Exception:
return 'Invalid JSON nodes'
2015-11-21 11:47:21 +00:00
print "Accepted %d nodes and %d links." % (len(nodes), len(edges))
2015-11-21 11:47:21 +00:00
if len(nodes) == 0 or len(edges) == 0:
return 'No valid nodes or edges'
2015-11-21 13:12:23 +00:00
uploaded_by = ip
2015-11-21 11:44:45 +00:00
2015-11-21 11:47:21 +00:00
try:
with NodeDB(config) as db:
2015-11-21 13:12:23 +00:00
db.insert_graph(nodes, edges, uploaded_by)
2015-11-21 11:47:21 +00:00
except Exception:
2015-11-21 12:03:19 +00:00
traceback.print_exc()
2015-11-21 11:47:21 +00:00
return 'Database failure'
2015-11-21 11:47:21 +00:00
return None