ソースを参照

refactor setData

Nils Schneider 9 年 前
コミット
a031ac336d
7 ファイル変更47 行追加35 行削除
  1. 3 3
      lib/linklist.js
  2. 21 11
      lib/main.js
  3. 7 7
      lib/map.js
  4. 5 5
      lib/meshstats.js
  5. 4 4
      lib/nodelist.js
  6. 3 3
      lib/router.js
  7. 4 2
      lib/simplenodelist.js

+ 3 - 3
lib/linklist.js

@@ -8,8 +8,8 @@ define(["tablesort", "tablesort.numeric"], function (Tablesort) {
       d.appendChild(el)
     }
 
-    self.setData = function (links) {
-      if (links.length === 0)
+    self.setData = function (data) {
+      if (data.links.length === 0)
         return
 
       var h2 = document.createElement("h2")
@@ -39,7 +39,7 @@ define(["tablesort", "tablesort.numeric"], function (Tablesort) {
 
       var tbody = document.createElement("tbody")
 
-      links.forEach( function (d) {
+      data.links.forEach( function (d) {
         var row = document.createElement("tr")
         var td1 = document.createElement("td")
         var a = document.createElement("a")

+ 21 - 11
lib/main.js

@@ -18,8 +18,8 @@ function (config, moment, chroma, Router, Map, Sidebar, Tabs, Container, Meshsta
       document.body.insertBefore(map.div, document.body.firstChild)
 
       meshstats = new Meshstats()
-      newnodeslist = new SimpleNodelist(config, "firstseen", router, "Neue Knoten")
-      lostnodeslist = new SimpleNodelist(config, "lastseen", router, "Verschwundene Knoten")
+      newnodeslist = new SimpleNodelist(config, "new", "firstseen", router, "Neue Knoten")
+      lostnodeslist = new SimpleNodelist(config, "lost", "lastseen", router, "Verschwundene Knoten")
       nodelist = new Nodelist(router)
       linklist = new Linklist(linkScale, router)
 
@@ -37,8 +37,7 @@ function (config, moment, chroma, Router, Map, Sidebar, Tabs, Container, Meshsta
     }
 
     function handleData(data) {
-      var nodedict = data[0]
-      var nodes = Object.keys(nodedict.nodes).map(function (key) { return nodedict.nodes[key] })
+      var nodes = Object.keys(data[0].nodes).map(function (key) { return data[0].nodes[key] })
 
       nodes = nodes.filter( function (d) {
         return "firstseen" in d && "lastseen" in d
@@ -99,13 +98,15 @@ function (config, moment, chroma, Router, Map, Sidebar, Tabs, Container, Meshsta
         d.target.node.neighbours.push({ node: d.source.node, link: d })
       })
 
-      map.setData(now, nodes, links, newnodes, lostnodes)
-      meshstats.setData(nodes)
-      nodelist.setData(now, nodes)
-      linklist.setData(links)
-      newnodeslist.setData(newnodes)
-      lostnodeslist.setData(lostnodes)
-      router.setData(nodes, links)
+      return { now: now,
+               timestamp: data[0].timestamp,
+               nodes: {
+                 all: nodes,
+                 new: newnodes,
+                 lost: lostnodes
+               },
+               links: links
+             }
     }
 
     var urls = [ config.dataPath + "nodes.json",
@@ -118,6 +119,15 @@ function (config, moment, chroma, Router, Map, Sidebar, Tabs, Container, Meshsta
         return d
       })
       .then(handleData)
+      .then(function (d) {
+        map.setData(d)
+        meshstats.setData(d)
+        nodelist.setData(d)
+        linklist.setData(d)
+        newnodeslist.setData(d)
+        lostnodeslist.setData(d)
+        router.setData(d)
+      })
       .then(function () { router.start() })
 
   }

+ 7 - 7
lib/map.js

@@ -77,15 +77,15 @@ define(["leaflet", "moment", "leaflet.label"], function (L, moment) {
     var nodeDict = {}
     var linkDict = {}
 
-    self.setData = function (now, nodes, links, newnodes, lostnodes) {
+    self.setData = function (data) {
       nodeDict = {}
       linkDict = {}
 
-      var lines = addLinksToMap(linkDict, linkScale, links, router)
+      var lines = addLinksToMap(linkDict, linkScale, data.links, router)
       L.featureGroup(lines).addTo(map)
 
-      var nodesOnline = subtract(nodes.filter(online), newnodes)
-      var nodesOffline = subtract(nodes.filter(offline), lostnodes)
+      var nodesOnline = subtract(data.nodes.all.filter(online), data.nodes.new)
+      var nodesOffline = subtract(data.nodes.all.filter(offline), data.nodes.lost)
 
       var markersOnline = nodesOnline.filter(has_location)
         .map(mkMarker(nodeDict, function () { return iconOnline }, router))
@@ -93,12 +93,12 @@ define(["leaflet", "moment", "leaflet.label"], function (L, moment) {
       var markersOffline = nodesOffline.filter(has_location)
         .map(mkMarker(nodeDict, function () { return iconOffline }, router))
 
-      var markersNew = newnodes.filter(has_location)
+      var markersNew = data.nodes.new.filter(has_location)
         .map(mkMarker(nodeDict, function () { return iconNew }, router))
 
-      var markersLost = lostnodes.filter(has_location)
+      var markersLost = data.nodes.lost.filter(has_location)
         .map(mkMarker(nodeDict, function (d) {
-          if (d.lastseen.isAfter(moment(now).subtract(3, "days")))
+          if (d.lastseen.isAfter(moment(data.now).subtract(3, "days")))
             return iconAlert
 
           return iconLost

+ 5 - 5
lib/meshstats.js

@@ -3,12 +3,12 @@ define(["moment"], function (moment) {
     var self = this
     var p
 
-    self.setData = function (nodes) {
-      var totalNodes = sum(nodes.filter(online).map(one))
-      var totalClients = sum(nodes.filter(online).map( function (d) {
+    self.setData = function (d) {
+      var totalNodes = sum(d.nodes.all.filter(online).map(one))
+      var totalClients = sum(d.nodes.all.filter(online).map( function (d) {
         return d.statistics.clients
       }))
-      var totalGateways = sum(nodes.filter(online).filter( function (d) {
+      var totalGateways = sum(d.nodes.all.filter(online).filter( function (d) {
         return d.flags.gateway
       }).map(one))
 
@@ -17,7 +17,7 @@ define(["moment"], function (moment) {
                       totalGateways + " Gateways"
 
       p.appendChild(document.createElement("br"))
-      p.appendChild(document.createTextNode("Diese Daten sind von " + moment(nodes.timestamp).format("LLLL") + "."))
+      p.appendChild(document.createTextNode("Diese Daten sind von " + moment(d.timestamp).format("LLLL") + "."))
     }
 
     self.render = function (el) {

+ 4 - 4
lib/nodelist.js

@@ -27,8 +27,8 @@ define(["tablesort", "tablesort.numeric"], function (Tablesort) {
       d.appendChild(el)
     }
 
-    self.setData = function (now, nodes) {
-      if (nodes.length === 0)
+    self.setData = function (data) {
+      if (data.nodes.all.length === 0)
         return
 
       var h2 = document.createElement("h2")
@@ -58,7 +58,7 @@ define(["tablesort", "tablesort.numeric"], function (Tablesort) {
 
       var tbody = document.createElement("tbody")
 
-      nodes.forEach( function (d) {
+      data.nodes.all.forEach( function (d) {
         var row = document.createElement("tr")
 
         var td1 = document.createElement("td")
@@ -79,7 +79,7 @@ define(["tablesort", "tablesort.numeric"], function (Tablesort) {
         }
 
         var td2 = document.createElement("td")
-        showUptime(td2, now, d)
+        showUptime(td2, data.now, d)
         row.appendChild(td2)
 
         var td3 = document.createElement("td")

+ 3 - 3
lib/router.js

@@ -105,15 +105,15 @@ define(function () {
 
     self.addTarget = function (d) { targets.push(d) }
 
-    self.setData = function (nodes, links) {
+    self.setData = function (data) {
       objects.nodes = {}
       objects.links = {}
 
-      nodes.forEach( function (d) {
+      data.nodes.all.forEach( function (d) {
         objects.nodes[d.nodeinfo.node_id] = d
       })
 
-      links.forEach( function (d) {
+      data.links.forEach( function (d) {
         objects.links[linkId(d)] = d
       })
     }

+ 4 - 2
lib/simplenodelist.js

@@ -1,5 +1,5 @@
 define(["moment"], function (moment) {
-  return function(config, field, router, title) {
+  return function(config, nodes, field, router, title) {
     var self = this
     var el
 
@@ -8,7 +8,9 @@ define(["moment"], function (moment) {
       d.appendChild(el)
     }
 
-    self.setData = function (list) {
+    self.setData = function (data) {
+      var list = data.nodes[nodes]
+
       if (list.length === 0)
         return