meshviewer/history.js

391 lines
9.9 KiB
JavaScript
Raw Normal View History

2015-03-20 13:01:25 +00:00
L.AwesomeMarkers.Icon.prototype.options.prefix = 'ion'
2015-03-20 08:46:24 +00:00
document.addEventListener('DOMContentLoaded', main)
function get(url) {
return new Promise(function(resolve, reject) {
var req = new XMLHttpRequest();
req.open('GET', url);
req.onload = function() {
if (req.status == 200) {
resolve(req.response);
}
else {
reject(Error(req.statusText));
}
};
req.onerror = function() {
reject(Error("Network Error"));
};
req.send();
});
}
function getJSON(url) {
return get(url).then(JSON.parse)
}
function main() {
2015-03-20 20:03:20 +00:00
getJSON("config.json").then( function (config) {
var options = { worldCopyJump: true,
zoomControl: false
}
2015-03-20 12:30:28 +00:00
2015-03-20 20:03:20 +00:00
var map = L.map(document.getElementById("map"), options)
2015-03-20 12:30:28 +00:00
2015-03-20 20:03:20 +00:00
var sh = document.getElementById("sidebarhandle")
sh.onclick = function () {
var sb = document.getElementById("sidebar")
2015-03-20 12:30:28 +00:00
2015-03-20 20:03:20 +00:00
if (sb.classList.contains("hidden"))
sb.classList.remove("hidden")
else
sb.classList.add("hidden")
2015-03-20 12:30:28 +00:00
2015-03-20 20:03:20 +00:00
map.invalidateSize()
}
2015-03-20 12:30:28 +00:00
2015-03-20 12:43:30 +00:00
2015-03-20 20:03:20 +00:00
var urls = [ config.dataPath + 'nodes.json',
config.dataPath + 'graph.json'
]
2015-03-20 12:43:30 +00:00
2015-03-20 20:03:20 +00:00
var p = Promise.all(urls.map(getJSON))
p.then(handle_data(config, map))
})
2015-03-20 08:46:24 +00:00
}
function sort(key, d) {
return d.slice().sort( function (a, b) {
return a[key] - b[key]
}).reverse()
}
function limit(key, m, d) {
return d.filter( function (d) {
return d[key].isAfter(m)
})
}
function offline(d) {
return !d.flags.online
}
function online(d) {
return d.flags.online
}
function has_location(d) {
return "location" in d.nodeinfo
}
function subtract(a, b) {
var ids = {}
b.forEach( function (d) {
ids[d.nodeinfo.node_id] = true
})
return a.filter( function (d) {
return !(d.nodeinfo.node_id in ids)
})
}
2015-03-20 20:03:20 +00:00
function handle_data(config, map) {
2015-03-20 12:30:28 +00:00
return function (data) {
2015-03-20 12:43:30 +00:00
var nodedict = data[0]
var nodes = Object.keys(nodedict.nodes).map(function (key) { return nodedict.nodes[key] })
2015-03-20 08:46:24 +00:00
2015-03-20 12:30:28 +00:00
nodes = nodes.filter( function (d) {
return "firstseen" in d && "lastseen" in d
})
2015-03-20 08:46:24 +00:00
2015-03-20 12:30:28 +00:00
nodes.forEach( function(node) {
node.firstseen = moment(node.firstseen)
node.lastseen = moment(node.lastseen)
})
2015-03-20 08:46:24 +00:00
2015-03-20 12:30:28 +00:00
var age = moment().subtract(14, 'days')
2015-03-20 08:46:24 +00:00
2015-03-20 12:30:28 +00:00
var newnodes = limit("firstseen", age, sort("firstseen", nodes).filter(online))
var lostnodes = limit("lastseen", age, sort("lastseen", nodes).filter(offline))
2015-03-20 08:46:24 +00:00
2015-03-20 12:30:28 +00:00
var onlinenodes = subtract(nodes.filter(online).filter(has_location), newnodes)
2015-03-20 08:46:24 +00:00
2015-03-20 14:03:39 +00:00
var graph = data[1].batadv
2015-03-21 09:39:55 +00:00
var graphnodes = data[0].nodes
2015-03-20 14:03:39 +00:00
graph.nodes.forEach( function (d) {
2015-03-21 09:39:55 +00:00
if (d.node_id in graphnodes && "location" in graphnodes[d.node_id].nodeinfo)
d.node = graphnodes[d.node_id]
2015-03-20 14:03:39 +00:00
})
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
})
graph = graph.links.filter( function (d) {
var ok = d.source !== undefined && d.target !== undefined
return ok
})
2015-03-20 19:08:28 +00:00
graph.forEach( function (d) {
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])
})
longlinks = graph.slice().sort( function (a, b) {
return a.distance - b.distance
2015-03-20 20:03:20 +00:00
}).reverse().slice(0, Math.ceil(config.longLinkPercentile * graph.length))
2015-03-20 19:08:28 +00:00
2015-03-20 21:55:23 +00:00
var markers = mkmap(map, newnodes, lostnodes, onlinenodes, graph)
2015-03-21 14:59:25 +00:00
var gotoAnything = gotoBuilder(markers, showNodeinfo)
addToList(document.getElementById("newnodes"), config.showContact, "firstseen", gotoAnything.node, newnodes)
addToList(document.getElementById("lostnodes"), config.showContact, "lastseen", gotoAnything.node, lostnodes)
addToLongLinksList(document.getElementById("longlinks"), gotoAnything.link, longlinks)
2015-03-21 09:40:58 +00:00
showMeshstats(document.getElementById("meshstats"), nodes)
2015-03-20 12:30:28 +00:00
}
2015-03-20 08:46:24 +00:00
}
2015-03-20 19:08:28 +00:00
function showDistance(d) {
return (new Intl.NumberFormat("de-DE", {maximumFractionDigits: 0}).format(d.distance)) + " m"
}
function showTq(d) {
var opts = { maximumFractionDigits: 2,
minimumFractionDigits: 2
}
return (new Intl.NumberFormat("de-DE", opts).format(d.tq)) + " TQ"
}
2015-03-20 21:55:23 +00:00
function linkId(d) {
return d.source.node.nodeinfo.node_id + "-" + d.target.node.nodeinfo.node_id
}
2015-03-20 14:03:39 +00:00
function mkmap(map, newnodes, lostnodes, onlinenodes, graph) {
2015-03-20 08:57:53 +00:00
L.control.zoom({ position: "topright" }).addTo(map)
2015-03-20 08:46:24 +00:00
L.tileLayer("http://otile{s}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.jpg", {
subdomains: "1234",
type: "osm",
attribution: "Map data Tiles &copy; <a href=\"http://www.mapquest.com/\" target=\"_blank\">MapQuest</a> <img src=\"http://developer.mapquest.com/content/osm/mq_logo.png\" />, Map data © OpenStreetMap contributors, CC-BY-SA",
maxZoom: 18
}).addTo(map)
2015-03-20 21:55:23 +00:00
var markersDict = addLinksToMap(map, graph)
2015-03-20 14:03:39 +00:00
2015-03-20 08:46:24 +00:00
var nodes = newnodes.concat(lostnodes).filter( function (d) {
return "location" in d.nodeinfo
})
var markers = nodes.map( function (d) {
2015-03-20 13:01:25 +00:00
var icon = L.AwesomeMarkers.icon({ markerColor: d.flags.online ? "green" : "red",
icon: d.flags.online ? "lightbulb" : "bug" })
2015-03-20 08:46:24 +00:00
2015-03-20 19:48:52 +00:00
var opt = { icon: icon,
title: d.nodeinfo.hostname
}
2015-03-20 08:46:24 +00:00
var m = L.marker([d.nodeinfo.location.latitude, d.nodeinfo.location.longitude], opt)
2015-03-21 09:40:09 +00:00
2015-03-20 08:46:24 +00:00
m.bindPopup(d.nodeinfo.hostname)
2015-03-20 21:55:23 +00:00
markersDict[d.nodeinfo.node_id] = m
2015-03-20 08:46:24 +00:00
return m
})
var onlinemarkers = onlinenodes.map( function (d) {
2015-03-20 13:01:25 +00:00
var opt = { color: "#76B22D",
fillColor: "#76B22D",
radius: 5,
opacity: 0.7,
fillOpacity: 0.5
2015-03-20 08:46:24 +00:00
}
var m = L.circleMarker([d.nodeinfo.location.latitude, d.nodeinfo.location.longitude], opt)
m.bindPopup(d.nodeinfo.hostname)
2015-03-20 21:55:23 +00:00
markersDict[d.nodeinfo.node_id] = m
2015-03-20 08:46:24 +00:00
return m
})
var group = L.featureGroup(markers).addTo(map)
var group_online = L.featureGroup(onlinemarkers).addTo(map)
map.fitBounds(group.getBounds())
2015-03-20 21:55:23 +00:00
var funcDict = {}
Object.keys(markersDict).map( function(k) {
funcDict[k] = function (d) {
var m = markersDict[k]
if ("getBounds" in m) {
var bounds = m.getBounds()
map.fitBounds(bounds)
m.openPopup(bounds.getCenter())
} else {
map.setView(m.getLatLng(), map.getMaxZoom())
m.openPopup()
}
return false
2015-03-20 21:55:23 +00:00
}
});
return funcDict
2015-03-20 08:46:24 +00:00
}
2015-03-20 14:03:39 +00:00
function addLinksToMap(map, graph) {
2015-03-20 21:55:23 +00:00
var markersDict = {}
2015-03-20 14:03:39 +00:00
var scale = chroma.scale(['green', 'orange', 'red']).domain([1, 10])
var lines = graph.map( function (d) {
var opts = { color: scale(d.tq).hex(),
2015-03-20 19:48:52 +00:00
weight: 4
2015-03-20 14:03:39 +00:00
}
2015-03-20 19:08:28 +00:00
var line = L.polyline(d.latlngs, opts)
2015-03-20 14:03:39 +00:00
2015-03-21 12:32:07 +00:00
line.bindPopup(d.source.node.nodeinfo.hostname + " " + d.target.node.nodeinfo.hostname + "<br><strong>" + showDistance(d) + " / " + showTq(d) + "</strong>")
2015-03-20 16:58:20 +00:00
2015-03-20 21:55:23 +00:00
markersDict[linkId(d)] = line
2015-03-20 14:03:39 +00:00
return line
})
var group = L.featureGroup(lines).addTo(map)
2015-03-20 21:55:23 +00:00
return markersDict
2015-03-20 14:03:39 +00:00
}
2015-03-21 14:59:25 +00:00
function addToLongLinksList(el, gotoProxy, links) {
2015-03-20 19:08:28 +00:00
links.forEach( function (d) {
var row = document.createElement("tr")
var td1 = document.createElement("td")
2015-03-20 21:55:23 +00:00
var a = document.createElement("a")
a.textContent = d.source.node.nodeinfo.hostname + " " + d.target.node.nodeinfo.hostname
a.href = "#"
2015-03-21 14:59:25 +00:00
a.onclick = gotoProxy(d)
2015-03-20 21:55:23 +00:00
td1.appendChild(a)
2015-03-20 19:08:28 +00:00
row.appendChild(td1)
var td2 = document.createElement("td")
td2.textContent = showDistance(d)
row.appendChild(td2)
var td3 = document.createElement("td")
td3.textContent = showTq(d)
row.appendChild(td3)
2015-03-20 19:08:28 +00:00
el.appendChild(row)
})
}
2015-03-21 14:59:25 +00:00
function addToList(el, showContact, tf, gotoProxy, list) {
2015-03-20 08:46:24 +00:00
list.forEach( function (d) {
var time = moment(d[tf]).fromNow()
var row = document.createElement("tr")
var td1 = document.createElement("td")
2015-03-20 21:55:23 +00:00
var a = document.createElement("a")
a.classList.add("hostname")
a.classList.add(d.flags.online ? "online" : "offline")
a.textContent = d.nodeinfo.hostname
2015-03-21 14:59:25 +00:00
a.href = "#"
a.onclick = gotoProxy(d)
2015-03-20 21:55:23 +00:00
td1.appendChild(a)
2015-03-20 08:46:24 +00:00
2015-03-20 19:36:49 +00:00
if ("location" in d.nodeinfo) {
var span = document.createElement("span")
span.classList.add("icon")
span.classList.add("ion-location")
td1.appendChild(span)
}
2015-03-20 20:03:20 +00:00
if ("owner" in d.nodeinfo && showContact) {
2015-03-20 08:46:24 +00:00
var contact = d.nodeinfo.owner.contact
td1.appendChild(document.createTextNode(" " + contact + ""))
}
var td2 = document.createElement("td")
td2.textContent = time
row.appendChild(td1)
row.appendChild(td2)
el.appendChild(row)
})
}
2015-03-21 09:40:58 +00:00
function sum(a) {
return a.reduce( function (a, b) {
return a + b
}, 0)
}
function one() {
return 1
}
function showMeshstats(el, nodes) {
var totalNodes = sum(nodes.filter(online).map(one))
var totalClients = sum(nodes.filter(online).map( function (d) {
return d.statistics.clients
}))
var totalGateways = sum(nodes.filter(online).filter( function (d) {
return d.flags.gateway
}).map(one))
el.textContent = totalNodes + " Knoten (online), " +
totalClients + " Clients, " +
totalGateways + " Gateways"
}
function showNodeinfo(d) {
var object = document.getElementById("nodeinfo")
}
2015-03-21 14:59:25 +00:00
function gotoBuilder(markers, nodes) {
function gotoNode(d) {
if (d.nodeinfo.node_id in markers)
markers[d.nodeinfo.node_id]()
return false
}
function gotoLink(d) {
if (linkId(d) in markers)
markers[linkId(d)]()
return false
}
return { node: function (d) { return function () { return gotoNode(d) }},
link: function (d) { return function () { return gotoLink(d) }}
}
}