meshviewer/lib/main.js

124 lines
3.6 KiB
JavaScript
Raw Normal View History

2015-04-20 22:10:29 +00:00
define(["../config", "moment", "router", "leaflet", "gui", "numeral"],
function (config, moment, Router, L, GUI, numeral) {
2015-03-29 02:35:20 +00:00
return function () {
2015-03-29 14:14:10 +00:00
function handleData(data) {
2015-04-07 19:10:37 +00:00
var dataNodes = data[0]
var dataGraph = data[1]
if (dataNodes.version !== 1 || dataGraph.version !== 1) {
var err = "Unsupported nodes or graph version: " + dataNodes.version + " " + dataGraph.version
throw err
}
var nodes = Object.keys(dataNodes.nodes).map(function (key) { return dataNodes.nodes[key] })
2015-03-24 23:54:00 +00:00
nodes = nodes.filter( function (d) {
return "firstseen" in d && "lastseen" in d
})
nodes.forEach( function(node) {
2015-03-29 17:10:24 +00:00
node.firstseen = moment.utc(node.firstseen).local()
node.lastseen = moment.utc(node.lastseen).local()
2015-03-24 23:54:00 +00:00
})
var now = moment()
2015-04-26 11:12:11 +00:00
var age = moment(now).subtract(config.maxAge, "days")
2015-03-24 23:54:00 +00:00
var newnodes = limit("firstseen", age, sortByKey("firstseen", nodes).filter(online))
var lostnodes = limit("lastseen", age, sortByKey("lastseen", nodes).filter(offline))
2015-04-07 19:10:37 +00:00
var graphnodes = dataNodes.nodes
var graph = dataGraph.batadv
2015-03-24 23:54:00 +00:00
graph.nodes.forEach( function (d) {
if (d.node_id in graphnodes)
d.node = graphnodes[d.node_id]
})
graph.links.forEach( function (d) {
if (graph.nodes[d.source].node)
d.source = graph.nodes[d.source]
else
d.source = undefined
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.source !== undefined && d.target !== undefined
})
links.forEach( function (d) {
2015-04-06 21:10:37 +00:00
var ids = [d.source.node.nodeinfo.node_id, d.target.node.nodeinfo.node_id]
d.id = ids.sort().join("-")
2015-03-24 23:54:00 +00:00
if (!("location" in d.source.node.nodeinfo && "location" in d.target.node.nodeinfo))
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) {
d.source.node.neighbours.push({ node: d.target.node, link: d })
d.target.node.neighbours.push({ node: d.source.node, link: d })
})
2015-03-29 15:48:25 +00:00
return { now: now,
2015-03-29 17:10:24 +00:00
timestamp: moment.utc(data[0].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
}
2015-03-29 15:48:25 +00:00
}
2015-03-24 23:54:00 +00:00
}
2015-03-29 14:14:10 +00:00
numeral.language("de")
2015-03-29 17:54:43 +00:00
moment.locale("de")
2015-03-29 17:54:43 +00:00
var router = new Router()
2015-03-29 15:54:43 +00:00
2015-03-29 14:14:10 +00:00
var urls = [ config.dataPath + "nodes.json",
config.dataPath + "graph.json"
]
2015-04-03 00:32:32 +00:00
function update() {
return Promise.all(urls.map(getJSON))
.then(handleData)
}
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) {
2015-03-29 17:54:43 +00:00
var gui = new GUI(config, router)
gui.setData(d)
router.setData(d)
2015-03-29 17:38:50 +00:00
router.start()
2015-04-03 00:32:32 +00:00
window.setInterval(function () {
update().then(function (d) {
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) {
2015-04-07 19:10:37 +00:00
document.body.textContent = e
2015-03-31 16:51:35 +00:00
console.log(e)
})
2015-03-24 23:54:00 +00:00
}
})