2017-01-28 14:33:13 +00:00
|
|
|
define(['polyglot', 'moment', 'router', 'leaflet', 'gui', 'helper'],
|
|
|
|
function (Polyglot, moment, Router, L, GUI, helper) {
|
2017-01-29 23:51:08 +00:00
|
|
|
'use strict';
|
2016-05-27 21:59:01 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
return function (config) {
|
|
|
|
function handleData(data) {
|
|
|
|
var dataNodes = {};
|
|
|
|
dataNodes.nodes = [];
|
|
|
|
var dataGraph = {};
|
|
|
|
dataGraph.batadv = {};
|
|
|
|
dataGraph.batadv.nodes = [];
|
|
|
|
dataGraph.batadv.links = [];
|
|
|
|
|
|
|
|
function rearrangeLinks(d) {
|
|
|
|
d.source += dataGraph.batadv.nodes.length;
|
|
|
|
d.target += dataGraph.batadv.nodes.length;
|
|
|
|
}
|
2016-02-05 12:49:33 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
for (var i = 0; i < data.length; ++i) {
|
|
|
|
var vererr;
|
|
|
|
if (i % 2) {
|
|
|
|
if (data[i].version !== 1) {
|
2017-01-29 23:51:08 +00:00
|
|
|
vererr = 'Unsupported graph version: ' + data[i].version;
|
|
|
|
console.error(vererr); // silent fail
|
2016-05-22 12:51:30 +00:00
|
|
|
} else {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
} else if (data[i].version !== 2) {
|
2017-01-29 23:51:08 +00:00
|
|
|
vererr = 'Unsupported nodes version: ' + data[i].version;
|
|
|
|
console.error(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
|
|
|
}
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-04-07 19:10:37 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
var nodes = dataNodes.nodes.filter(function (d) {
|
2017-01-29 23:51:08 +00:00
|
|
|
return 'firstseen' in d && 'lastseen' in d;
|
2016-05-22 12:51:30 +00:00
|
|
|
});
|
2015-03-24 23:54:00 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
nodes.forEach(function (node) {
|
|
|
|
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 12:51:30 +00:00
|
|
|
var now = moment();
|
2017-01-29 23:51:08 +00:00
|
|
|
var age = moment(now).subtract(config.maxAge, 'days');
|
2015-03-24 23:54:00 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
var newnodes = helper.limit('firstseen', age, helper.sortByKey('firstseen', nodes).filter(helper.online));
|
|
|
|
var lostnodes = helper.limit('lastseen', age, helper.sortByKey('lastseen', nodes).filter(helper.offline));
|
2015-03-24 23:54:00 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
var graphnodes = {};
|
2015-07-30 17:20:16 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
dataNodes.nodes.forEach(function (d) {
|
|
|
|
graphnodes[d.nodeinfo.node_id] = d;
|
|
|
|
});
|
2015-07-30 17:20:16 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
var graph = dataGraph.batadv;
|
2015-03-24 23:54:00 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
graph.nodes.forEach(function (d) {
|
|
|
|
if (d.node_id in graphnodes) {
|
|
|
|
d.node = graphnodes[d.node_id];
|
|
|
|
if (d.unseen) {
|
|
|
|
d.node.flags.online = true;
|
|
|
|
d.node.flags.unseen = true;
|
|
|
|
}
|
2016-03-05 11:38:43 +00:00
|
|
|
}
|
2016-05-22 12:51:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
graph.links.forEach(function (d) {
|
|
|
|
d.source = graph.nodes[d.source];
|
|
|
|
|
|
|
|
if (graph.nodes[d.target].node) {
|
|
|
|
d.target = graph.nodes[d.target];
|
|
|
|
} else {
|
|
|
|
d.target = undefined;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var links = graph.links.filter(function (d) {
|
|
|
|
return d.target !== undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
links.forEach(function (d) {
|
|
|
|
var unknown = (d.source.node === undefined);
|
|
|
|
var ids;
|
|
|
|
if (unknown) {
|
2017-01-29 23:51:08 +00:00
|
|
|
ids = [d.source.id.replace(/:/g, ''), d.target.node.nodeinfo.node_id];
|
2016-05-22 12:51:30 +00:00
|
|
|
} else {
|
|
|
|
ids = [d.source.node.nodeinfo.node_id, d.target.node.nodeinfo.node_id];
|
|
|
|
}
|
2017-01-29 23:51:08 +00:00
|
|
|
d.id = ids.join('-');
|
2016-05-22 12:51:30 +00:00
|
|
|
|
|
|
|
if (unknown || !d.source.node.nodeinfo.location || !d.target.node.nodeinfo.location ||
|
2016-03-01 18:50:14 +00:00
|
|
|
isNaN(d.source.node.nodeinfo.location.latitude) ||
|
|
|
|
isNaN(d.source.node.nodeinfo.location.longitude) ||
|
|
|
|
isNaN(d.target.node.nodeinfo.location.latitude) ||
|
2016-05-22 12:51:30 +00:00
|
|
|
isNaN(d.target.node.nodeinfo.location.longitude)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
d.distance = d.latlngs[0].distanceTo(d.latlngs[1]);
|
|
|
|
});
|
|
|
|
|
|
|
|
nodes.forEach(function (d) {
|
|
|
|
d.neighbours = [];
|
|
|
|
});
|
|
|
|
|
|
|
|
links.forEach(function (d) {
|
2016-06-08 22:20:10 +00:00
|
|
|
if (d.target.node.flags.gateway === true || (d.source.node !== undefined && d.source.node.flags.gateway === true)) {
|
2017-01-29 23:51:08 +00:00
|
|
|
d.type = 'fastd';
|
2016-05-22 12:51:30 +00:00
|
|
|
} else {
|
2017-01-29 23:51:08 +00:00
|
|
|
d.type = 'N/A';
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
|
|
|
var unknown = (d.source.node === undefined);
|
|
|
|
if (unknown) {
|
2017-01-29 23:51:08 +00:00
|
|
|
d.target.node.neighbours.push({ id: d.source.id, link: d, incoming: true });
|
2016-05-22 12:51:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-01-29 23:51:08 +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 });
|
|
|
|
if (d.type !== 'fastd' && d.type !== 'L2TP') {
|
2016-05-22 12:51:30 +00:00
|
|
|
d.source.node.meshlinks = d.source.node.meshlinks ? d.source.node.meshlinks + 1 : 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
links.sort(function (a, b) {
|
|
|
|
return b.tq - a.tq;
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
now: now,
|
|
|
|
timestamp: moment.utc(dataNodes.timestamp).local(),
|
|
|
|
nodes: {
|
|
|
|
all: nodes,
|
|
|
|
new: newnodes,
|
|
|
|
lost: lostnodes
|
|
|
|
},
|
|
|
|
graph: {
|
|
|
|
links: links,
|
|
|
|
nodes: graph.nodes
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2016-05-26 16:37:24 +00:00
|
|
|
|
2017-01-28 14:33:13 +00:00
|
|
|
function setTranslation(json) {
|
|
|
|
_.extend(json);
|
|
|
|
|
|
|
|
moment.locale(_.locale(), {
|
|
|
|
longDateFormat: {
|
|
|
|
LT: 'HH:mm',
|
|
|
|
LTS: 'HH:mm:ss',
|
|
|
|
L: 'DD.MM.YYYY',
|
|
|
|
LL: 'D. MMMM YYYY',
|
|
|
|
LLL: 'D. MMMM YYYY HH:mm',
|
|
|
|
LLLL: 'dddd, D. MMMM YYYY HH:mm'
|
|
|
|
},
|
|
|
|
calendar: json.momentjs.calendar,
|
|
|
|
relativeTime: json.momentjs.relativeTime
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var language = navigator.languages && navigator.languages[0] || navigator.language || navigator.userLanguage;
|
|
|
|
var locale = config.supportedLocale[0];
|
|
|
|
config.supportedLocale.some(function (item) {
|
|
|
|
if (language.indexOf(item) !== -1) {
|
|
|
|
locale = item;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
window._ = new Polyglot({locale: locale, allowMissing: true});
|
2017-02-01 21:41:29 +00:00
|
|
|
helper.getJSON('locale/' + _.locale() + '.json?' + config.cacheBreaker).then(setTranslation);
|
2016-05-22 12:51:30 +00:00
|
|
|
|
|
|
|
var router = new Router();
|
|
|
|
|
|
|
|
var urls = [];
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
if (typeof config.dataPath === 'string' || config.dataPath instanceof String) {
|
2016-05-22 12:51:30 +00:00
|
|
|
config.dataPath = [config.dataPath];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i in config.dataPath) {
|
2017-01-29 23:51:08 +00:00
|
|
|
urls.push(config.dataPath[i] + 'nodes.json');
|
|
|
|
urls.push(config.dataPath[i] + 'graph.json');
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function update() {
|
2016-05-26 16:37:24 +00:00
|
|
|
return Promise.all(urls.map(helper.getJSON))
|
2016-05-22 12:51:30 +00:00
|
|
|
.then(handleData);
|
|
|
|
}
|
|
|
|
|
|
|
|
update()
|
|
|
|
.then(function (d) {
|
|
|
|
var gui = new GUI(config, router);
|
|
|
|
gui.setData(d);
|
|
|
|
router.setData(d);
|
|
|
|
router.start();
|
|
|
|
|
|
|
|
window.setInterval(function () {
|
2017-01-29 23:51:08 +00:00
|
|
|
update().then(function (n) {
|
|
|
|
gui.setData(n);
|
|
|
|
router.setData(n);
|
2016-06-22 01:15:53 +00:00
|
|
|
router.update();
|
2016-05-22 12:51:30 +00:00
|
|
|
});
|
|
|
|
}, 60000);
|
|
|
|
})
|
|
|
|
.catch(function (e) {
|
|
|
|
document.body.textContent = e;
|
2016-05-26 16:37:24 +00:00
|
|
|
console.warn(e);
|
2016-05-22 12:51:30 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|