From 755dfe6822eb76fd254c7556797291a1aaf67d3a Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Wed, 27 Apr 2016 18:30:05 +0200 Subject: [PATCH] Append dataviews related columns to layers --- .../models/analysis-mapconfig-adapter.js | 47 ++++++++++- npm-shrinkwrap.json | 14 ++-- package.json | 2 +- .../analysis/analysis-layers-geojson.js | 81 +++++++++++++++++++ test/support/test-client.js | 8 +- 5 files changed, 139 insertions(+), 13 deletions(-) create mode 100644 test/acceptance/analysis/analysis-layers-geojson.js diff --git a/lib/cartodb/models/analysis-mapconfig-adapter.js b/lib/cartodb/models/analysis-mapconfig-adapter.js index b2640101..08050287 100644 --- a/lib/cartodb/models/analysis-mapconfig-adapter.js +++ b/lib/cartodb/models/analysis-mapconfig-adapter.js @@ -135,11 +135,15 @@ AnalysisMapConfigAdapter.prototype.getMapConfig = function(analysisConfiguration var missingNodesErrors = []; requestMapConfig.layers = requestMapConfig.layers.map(function(layer, layerIndex) { - if (layer.options.source && layer.options.source.id) { - var layerSourceId = layer.options.source.id; + if (getLayerSourceId(layer)) { + var layerSourceId = getLayerSourceId(layer); var layerNode = sourceId2Node[layerSourceId]; if (layerNode) { layer.options.sql = layerQuery(layerNode.getQuery(), layerNode.getColumns()); + var layerDataviews = getLayerDataviews(layer, dataviews); + layer.options.columns = layerDataviews.reduce(function(columns, dataview) { + return columns.concat(getDataviewColumns(dataview)); + }, []); } else { missingNodesErrors.push( new Error('Missing analysis node.id="' + layerSourceId +'" for layer='+layerIndex) @@ -159,6 +163,45 @@ AnalysisMapConfigAdapter.prototype.getMapConfig = function(analysisConfiguration }); }; +function getLayerSourceId(layer) { + return layer.options.source && layer.options.source.id; +} + +function getDataviewSourceId(dataview) { + return dataview.source && dataview.source.id; +} + +function getLayerDataviews(layer, dataviews) { + var layerDataviews = []; + + var layerSourceId = getLayerSourceId(layer); + if (layerSourceId) { + var dataviewsList = getDataviewsList(dataviews); + dataviewsList.forEach(function(dataview) { + if (getDataviewSourceId(dataview) === layerSourceId) { + layerDataviews.push(dataview); + } + }); + } + + return layerDataviews; +} + +function getDataviewColumns(dataview) { + var columns = []; + var options = dataview.options; + ['column', 'aggregationColumn'].forEach(function(opt) { + if (options.hasOwnProperty(opt)) { + columns.push(options[opt]); + } + }); + return columns; +} + +function getDataviewsList(dataviews) { + return Object.keys(dataviews).map(function(dataviewKey) { return dataviews[dataviewKey]; }); +} + function getDataviewsErrors(dataviews) { var errors = []; diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 77762c93..8db3958b 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,6 +1,6 @@ { "name": "windshaft-cartodb", - "version": "2.34.1", + "version": "2.34.2", "dependencies": { "body-parser": { "version": "1.14.2", @@ -73,14 +73,14 @@ }, "unpipe": { "version": "1.0.0", - "from": "unpipe@1.0.0", + "from": "unpipe@>=1.0.0 <1.1.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz" } } }, "type-is": { "version": "1.6.12", - "from": "type-is@>=1.6.10 <1.7.0", + "from": "type-is@>=1.6.6 <1.7.0", "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.12.tgz", "dependencies": { "media-typer": { @@ -111,7 +111,7 @@ "dependencies": { "async": { "version": "1.5.2", - "from": "async@>=1.5.2 <2.0.0", + "from": "async@>=1.0.0 <2.0.0", "resolved": "https://registry.npmjs.org/async/-/async-1.5.2.tgz" }, "request": { @@ -1291,9 +1291,9 @@ "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.6.0.tgz" }, "windshaft": { - "version": "1.17.3", - "from": "windshaft@1.17.3", - "resolved": "https://registry.npmjs.org/windshaft/-/windshaft-1.17.3.tgz", + "version": "1.18.0", + "from": "windshaft@1.18.0", + "resolved": "https://registry.npmjs.org/windshaft/-/windshaft-1.18.0.tgz", "dependencies": { "abaculus": { "version": "1.1.0-cdb4", diff --git a/package.json b/package.json index 3b1b3a8a..619574df 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "node-statsd": "~0.0.7", "underscore" : "~1.6.0", "dot": "~1.0.2", - "windshaft": "1.17.3", + "windshaft": "1.18.0", "step": "~0.0.6", "queue-async": "~1.0.7", "request": "~2.62.0", diff --git a/test/acceptance/analysis/analysis-layers-geojson.js b/test/acceptance/analysis/analysis-layers-geojson.js new file mode 100644 index 00000000..59a0900b --- /dev/null +++ b/test/acceptance/analysis/analysis-layers-geojson.js @@ -0,0 +1,81 @@ +require('../../support/test_helper'); + +var assert = require('../../support/assert'); +var TestClient = require('../../support/test-client'); + +describe('analysis-layers-dataviews-geojson', function() { + + function createMapConfig(layers, dataviews, analysis) { + return { + version: '1.5.0', + layers: layers, + dataviews: dataviews || {}, + analyses: analysis || [] + }; + } + + var CARTOCSS = [ + "#points {", + " marker-fill-opacity: 1.0;", + " marker-line-color: #FFF;", + " marker-line-width: 0.5;", + " marker-line-opacity: 1.0;", + " marker-placement: point;", + " marker-type: ellipse;", + " marker-width: 8;", + " marker-fill: red;", + " marker-allow-overlap: true;", + "}" + ].join('\n'); + + var mapConfig = createMapConfig( + [ + { + "type": "cartodb", + "options": { + "source": { + "id": "2570e105-7b37-40d2-bdf4-1af889598745" + }, + "cartocss": CARTOCSS, + "cartocss_version": "2.3.0" + } + } + ], + { + pop_max_histogram: { + source: { + id: '2570e105-7b37-40d2-bdf4-1af889598745' + }, + type: 'histogram', + options: { + column: 'pop_max' + } + } + }, + [ + { + "id": "2570e105-7b37-40d2-bdf4-1af889598745", + "type": "source", + "params": { + "query": "select * from populated_places_simple_reduced" + } + } + ] + ); + + it('should get pop_max column from dataview', function(done) { + var testClient = new TestClient(mapConfig, 1234); + + testClient.getTile(0, 0, 0, {format: 'geojson', layers: 0}, function(err, res, geojson) { + assert.ok(!err, err); + + assert.ok(Array.isArray(geojson.features)); + assert.ok(geojson.features.length > 0); + var feature = geojson.features[0]; + assert.ok(feature.properties.hasOwnProperty('pop_max'), 'Missing pop_max property'); + + testClient.drain(done); + }); + }); + +}); diff --git a/test/support/test-client.js b/test/support/test-client.js index 1da653a7..2d6213f6 100644 --- a/test/support/test-client.js +++ b/test/support/test-client.js @@ -313,13 +313,15 @@ TestClient.prototype.getTile = function(z, x, y, params, callback) { assert.response(server, request, expectedResponse, function(res, err) { assert.ifError(err); - var image; + var obj; if (isPng) { - image = mapnik.Image.fromBytes(new Buffer(res.body, 'binary')); + obj = mapnik.Image.fromBytes(new Buffer(res.body, 'binary')); + } else { + obj = JSON.parse(res.body); } - next(null, res, image); + next(null, res, obj); }); }, function finish(err, res, image) {