main.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. define(["config", "moment", "router", "leaflet", "gui", "numeral"],
  2. function (config, moment, Router, L, GUI, numeral) {
  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. var ids = [d.source.node.nodeinfo.node_id, d.target.node.nodeinfo.node_id]
  38. d.id = ids.sort().join("-")
  39. if (!("location" in d.source.node.nodeinfo && "location" in d.target.node.nodeinfo))
  40. return
  41. d.latlngs = []
  42. d.latlngs.push(L.latLng(d.source.node.nodeinfo.location.latitude, d.source.node.nodeinfo.location.longitude))
  43. d.latlngs.push(L.latLng(d.target.node.nodeinfo.location.latitude, d.target.node.nodeinfo.location.longitude))
  44. d.distance = d.latlngs[0].distanceTo(d.latlngs[1])
  45. })
  46. nodes.forEach( function (d) {
  47. d.neighbours = []
  48. })
  49. links.forEach( function (d) {
  50. d.source.node.neighbours.push({ node: d.target.node, link: d })
  51. d.target.node.neighbours.push({ node: d.source.node, link: d })
  52. })
  53. return { now: now,
  54. timestamp: moment.utc(data[0].timestamp).local(),
  55. nodes: {
  56. all: nodes,
  57. new: newnodes,
  58. lost: lostnodes
  59. },
  60. graph: {
  61. links: links,
  62. nodes: graph.nodes
  63. }
  64. }
  65. }
  66. numeral.language("de")
  67. moment.locale("de")
  68. var router = new Router()
  69. var urls = [ config.dataPath + "nodes.json",
  70. config.dataPath + "graph.json"
  71. ]
  72. function update() {
  73. return Promise.all(urls.map(getJSON))
  74. .then(handleData)
  75. }
  76. update()
  77. .then(function (d) {
  78. var gui = new GUI(config, router)
  79. gui.setData(d)
  80. router.setData(d)
  81. router.start()
  82. window.setInterval(function () {
  83. update().then(function (d) {
  84. gui.setData(d)
  85. router.setData(d)
  86. })
  87. }, 60000)
  88. })
  89. .catch(function (e) {
  90. console.log(e)
  91. })
  92. }
  93. })