main.js 4.0 KB

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