Refactor stats collection

This commit is contained in:
Javier Goizueta 2018-10-09 13:24:08 +02:00
parent d66a304b00
commit 73b3402d85

View File

@ -2,7 +2,6 @@ const queryUtils = require('../../utils/query-utils');
const AggregationMapConfig = require('../../models/aggregation/aggregation-mapconfig');
const aggregationQuery = require('../../models/aggregation/aggregation-query');
function MapnikLayerStats () {
this._types = {
mapnik: true,
@ -157,7 +156,7 @@ function _columnStats(ctx, columns, dimensions) {
let queries = [];
let aggr = [];
if (ctx.metaOptions.columnStats) {
queries.push(new Promise(resolve => resolve(columns))); // add columns as first result
queries.push(new Promise(resolve => resolve({ columns }))); // add columns as first result
Object.keys(columns).forEach(name => {
aggr = aggr.concat(
columnAggregations(columns[name])
@ -175,7 +174,7 @@ function _columnStats(ctx, columns, dimensions) {
queryUtils.queryPromise(
ctx.dbConnection,
_getSQL(ctx, sql => queryUtils.getQueryTopCategories(sql, name, topN, includeNulls))
).then(res => ({ [name]: { categories: res.rows } }))
).then(res => ({ columns: { [name]: { categories: res.rows } } }))
);
}
});
@ -201,29 +200,30 @@ function _columnStats(ctx, columns, dimensions) {
ctx.dbConnection,
_getSQL(ctx, sql => `SELECT ${aggr.join(',')} FROM (${sql}) AS __cdb_query`)
).then(res => {
let stats = {};
let stats = { columns: {}, dimensions: {} };
Object.keys(columns).forEach(name => {
stats[name] = {};
stats.columns[name] = {};
columnAggregations(columns[name]).forEach(fn => {
stats[name][fn] = res.rows[0][`${name}_${fn}`];
stats.columns[name][fn] = res.rows[0][`${name}_${fn}`];
});
});
Object.keys(dimensionsInfo).forEach(name => {
// Temporalily place dimensions info in stats.columns.__dimensions
stats.__dimensions = stats.__dimensions || {};
stats.__dimensions[name] = stats.__dimensions[name] || Object.assign({}, dimensionsStats[name]);
stats.dimensions[name] = stats.dimensions[name] || Object.assign({}, dimensionsStats[name]);
let type = null;
columnAggregations(dimensionsInfo[name]).forEach(fn => {
type = type ||
fieldTypeSafe(ctx.dbConnection, res.fields.find(f => f.name === `${name}_${fn}`));
stats.__dimensions[name][fn] = res.rows[0][`${name}_${fn}`];
stats.dimensions[name][fn] = res.rows[0][`${name}_${fn}`];
});
stats.__dimensions[name].type = type;
stats.dimensions[name].type = type;
});
return stats;
})
);
return Promise.all(queries).then(results => ({ columns: mergeColumns(results) }));
return Promise.all(queries).then(results => ({
columns: mergeColumns(results.map(r => r.columns)),
dimensions: mergeColumns(results.map( r => r.dimensions))
}));
}
return Promise.resolve({ columns });
}
@ -301,11 +301,6 @@ function (layer, dbConnection, callback) {
_columns(ctx).then(columns => _columnStats(ctx, columns, dimensions))
]).then(results => {
results = mergeResults(results);
const dimensions = results.columns && results.columns.__dimensions;
if (dimensions) {
delete results.columns.__dimensions;
results.dimensions = dimensions;
}
callback(null, results);
}).catch(error => {
callback(error);