2015-07-19 17:45:25 +00:00
|
|
|
define(["moment", "router", "leaflet", "gui", "numeral"],
|
|
|
|
function (moment, Router, L, GUI, numeral) {
|
|
|
|
return function (config) {
|
2015-03-29 14:14:10 +00:00
|
|
|
function handleData(data) {
|
2016-05-22 11:23:43 +00:00
|
|
|
var dataNodes = {};
|
|
|
|
dataNodes.nodes = [];
|
|
|
|
var dataGraph = {};
|
|
|
|
dataGraph.batadv = {};
|
|
|
|
dataGraph.batadv.nodes = [];
|
|
|
|
dataGraph.batadv.links = [];
|
2015-04-07 19:10:37 +00:00
|
|
|
|
2016-02-05 12:49:33 +00:00
|
|
|
|
|
|
|
function rearrangeLinks(d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
d.source += dataGraph.batadv.nodes.length;
|
|
|
|
d.target += dataGraph.batadv.nodes.length;
|
2016-02-05 12:49:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < data.length; ++i) {
|
2016-05-22 11:23:43 +00:00
|
|
|
var vererr;
|
2016-02-05 12:49:33 +00:00
|
|
|
if(i % 2)
|
|
|
|
if (data[i].version !== 1) {
|
2016-05-22 11:23:43 +00:00
|
|
|
vererr = "Unsupported graph version: " + data[i].version;
|
|
|
|
console.log(vererr); //silent fail
|
2016-02-05 12:49:33 +00:00
|
|
|
} else {
|
2016-05-22 11:23:43 +00:00
|
|
|
data[i].batadv.links.forEach(rearrangeLinks);
|
|
|
|
dataGraph.batadv.nodes = dataGraph.batadv.nodes.concat(data[i].batadv.nodes);
|
|
|
|
dataGraph.batadv.links = dataGraph.batadv.links.concat(data[i].batadv.links);
|
|
|
|
dataGraph.timestamp = data[i].timestamp;
|
2016-02-05 12:49:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (data[i].version !== 2) {
|
2016-05-22 11:23:43 +00:00
|
|
|
vererr = "Unsupported nodes version: " + data[i].version;
|
|
|
|
console.log(vererr); //silent fail
|
2016-02-05 12:49:33 +00:00
|
|
|
} else {
|
2016-05-22 11:23:43 +00:00
|
|
|
dataNodes.nodes = dataNodes.nodes.concat(data[i].nodes);
|
|
|
|
dataNodes.timestamp = data[i].timestamp;
|
2016-02-05 12:49:33 +00:00
|
|
|
}
|
2015-04-07 19:10:37 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 17:20:16 +00:00
|
|
|
var nodes = dataNodes.nodes.filter( function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return "firstseen" in d && "lastseen" in d;
|
|
|
|
});
|
2015-03-24 23:54:00 +00:00
|
|
|
|
|
|
|
nodes.forEach( function(node) {
|
2016-05-22 11:23:43 +00:00
|
|
|
node.firstseen = moment.utc(node.firstseen).local();
|
|
|
|
node.lastseen = moment.utc(node.lastseen).local();
|
|
|
|
});
|
2015-03-24 23:54:00 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var now = moment();
|
|
|
|
var age = moment(now).subtract(config.maxAge, "days");
|
2015-03-24 23:54:00 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var newnodes = limit("firstseen", age, sortByKey("firstseen", nodes).filter(online));
|
|
|
|
var lostnodes = limit("lastseen", age, sortByKey("lastseen", nodes).filter(offline));
|
2015-03-24 23:54:00 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var graphnodes = {};
|
2015-07-30 17:20:16 +00:00
|
|
|
|
|
|
|
dataNodes.nodes.forEach( function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
graphnodes[d.nodeinfo.node_id] = d;
|
|
|
|
});
|
2015-07-30 17:20:16 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var graph = dataGraph.batadv;
|
2015-03-24 23:54:00 +00:00
|
|
|
|
|
|
|
graph.nodes.forEach( function (d) {
|
2016-03-05 11:38:43 +00:00
|
|
|
if (d.node_id in graphnodes) {
|
2016-05-22 11:23:43 +00:00
|
|
|
d.node = graphnodes[d.node_id];
|
2016-03-05 11:38:43 +00:00
|
|
|
if (d.unseen) {
|
2016-05-22 11:23:43 +00:00
|
|
|
d.node.flags.online = true;
|
|
|
|
d.node.flags.unseen = true;
|
2016-03-05 11:38:43 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-22 11:23:43 +00:00
|
|
|
});
|
2015-03-24 23:54:00 +00:00
|
|
|
|
|
|
|
graph.links.forEach( function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
d.source = graph.nodes[d.source];
|
2015-03-24 23:54:00 +00:00
|
|
|
|
|
|
|
if (graph.nodes[d.target].node)
|
2016-05-22 11:23:43 +00:00
|
|
|
d.target = graph.nodes[d.target];
|
2015-03-24 23:54:00 +00:00
|
|
|
else
|
2016-05-22 11:23:43 +00:00
|
|
|
d.target = undefined;
|
|
|
|
});
|
2015-03-24 23:54:00 +00:00
|
|
|
|
|
|
|
var links = graph.links.filter( function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return d.target !== undefined;
|
|
|
|
});
|
2015-03-24 23:54:00 +00:00
|
|
|
|
|
|
|
links.forEach( function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
var unknown = (d.source.node === undefined);
|
|
|
|
var ids;
|
2016-02-27 13:43:27 +00:00
|
|
|
if (unknown)
|
2016-05-22 11:23:43 +00:00
|
|
|
ids = [d.source.id.replace(/:/g, ""), d.target.node.nodeinfo.node_id];
|
2016-02-27 13:43:27 +00:00
|
|
|
else
|
2016-05-22 11:23:43 +00:00
|
|
|
ids = [d.source.node.nodeinfo.node_id, d.target.node.nodeinfo.node_id];
|
|
|
|
d.id = ids.join("-");
|
2015-04-06 21:10:37 +00:00
|
|
|
|
2016-03-01 18:50:14 +00:00
|
|
|
if (unknown ||
|
|
|
|
!d.source.node.nodeinfo.location ||
|
|
|
|
!d.target.node.nodeinfo.location ||
|
|
|
|
isNaN(d.source.node.nodeinfo.location.latitude) ||
|
|
|
|
isNaN(d.source.node.nodeinfo.location.longitude) ||
|
|
|
|
isNaN(d.target.node.nodeinfo.location.latitude) ||
|
|
|
|
isNaN(d.target.node.nodeinfo.location.longitude))
|
2016-05-22 11:23:43 +00:00
|
|
|
return;
|
2015-03-24 23:54:00 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
d.latlngs = [];
|
|
|
|
d.latlngs.push(L.latLng(d.source.node.nodeinfo.location.latitude, d.source.node.nodeinfo.location.longitude));
|
|
|
|
d.latlngs.push(L.latLng(d.target.node.nodeinfo.location.latitude, d.target.node.nodeinfo.location.longitude));
|
2015-03-24 23:54:00 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
d.distance = d.latlngs[0].distanceTo(d.latlngs[1]);
|
|
|
|
});
|
2015-03-24 23:54:00 +00:00
|
|
|
|
|
|
|
nodes.forEach( function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
d.neighbours = [];
|
|
|
|
});
|
2015-03-24 23:54:00 +00:00
|
|
|
|
|
|
|
links.forEach( function (d) {
|
2016-04-23 23:02:17 +00:00
|
|
|
if (d.type === "tunnel" || d.type === "fastd")
|
2016-05-22 11:23:43 +00:00
|
|
|
d.type = "fastd";
|
2016-04-23 23:02:17 +00:00
|
|
|
else if (d.type === "l2tp") {
|
2016-05-22 11:23:43 +00:00
|
|
|
d.type = "L2TP";
|
|
|
|
d.target.node.flags.uplink = true;
|
2016-04-23 23:02:17 +00:00
|
|
|
} else if (d.type === "wireless")
|
2016-05-22 11:23:43 +00:00
|
|
|
d.type = "Wifi";
|
2016-02-18 19:26:05 +00:00
|
|
|
else if (d.type === "other")
|
2016-05-22 11:23:43 +00:00
|
|
|
d.type = "Kabel";
|
2016-02-18 19:26:05 +00:00
|
|
|
else
|
2016-05-22 11:23:43 +00:00
|
|
|
d.type = "N/A";
|
|
|
|
var unknown = (d.source.node === undefined);
|
2016-02-27 13:43:27 +00:00
|
|
|
if (unknown) {
|
2016-05-22 11:23:43 +00:00
|
|
|
d.target.node.neighbours.push({ id: d.source.id, link: d, incoming: true });
|
|
|
|
return;
|
2016-02-27 13:43:27 +00:00
|
|
|
}
|
2016-05-22 11:23:43 +00:00
|
|
|
d.source.node.neighbours.push({ node: d.target.node, link: d, incoming: false });
|
|
|
|
d.target.node.neighbours.push({ node: d.source.node, link: d, incoming: true });
|
2016-04-23 23:02:17 +00:00
|
|
|
if (d.type !== "fastd" && d.type !== "L2TP")
|
2016-05-22 11:23:43 +00:00
|
|
|
d.source.node.meshlinks = d.source.node.meshlinks ? d.source.node.meshlinks + 1 : 1;
|
|
|
|
});
|
2015-03-24 23:54:00 +00:00
|
|
|
|
2016-02-18 16:48:35 +00:00
|
|
|
links.sort( function (a, b) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return b.tq - a.tq;
|
|
|
|
});
|
2016-02-18 16:48:35 +00:00
|
|
|
|
2015-03-29 15:48:25 +00:00
|
|
|
return { now: now,
|
2015-07-30 17:20:16 +00:00
|
|
|
timestamp: moment.utc(dataNodes.timestamp).local(),
|
2015-03-29 15:48:25 +00:00
|
|
|
nodes: {
|
|
|
|
all: nodes,
|
|
|
|
new: newnodes,
|
|
|
|
lost: lostnodes
|
|
|
|
},
|
2015-03-31 15:21:58 +00:00
|
|
|
graph: {
|
|
|
|
links: links,
|
|
|
|
nodes: graph.nodes
|
|
|
|
}
|
2016-05-22 11:23:43 +00:00
|
|
|
};
|
2015-03-24 23:54:00 +00:00
|
|
|
}
|
2015-03-29 14:14:10 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
numeral.language("de");
|
|
|
|
moment.locale("de");
|
2015-04-02 00:33:17 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var router = new Router();
|
2015-03-29 15:54:43 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var urls = [];
|
2016-02-05 12:49:33 +00:00
|
|
|
|
|
|
|
if (typeof config.dataPath === "string" || config.dataPath instanceof String)
|
2016-05-22 11:23:43 +00:00
|
|
|
config.dataPath = [config.dataPath];
|
2016-02-05 12:49:33 +00:00
|
|
|
|
|
|
|
for (var i in config.dataPath) {
|
2016-05-22 11:23:43 +00:00
|
|
|
urls.push(config.dataPath[i] + "nodes.json");
|
|
|
|
urls.push(config.dataPath[i] + "graph.json");
|
2016-02-05 12:49:33 +00:00
|
|
|
}
|
|
|
|
|
2015-04-03 00:32:32 +00:00
|
|
|
function update() {
|
|
|
|
return Promise.all(urls.map(getJSON))
|
2016-05-22 11:23:43 +00:00
|
|
|
.then(handleData);
|
2015-04-03 00:32:32 +00:00
|
|
|
}
|
2015-03-29 14:14:10 +00:00
|
|
|
|
2015-04-03 00:32:32 +00:00
|
|
|
update()
|
2015-03-29 15:48:25 +00:00
|
|
|
.then(function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
var gui = new GUI(config, router);
|
|
|
|
gui.setData(d);
|
|
|
|
router.setData(d);
|
|
|
|
router.start();
|
2015-04-03 00:32:32 +00:00
|
|
|
|
|
|
|
window.setInterval(function () {
|
|
|
|
update().then(function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
gui.setData(d);
|
|
|
|
router.setData(d);
|
|
|
|
});
|
|
|
|
}, 60000);
|
2015-03-29 15:48:25 +00:00
|
|
|
})
|
2015-03-31 16:51:35 +00:00
|
|
|
.catch(function (e) {
|
2016-05-22 11:23:43 +00:00
|
|
|
document.body.textContent = e;
|
|
|
|
console.log(e);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|