From d42257127b8e664d4f27e1dc2482f549f907b11c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Thu, 28 Dec 2017 13:11:32 +0100 Subject: [PATCH] Add method to discover required columns or all of them if it's a default aggregation --- .../aggregation/aggregation-mapconfig.js | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/cartodb/models/aggregation/aggregation-mapconfig.js b/lib/cartodb/models/aggregation/aggregation-mapconfig.js index a463d136..ef12518e 100644 --- a/lib/cartodb/models/aggregation/aggregation-mapconfig.js +++ b/lib/cartodb/models/aggregation/aggregation-mapconfig.js @@ -9,6 +9,8 @@ const { const SubstitutionTokens = require('../../utils/substitution-tokens'); +const removeDuplicates = arr => [...new Set(arr)]; + function prepareSql(sql) { return sql && SubstitutionTokens.replace(sql, { bbox: 'ST_MakeEnvelope(0,0,0,0)', @@ -125,6 +127,43 @@ module.exports = class AggregationMapConfig extends MapConfig { return aggregation; } + getLayerAggregationColumns (index, callback) { + if (this._isDefaultLayerAggregation(index)) { + const skipGeoms = true; + return this.getLayerColumns(index, skipGeoms, (err, columns) => { + if (err) { + return callback(err); + } + + return callback(null, columns); + }); + } + + const columns = this._getLayerAggregationRequiredColumns(index); + + return callback(null, columns); + } + + _getLayerAggregationRequiredColumns (index) { + const { columns, dimensions } = this.getAggregation(index); + + let aggregatedColumns = []; + if (columns) { + aggregatedColumns = Object.keys(columns) + .map(key => columns[key].aggregated_column) + .filter(aggregatedColumn => typeof aggregatedColumn === 'string'); + } + + let dimensionsColumns = []; + if (dimensions) { + dimensionsColumns = Object.keys(dimensions) + .map(key => dimensions[key]) + .filter(dimension => typeof dimension === 'string'); + } + + return removeDuplicates(aggregatedColumns.concat(dimensionsColumns)); + } + doesLayerReachThreshold(index, featureCount) { const threshold = this.getAggregation(index) && this.getAggregation(index).threshold ? this.getAggregation(index).threshold : @@ -165,7 +204,7 @@ module.exports = class AggregationMapConfig extends MapConfig { }); } - isDefaultLayerAggregation (index) { + _isDefaultLayerAggregation (index) { const aggregation = this.getAggregation(index); return (this.isVectorOnlyMapConfig() && !this.hasLayerAggregation(index)) ||