Windshaft-cartodb/lib/models/mapconfig/adapter/mapconfig-overviews-adapter.js

140 lines
4.7 KiB
JavaScript
Raw Normal View History

'use strict';
2016-01-20 02:31:43 +08:00
var queue = require('queue-async');
2016-01-20 19:44:00 +08:00
var _ = require('underscore');
const AggregationMapConfig = require('../../aggregation/aggregation-mapconfig');
2016-01-20 02:31:43 +08:00
2019-10-22 01:07:24 +08:00
function MapConfigOverviewsAdapter (overviewsMetadataBackend, filterStatsBackend) {
this.overviewsMetadataBackend = overviewsMetadataBackend;
this.filterStatsBackend = filterStatsBackend;
2016-01-20 02:31:43 +08:00
}
2016-01-21 19:03:50 +08:00
module.exports = MapConfigOverviewsAdapter;
2016-01-20 02:31:43 +08:00
2018-07-30 23:31:42 +08:00
MapConfigOverviewsAdapter.prototype.getMapConfig = function (user, requestMapConfig, params, context, callback) {
var layers = requestMapConfig.layers;
var analysesResults = context.analysesResults;
const aggMapConfig = new AggregationMapConfig(null, requestMapConfig);
if (aggMapConfig.isVectorOnlyMapConfig() || aggMapConfig.isAggregationMapConfig() ||
!layers || layers.length === 0) {
2018-07-30 23:31:42 +08:00
return callback(null, requestMapConfig);
}
2016-01-20 02:31:43 +08:00
2018-07-30 23:31:42 +08:00
var augmentLayersQueue = queue(layers.length);
2016-01-20 02:31:43 +08:00
layers.forEach(layer => augmentLayersQueue.defer(this._augmentLayer.bind(this), user, layer, analysesResults));
2018-07-30 23:49:20 +08:00
augmentLayersQueue.awaitAll(function layersAugmentQueueFinish (err, results) {
if (err) {
return callback(err);
}
2018-07-31 21:01:22 +08:00
const layers = results.map(result => result.layer);
2018-09-05 17:56:45 +08:00
const overviewsAddedToMapconfig = results.some(result => result.overviewsAddedToMapconfig);
if (!layers || layers.length === 0) {
return callback(new Error('Missing layers array from layergroup config'));
}
2018-08-01 21:53:43 +08:00
requestMapConfig.layers = layers;
2018-08-01 21:53:43 +08:00
const stats = { overviewsAddedToMapconfig };
return callback(null, requestMapConfig, stats);
});
};
2018-08-01 21:53:43 +08:00
MapConfigOverviewsAdapter.prototype._augmentLayer = function (user, layer, analysesResults, callback) {
let overviewsAddedToMapconfig = false;
if (layer.type !== 'mapnik' && layer.type !== 'cartodb') {
return callback(null, { layer, overviewsAddedToMapconfig });
2016-01-20 18:51:46 +08:00
}
2016-01-20 02:31:43 +08:00
this.overviewsMetadataBackend.getOverviewsMetadata(user, layer.options.sql, (err, metadata) => {
2018-07-30 23:31:42 +08:00
if (err) {
return callback(err, { layer, overviewsAddedToMapconfig });
2018-07-30 23:31:42 +08:00
}
if (_.isEmpty(metadata)) {
return callback(null, { layer, overviewsAddedToMapconfig });
}
var filters = getFilters(analysesResults, layer);
overviewsAddedToMapconfig = true;
if (!filters) {
layer.options = Object.assign({}, layer.options, getQueryRewriteData(layer, analysesResults, {
overviews: metadata
}));
return callback(null, { layer, overviewsAddedToMapconfig });
}
2016-01-20 19:44:00 +08:00
var unfilteredQuery = getUnfilteredQuery(analysesResults, layer);
this.filterStatsBackend.getFilterStats(user, unfilteredQuery, filters, function (err, stats) {
if (err) {
return callback(null, { layer, overviewsAddedToMapconfig });
}
2016-01-20 19:44:00 +08:00
layer.options = Object.assign({}, layer.options, getQueryRewriteData(layer, analysesResults, {
overviews: metadata,
filter_stats: stats
}));
return callback(null, { layer, overviewsAddedToMapconfig });
});
});
};
function getRootNode (analysesResults, sourceId) {
var node = _.find(analysesResults, function (a) {
return a.rootNode.params.id === sourceId;
});
return node ? node.rootNode : undefined;
}
function getFilters (analysesResults, layer) {
if (layer.options.source && analysesResults && !layer.options.sql_wrap) {
var sourceId = layer.options.source.id;
var node = getRootNode(analysesResults, sourceId);
if (node) {
return node.getFilters();
}
}
}
function getUnfilteredQuery (analysesResults, layer) {
if (layer.options.source && analysesResults && !layer.options.sql_wrap) {
var sourceId = layer.options.source.id;
var node = getRootNode(analysesResults, sourceId);
if (node) {
var filters = node.getFilters();
var filtersDisabler = Object.keys(filters).reduce(function (disabler, filterId) {
disabler[filterId] = false;
return disabler;
}, {});
return node.getQuery(filtersDisabler);
}
}
}
function getQueryRewriteData (layer, analysesResults, extend = {}) {
var queryRewriteData = {};
if (layer.options.source && analysesResults && !layer.options.sql_wrap) {
queryRewriteData.filters = getFilters(analysesResults, layer);
queryRewriteData.unfiltered_query = getUnfilteredQuery(analysesResults, layer);
}
queryRewriteData = Object.assign({}, queryRewriteData, extend);
return { query_rewrite_data: queryRewriteData };
}