main.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. define(["moment", "router", "leaflet", "gui", "numeral"],
  2. function (moment, Router, L, GUI, numeral) {
  3. return function (config) {
  4. function handleData(data) {
  5. var dataNodes = data[0]
  6. var dataGraph = data[1]
  7. if (dataNodes.version !== 2 || dataGraph.version !== 1) {
  8. var err = "Unsupported nodes or graph version: " + dataNodes.version + " " + dataGraph.version
  9. throw err
  10. }
  11. var nodes = dataNodes.nodes.filter( function (d) {
  12. return "firstseen" in d && "lastseen" in d
  13. })
  14. nodes.forEach( function(node) {
  15. node.firstseen = moment.utc(node.firstseen).local()
  16. node.lastseen = moment.utc(node.lastseen).local()
  17. })
  18. var now = moment()
  19. var age = moment(now).subtract(config.maxAge, "days")
  20. var newnodes = limit("firstseen", age, sortByKey("firstseen", nodes).filter(online))
  21. var lostnodes = limit("lastseen", age, sortByKey("lastseen", nodes).filter(offline))
  22. var graphnodes = {}
  23. dataNodes.nodes.forEach( function (d) {
  24. graphnodes[d.nodeinfo.node_id] = d
  25. })
  26. var graph = dataGraph.batadv
  27. graph.nodes.forEach( function (d) {
  28. if (d.node_id in graphnodes)
  29. d.node = graphnodes[d.node_id]
  30. })
  31. graph.links.forEach( function (d) {
  32. if (graph.nodes[d.source].node)
  33. d.source = graph.nodes[d.source]
  34. else
  35. d.source = undefined
  36. if (graph.nodes[d.target].node)
  37. d.target = graph.nodes[d.target]
  38. else
  39. d.target = undefined
  40. })
  41. var links = graph.links.filter( function (d) {
  42. return d.source !== undefined && d.target !== undefined
  43. })
  44. links.forEach( function (d) {
  45. var ids = [d.source.node.nodeinfo.node_id, d.target.node.nodeinfo.node_id]
  46. d.id = ids.sort().join("-")
  47. if (!("location" in d.source.node.nodeinfo && "location" in d.target.node.nodeinfo))
  48. return
  49. d.latlngs = []
  50. d.latlngs.push(L.latLng(d.source.node.nodeinfo.location.latitude, d.source.node.nodeinfo.location.longitude))
  51. d.latlngs.push(L.latLng(d.target.node.nodeinfo.location.latitude, d.target.node.nodeinfo.location.longitude))
  52. d.distance = d.latlngs[0].distanceTo(d.latlngs[1])
  53. })
  54. nodes.forEach( function (d) {
  55. d.neighbours = []
  56. })
  57. links.forEach( function (d) {
  58. d.source.node.neighbours.push({ node: d.target.node, link: d })
  59. d.target.node.neighbours.push({ node: d.source.node, link: d })
  60. })
  61. return { now: now,
  62. timestamp: moment.utc(dataNodes.timestamp).local(),
  63. nodes: {
  64. all: nodes,
  65. new: newnodes,
  66. lost: lostnodes
  67. },
  68. graph: {
  69. links: links,
  70. nodes: graph.nodes
  71. }
  72. }
  73. }
  74. numeral.language("de")
  75. moment.locale("de")
  76. var router = new Router()
  77. var urls = [ config.dataPath + "nodes.json",
  78. config.dataPath + "graph.json"
  79. ]
  80. function update() {
  81. return Promise.all(urls.map(getJSON))
  82. .then(handleData)
  83. }
  84. update()
  85. .then(function (d) {
  86. var gui = new GUI(config, router)
  87. gui.setData(d)
  88. router.setData(d)
  89. router.start()
  90. window.setInterval(function () {
  91. update().then(function (d) {
  92. gui.setData(d)
  93. router.setData(d)
  94. })
  95. }, 60000)
  96. })
  97. .catch(function (e) {
  98. document.body.textContent = e
  99. console.log(e)
  100. })
  101. }
  102. })