hopglass/lib/main.js

139 lines
4.3 KiB
JavaScript
Raw Normal View History

2015-03-29 16:14:10 +02:00
define(["config", "moment", "chroma-js", "router", "map", "sidebar", "tabs", "container", "meshstats", "linklist", "nodelist", "simplenodelist", "infobox/main", "leaflet"],
function (config, moment, chroma, Router, Map, Sidebar, Tabs, Container, Meshstats, Linklist, Nodelist, SimpleNodelist, Infobox, L) {
2015-03-29 04:35:20 +02:00
return function () {
2015-03-29 17:54:43 +02:00
var dataTargets = []
var router
2015-03-25 00:54:00 +01:00
function createGUI() {
moment.locale("de")
2015-03-25 00:54:00 +01:00
2015-03-29 16:14:10 +02:00
var linkScale = chroma.scale(chroma.interpolate.bezier(["green", "yellow", "red"])).domain([1, 5])
var sidebar = new Sidebar(document.body)
var infobox = new Infobox(config, sidebar, router)
2015-03-26 00:33:11 +01:00
var tabs = new Tabs()
2015-03-26 13:18:55 +01:00
var overview = new Container()
2015-03-25 11:21:09 +01:00
2015-03-29 17:54:43 +02:00
var map = new Map(linkScale, sidebar, router)
document.body.insertBefore(map.div, document.body.firstChild)
2015-03-25 11:21:09 +01:00
2015-03-29 17:54:43 +02:00
var meshstats = new Meshstats()
var newnodeslist = new SimpleNodelist(config, "new", "firstseen", router, "Neue Knoten")
var lostnodeslist = new SimpleNodelist(config, "lost", "lastseen", router, "Verschwundene Knoten")
var nodelist = new Nodelist(router)
var linklist = new Linklist(linkScale, router)
dataTargets.push(map)
dataTargets.push(meshstats)
dataTargets.push(newnodeslist)
dataTargets.push(lostnodeslist)
dataTargets.push(nodelist)
dataTargets.push(linklist)
2015-03-26 13:18:55 +01:00
overview.add(meshstats)
overview.add(newnodeslist)
overview.add(lostnodeslist)
2015-03-26 00:33:11 +01:00
sidebar.add(tabs)
2015-03-26 13:18:55 +01:00
tabs.add("Übersicht", overview)
2015-03-26 01:31:46 +01:00
tabs.add("Alle Knoten", nodelist)
2015-03-26 00:33:11 +01:00
tabs.add("Verbindungen", linklist)
router.addTarget(infobox)
router.addTarget(map)
}
2015-03-25 00:54:00 +01:00
2015-03-29 16:14:10 +02:00
function handleData(data) {
2015-03-29 17:48:25 +02:00
var nodes = Object.keys(data[0].nodes).map(function (key) { return data[0].nodes[key] })
2015-03-25 00:54:00 +01:00
nodes = nodes.filter( function (d) {
return "firstseen" in d && "lastseen" in d
})
nodes.forEach( function(node) {
2015-03-29 19:10:24 +02:00
node.firstseen = moment.utc(node.firstseen).local()
node.lastseen = moment.utc(node.lastseen).local()
2015-03-25 00:54:00 +01:00
})
var now = moment()
2015-03-29 16:14:10 +02:00
var age = moment(now).subtract(14, "days")
2015-03-25 00:54:00 +01:00
var newnodes = limit("firstseen", age, sortByKey("firstseen", nodes).filter(online))
var lostnodes = limit("lastseen", age, sortByKey("lastseen", nodes).filter(offline))
var graph = data[1].batadv
var graphnodes = data[0].nodes
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) {
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 17:48:25 +02:00
return { now: now,
2015-03-29 19:10:24 +02:00
timestamp: moment.utc(data[0].timestamp).local(),
2015-03-29 17:48:25 +02:00
nodes: {
all: nodes,
new: newnodes,
lost: lostnodes
},
links: links
}
2015-03-25 00:54:00 +01:00
}
2015-03-29 16:14:10 +02:00
2015-03-29 17:54:43 +02:00
router = new Router()
2015-03-29 16:14:10 +02:00
var urls = [ config.dataPath + "nodes.json",
config.dataPath + "graph.json"
]
Promise.all(urls.map(getJSON))
.then(function (d) {
createGUI()
return d
})
.then(handleData)
2015-03-29 17:48:25 +02:00
.then(function (d) {
2015-03-29 17:54:43 +02:00
dataTargets.forEach(function (t) {
t.setData(d)
})
2015-03-29 17:48:25 +02:00
})
2015-03-29 16:14:10 +02:00
.then(function () { router.start() })
2015-03-25 00:54:00 +01:00
}
})