From 25e33955804cdbf0a815a777708795a7534bbeb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 23 Aug 2019 17:09:24 +0200 Subject: [PATCH] Modify sample metadata --- .../layer-stats/mapnik-layer-stats.js | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/cartodb/backends/layer-stats/mapnik-layer-stats.js b/lib/cartodb/backends/layer-stats/mapnik-layer-stats.js index a8a181cc..d4d033ca 100644 --- a/lib/cartodb/backends/layer-stats/mapnik-layer-stats.js +++ b/lib/cartodb/backends/layer-stats/mapnik-layer-stats.js @@ -125,22 +125,28 @@ function mergeColumns(results) { } } -const SAMPLE_SEED = 0.5; const DEFAULT_SAMPLE_ROWS = 100; -function _sample(ctx, numRows) { - if (ctx.metaOptions.sample) { - const sampleProb = Math.min(ctx.metaOptions.sample.num_rows / numRows, 1); - // We'll use a safety limit just in case numRows is a bad estimate - const requestedRows = ctx.metaOptions.sample.num_rows || DEFAULT_SAMPLE_ROWS; - const limit = Math.ceil(requestedRows * 1.5); - let columns = ctx.metaOptions.sample.include_columns; - return queryUtils.queryPromise(ctx.dbConnection, _getSQL( - ctx, - sql => queryUtils.getQuerySample(sql, sampleProb, limit, SAMPLE_SEED, columns) - )).then(res => ({ sample: res.rows })); +function _sample(ctx) { + if (!ctx.metaOptions.sample) { + return Promise.resolve(); } - return Promise.resolve(); + + // We'll use a safety limit just in case numRows is a bad estimate + const requestedRows = ctx.metaOptions.sample.num_rows || DEFAULT_SAMPLE_ROWS; + const limit = Math.ceil(requestedRows * 1.5); + const columns = ctx.metaOptions.sample.include_columns; + + const sqlMaxMin = _getSQL(ctx, sql => queryUtils.getMaxMinColumnQuery(sql)); + return queryUtils.queryPromise(ctx.dbConnection, sqlMaxMin) + .then(maxMinRes => { + const { min_id: min, id_span: span } = maxMinRes.rows[0]; + const ids = queryUtils.getSampleValuesFromRange(min, span, limit); + const sqlSample = _getSQL(ctx, sql => queryUtils.getSampleFromIdsQuery(sql, ids, columns)); + + return queryUtils.queryPromise(ctx.dbConnection, sqlSample); + }) + .then(res => ({ sample: res.rows })); } function _columnsMetadataRequired(options) {