Reorganize code, estract methods and rename others

This commit is contained in:
Daniel García Aubert 2018-08-02 11:40:41 +02:00
parent df14afb55f
commit 593a72a967

View File

@ -9,8 +9,6 @@ function MapConfigOverviewsAdapter(overviewsMetadataBackend, filterStatsBackend)
module.exports = MapConfigOverviewsAdapter;
MapConfigOverviewsAdapter.prototype.getMapConfig = function (user, requestMapConfig, params, context, callback) {
var self = this;
var layers = requestMapConfig.layers;
var analysesResults = context.analysesResults;
@ -23,8 +21,8 @@ MapConfigOverviewsAdapter.prototype.getMapConfig = function (user, requestMapCon
layers.forEach(function (layer) {
augmentLayersQueue.defer(
augmentLayer,
self.overviewsMetadataBackend,
self.filterStatsBackend,
this.overviewsMetadataBackend,
this.filterStatsBackend,
user,
layer,
analysesResults
@ -44,7 +42,6 @@ MapConfigOverviewsAdapter.prototype.getMapConfig = function (user, requestMapCon
return callback(null, requestMapConfig);
});
};
function augmentLayer(overviewsMetadataBackend, filterStatsBackend, user, layer, analysesResults, callback) {
@ -52,15 +49,6 @@ function augmentLayer(overviewsMetadataBackend, filterStatsBackend, user, layer,
return callback(null, layer);
}
var queryRewriteData = {};
if (layer.options.source && analysesResults && !layer.options.sql_wrap) {
var sourceId = layer.options.source.id;
queryRewriteData.filters = getFilters(analysesResults, sourceId);
queryRewriteData.unfiltered_query = getUnfilteredQuery(analysesResults, sourceId);
}
overviewsMetadataBackend.getOverviewsMetadata(user, layer.options.sql, function (err, metadata) {
if (err) {
return callback(err, layer);
@ -70,26 +58,27 @@ function augmentLayer(overviewsMetadataBackend, filterStatsBackend, user, layer,
return callback(null, layer);
}
queryRewriteData.overviews = metadata;
var filters = queryRewriteData.filters;
var filters = getFilters(analysesResults, layer);
if (!filters) {
layer.options = Object.assign({}, layer.options, { query_rewrite_data: queryRewriteData });
layer.options = Object.assign({}, layer.options, getQueryRewriteData(layer, analysesResults, {
overviews: metadata
}));
return callback(null, layer);
}
var unfilteredQuery = queryRewriteData.unfiltered_query;
var unfilteredQuery = getUnfilteredQuery(analysesResults, layer);
filterStatsBackend.getFilterStats(user, unfilteredQuery, filters, function (err, stats) {
if (err) {
return callback(null, layer);
}
queryRewriteData.filter_stats = stats;
layer.options = Object.assign({}, layer.options, { query_rewrite_data: queryRewriteData });
layer.options = Object.assign({}, layer.options, getQueryRewriteData(layer, analysesResults, {
overviews: metadata,
filter_stats: stats
}));
return callback(null, layer);
});
@ -104,24 +93,43 @@ function getRootNode (analysesResults, sourceId) {
return node ? node.rootNode : undefined;
}
function getUnfilteredQuery (analysesResults, sourceId) {
var node = getRootNode(analysesResults, sourceId);
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) {
var filters = node.getFilters();
var filters_disabler = Object.keys(filters).reduce(function (disabler, filter_id) {
disabler[filter_id] = false;
return disabler;
}, {});
return node.getQuery(filters_disabler);
if (node) {
return node.getFilters();
}
}
}
function getFilters (analysesResults, sourceId) {
var node = getRootNode(analysesResults, sourceId);
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) {
return node.getFilters();
if (node) {
var filters = node.getFilters();
var filters_disabler = Object.keys(filters).reduce(function (disabler, filter_id) {
disabler[filter_id] = false;
return disabler;
}, {});
return node.getQuery(filters_disabler);
}
}
}
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 };
}