diff --git a/web/database.py b/web/database.py index 1b7bd97..3ec297e 100644 --- a/web/database.py +++ b/web/database.py @@ -19,8 +19,6 @@ class NodeDB: self.con.commit() self.con.close() - - def insert_node(self, node): now = int(time.time()) self.cur.execute(''' @@ -31,7 +29,7 @@ class NodeDB: node.ip, node.label, node.version, now, now, node.label, node.version, now)) - def insert_edge(self, edge, uploaded_by): + def insert_edge(self, edge, uploaded_by): now = int(time.time()) self.cur.execute(''' INSERT INTO edges (a, b, first_seen, last_seen, uploaded_by) @@ -41,7 +39,7 @@ class NodeDB: edge.a.ip, edge.b.ip, now, now, uploaded_by, now)) - def insert_graph(self, nodes, edges, uploaded_by): + def insert_graph(self, nodes, edges, uploaded_by): for n in nodes.itervalues(): self.insert_node(n) diff --git a/web/graphData.py b/web/graphData.py index 0bf2f1c..a75cf2d 100644 --- a/web/graphData.py +++ b/web/graphData.py @@ -2,26 +2,34 @@ import json from database import NodeDB from graph import Node, Edge import traceback +import time -def insert_graph_data(config, data, mail, ip): +def insert_graph_data(config, data, mail, ip, version): try: - graph_data = json.loads(data) + graph_data = json.loads(data) except ValueError: return 'Invalid JSON' + log = '[%s] version: %d, mail: %r, nodes: %d, edges: %d' % ( + time.strftime('%Y-%m-%d %H:%M:%S'), + version, mail, len(graph_data['nodes']), len(graph_data['edges'])) + + with open(config['LOG'], 'a') as f: + f.write(log + '\n') + nodes = dict() edges = [] try: for n in graph_data['nodes']: - try: + try: node = Node(n['ip'], version=n['version']) nodes[n['ip']] = node except Exception: pass for e in graph_data['edges']: - try: + try: edge = Edge(nodes[e['a']], nodes[e['b']]) edges.append(edge) except Exception: @@ -40,7 +48,7 @@ def insert_graph_data(config, data, mail, ip): with NodeDB(config) as db: db.insert_graph(nodes, edges, uploaded_by) except Exception: - traceback.print_exc() + traceback.print_exc() return 'Database failure' return None diff --git a/web/graphPlotter.py b/web/graphPlotter.py index 92edaaa..ff48d44 100644 --- a/web/graphPlotter.py +++ b/web/graphPlotter.py @@ -1,8 +1,6 @@ import pygraphviz as pgv import time import json -import collections -import math import networkx as nx from networkx.algorithms import centrality diff --git a/web/web.py b/web/web.py index 742714b..3eea61b 100644 --- a/web/web.py +++ b/web/web.py @@ -31,9 +31,11 @@ def page_about(): def page_sendGraph(): print "Receiving graph from %s" % (request.remote_addr) - data = request.form['data'] - mail = request.form.get('mail', 'none') - ret = insert_graph_data(ip=get_ip(), config=app.config, data=data, mail=mail) + data = request.form['data'] + mail = request.form.get('mail', 'none') + version = int(request.form.get('version', '1')) + ret = insert_graph_data(ip=get_ip(), config=app.config, data=data, mail=mail, version=version) + if ret == None: return 'OK' else: