From deb29f2c7774e0427ce2c8383971d360584550a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Thu, 30 Nov 2017 19:20:59 +0100 Subject: [PATCH] Implement aggregation mapconfig adapter (happy case) --- .../adapter/aggregation-mapconfig-adapter.js | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 lib/cartodb/models/mapconfig/adapter/aggregation-mapconfig-adapter.js diff --git a/lib/cartodb/models/mapconfig/adapter/aggregation-mapconfig-adapter.js b/lib/cartodb/models/mapconfig/adapter/aggregation-mapconfig-adapter.js new file mode 100644 index 00000000..1d5646ee --- /dev/null +++ b/lib/cartodb/models/mapconfig/adapter/aggregation-mapconfig-adapter.js @@ -0,0 +1,63 @@ +const AggregationProxy = require('../../aggregation/aggregation-proxy'); + +module.exports = class AggregationMapConfigAdapter { + getMapConfig (user, requestMapConfig, params, context, callback) { + if (!this._shouldAdaptLayers(requestMapConfig, params)) { + return callback(null, requestMapConfig); + } + + requestMapConfig.layers.forEach(layer => { + if (!this._hasLayerAggregation(layer)) { + return; + } + + const aggregation = new AggregationProxy(requestMapConfig, layer.options.aggregation); + + let aggregationSql = aggregation.sql(); + + const sqlQueryWrap = layer.options.sql_wrap; + + if (sqlQueryWrap) { + layer.options.sql_raw = aggregationSql; + aggregationSql = sqlQueryWrap.replace(/<%=\s*sql\s*%>/g, aggregationSql); + } + + layer.options.sql = aggregationSql; + }); + + callback(null, requestMapConfig); + } + + _shouldAdaptLayers (requestMapConfig, params) { + if (typeof params.aggregation === 'boolean') { + return params.aggregation; + } + + if (params.aggregation === undefined) { + if (requestMapConfig.isVectorLayergroup()) { + return true; + } else if (this._hasAggregation(requestMapConfig)){ + return true; + } + } + + return false; + } + + _hasAggregation (requestMapConfig) { + for (const layer of requestMapConfig.layers) { + + + if (this._hasLayerAggregation(layer)) { + return true; + } + } + + return false; + } + + _hasLayerAggregation (layer) { + const { aggregation } = layer.options; + return aggregation !== undefined && (typeof aggregation === 'object' && typeof aggregation === 'boolean'); + } +};