From 0c9d60b57397c3651126f7e702cf3f646c966c6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 20 Oct 2017 16:19:24 +0200 Subject: [PATCH] Add support for no_filters params in dataviews --- lib/cartodb/backends/dataview.js | 19 ++- .../analysis/analyses-filters-params.js | 125 ++++++++++++++++++ 2 files changed, 139 insertions(+), 5 deletions(-) create mode 100644 test/acceptance/analysis/analyses-filters-params.js diff --git a/lib/cartodb/backends/dataview.js b/lib/cartodb/backends/dataview.js index b6037ae6..edb0449b 100644 --- a/lib/cartodb/backends/dataview.js +++ b/lib/cartodb/backends/dataview.js @@ -24,7 +24,7 @@ module.exports = DataviewBackend; DataviewBackend.prototype.getDataview = function (mapConfigProvider, user, params, callback) { - var dataviewName = params.dataviewName; + var dataviewName = params.dataviewName; step( function getMapConfig() { mapConfigProvider.getMapConfig(this); @@ -40,9 +40,8 @@ DataviewBackend.prototype.getDataview = function (mapConfigProvider, user, param var pg = new PSQL(dbParamsFromReqParams(params)); var ownFilter = +params.own_filter; - ownFilter = !!ownFilter; - - var query = (ownFilter) ? dataviewDefinition.sql.own_filter_on : dataviewDefinition.sql.own_filter_off; + // ownFilter = !!ownFilter; + var query = getDaviewQuery(dataviewDefinition, ownFilter); if (params.bbox) { var bboxFilter = new BBoxFilter({column: 'the_geom_webmercator', srid: 3857}, {bbox: params.bbox}); query = bboxFilter.sql(query); @@ -55,7 +54,7 @@ DataviewBackend.prototype.getDataview = function (mapConfigProvider, user, param ); var dataview = dataviewFactory.getDataview(query, dataviewDefinition); - dataview.getResult(pg, getOverrideParams(params, ownFilter), this); + dataview.getResult(pg, getOverrideParams(params, !!ownFilter), this); }, function returnCallback(err, result) { return callback(err, result); @@ -63,6 +62,16 @@ DataviewBackend.prototype.getDataview = function (mapConfigProvider, user, param ); }; +function getDaviewQuery(dataviewDefinition, ownFilter) { + if (ownFilter === -1) { + return dataviewDefinition.sql.no_filters; + } else if (ownFilter === 0) { + return dataviewDefinition.sql.own_filter_off; + } else if (ownFilter === 1) { + return dataviewDefinition.sql.own_filter_on; + } +} + function getQueryRewriteData(mapConfig, dataviewDefinition, params) { var sourceId = dataviewDefinition.source.id; // node.id var layer = _.find(mapConfig.obj().layers, function(l) { diff --git a/test/acceptance/analysis/analyses-filters-params.js b/test/acceptance/analysis/analyses-filters-params.js new file mode 100644 index 00000000..adb4d787 --- /dev/null +++ b/test/acceptance/analysis/analyses-filters-params.js @@ -0,0 +1,125 @@ +require('../../support/test_helper'); + +const assert = require('../../support/assert'); +const TestClient = require('../../support/test-client'); + +describe('analysis-filters-params', () => { + + const CARTOCSS = `#layer { + marker-fill-opacity: 1; + marker-line-color: white; + marker-line-width: 0.5; + marker-line-opacity: 1; + marker-placement: point; + marker-type: ellipse; + marker-width: 8; + marker-fill: red; + marker-allow-overlap: true; + }`; + + const mapConfig = { + version: '1.6.0', + layers: [ + { + "type": "cartodb", + "options": { + "source": { + "id": "a1" + }, + "cartocss": CARTOCSS, + "cartocss_version": "2.3.0" + } + } + ], + dataviews: { + pop_max_histogram: { + source: { + id: 'a1' + }, + type: 'histogram', + options: { + column: 'pop_max' + } + }, + pop_min_histogram: { + source: { + id: 'a1' + }, + type: 'histogram', + options: { + column: 'pop_min' + } + } + }, + analyses: [ + { + "id": "a1", + "type": "source", + "params": { + "query": "select * from populated_places_simple_reduced" + } + } + ] + }; + + var params = { + filters: { + dataviews: { + pop_max_histogram: { + min: 2e6 + }, + pop_min_histogram: { + max: 2e6 + } + } + }, + own_filter: -1 + }; + + + it('should get a filtered histogram dataview with all filters', function(done) { + const testClient = new TestClient(mapConfig, 1234); + + params.own_filter = 1; + + testClient.getDataview('pop_max_histogram', params, (err, dataview) => { + assert.ok(!err, err); + + assert.equal(dataview.type, 'histogram'); + assert.equal(dataview.bins_count, 6); + + testClient.drain(done); + }); + }); + + it('should get a filtered histogram dataview with all filters except my own filter', function(done) { + const testClient = new TestClient(mapConfig, 1234); + + params.own_filter = 0; + + testClient.getDataview('pop_max_histogram', params, (err, dataview) => { + assert.ok(!err, err); + + assert.equal(dataview.type, 'histogram'); + assert.equal(dataview.bins_count, 24); + + testClient.drain(done); + }); + }); + + it('should get a filtered histogram dataview without filters', function(done) { + const testClient = new TestClient(mapConfig, 1234); + + params.own_filter = -1; + + testClient.getDataview('pop_max_histogram', params, (err, dataview) => { + assert.ok(!err, err); + + assert.equal(dataview.type, 'histogram'); + assert.equal(dataview.bins_count, 48); + + testClient.drain(done); + }); + }); + +});