main.js 3.6 KB

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