main.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. links: links
  59. }
  60. }
  61. moment.locale("de")
  62. var router = new Router()
  63. var urls = [ config.dataPath + "nodes.json",
  64. config.dataPath + "graph.json"
  65. ]
  66. Promise.all(urls.map(getJSON))
  67. .then(handleData)
  68. .then(function (d) {
  69. var gui = new GUI(config, router)
  70. gui.setData(d)
  71. router.setData(d)
  72. router.start()
  73. })
  74. }
  75. })