def insertData(graph_data, config): nodes = dict() edges = [] for ind, n in enumerate(graph_data['nodes']): node = Node(str(n['asn'])) nodes[ind] = node for e in graph_data['links']: edge = Edge(nodes[e['source']], nodes[e['target']]) edges.append(edge) print("Accepted %d nodes and %d links." % (len(nodes), len(edges))) if len(nodes) == 0 or len(edges) == 0: return 'No valid nodes or edges' uploaded_by = "admin" try: with NodeDB(config) as db: db.insert_graph(nodes, edges, uploaded_by) except Exception: traceback.print_exc() return 'Database failure' return None
def load_graph_from_db(time_limit): config = Config('./') config.from_pyfile('web_config.cfg') with NodeDB(config) as db: nodes = db.get_nodes(time_limit) edges = db.get_edges(nodes, 60 * 60 * 24 * 7) return (nodes, edges)
def insert_graph_data(config, data, mail, ip, version): try: graph_data = json.loads(data) except ValueError: return 'Invalid JSON' log = '[%s] ip: %s, version: %d, mail: %r, nodes: %d, edges: %d' % ( time.strftime('%Y-%m-%d %H:%M:%S'), ip, version, mail, len(graph_data['nodes']), len(graph_data['edges'])) with open(config['LOG'], 'a') as f: f.write(log + '\n') if mail == '*****@*****.**': 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' nodes = dict() edges = [] try: for n in graph_data['nodes']: try: node = Node(n['ip'], version=n['version']) nodes[n['ip']] = node except Exception: pass for e in graph_data['edges']: try: edge = Edge(nodes[e['a']], nodes[e['b']]) edges.append(edge) except Exception: pass except Exception: return 'Invalid JSON nodes' print "Accepted %d nodes and %d links." % (len(nodes), len(edges)) if len(nodes) == 0 or len(edges) == 0: return 'No valid nodes or edges' uploaded_by = ip try: with NodeDB(config) as db: db.insert_graph(nodes, edges, uploaded_by) except Exception: traceback.print_exc() return 'Database failure' return None
def add_node(address): ndb = NodeDB() all_nodes = ndb.find_all() if address.find('http') != 0: address = 'http://' + address all_nodes.append(address) ndb.clear() ndb.write(rm_dup(all_nodes)) return address
def insert_graph_data(config, json_str): try: graph_data = json.loads(json_str) except ValueError: return 'Invalid JSON' nodes = dict() edges = [] try: for n in graph_data['nodes']: try: node = Node(n['ip'], version=n['version']) nodes[n['ip']] = node except Exception: pass for e in graph_data['edges']: try: edge = Edge(nodes[e['a']], nodes[e['b']]) edges.append(edge) except Exception: pass except Exception: return 'Invalid JSON nodes' print "Accepted %d nodes and %d links." % (len(nodes), len(edges)) if len(nodes) == 0 or len(edges) == 0: return 'No valid nodes or edges' try: with NodeDB(config) as db: db.insert_graph(nodes, edges) except Exception: return 'Database failure' return None
def get_nodes(): return NodeDB().find_all()