<commit_before>var queue = require('queue-async');
var _ = require('underscore');

function MapConfigOverviewsAdapter(overviewsMetadataBackend, filterStatsBackend) {
    this.overviewsMetadataBackend = overviewsMetadataBackend;
    this.filterStatsBackend = filterStatsBackend;
}

module.exports = MapConfigOverviewsAdapter;

MapConfigOverviewsAdapter.prototype.getMapConfig = function (user, requestMapConfig, params, context, callback) {
    var self = this;

    var layers = requestMapConfig.layers;
    var analysesResults = context.analysesResults;

    if (!layers || layers.length === 0) {
        return callback(null, requestMapConfig);
    }

    var augmentLayersQueue = queue(layers.length);

    function augmentLayer(layer, done) {
        if (layer.type !== 'mapnik' && layer.type !== 'cartodb') {
            return done(null, layer);
        }
        self.overviewsMetadataBackend.getOverviewsMetadata(user, layer.options.sql, function (err, metadata) {
            if (err) {
                done(err, layer);
            } else {
                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;
                    }
                }

                if (filters) {
                    self.filterStatsBackend.getFilterStats(
                        user,
                        unfiltered_query,
                        filters,
                        function (err, stats) {
                            if (err) {
                                return done(null, layer);
                            }

                            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);
                        }
                    );
                } else {
                    if (!_.isEmpty(metadata)) {
                        layer = Object.assign({}, layer);
                        layer.options = Object.assign({}, layer.options, { query_rewrite_data });
                    }

                    return done(null, layer);
                }
            }
        });
    }

    function layersAugmentQueueFinish(err, layers) {<commit_msg>early return<commit_after>var queue = require('queue-async');
var _ = require('underscore');

function MapConfigOverviewsAdapter(overviewsMetadataBackend, filterStatsBackend) {
    this.overviewsMetadataBackend = overviewsMetadataBackend;
    this.filterStatsBackend = filterStatsBackend;
}

module.exports = MapConfigOverviewsAdapter;

MapConfigOverviewsAdapter.prototype.getMapConfig = function (user, requestMapConfig, params, context, callback) {
    var self = this;

    var layers = requestMapConfig.layers;
    var analysesResults = context.analysesResults;

    if (!layers || layers.length === 0) {
        return callback(null, requestMapConfig);
    }

    var augmentLayersQueue = queue(layers.length);

    function augmentLayer(layer, done) {
        if (layer.type !== 'mapnik' && layer.type !== 'cartodb') {
            return done(null, layer);
        }
        self.overviewsMetadataBackend.getOverviewsMetadata(user, layer.options.sql, function (err, metadata) {
            if (err) {
                return done(err, layer);
            }

            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;
                }
            }

            if (filters) {
                self.filterStatsBackend.getFilterStats(
                    user,
                    unfiltered_query,
                    filters,
                    function (err, stats) {
                        if (err) {
                            return done(null, layer);
                        }

                        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);
                    }
                );
            } else {
                if (!_.isEmpty(metadata)) {
                    layer = Object.assign({}, layer);
                    layer.options = Object.assign({}, layer.options, { query_rewrite_data });
                }

                return done(null, layer);
            }
        });
    }

    function layersAugmentQueueFinish(err, layers) {
{'fraction_non_alphanumeric': 0.09520826808643909, 'fraction_numerical': 0.00031318509238960227, 'mean_word_length': 1.4631700732741997, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1918730', 'n_tokens_mistral': 1596, 'n_tokens_neox': 1491, 'n_words': 299}