2015-04-06 13:54:26 +00:00
|
|
|
|
define(["d3", "leaflet", "moment", "leaflet.label"], function (d3, L, moment) {
|
2015-03-24 23:54:00 +00:00
|
|
|
|
var options = { worldCopyJump: true,
|
|
|
|
|
zoomControl: false
|
|
|
|
|
}
|
2015-03-25 18:45:21 +00:00
|
|
|
|
function mkMarker(dict, iconFunc, router) {
|
2015-03-24 23:54:00 +00:00
|
|
|
|
return function (d) {
|
2015-03-25 00:27:05 +00:00
|
|
|
|
var m = L.circleMarker([d.nodeinfo.location.latitude, d.nodeinfo.location.longitude], iconFunc(d))
|
2015-03-24 23:54:00 +00:00
|
|
|
|
|
2015-03-25 01:15:17 +00:00
|
|
|
|
m.resetStyle = function () {
|
|
|
|
|
m.setStyle(iconFunc(d))
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-29 14:14:10 +00:00
|
|
|
|
m.on("click", router.node(d))
|
2015-03-25 01:14:56 +00:00
|
|
|
|
m.bindLabel(d.nodeinfo.hostname)
|
2015-03-24 23:54:00 +00:00
|
|
|
|
|
|
|
|
|
dict[d.nodeinfo.node_id] = m
|
|
|
|
|
|
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-25 18:45:21 +00:00
|
|
|
|
function addLinksToMap(dict, linkScale, graph, router) {
|
2015-03-24 23:54:00 +00:00
|
|
|
|
graph = graph.filter( function (d) {
|
|
|
|
|
return "distance" in d
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
var lines = graph.map( function (d) {
|
|
|
|
|
var opts = { color: linkScale(d.tq).hex(),
|
2015-03-25 01:15:17 +00:00
|
|
|
|
weight: 4,
|
|
|
|
|
opacity: 0.5,
|
|
|
|
|
dashArray: "none"
|
2015-03-24 23:54:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var line = L.polyline(d.latlngs, opts)
|
|
|
|
|
|
2015-03-25 01:15:17 +00:00
|
|
|
|
line.resetStyle = function () {
|
|
|
|
|
line.setStyle(opts)
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-25 01:14:56 +00:00
|
|
|
|
line.bindLabel(d.source.node.nodeinfo.hostname + " – " + d.target.node.nodeinfo.hostname + "<br><strong>" + showDistance(d) + " / " + showTq(d) + "</strong>")
|
2015-03-29 14:14:10 +00:00
|
|
|
|
line.on("click", router.link(d))
|
2015-03-24 23:54:00 +00:00
|
|
|
|
|
2015-04-06 21:10:37 +00:00
|
|
|
|
dict[d.id] = line
|
2015-03-24 23:54:00 +00:00
|
|
|
|
|
|
|
|
|
return line
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return lines
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-25 01:15:17 +00:00
|
|
|
|
var iconOnline = { color: "#1566A9", radius: 6, fillOpacity: 0.5, weight: 2, className: "stroke-first" }
|
2015-03-26 12:51:08 +00:00
|
|
|
|
var iconOffline = { color: "#D43E2A", radius: 3, fillOpacity: 0.5, weight: 1, className: "stroke-first" }
|
|
|
|
|
var iconLost = { color: "#D43E2A", radius: 6, fillOpacity: 0.5, weight: 1, className: "stroke-first" }
|
2015-03-25 09:29:41 +00:00
|
|
|
|
var iconAlert = { color: "#D43E2A", radius: 6, fillOpacity: 0.5, weight: 2, className: "stroke-first node-alert" }
|
2015-03-25 01:15:17 +00:00
|
|
|
|
var iconNew = { color: "#558020", radius: 6, fillOpacity: 0.5, weight: 2, className: "stroke-first" }
|
2015-03-24 23:54:00 +00:00
|
|
|
|
|
2015-04-06 13:54:26 +00:00
|
|
|
|
return function (config, linkScale, sidebar, router) {
|
2015-03-24 23:54:00 +00:00
|
|
|
|
var self = this
|
2015-04-06 13:54:26 +00:00
|
|
|
|
var barycenter
|
2015-04-02 22:02:00 +00:00
|
|
|
|
var groupOnline, groupOffline, groupNew, groupLost, groupLines
|
2015-03-24 23:54:00 +00:00
|
|
|
|
|
|
|
|
|
var el = document.createElement("div")
|
2015-03-29 14:14:10 +00:00
|
|
|
|
el.classList.add("map")
|
2015-03-24 23:54:00 +00:00
|
|
|
|
self.div = el
|
|
|
|
|
|
|
|
|
|
var map = L.map(el, options)
|
|
|
|
|
|
|
|
|
|
L.tileLayer("https://otile{s}-s.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.jpg", {
|
|
|
|
|
subdomains: "1234",
|
|
|
|
|
type: "osm",
|
2015-03-26 12:51:08 +00:00
|
|
|
|
attribution: "Tiles © <a href=\"https://www.mapquest.com/\" target=\"_blank\">MapQuest</a>, Data CC-BY-SA OpenStreetMap",
|
2015-03-24 23:54:00 +00:00
|
|
|
|
maxZoom: 18
|
|
|
|
|
}).addTo(map)
|
|
|
|
|
|
2015-03-25 00:16:15 +00:00
|
|
|
|
var nodeDict = {}
|
|
|
|
|
var linkDict = {}
|
2015-04-06 09:58:02 +00:00
|
|
|
|
var highlight
|
|
|
|
|
|
|
|
|
|
function resetMarkerStyles(nodes, links) {
|
|
|
|
|
Object.keys(nodes).forEach( function (d) {
|
|
|
|
|
nodes[d].resetStyle()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Object.keys(links).forEach( function (d) {
|
|
|
|
|
links[d].resetStyle()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setView(bounds) {
|
|
|
|
|
map.fitBounds(bounds, {paddingTopLeft: [sidebar.getWidth(), 0]})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function resetZoom() {
|
|
|
|
|
setView(barycenter.getBounds())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function goto(m) {
|
|
|
|
|
var bounds
|
|
|
|
|
|
|
|
|
|
if ("getBounds" in m)
|
|
|
|
|
bounds = m.getBounds()
|
|
|
|
|
else
|
|
|
|
|
bounds = L.latLngBounds([m.getLatLng()])
|
|
|
|
|
|
|
|
|
|
setView(bounds)
|
|
|
|
|
|
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateView(nopanzoom) {
|
|
|
|
|
resetMarkerStyles(nodeDict, linkDict)
|
|
|
|
|
var m
|
|
|
|
|
|
|
|
|
|
if (highlight !== undefined)
|
|
|
|
|
if (highlight.type === "node") {
|
|
|
|
|
m = nodeDict[highlight.o.nodeinfo.node_id]
|
|
|
|
|
|
|
|
|
|
if (m)
|
|
|
|
|
m.setStyle({ fillColor: m.options.color, color: "orange", weight: 20, fillOpacity: 1, opacity: 0.7 })
|
|
|
|
|
} else if (highlight.type === "link") {
|
|
|
|
|
m = linkDict[linkId(highlight.o)]
|
|
|
|
|
|
|
|
|
|
if (m)
|
|
|
|
|
m.setStyle({ weight: 7, opacity: 1, dashArray: "10, 10" })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!nopanzoom)
|
|
|
|
|
if (m)
|
|
|
|
|
goto(m)
|
|
|
|
|
else
|
|
|
|
|
resetZoom()
|
|
|
|
|
}
|
2015-03-25 00:16:15 +00:00
|
|
|
|
|
2015-04-06 13:54:26 +00:00
|
|
|
|
function calcBarycenter(nodes) {
|
|
|
|
|
nodes = nodes.map(function (d) { return d.nodeinfo.location })
|
|
|
|
|
var lats = nodes.map(function (d) { return d.latitude })
|
|
|
|
|
var lngs = nodes.map(function (d) { return d.longitude })
|
|
|
|
|
|
|
|
|
|
var barycenter = L.latLng(d3.median(lats), d3.median(lngs))
|
|
|
|
|
var barycenterDev = [d3.deviation(lats), d3.deviation(lngs)]
|
|
|
|
|
|
|
|
|
|
var barycenterCircle = L.latLng(barycenter.lat + barycenterDev[0],
|
|
|
|
|
barycenter.lng + barycenterDev[1])
|
|
|
|
|
|
|
|
|
|
var r = barycenter.distanceTo(barycenterCircle)
|
|
|
|
|
|
|
|
|
|
return L.circle(barycenter, r * config.mapSigmaScale)
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-29 15:48:25 +00:00
|
|
|
|
self.setData = function (data) {
|
2015-03-25 00:16:15 +00:00
|
|
|
|
nodeDict = {}
|
|
|
|
|
linkDict = {}
|
2015-03-24 23:54:00 +00:00
|
|
|
|
|
2015-04-02 22:02:00 +00:00
|
|
|
|
if (groupOffline)
|
|
|
|
|
groupOffline.clearLayers()
|
|
|
|
|
|
|
|
|
|
if (groupOnline)
|
|
|
|
|
groupOnline.clearLayers()
|
|
|
|
|
|
|
|
|
|
if (groupNew)
|
|
|
|
|
groupNew.clearLayers()
|
|
|
|
|
|
|
|
|
|
if (groupLost)
|
|
|
|
|
groupLost.clearLayers()
|
|
|
|
|
|
|
|
|
|
if (groupLines)
|
|
|
|
|
groupLines.clearLayers()
|
|
|
|
|
|
2015-03-31 15:21:58 +00:00
|
|
|
|
var lines = addLinksToMap(linkDict, linkScale, data.graph.links, router)
|
2015-04-02 22:02:00 +00:00
|
|
|
|
groupLines = L.featureGroup(lines).addTo(map)
|
2015-03-24 23:54:00 +00:00
|
|
|
|
|
2015-04-06 13:54:26 +00:00
|
|
|
|
barycenter = calcBarycenter(data.nodes.all.filter(has_location))
|
|
|
|
|
|
2015-03-29 15:48:25 +00:00
|
|
|
|
var nodesOnline = subtract(data.nodes.all.filter(online), data.nodes.new)
|
|
|
|
|
var nodesOffline = subtract(data.nodes.all.filter(offline), data.nodes.lost)
|
2015-03-24 23:54:00 +00:00
|
|
|
|
|
2015-03-26 12:51:08 +00:00
|
|
|
|
var markersOnline = nodesOnline.filter(has_location)
|
2015-03-29 14:14:10 +00:00
|
|
|
|
.map(mkMarker(nodeDict, function () { return iconOnline }, router))
|
2015-03-24 23:54:00 +00:00
|
|
|
|
|
2015-03-26 12:51:08 +00:00
|
|
|
|
var markersOffline = nodesOffline.filter(has_location)
|
2015-03-29 14:14:10 +00:00
|
|
|
|
.map(mkMarker(nodeDict, function () { return iconOffline }, router))
|
2015-03-25 09:29:41 +00:00
|
|
|
|
|
2015-03-29 15:48:25 +00:00
|
|
|
|
var markersNew = data.nodes.new.filter(has_location)
|
2015-03-29 14:14:10 +00:00
|
|
|
|
.map(mkMarker(nodeDict, function () { return iconNew }, router))
|
2015-03-24 23:54:00 +00:00
|
|
|
|
|
2015-03-29 15:48:25 +00:00
|
|
|
|
var markersLost = data.nodes.lost.filter(has_location)
|
2015-03-26 12:51:08 +00:00
|
|
|
|
.map(mkMarker(nodeDict, function (d) {
|
2015-03-29 15:48:25 +00:00
|
|
|
|
if (d.lastseen.isAfter(moment(data.now).subtract(3, "days")))
|
2015-03-26 12:51:08 +00:00
|
|
|
|
return iconAlert
|
|
|
|
|
|
|
|
|
|
return iconLost
|
|
|
|
|
}, router))
|
|
|
|
|
|
2015-04-06 09:58:02 +00:00
|
|
|
|
groupOffline = L.featureGroup(markersOffline).addTo(map)
|
|
|
|
|
groupOnline = L.featureGroup(markersOnline).addTo(map)
|
|
|
|
|
groupNew = L.featureGroup(markersNew).addTo(map)
|
|
|
|
|
groupLost = L.featureGroup(markersLost).addTo(map)
|
2015-03-29 14:14:10 +00:00
|
|
|
|
|
2015-04-06 09:58:02 +00:00
|
|
|
|
updateView(true)
|
2015-03-29 14:14:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-06 09:58:02 +00:00
|
|
|
|
self.resetView = function () {
|
|
|
|
|
highlight = undefined
|
|
|
|
|
updateView()
|
2015-03-25 00:16:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.gotoNode = function (d) {
|
2015-04-06 09:58:02 +00:00
|
|
|
|
highlight = {type: "node", o: d}
|
|
|
|
|
updateView()
|
2015-03-25 00:16:15 +00:00
|
|
|
|
}
|
2015-03-24 23:54:00 +00:00
|
|
|
|
|
2015-03-25 00:16:15 +00:00
|
|
|
|
self.gotoLink = function (d) {
|
2015-04-06 09:58:02 +00:00
|
|
|
|
highlight = {type: "link", o: d}
|
|
|
|
|
updateView()
|
2015-03-24 23:54:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-31 21:24:24 +00:00
|
|
|
|
self.destroy = function () {
|
|
|
|
|
map.remove()
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-24 23:54:00 +00:00
|
|
|
|
return self
|
|
|
|
|
}
|
|
|
|
|
})
|