diff --git a/lib/cartodb/models/mapconfig/adapter/aggregation-mapconfig-adapter.js b/lib/cartodb/models/mapconfig/adapter/aggregation-mapconfig-adapter.js index bc401542..25677ca3 100644 --- a/lib/cartodb/models/mapconfig/adapter/aggregation-mapconfig-adapter.js +++ b/lib/cartodb/models/mapconfig/adapter/aggregation-mapconfig-adapter.js @@ -118,48 +118,7 @@ module.exports = class AggregationMapConfigAdapter { const isVectorOnlyMapConfig = mapConfig.isVectorOnlyMapConfig(); const adaptLayerPromises = requestMapConfig.layers.map((layer, index) => { - return new Promise((resolve, reject) => { - if (!isVectorOnlyMapConfig && !this._hasLayerAggregation(layer)) { - return resolve({ layer, index, adapted: false }); - } - - const threshold = layer.options.aggregation && layer.options.aggregation.threshold ? - layer.options.aggregation.threshold : - 1e5; - - const aggregationMetadata = queryUtils.getAggregationMetadata({ query: layer.options.sql }); - - connection.query(aggregationMetadata, (err, res) => { - if (err) { - return resolve({ layer, index, adapted: false }); - } - - const estimatedFeatureCount = res.rows[0].count; - const geometryType = res.rows[0].type; - - if (estimatedFeatureCount < threshold) { - return resolve({ layer, index, adapted: false }); - } - - if (geometryType !== 'ST_Point') { - return reject(new Error(unsupportedGeometryTypeErrorMessage({ geometryType }))); - } - - const aggregation = new AggregationProxy(mapConfig, layer.options.aggregation); - const sqlQueryWrap = layer.options.sql_wrap; - - let aggregationSql = aggregation.sql(layer.options); - - if (sqlQueryWrap) { - layer.options.sql_raw = aggregationSql; - aggregationSql = sqlQueryWrap.replace(/<%=\s*sql\s*%>/g, aggregationSql); - } - - layer.options.sql = aggregationSql; - - return resolve({ layer, index, adapted: true }); - }); - }); + return this._adaptLayer(connection, layer, index, isVectorOnlyMapConfig, mapConfig); }); Promise.all(adaptLayerPromises) @@ -183,6 +142,51 @@ module.exports = class AggregationMapConfigAdapter { }); } + _adaptLayer (connection, layer, index, isVectorOnlyMapConfig, mapConfig) { + return new Promise((resolve, reject) => { + if (!isVectorOnlyMapConfig && !this._hasLayerAggregation(layer)) { + return resolve({ layer, index, adapted: false }); + } + + const threshold = layer.options.aggregation && layer.options.aggregation.threshold ? + layer.options.aggregation.threshold : + 1e5; + + const aggregationMetadata = queryUtils.getAggregationMetadata({ query: layer.options.sql }); + + connection.query(aggregationMetadata, (err, res) => { + if (err) { + return resolve({ layer, index, adapted: false }); + } + + const estimatedFeatureCount = res.rows[0].count; + const geometryType = res.rows[0].type; + + if (estimatedFeatureCount < threshold) { + return resolve({ layer, index, adapted: false }); + } + + if (geometryType !== 'ST_Point') { + return reject(new Error(unsupportedGeometryTypeErrorMessage({ geometryType }))); + } + + const aggregation = new AggregationProxy(mapConfig, layer.options.aggregation); + const sqlQueryWrap = layer.options.sql_wrap; + + let aggregationSql = aggregation.sql(layer.options); + + if (sqlQueryWrap) { + layer.options.sql_raw = aggregationSql; + aggregationSql = sqlQueryWrap.replace(/<%=\s*sql\s*%>/g, aggregationSql); + } + + layer.options.sql = aggregationSql; + + return resolve({ layer, index, adapted: true }); + }); + }); + } + _getAggregationMetadata (isVectorOnlyMapConfig, layer, adapted) { if (adapted) { if (isVectorOnlyMapConfig) {