main.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. define(["config", "moment", "router", "leaflet", "gui"],
  2. function (config, moment, Router, L, GUI) {
  3. return function () {
  4. function handleData(data) {
  5. var nodes = Object.keys(data[0].nodes).map(function (key) { return data[0].nodes[key] })
  6. nodes = nodes.filter( function (d) {
  7. return "firstseen" in d && "lastseen" in d
  8. })
  9. nodes.forEach( function(node) {
  10. node.firstseen = moment.utc(node.firstseen).local()
  11. node.lastseen = moment.utc(node.lastseen).local()
  12. })
  13. var now = moment()
  14. var age = moment(now).subtract(14, "days")
  15. var newnodes = limit("firstseen", age, sortByKey("firstseen", nodes).filter(online))
  16. var lostnodes = limit("lastseen", age, sortByKey("lastseen", nodes).filter(offline))
  17. var graph = data[1].batadv
  18. var graphnodes = data[0].nodes
  19. graph.nodes.forEach( function (d) {
  20. if (d.node_id in graphnodes)
  21. d.node = graphnodes[d.node_id]
  22. })
  23. graph.links.forEach( function (d) {
  24. if (graph.nodes[d.source].node)
  25. d.source = graph.nodes[d.source]
  26. else
  27. d.source = undefined
  28. if (graph.nodes[d.target].node)
  29. d.target = graph.nodes[d.target]
  30. else
  31. d.target = undefined
  32. })
  33. var links = graph.links.filter( function (d) {
  34. return d.source !== undefined && d.target !== undefined
  35. })
  36. links.forEach( function (d) {
  37. if (!("location" in d.source.node.nodeinfo && "location" in d.target.node.nodeinfo))
  38. return
  39. d.latlngs = []
  40. d.latlngs.push(L.latLng(d.source.node.nodeinfo.location.latitude, d.source.node.nodeinfo.location.longitude))
  41. d.latlngs.push(L.latLng(d.target.node.nodeinfo.location.latitude, d.target.node.nodeinfo.location.longitude))
  42. d.distance = d.latlngs[0].distanceTo(d.latlngs[1])
  43. })
  44. nodes.forEach( function (d) {
  45. d.neighbours = []
  46. })
  47. links.forEach( function (d) {
  48. d.source.node.neighbours.push({ node: d.target.node, link: d })
  49. d.target.node.neighbours.push({ node: d.source.node, link: d })
  50. })
  51. return { now: now,
  52. timestamp: moment.utc(data[0].timestamp).local(),
  53. nodes: {
  54. all: nodes,
  55. new: newnodes,
  56. lost: lostnodes
  57. },
  58. graph: {
  59. links: links,
  60. nodes: graph.nodes
  61. }
  62. }
  63. }
  64. moment.locale("de")
  65. var router = new Router()
  66. var urls = [ config.dataPath + "nodes.json",
  67. config.dataPath + "graph.json"
  68. ]
  69. Promise.all(urls.map(getJSON))
  70. .then(handleData)
  71. .then(function (d) {
  72. var gui = new GUI(config, router)
  73. gui.setData(d)
  74. router.setData(d)
  75. router.start()
  76. })
  77. .catch(function (e) {
  78. console.log(e)
  79. })
  80. }
  81. })