mirror of
https://github.com/cwinfo/yggdrasil-map
synced 2024-11-22 07:00:28 +00:00
replace tabs with spaces
This commit is contained in:
parent
26343b47fb
commit
e4b9b130db
106
web/database.py
106
web/database.py
@ -4,79 +4,79 @@ import time
|
||||
|
||||
|
||||
class NodeDB:
|
||||
def __init__(self, config):
|
||||
self.con = mdb.connect(
|
||||
config['MYSQL_DATABASE_HOST'],
|
||||
config['MYSQL_DATABASE_USER'],
|
||||
config['MYSQL_DATABASE_PASSWORD'],
|
||||
config['MYSQL_DATABASE_DB'])
|
||||
self.cur = self.con.cursor()
|
||||
def __init__(self, config):
|
||||
self.con = mdb.connect(
|
||||
config['MYSQL_DATABASE_HOST'],
|
||||
config['MYSQL_DATABASE_USER'],
|
||||
config['MYSQL_DATABASE_PASSWORD'],
|
||||
config['MYSQL_DATABASE_DB'])
|
||||
self.cur = self.con.cursor()
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
self.con.commit()
|
||||
self.con.close()
|
||||
def __exit__(self, type, value, traceback):
|
||||
self.con.commit()
|
||||
self.con.close()
|
||||
|
||||
|
||||
|
||||
def insert_node(self, node):
|
||||
now = int(time.time())
|
||||
self.cur.execute('''
|
||||
INSERT INTO nodes (ip, name, version, first_seen, last_seen)
|
||||
VALUES (%s, %s, %s, %s, %s)
|
||||
ON DUPLICATE KEY
|
||||
UPDATE name = %s, version = %s, last_seen = %s''', (
|
||||
node.ip, node.label, node.version, now, now,
|
||||
node.label, node.version, now))
|
||||
def insert_node(self, node):
|
||||
now = int(time.time())
|
||||
self.cur.execute('''
|
||||
INSERT INTO nodes (ip, name, version, first_seen, last_seen)
|
||||
VALUES (%s, %s, %s, %s, %s)
|
||||
ON DUPLICATE KEY
|
||||
UPDATE name = %s, version = %s, last_seen = %s''', (
|
||||
node.ip, node.label, node.version, now, now,
|
||||
node.label, node.version, now))
|
||||
|
||||
def insert_edge(self, edge, uploaded_by):
|
||||
now = int(time.time())
|
||||
self.cur.execute('''
|
||||
now = int(time.time())
|
||||
self.cur.execute('''
|
||||
INSERT INTO edges (a, b, first_seen, last_seen, uploaded_by)
|
||||
VALUES (%s, %s, %s, %s, %s)
|
||||
ON DUPLICATE KEY
|
||||
UPDATE last_seen = %s''', (
|
||||
ON DUPLICATE KEY
|
||||
UPDATE last_seen = %s''', (
|
||||
edge.a.ip, edge.b.ip, now, now, uploaded_by,
|
||||
now))
|
||||
|
||||
def insert_graph(self, nodes, edges, uploaded_by):
|
||||
for n in nodes.itervalues():
|
||||
self.insert_node(n)
|
||||
for n in nodes.itervalues():
|
||||
self.insert_node(n)
|
||||
|
||||
for e in edges:
|
||||
for e in edges:
|
||||
self.insert_edge(e, uploaded_by)
|
||||
|
||||
|
||||
|
||||
def get_nodes(self, time_limit):
|
||||
since = int(time.time() - time_limit)
|
||||
cur = self.con.cursor(mdb.cursors.DictCursor)
|
||||
cur.execute("SELECT ip, version, name FROM nodes WHERE last_seen > %s", (since,))
|
||||
db_nodes = cur.fetchall()
|
||||
def get_nodes(self, time_limit):
|
||||
since = int(time.time() - time_limit)
|
||||
cur = self.con.cursor(mdb.cursors.DictCursor)
|
||||
cur.execute("SELECT ip, version, name FROM nodes WHERE last_seen > %s", (since,))
|
||||
db_nodes = cur.fetchall()
|
||||
|
||||
nodes = dict()
|
||||
for n in db_nodes:
|
||||
nodes[n['ip']] = Node(n['ip'], n['version'], n['name'])
|
||||
nodes = dict()
|
||||
for n in db_nodes:
|
||||
nodes[n['ip']] = Node(n['ip'], n['version'], n['name'])
|
||||
|
||||
return nodes
|
||||
return nodes
|
||||
|
||||
def get_edges(self, nodes, time_limit):
|
||||
since = int(time.time() - time_limit)
|
||||
cur = self.con.cursor(mdb.cursors.DictCursor)
|
||||
cur.execute("SELECT a, b FROM edges WHERE last_seen > %s", (since,))
|
||||
db_edges = cur.fetchall()
|
||||
def get_edges(self, nodes, time_limit):
|
||||
since = int(time.time() - time_limit)
|
||||
cur = self.con.cursor(mdb.cursors.DictCursor)
|
||||
cur.execute("SELECT a, b FROM edges WHERE last_seen > %s", (since,))
|
||||
db_edges = cur.fetchall()
|
||||
|
||||
edges = []
|
||||
for e in db_edges:
|
||||
try:
|
||||
edges.append(Edge(nodes[e['a']], nodes[e['b']]))
|
||||
except KeyError:
|
||||
pass
|
||||
return edges
|
||||
edges = []
|
||||
for e in db_edges:
|
||||
try:
|
||||
edges.append(Edge(nodes[e['a']], nodes[e['b']]))
|
||||
except KeyError:
|
||||
pass
|
||||
return edges
|
||||
|
||||
def get_graph(self, time_limit):
|
||||
nodes = self.get_nodes(time_limit)
|
||||
edges = self.get_edges(nodes, time_limit)
|
||||
return (nodes, edges)
|
||||
def get_graph(self, time_limit):
|
||||
nodes = self.get_nodes(time_limit)
|
||||
edges = self.get_edges(nodes, time_limit)
|
||||
return (nodes, edges)
|
||||
|
58
web/graph.py
58
web/graph.py
@ -2,48 +2,48 @@ import re
|
||||
|
||||
|
||||
class Node:
|
||||
def __init__(self, ip, version=None, label=None):
|
||||
if not valid_cjdns_ip(ip):
|
||||
raise ValueError('Invalid IP address')
|
||||
if not valid_version(version):
|
||||
raise ValueError('Invalid version')
|
||||
def __init__(self, ip, version=None, label=None):
|
||||
if not valid_cjdns_ip(ip):
|
||||
raise ValueError('Invalid IP address')
|
||||
if not valid_version(version):
|
||||
raise ValueError('Invalid version')
|
||||
|
||||
self.ip = ip
|
||||
self.version = int(version)
|
||||
self.label = ip[-4:] if label == None else label
|
||||
self.ip = ip
|
||||
self.version = int(version)
|
||||
self.label = ip[-4:] if label == None else label
|
||||
|
||||
def __lt__(self, b):
|
||||
return self.ip < b.ip
|
||||
def __lt__(self, b):
|
||||
return self.ip < b.ip
|
||||
|
||||
def __repr__(self):
|
||||
return 'Node(ip="%s", version=%s, label="%s")' % (
|
||||
self.ip,
|
||||
self.version,
|
||||
self.label)
|
||||
def __repr__(self):
|
||||
return 'Node(ip="%s", version=%s, label="%s")' % (
|
||||
self.ip,
|
||||
self.version,
|
||||
self.label)
|
||||
|
||||
class Edge:
|
||||
def __init__(self, a, b):
|
||||
self.a, self.b = sorted([a, b])
|
||||
def __init__(self, a, b):
|
||||
self.a, self.b = sorted([a, b])
|
||||
|
||||
def __eq__(self, that):
|
||||
return self.a.ip == that.a.ip and self.b.ip == that.b.ip
|
||||
def __eq__(self, that):
|
||||
return self.a.ip == that.a.ip and self.b.ip == that.b.ip
|
||||
|
||||
def __repr__(self):
|
||||
return 'Edge(a.ip="%s", b.ip="%s")' % (
|
||||
self.a.ip,
|
||||
self.b.ip)
|
||||
def __repr__(self):
|
||||
return 'Edge(a.ip="%s", b.ip="%s")' % (
|
||||
self.a.ip,
|
||||
self.b.ip)
|
||||
|
||||
|
||||
|
||||
_re_cjdns_ip = re.compile(r'^fc[0-9a-f]{2}(:[0-9a-f]{4}){7}$', re.IGNORECASE)
|
||||
|
||||
def valid_cjdns_ip(ip):
|
||||
return _re_cjdns_ip.match(ip) != None
|
||||
return _re_cjdns_ip.match(ip) != None
|
||||
|
||||
def valid_version(version):
|
||||
try:
|
||||
return int(version) < 20
|
||||
except ValueError:
|
||||
return False
|
||||
try:
|
||||
return int(version) < 20
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
|
||||
|
@ -4,43 +4,43 @@ from graph import Node, Edge
|
||||
import traceback
|
||||
|
||||
def insert_graph_data(config, data, mail, ip):
|
||||
try:
|
||||
try:
|
||||
graph_data = json.loads(data)
|
||||
except ValueError:
|
||||
return 'Invalid JSON'
|
||||
except ValueError:
|
||||
return 'Invalid JSON'
|
||||
|
||||
nodes = dict()
|
||||
edges = []
|
||||
nodes = dict()
|
||||
edges = []
|
||||
|
||||
try:
|
||||
for n in graph_data['nodes']:
|
||||
try:
|
||||
for n in graph_data['nodes']:
|
||||
try:
|
||||
node = Node(n['ip'], version=n['version'])
|
||||
nodes[n['ip']] = node
|
||||
except Exception:
|
||||
pass
|
||||
node = Node(n['ip'], version=n['version'])
|
||||
nodes[n['ip']] = node
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
for e in graph_data['edges']:
|
||||
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'
|
||||
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))
|
||||
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'
|
||||
if len(nodes) == 0 or len(edges) == 0:
|
||||
return 'No valid nodes or edges'
|
||||
|
||||
uploaded_by = ip
|
||||
|
||||
try:
|
||||
with NodeDB(config) as db:
|
||||
try:
|
||||
with NodeDB(config) as db:
|
||||
db.insert_graph(nodes, edges, uploaded_by)
|
||||
except Exception:
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
return 'Database failure'
|
||||
return 'Database failure'
|
||||
|
||||
return None
|
||||
return None
|
||||
|
@ -8,419 +8,419 @@ var mapOffset = {x: 0, y: 0};
|
||||
var zoom = 1.0;
|
||||
|
||||
function changeHash(hash) {
|
||||
window.location.replace(('' + window.location).split('#')[0] + '#' + hash);
|
||||
window.location.replace(('' + window.location).split('#')[0] + '#' + hash);
|
||||
}
|
||||
|
||||
function updateCanvasSize() {
|
||||
$(canvas).attr({height: $(canvas).height(), width: $(canvas).width()});
|
||||
ctx.translate(mapOffset.x, mapOffset.y);
|
||||
$(canvas).attr({height: $(canvas).height(), width: $(canvas).width()});
|
||||
ctx.translate(mapOffset.x, mapOffset.y);
|
||||
}
|
||||
|
||||
function drawCircle(ctx, x, y, radius, color) {
|
||||
ctx.fillStyle = color;
|
||||
ctx.beginPath();
|
||||
ctx.arc(x, y, radius, 0, Math.PI*2, true);
|
||||
ctx.fill();
|
||||
ctx.fillStyle = color;
|
||||
ctx.beginPath();
|
||||
ctx.arc(x, y, radius, 0, Math.PI*2, true);
|
||||
ctx.fill();
|
||||
}
|
||||
|
||||
function drawLine(ctx, x1, y1, x2, y2, color) {
|
||||
ctx.strokeStyle = color;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(x1, y1);
|
||||
ctx.lineTo(x2, y2);
|
||||
ctx.closePath();
|
||||
ctx.stroke();
|
||||
ctx.strokeStyle = color;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(x1, y1);
|
||||
ctx.lineTo(x2, y2);
|
||||
ctx.closePath();
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
function drawText(ctx, x, y, text, color, font) {
|
||||
// ctx.save();
|
||||
// ctx.translate(x, y);
|
||||
// ctx.rotate(Math.PI/4);
|
||||
ctx.fillStyle = color;
|
||||
ctx.font = font;
|
||||
ctx.textAlign = 'center';
|
||||
ctx.fillText(text, x, y);
|
||||
// ctx.restore();
|
||||
// ctx.save();
|
||||
// ctx.translate(x, y);
|
||||
// ctx.rotate(Math.PI/4);
|
||||
ctx.fillStyle = color;
|
||||
ctx.font = font;
|
||||
ctx.textAlign = 'center';
|
||||
ctx.fillText(text, x, y);
|
||||
// ctx.restore();
|
||||
}
|
||||
|
||||
function drawNetwork() {
|
||||
ctx.save();
|
||||
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||
ctx.restore();
|
||||
ctx.save();
|
||||
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||
ctx.restore();
|
||||
|
||||
|
||||
// Draw edges
|
||||
for (var i = 0; i < edges.length; ++i) {
|
||||
var edge = edges[i];
|
||||
var highlight = edge.sourceNode.hover || edge.targetNode.hover;
|
||||
var color = highlight ? 'rgba(0, 0, 0, 0.5)' : 'rgba(0, 0, 0, 0.15)';
|
||||
// Draw edges
|
||||
for (var i = 0; i < edges.length; ++i) {
|
||||
var edge = edges[i];
|
||||
var highlight = edge.sourceNode.hover || edge.targetNode.hover;
|
||||
var color = highlight ? 'rgba(0, 0, 0, 0.5)' : 'rgba(0, 0, 0, 0.15)';
|
||||
|
||||
drawLine(ctx,
|
||||
edge.sourceNode.x, edge.sourceNode.y,
|
||||
edge.targetNode.x, edge.targetNode.y,
|
||||
color);
|
||||
}
|
||||
drawLine(ctx,
|
||||
edge.sourceNode.x, edge.sourceNode.y,
|
||||
edge.targetNode.x, edge.targetNode.y,
|
||||
color);
|
||||
}
|
||||
|
||||
// Draw nodes
|
||||
for (var i = 0; i < nodes.length; ++i) {
|
||||
var node = nodes[i];
|
||||
// Draw nodes
|
||||
for (var i = 0; i < nodes.length; ++i) {
|
||||
var node = nodes[i];
|
||||
|
||||
drawCircle(ctx, node.x, node.y, node.radius, node.color);
|
||||
}
|
||||
drawCircle(ctx, node.x, node.y, node.radius, node.color);
|
||||
}
|
||||
|
||||
// Draw labels
|
||||
for (var i = 0; i < nodes.length; ++i) {
|
||||
var node = nodes[i];
|
||||
// Draw labels
|
||||
for (var i = 0; i < nodes.length; ++i) {
|
||||
var node = nodes[i];
|
||||
|
||||
if (node.radius > 4 || node.selected || node.hover) {
|
||||
var fontSize = 4 + node.radius * 0.4;
|
||||
if (node.radius > 4 || node.selected || node.hover) {
|
||||
var fontSize = 4 + node.radius * 0.4;
|
||||
|
||||
drawText(ctx, node.x, node.y - node.radius - 1,
|
||||
node.label, node.textColor, fontSize + 'pt "ubuntu mono"');
|
||||
}
|
||||
}
|
||||
drawText(ctx, node.x, node.y - node.radius - 1,
|
||||
node.label, node.textColor, fontSize + 'pt "ubuntu mono"');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getNodeAt(x, y) {
|
||||
x -= mapOffset.x;
|
||||
y -= mapOffset.y;
|
||||
for (var i = nodes.length - 1; i >= 0; --i) {
|
||||
var node = nodes[i];
|
||||
var distPow2 = (node.x - x) * (node.x - x) + (node.y - y) * (node.y - y);
|
||||
x -= mapOffset.x;
|
||||
y -= mapOffset.y;
|
||||
for (var i = nodes.length - 1; i >= 0; --i) {
|
||||
var node = nodes[i];
|
||||
var distPow2 = (node.x - x) * (node.x - x) + (node.y - y) * (node.y - y);
|
||||
|
||||
if (distPow2 <= node.radius * node.radius) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
if (distPow2 <= node.radius * node.radius) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function searchNode(id) {
|
||||
for (var i = 0; i < nodes.length; ++i) {
|
||||
if (nodes[i].id == id)
|
||||
return nodes[i];
|
||||
}
|
||||
return null;
|
||||
for (var i = 0; i < nodes.length; ++i) {
|
||||
if (nodes[i].id == id)
|
||||
return nodes[i];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function clearNodes() {
|
||||
changeHash('');
|
||||
$('#node-info').html('');
|
||||
changeHash('');
|
||||
$('#node-info').html('');
|
||||
|
||||
for (var i = 0; i < nodes.length; ++i) {
|
||||
var node = nodes[i];
|
||||
node.depth = 0xFFFF;
|
||||
node.color = node.originalColor;
|
||||
node.textColor = node.color;
|
||||
node.selected = false;
|
||||
}
|
||||
for (var i = 0; i < nodes.length; ++i) {
|
||||
var node = nodes[i];
|
||||
node.depth = 0xFFFF;
|
||||
node.color = node.originalColor;
|
||||
node.textColor = node.color;
|
||||
node.selected = false;
|
||||
}
|
||||
}
|
||||
|
||||
function selectNode(node, redraw) {
|
||||
clearNodes();
|
||||
clearNodes();
|
||||
|
||||
changeHash(node.id);
|
||||
changeHash(node.id);
|
||||
|
||||
node.selected = true;
|
||||
showNodeInfo(node);
|
||||
node.selected = true;
|
||||
showNodeInfo(node);
|
||||
|
||||
markPeers(node, 0);
|
||||
if (redraw)
|
||||
drawNetwork();
|
||||
markPeers(node, 0);
|
||||
if (redraw)
|
||||
drawNetwork();
|
||||
}
|
||||
|
||||
function markPeers(node, depth) {
|
||||
node.depth = depth;
|
||||
node.depth = depth;
|
||||
|
||||
// var colors = ['#000000', '#333333', '#555555', '#777777', '#999999', '#BBBBBB', '#DDDDDD'];
|
||||
// var colors = ['#000000', '#29BBFF', '#09E844', '#FFBD0F', '#FF5E14', '#FF3C14', '#FF7357', '#FF9782', '#FFC8BD', '#FFE6E0'];
|
||||
var colors = ['#000000', '#096EE8', '#09E8B8', '#36E809', '#ADE809', '#E8B809', '#E87509', '#E83A09', '#E86946', '#E8AC9B', '#E8C9C1'];
|
||||
var txtCol = ['#000000', '#032247', '#034537', '#0E3D02', '#354703', '#403203', '#3D1F02', '#3B0E02', '#3B0E02', '#3B0E02', '#3B0E02'];
|
||||
// var colors = ['#000000', '#064F8F', '#068F81', '#068F38', '#218F06', '#6F8F06', '#8F7806', '#8F5106'];
|
||||
// var colors = ['#FFFFFF', '#29BBFF', '#17FF54', '#FFBD0F', '#FF3C14', '#590409'];
|
||||
node.color = (depth >= colors.length) ? '#FFFFFF' : colors[depth];
|
||||
node.textColor = (depth >= txtCol.length) ? '#FFFFFF' : txtCol[depth];
|
||||
// var colors = ['#000000', '#333333', '#555555', '#777777', '#999999', '#BBBBBB', '#DDDDDD'];
|
||||
// var colors = ['#000000', '#29BBFF', '#09E844', '#FFBD0F', '#FF5E14', '#FF3C14', '#FF7357', '#FF9782', '#FFC8BD', '#FFE6E0'];
|
||||
var colors = ['#000000', '#096EE8', '#09E8B8', '#36E809', '#ADE809', '#E8B809', '#E87509', '#E83A09', '#E86946', '#E8AC9B', '#E8C9C1'];
|
||||
var txtCol = ['#000000', '#032247', '#034537', '#0E3D02', '#354703', '#403203', '#3D1F02', '#3B0E02', '#3B0E02', '#3B0E02', '#3B0E02'];
|
||||
// var colors = ['#000000', '#064F8F', '#068F81', '#068F38', '#218F06', '#6F8F06', '#8F7806', '#8F5106'];
|
||||
// var colors = ['#FFFFFF', '#29BBFF', '#17FF54', '#FFBD0F', '#FF3C14', '#590409'];
|
||||
node.color = (depth >= colors.length) ? '#FFFFFF' : colors[depth];
|
||||
node.textColor = (depth >= txtCol.length) ? '#FFFFFF' : txtCol[depth];
|
||||
|
||||
for (var i = 0; i < node.peers.length; ++i) {
|
||||
var n = node.peers[i];
|
||||
if (n.depth > depth + 1)
|
||||
markPeers(n, depth + 1);
|
||||
}
|
||||
for (var i = 0; i < node.peers.length; ++i) {
|
||||
var n = node.peers[i];
|
||||
if (n.depth > depth + 1)
|
||||
markPeers(n, depth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
function showNodeInfo(node) {
|
||||
var ip_peers = [];
|
||||
var dns_peers = [];
|
||||
var ip_peers = [];
|
||||
var dns_peers = [];
|
||||
|
||||
for (var i = 0; i < node.peers.length; ++i) {
|
||||
var n = node.peers[i];
|
||||
if (/^[0-9A-F]{4}$/i.test(n.label))
|
||||
ip_peers.push(n);
|
||||
else
|
||||
dns_peers.push(n);
|
||||
}
|
||||
for (var i = 0; i < node.peers.length; ++i) {
|
||||
var n = node.peers[i];
|
||||
if (/^[0-9A-F]{4}$/i.test(n.label))
|
||||
ip_peers.push(n);
|
||||
else
|
||||
dns_peers.push(n);
|
||||
}
|
||||
|
||||
var label_compare = function(a, b) {
|
||||
return a.label.localeCompare(b.label);
|
||||
}
|
||||
var label_compare = function(a, b) {
|
||||
return a.label.localeCompare(b.label);
|
||||
}
|
||||
|
||||
dns_peers.sort(label_compare);
|
||||
ip_peers.sort(label_compare);
|
||||
dns_peers.sort(label_compare);
|
||||
ip_peers.sort(label_compare);
|
||||
|
||||
var peers = dns_peers.concat(ip_peers);
|
||||
var peers = dns_peers.concat(ip_peers);
|
||||
|
||||
var html =
|
||||
'<h2>' + node.label + '</h2>' +
|
||||
'<span class="tt">' + node.id + '</span><br>' +
|
||||
'<br>' +
|
||||
'<strong>Version:</strong> ' + node.version + '<br>' +
|
||||
var html =
|
||||
'<h2>' + node.label + '</h2>' +
|
||||
'<span class="tt">' + node.id + '</span><br>' +
|
||||
'<br>' +
|
||||
'<strong>Version:</strong> ' + node.version + '<br>' +
|
||||
'<strong>Peers:</strong> ' + node.peers.length + '<br>' +
|
||||
'<strong>Centrality:</strong> ' + node.centrality + '<br>' +
|
||||
'<table>' +
|
||||
// '<tr><td></td><td><strong>Their peers #</strong></td></tr>' +
|
||||
peers.map(function (n) {
|
||||
return '<tr>' +
|
||||
'<td><a href="#' + n.id + '" class="tt">' + n.label + '</a></td>' +
|
||||
'<td>' + n.peers.length + '</td></tr>';
|
||||
}).join('') +
|
||||
'</table>';
|
||||
'<table>' +
|
||||
// '<tr><td></td><td><strong>Their peers #</strong></td></tr>' +
|
||||
peers.map(function (n) {
|
||||
return '<tr>' +
|
||||
'<td><a href="#' + n.id + '" class="tt">' + n.label + '</a></td>' +
|
||||
'<td>' + n.peers.length + '</td></tr>';
|
||||
}).join('') +
|
||||
'</table>';
|
||||
|
||||
$('#node-info').html(html);
|
||||
$('#node-info').html(html);
|
||||
}
|
||||
|
||||
function mousePos(e) {
|
||||
var rect = canvas.getBoundingClientRect();
|
||||
return {x: e.clientX - rect.left, y: e.clientY - rect.top};
|
||||
var rect = canvas.getBoundingClientRect();
|
||||
return {x: e.clientX - rect.left, y: e.clientY - rect.top};
|
||||
}
|
||||
|
||||
|
||||
|
||||
$(document).ready(function() {
|
||||
canvas = document.getElementById('map');
|
||||
ctx = canvas.getContext('2d');
|
||||
updateCanvasSize();
|
||||
canvas = document.getElementById('map');
|
||||
ctx = canvas.getContext('2d');
|
||||
updateCanvasSize();
|
||||
|
||||
|
||||
jQuery.getJSON('/static/graph.json', function(data) {
|
||||
nodes = data.nodes;
|
||||
jQuery.getJSON('/static/graph.json', function(data) {
|
||||
nodes = data.nodes;
|
||||
edges = data.edges;
|
||||
|
||||
// Calculate node radiuses
|
||||
for (var i = 0; i < nodes.length; ++i) {
|
||||
var node = nodes[i];
|
||||
node.x = node.x * 1.2;
|
||||
node.y = node.y * 1.2;
|
||||
// Calculate node radiuses
|
||||
for (var i = 0; i < nodes.length; ++i) {
|
||||
var node = nodes[i];
|
||||
node.x = node.x * 1.2;
|
||||
node.y = node.y * 1.2;
|
||||
node.radius = node.size;
|
||||
node.hover = false;
|
||||
node.selected = false;
|
||||
node.edges = [];
|
||||
node.peers = [];
|
||||
node.depth = 0xFFFF;
|
||||
// node.color = '#000';
|
||||
node.originalColor = node.color;
|
||||
node.textColor = node.color;
|
||||
}
|
||||
node.hover = false;
|
||||
node.selected = false;
|
||||
node.edges = [];
|
||||
node.peers = [];
|
||||
node.depth = 0xFFFF;
|
||||
// node.color = '#000';
|
||||
node.originalColor = node.color;
|
||||
node.textColor = node.color;
|
||||
}
|
||||
|
||||
var newEdges = []
|
||||
// Find node references for edges
|
||||
for (var i = 0; i < edges.length; ++i) {
|
||||
var edge = edges[i];
|
||||
// Find node references for edges
|
||||
for (var i = 0; i < edges.length; ++i) {
|
||||
var edge = edges[i];
|
||||
|
||||
for (var n = 0; n < nodes.length; ++n) {
|
||||
if (nodes[n].id == edge.sourceID) {
|
||||
edge.sourceNode = nodes[n];
|
||||
// edge.sourceNode.edges.append(edge);
|
||||
}
|
||||
else if (nodes[n].id == edge.targetID)
|
||||
edge.targetNode = nodes[n];
|
||||
}
|
||||
for (var n = 0; n < nodes.length; ++n) {
|
||||
if (nodes[n].id == edge.sourceID) {
|
||||
edge.sourceNode = nodes[n];
|
||||
// edge.sourceNode.edges.append(edge);
|
||||
}
|
||||
else if (nodes[n].id == edge.targetID)
|
||||
edge.targetNode = nodes[n];
|
||||
}
|
||||
|
||||
if (!edge.sourceNode || !edge.targetNode)
|
||||
continue;
|
||||
|
||||
edge.sourceNode.edges.push(edge);
|
||||
edge.targetNode.edges.push(edge);
|
||||
edge.sourceNode.peers.push(edge.targetNode);
|
||||
edge.targetNode.peers.push(edge.sourceNode);
|
||||
edge.sourceNode.edges.push(edge);
|
||||
edge.targetNode.edges.push(edge);
|
||||
edge.sourceNode.peers.push(edge.targetNode);
|
||||
edge.targetNode.peers.push(edge.sourceNode);
|
||||
|
||||
newEdges.push(edge);
|
||||
}
|
||||
}
|
||||
edges = newEdges;
|
||||
|
||||
// Set update time
|
||||
var delta = Math.round(new Date().getTime() / 1000) - data.created;
|
||||
var min = Math.floor(delta / 60);
|
||||
var sec = delta % 60;
|
||||
$('#update-time').text(min + ' min, ' + sec + ' s ago');
|
||||
// Set update time
|
||||
var delta = Math.round(new Date().getTime() / 1000) - data.created;
|
||||
var min = Math.floor(delta / 60);
|
||||
var sec = delta % 60;
|
||||
$('#update-time').text(min + ' min, ' + sec + ' s ago');
|
||||
|
||||
// Set stats
|
||||
$('#number-of-nodes').text(nodes.length);
|
||||
$('#number-of-connections').text(edges.length);
|
||||
// Set stats
|
||||
$('#number-of-nodes').text(nodes.length);
|
||||
$('#number-of-connections').text(edges.length);
|
||||
|
||||
|
||||
if (window.location.hash) {
|
||||
var id = window.location.hash.substring(1);
|
||||
var node = searchNode(id);
|
||||
if (node) selectNode(node, false);
|
||||
}
|
||||
if (window.location.hash) {
|
||||
var id = window.location.hash.substring(1);
|
||||
var node = searchNode(id);
|
||||
if (node) selectNode(node, false);
|
||||
}
|
||||
|
||||
drawNetwork();
|
||||
drawNetwork();
|
||||
|
||||
$(window).resize(function() {
|
||||
updateCanvasSize();
|
||||
drawNetwork();
|
||||
});
|
||||
$(window).resize(function() {
|
||||
updateCanvasSize();
|
||||
drawNetwork();
|
||||
});
|
||||
|
||||
|
||||
// Initialize search
|
||||
var searchArray = [];
|
||||
for (var i = 0; i < nodes.length; ++i) {
|
||||
var node = nodes[i];
|
||||
// Initialize search
|
||||
var searchArray = [];
|
||||
for (var i = 0; i < nodes.length; ++i) {
|
||||
var node = nodes[i];
|
||||
|
||||
searchArray.push({
|
||||
value: node.label,
|
||||
data: node
|
||||
});
|
||||
searchArray.push({
|
||||
value: node.label,
|
||||
data: node
|
||||
});
|
||||
|
||||
searchArray.push({
|
||||
value: node.id,
|
||||
data: node
|
||||
});
|
||||
}
|
||||
searchArray.push({
|
||||
value: node.id,
|
||||
data: node
|
||||
});
|
||||
}
|
||||
|
||||
$('#search-box').autocomplete({
|
||||
lookup: searchArray,
|
||||
autoSelectFirst: true,
|
||||
lookupLimit: 7,
|
||||
onSelect: function(suggestion) {
|
||||
selectNode(suggestion.data, true);
|
||||
}
|
||||
});
|
||||
$('#search-box').autocomplete({
|
||||
lookup: searchArray,
|
||||
autoSelectFirst: true,
|
||||
lookupLimit: 7,
|
||||
onSelect: function(suggestion) {
|
||||
selectNode(suggestion.data, true);
|
||||
}
|
||||
});
|
||||
|
||||
$('#search-box').keypress(function(e) {
|
||||
if (e.which == 13) {
|
||||
selectNode(searchNode($('#search-box').val()), true);
|
||||
}
|
||||
});
|
||||
$('#search-box').keypress(function(e) {
|
||||
if (e.which == 13) {
|
||||
selectNode(searchNode($('#search-box').val()), true);
|
||||
}
|
||||
});
|
||||
|
||||
$(document).on('click', '#node-info a', function(e) {
|
||||
var id = e.target.hash.substring(1);
|
||||
selectNode(searchNode(id), true);
|
||||
});
|
||||
$(document).on('click', '#node-info a', function(e) {
|
||||
var id = e.target.hash.substring(1);
|
||||
selectNode(searchNode(id), true);
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
var mouseDownPos = null;
|
||||
var mouseLastPos = null;
|
||||
var mouseDownNode = null;
|
||||
var mouseHoverNode = null;
|
||||
var mouseDownPos = null;
|
||||
var mouseLastPos = null;
|
||||
var mouseDownNode = null;
|
||||
var mouseHoverNode = null;
|
||||
|
||||
|
||||
$(canvas).mousemove(function(e) {
|
||||
var mouse = mousePos(e);
|
||||
$(canvas).mousemove(function(e) {
|
||||
var mouse = mousePos(e);
|
||||
|
||||
// Dragging
|
||||
if (mouseDownPos != null) {
|
||||
$('body').css('cursor', 'move');
|
||||
var dx = mouse.x - mouseLastPos.x;
|
||||
var dy = mouse.y - mouseLastPos.y;
|
||||
mapOffset.x += dx;
|
||||
mapOffset.y += dy;
|
||||
ctx.translate(dx, dy);
|
||||
mouseLastPos = {x: mouse.x, y: mouse.y};
|
||||
drawNetwork();
|
||||
}
|
||||
// Hovering
|
||||
else {
|
||||
var node = getNodeAt(mouse.x, mouse.y);
|
||||
// Dragging
|
||||
if (mouseDownPos != null) {
|
||||
$('body').css('cursor', 'move');
|
||||
var dx = mouse.x - mouseLastPos.x;
|
||||
var dy = mouse.y - mouseLastPos.y;
|
||||
mapOffset.x += dx;
|
||||
mapOffset.y += dy;
|
||||
ctx.translate(dx, dy);
|
||||
mouseLastPos = {x: mouse.x, y: mouse.y};
|
||||
drawNetwork();
|
||||
}
|
||||
// Hovering
|
||||
else {
|
||||
var node = getNodeAt(mouse.x, mouse.y);
|
||||
|
||||
if (node == mouseHoverNode)
|
||||
return;
|
||||
if (node == mouseHoverNode)
|
||||
return;
|
||||
|
||||
if (node == null) {
|
||||
nodeMouseOut(mouseHoverNode);
|
||||
}
|
||||
else {
|
||||
if (mouseHoverNode != null)
|
||||
nodeMouseOut(mouseHoverNode);
|
||||
if (node == null) {
|
||||
nodeMouseOut(mouseHoverNode);
|
||||
}
|
||||
else {
|
||||
if (mouseHoverNode != null)
|
||||
nodeMouseOut(mouseHoverNode);
|
||||
|
||||
nodeMouseIn(node);
|
||||
}
|
||||
mouseHoverNode = node;
|
||||
nodeMouseIn(node);
|
||||
}
|
||||
mouseHoverNode = node;
|
||||
|
||||
drawNetwork();
|
||||
}
|
||||
});
|
||||
drawNetwork();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
$(canvas).mousedown(function(e) {
|
||||
var mouse = mousePos(e);
|
||||
mouseLastPos = mouseDownPos = {x: mouse.x, y: mouse.y};
|
||||
mouseDownNode = getNodeAt(mouse.x, mouse.y);
|
||||
return false;
|
||||
});
|
||||
$(canvas).mousedown(function(e) {
|
||||
var mouse = mousePos(e);
|
||||
mouseLastPos = mouseDownPos = {x: mouse.x, y: mouse.y};
|
||||
mouseDownNode = getNodeAt(mouse.x, mouse.y);
|
||||
return false;
|
||||
});
|
||||
|
||||
$(canvas).mouseup(function(e) {
|
||||
var mouse = mousePos(e);
|
||||
var mouseMoved =
|
||||
Math.abs(mouse.x - mouseDownPos.x) +
|
||||
Math.abs(mouse.y - mouseDownPos.y) > 3
|
||||
$(canvas).mouseup(function(e) {
|
||||
var mouse = mousePos(e);
|
||||
var mouseMoved =
|
||||
Math.abs(mouse.x - mouseDownPos.x) +
|
||||
Math.abs(mouse.y - mouseDownPos.y) > 3
|
||||
|
||||
if (!mouseMoved) {
|
||||
if (mouseDownNode)
|
||||
selectNode(mouseDownNode, true);
|
||||
else {
|
||||
clearNodes();
|
||||
drawNetwork();
|
||||
}
|
||||
}
|
||||
else {
|
||||
$('body').css('cursor', 'auto');
|
||||
}
|
||||
if (!mouseMoved) {
|
||||
if (mouseDownNode)
|
||||
selectNode(mouseDownNode, true);
|
||||
else {
|
||||
clearNodes();
|
||||
drawNetwork();
|
||||
}
|
||||
}
|
||||
else {
|
||||
$('body').css('cursor', 'auto');
|
||||
}
|
||||
|
||||
mouseDownPos = null;
|
||||
mouseDownNode = null;
|
||||
return false;
|
||||
});
|
||||
mouseDownPos = null;
|
||||
mouseDownNode = null;
|
||||
return false;
|
||||
});
|
||||
|
||||
|
||||
function handleScroll(e) {
|
||||
var mouse = mousePos(e);
|
||||
var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
|
||||
function handleScroll(e) {
|
||||
var mouse = mousePos(e);
|
||||
var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
|
||||
|
||||
var ratio = (delta < 0) ? (3 / 4) : 1 + (1 / 3);
|
||||
var mx = mouse.x - mapOffset.x;
|
||||
var my = mouse.y - mapOffset.y;
|
||||
var ratio = (delta < 0) ? (3 / 4) : 1 + (1 / 3);
|
||||
var mx = mouse.x - mapOffset.x;
|
||||
var my = mouse.y - mapOffset.y;
|
||||
|
||||
zoom *= ratio;
|
||||
zoom *= ratio;
|
||||
|
||||
for (var i = 0; i < nodes.length; ++i) {
|
||||
var node = nodes[i];
|
||||
node.x = (node.x - mx) * ratio + mx;
|
||||
node.y = (node.y - my) * ratio + my;
|
||||
// node.x *= ratio;
|
||||
// node.y *= ratio;
|
||||
// node.radius *= ratio;
|
||||
for (var i = 0; i < nodes.length; ++i) {
|
||||
var node = nodes[i];
|
||||
node.x = (node.x - mx) * ratio + mx;
|
||||
node.y = (node.y - my) * ratio + my;
|
||||
// node.x *= ratio;
|
||||
// node.y *= ratio;
|
||||
// node.radius *= ratio;
|
||||
node.radius = (node.size) * zoom;
|
||||
}
|
||||
}
|
||||
|
||||
drawNetwork();
|
||||
}
|
||||
canvas.addEventListener("mousewheel", handleScroll, false);
|
||||
canvas.addEventListener("DOMMouseScroll", handleScroll, false);
|
||||
drawNetwork();
|
||||
}
|
||||
canvas.addEventListener("mousewheel", handleScroll, false);
|
||||
canvas.addEventListener("DOMMouseScroll", handleScroll, false);
|
||||
|
||||
});
|
||||
|
||||
function nodeMouseIn(node) {
|
||||
node.hover = true;
|
||||
$('body').css('cursor', 'pointer');
|
||||
node.hover = true;
|
||||
$('body').css('cursor', 'pointer');
|
||||
}
|
||||
|
||||
function nodeMouseOut(node) {
|
||||
node.hover = false;
|
||||
$('body').css('cursor', 'auto');
|
||||
node.hover = false;
|
||||
$('body').css('cursor', 'auto');
|
||||
}
|
||||
|
@ -5,25 +5,25 @@ import graphPlotter
|
||||
|
||||
|
||||
def generate_graph(time_limit=60*60*3):
|
||||
nodes, edges = load_graph_from_db(time_limit)
|
||||
print '%d nodes, %d edges' % (len(nodes), len(edges))
|
||||
nodes, edges = load_graph_from_db(time_limit)
|
||||
print '%d nodes, %d edges' % (len(nodes), len(edges))
|
||||
|
||||
graph = graphPlotter.position_nodes(nodes, edges)
|
||||
json = graphPlotter.get_graph_json(graph)
|
||||
graph = graphPlotter.position_nodes(nodes, edges)
|
||||
json = graphPlotter.get_graph_json(graph)
|
||||
|
||||
with open('static/graph.json', 'w') as f:
|
||||
f.write(json)
|
||||
with open('static/graph.json', 'w') as f:
|
||||
f.write(json)
|
||||
|
||||
|
||||
def load_graph_from_db(time_limit):
|
||||
config = Config('./')
|
||||
config.from_pyfile('web_config.cfg')
|
||||
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)
|
||||
with NodeDB(config) as db:
|
||||
nodes = db.get_nodes(time_limit)
|
||||
edges = db.get_edges(nodes, 60*60*24*7)
|
||||
return (nodes, edges)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
generate_graph()
|
||||
generate_graph()
|
||||
|
18
web/web.py
18
web/web.py
@ -21,23 +21,23 @@ def add_ip():
|
||||
@app.route('/')
|
||||
@app.route('/network')
|
||||
def page_network():
|
||||
return render_template('network.html', page='network')
|
||||
return render_template('network.html', page='network')
|
||||
|
||||
@app.route('/about')
|
||||
def page_about():
|
||||
return render_template('about.html', page='about')
|
||||
return render_template('about.html', page='about')
|
||||
|
||||
@app.route('/sendGraph', methods=['POST'])
|
||||
def page_sendGraph():
|
||||
print "Receiving graph from %s" % (request.remote_addr)
|
||||
|
||||
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)
|
||||
if ret == None:
|
||||
return 'OK'
|
||||
else:
|
||||
return 'Error: %s' % ret
|
||||
if ret == None:
|
||||
return 'OK'
|
||||
else:
|
||||
return 'Error: %s' % ret
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='localhost', port=3000)
|
||||
app.run(host='localhost', port=3000)
|
||||
|
Loading…
Reference in New Issue
Block a user