From a031ac336db1f300fd3128aaed5db4df890ce4cf Mon Sep 17 00:00:00 2001 From: Nils Schneider Date: Sun, 29 Mar 2015 17:48:25 +0200 Subject: [PATCH] refactor setData --- lib/linklist.js | 6 +++--- lib/main.js | 32 +++++++++++++++++++++----------- lib/map.js | 14 +++++++------- lib/meshstats.js | 10 +++++----- lib/nodelist.js | 8 ++++---- lib/router.js | 6 +++--- lib/simplenodelist.js | 6 ++++-- 7 files changed, 47 insertions(+), 35 deletions(-) diff --git a/lib/linklist.js b/lib/linklist.js index e500ded..ca748bb 100644 --- a/lib/linklist.js +++ b/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") diff --git a/lib/main.js b/lib/main.js index 464bf7a..9d8a23f 100644 --- a/lib/main.js +++ b/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() }) } diff --git a/lib/map.js b/lib/map.js index 17bad63..b8dc851 100644 --- a/lib/map.js +++ b/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 diff --git a/lib/meshstats.js b/lib/meshstats.js index b8ca925..f972247 100644 --- a/lib/meshstats.js +++ b/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) { diff --git a/lib/nodelist.js b/lib/nodelist.js index 298464e..dabd48c 100644 --- a/lib/nodelist.js +++ b/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") diff --git a/lib/router.js b/lib/router.js index 303805d..ac5555f 100644 --- a/lib/router.js +++ b/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 }) } diff --git a/lib/simplenodelist.js b/lib/simplenodelist.js index 80b8dae..ce9d307 100644 --- a/lib/simplenodelist.js +++ b/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