main.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. require(["router", "map", "sidebar", "meshstats", "linklist", "simplenodelist", "infobox/main"],
  2. function (Router, Map, Sidebar, Meshstats, Linklist, SimpleNodelist, Infobox) {
  3. getJSON("config.json").then(main)
  4. function main(config) {
  5. var linklist, lostnodeslist, map, meshstats, newnodeslist, router
  6. function createGUI() {
  7. moment.locale("de")
  8. router = new Router(config)
  9. var linkScale = chroma.scale(chroma.interpolate.bezier(['green', 'yellow', 'red'])).domain([1, 5])
  10. var sidebar = new Sidebar(document.body)
  11. var infobox = new Infobox(config, sidebar, router)
  12. map = new Map(linkScale, sidebar, router)
  13. document.body.insertBefore(map.div, document.body.firstChild)
  14. meshstats = new Meshstats()
  15. newnodeslist = new SimpleNodelist(config, "firstseen", router, "Neue Knoten")
  16. lostnodeslist = new SimpleNodelist(config, "lastseen", router, "Verschwundene Knoten")
  17. linklist = new Linklist(linkScale, router)
  18. sidebar.add(meshstats)
  19. sidebar.add(newnodeslist)
  20. sidebar.add(lostnodeslist)
  21. sidebar.add(linklist)
  22. router.addTarget(infobox)
  23. router.addTarget(map)
  24. }
  25. var urls = [ config.dataPath + 'nodes.json',
  26. config.dataPath + 'graph.json'
  27. ]
  28. Promise.all(urls.map(getJSON))
  29. .then(function (d) { createGUI(); return d })
  30. .then(handle_data)
  31. .then(function () { router.loadState(window.location.hash) })
  32. function handle_data(data) {
  33. var nodedict = data[0]
  34. var nodes = Object.keys(nodedict.nodes).map(function (key) { return nodedict.nodes[key] })
  35. nodes = nodes.filter( function (d) {
  36. return "firstseen" in d && "lastseen" in d
  37. })
  38. nodes.forEach( function(node) {
  39. node.firstseen = moment.utc(node.firstseen)
  40. node.lastseen = moment.utc(node.lastseen)
  41. })
  42. var now = moment()
  43. var age = moment(now).subtract(14, 'days')
  44. var newnodes = limit("firstseen", age, sortByKey("firstseen", nodes).filter(online))
  45. var lostnodes = limit("lastseen", age, sortByKey("lastseen", nodes).filter(offline))
  46. var onlinenodes = nodes.filter(online)
  47. var graph = data[1].batadv
  48. var graphnodes = data[0].nodes
  49. graph.nodes.forEach( function (d) {
  50. if (d.node_id in graphnodes)
  51. d.node = graphnodes[d.node_id]
  52. })
  53. graph.links.forEach( function (d) {
  54. if (graph.nodes[d.source].node)
  55. d.source = graph.nodes[d.source]
  56. else
  57. d.source = undefined
  58. if (graph.nodes[d.target].node)
  59. d.target = graph.nodes[d.target]
  60. else
  61. d.target = undefined
  62. })
  63. var links = graph.links.filter( function (d) {
  64. return d.source !== undefined && d.target !== undefined
  65. })
  66. links.forEach( function (d) {
  67. if (!("location" in d.source.node.nodeinfo && "location" in d.target.node.nodeinfo))
  68. return
  69. d.latlngs = []
  70. d.latlngs.push(L.latLng(d.source.node.nodeinfo.location.latitude, d.source.node.nodeinfo.location.longitude))
  71. d.latlngs.push(L.latLng(d.target.node.nodeinfo.location.latitude, d.target.node.nodeinfo.location.longitude))
  72. d.distance = d.latlngs[0].distanceTo(d.latlngs[1])
  73. })
  74. nodes.forEach( function (d) {
  75. d.neighbours = []
  76. })
  77. links.forEach( function (d) {
  78. d.source.node.neighbours.push({ node: d.target.node, link: d })
  79. d.target.node.neighbours.push({ node: d.source.node, link: d })
  80. })
  81. map.setData(now, newnodes, lostnodes, onlinenodes, links)
  82. meshstats.setData(nodes)
  83. linklist.setData(links)
  84. newnodeslist.setData(newnodes)
  85. lostnodeslist.setData(lostnodes)
  86. router.setData(nodes, links)
  87. }
  88. }
  89. })