From 5708b6387355d1ca709955379b1b76bd7abebfb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20P=C3=A4ssler?= Date: Fri, 5 Feb 2016 13:49:33 +0100 Subject: [PATCH] main: support multiple data sources --- README.md | 4 ++-- lib/main.js | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c23d31d..15b4de2 100644 --- a/README.md +++ b/README.md @@ -46,9 +46,9 @@ This will generate `build/` containing all required files. Copy `config.json.example` to `build/config.json` and change it to match your community. -## dataPath (string) +## dataPath (string/array) -`dataPath` must point to the address of a [HopGlass Server](https://github.com/plumpudding/hopglass-server). +`dataPath` can be either a string containing the address of a [HopGlass Server](https://github.com/plumpudding/hopglass-server) or an array containing multiple addresses. Don't forget the trailing slash! Also, proxying the data through a webserver will allow GZip and thus will greatly reduce bandwidth consumption. It may help with firewall problems too. diff --git a/lib/main.js b/lib/main.js index 4f2aab4..a363537 100644 --- a/lib/main.js +++ b/lib/main.js @@ -2,12 +2,39 @@ define(["moment", "router", "leaflet", "gui", "numeral"], function (moment, Router, L, GUI, numeral) { return function (config) { function handleData(data) { - var dataNodes = data[0] - var dataGraph = data[1] + var dataNodes = {} + dataNodes.nodes = [] + var dataGraph = {} + dataGraph.batadv = {} + dataGraph.batadv.nodes = [] + dataGraph.batadv.links = [] - if (dataNodes.version !== 2 || dataGraph.version !== 1) { - var err = "Unsupported nodes or graph version: " + dataNodes.version + " " + dataGraph.version - throw err + + function rearrangeLinks(d) { + d.source += dataGraph.batadv.nodes.length + d.target += dataGraph.batadv.nodes.length + } + + for (var i = 0; i < data.length; ++i) { + var vererr + if(i % 2) + if (data[i].version !== 1) { + vererr = "Unsupported graph version: " + data[i].version + console.log(vererr) //silent fail + } else { + data[i].batadv.links.forEach(rearrangeLinks) + dataGraph.batadv.nodes = dataGraph.batadv.nodes.concat(data[i].batadv.nodes) + dataGraph.batadv.links = dataGraph.batadv.links.concat(data[i].batadv.links) + dataGraph.timestamp = data[i].timestamp + } + else + if (data[i].version !== 2) { + vererr = "Unsupported nodes version: " + data[i].version + console.log(vererr) //silent fail + } else { + dataNodes.nodes = dataNodes.nodes.concat(data[i].nodes) + dataNodes.timestamp = data[i].timestamp + } } var nodes = dataNodes.nodes.filter( function (d) { @@ -96,9 +123,16 @@ function (moment, Router, L, GUI, numeral) { var router = new Router() - var urls = [ config.dataPath + "nodes.json", - config.dataPath + "graph.json" - ] + var urls = [] + + if (typeof config.dataPath === "string" || config.dataPath instanceof String) + config.dataPath = [config.dataPath] + + for (var i in config.dataPath) { + urls.push(config.dataPath[i] + "nodes.json") + urls.push(config.dataPath[i] + "graph.json") + } + function update() { return Promise.all(urls.map(getJSON)) .then(handleData)