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

104 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-01-20 02:31:43 +08:00
var queue = require('queue-async');
2016-01-20 19:44:00 +08:00
var _ = require('underscore');
2016-01-20 02:31:43 +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 self = this;
2016-01-20 02:31:43 +08:00
2018-07-30 23:31:42 +08:00
var layers = requestMapConfig.layers;
var analysesResults = context.analysesResults;
2018-07-30 23:31:42 +08:00
if (!layers || layers.length === 0) {
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(function (layer) {
augmentLayersQueue.defer(
augmentLayer,
self.overviewsMetadataBackend,
self.filterStatsBackend,
user,
layer,
analysesResults
);
});
2018-07-30 23:49:20 +08:00
augmentLayersQueue.awaitAll(function layersAugmentQueueFinish (err, layers) {
if (err) {
return callback(err);
}
2018-07-31 21:01:22 +08:00
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
return callback(null, requestMapConfig);
});
2018-08-01 21:53:43 +08:00
};
2018-08-01 21:53:43 +08:00
function augmentLayer(overviewsMetadataBackend, filterStatsBackend, user, layer, analysesResults, done) {
if (layer.type !== 'mapnik' && layer.type !== 'cartodb') {
return done(null, layer);
2016-01-20 18:51:46 +08:00
}
2016-01-20 02:31:43 +08:00
overviewsMetadataBackend.getOverviewsMetadata(user, layer.options.sql, function (err, metadata) {
2018-07-30 23:31:42 +08:00
if (err) {
return done(err, layer);
2018-07-30 23:31:42 +08:00
}
var query_rewrite_data = { overviews: metadata };
var filters, unfiltered_query;
if (layer.options.source && analysesResults && !layer.options.sql_wrap) {
var sourceId = layer.options.source.id;
var node = _.find(analysesResults, function (a) { return a.rootNode.params.id === sourceId; });
if (node) {
node = node.rootNode;
filters = node.getFilters();
var filters_disabler = Object.keys(filters).reduce(
function (disabler, filter_id) { disabler[filter_id] = false; return disabler; },
{}
);
unfiltered_query = node.getQuery(filters_disabler);
query_rewrite_data.filters = filters;
query_rewrite_data.unfiltered_query = unfiltered_query;
}
2018-07-30 23:31:42 +08:00
}
2016-01-20 19:44:00 +08:00
if (!filters) {
if (!_.isEmpty(metadata)) {
layer = Object.assign({}, layer);
layer.options = Object.assign({}, layer.options, { query_rewrite_data });
}
return done(null, layer);
}
2016-01-20 19:44:00 +08:00
filterStatsBackend.getFilterStats(user, unfiltered_query, filters, function (err, stats) {
if (err) {
return done(null, layer);
}
2016-01-20 19:44:00 +08:00
query_rewrite_data.filter_stats = stats;
if (!_.isEmpty(metadata)) {
layer = Object.assign({}, layer);
layer.options = Object.assign({}, layer.options, { query_rewrite_data });
}
return done(null, layer);
});
});
}