Get columns from layer query a set them into layer opptions

This commit is contained in:
Daniel García Aubert 2017-12-22 18:19:57 +01:00
parent b7ecde5c9d
commit a43779b050
4 changed files with 55 additions and 24 deletions

View File

@ -38,7 +38,7 @@ module.exports = class AggregationMapConfig extends MapConfig {
return AggregationMapConfig.SUPPORTED_GEOMETRY_TYPES.includes(geometryType);
}
constructor (config, connection, datasource) {
constructor (user, config, connection, datasource) {
super(config, datasource);
const validate = aggregationValidator(this);
@ -51,7 +51,8 @@ module.exports = class AggregationMapConfig extends MapConfig {
validate('threshold', positiveNumberValidator);
validate('columns', aggregationColumnsValidator);
this.connection = connection;
this.user = user;
this.pgConnection = connection;
}
getAggregatedQuery (index) {
@ -128,20 +129,41 @@ module.exports = class AggregationMapConfig extends MapConfig {
const limitedQuery = ctx => `SELECT * FROM (${ctx.query}) __cdb_schema LIMIT 0`;
const layer = this.getLayer(index);
this.connection.query(limitedQuery({ query: layer.options.sql }), (err, result) => {
this.pgConnection.getConnection(this.user, (err, connection) => {
if (err) {
return callback(err);
}
let columns = result.fields || [];
const sql = limitedQuery({ query: layer.options.sql });
columns = columns.map(({ name, type }) => ({ name, type }));
connection.query(sql, (err, result) => {
if (err) {
return callback(err);
}
if (skipGeoms) {
columns = columns.filter((column) => !geomColumns.includes(column));
}
let columns = result.fields || [];
return callback(err, columns);
columns = columns.map(({ name, type }) => ({ name, type }));
if (skipGeoms) {
columns = columns.filter((column) => !geomColumns.includes(column));
}
return callback(err, columns);
});
});
}
isDefaultAggregation (index) {
const aggregation = this.getAggregation(index);
return !aggregation || (
!aggregation.placement &&
this._isEmptyParameter(aggregation.columns) &&
this._isEmptyParameter(aggregation.dimensions)
);
}
_isEmptyParameter (parameter) {
return !parameter || Object.keys(parameter).length === 0;
}
};

View File

@ -20,7 +20,7 @@ module.exports = class AggregationMapConfigAdapter {
let mapConfig;
try {
mapConfig = new AggregationMapConfig(requestMapConfig);
mapConfig = new AggregationMapConfig(user, requestMapConfig, this.pgConnection);
} catch (err) {
return callback(err);
}
@ -100,9 +100,18 @@ module.exports = class AggregationMapConfigAdapter {
layer.options.sql = aggregationSql;
const aggregatedColumns = mapConfig.getAggregatedColumns(index);
if (mapConfig.isDefaultAggregation(index)) {
const skipGeoms = true;
mapConfig.getLayerColumns(index, skipGeoms, (err, columns) => {
if (err) {
return reject(err);
}
layer.options.columns = aggregatedColumns;
layer.options.columns = columns;
return resolve({ layer, index, adapted: shouldAdapt });
});
}
}
return resolve({ layer, index, adapted: shouldAdapt });

View File

@ -46,7 +46,7 @@
"step-profiler": "~0.3.0",
"turbo-carto": "0.20.2",
"underscore": "~1.6.0",
"windshaft": "4.1.0",
"windshaft": "cartodb/windshaft#pg-mvt-do-not-filter-columns",
"yargs": "~5.0.0"
},
"devDependencies": {

View File

@ -244,14 +244,6 @@ carto@0.16.3:
semver "^5.1.0"
yargs "^4.2.0"
carto@CartoDB/carto#0.15.1-cdb1:
version "0.15.1-cdb1"
resolved "https://codeload.github.com/CartoDB/carto/tar.gz/8050ec843f1f32a6469e5d1cf49602773015d398"
dependencies:
mapnik-reference "~6.0.2"
optimist "~0.6.0"
underscore "~1.6.0"
carto@cartodb/carto#0.15.1-cdb3:
version "0.15.1-cdb3"
resolved "https://codeload.github.com/cartodb/carto/tar.gz/945f5efb74fd1af1f5e1f69f409f9567f94fb5a7"
@ -260,6 +252,14 @@ carto@cartodb/carto#0.15.1-cdb3:
optimist "~0.6.0"
underscore "1.8.3"
"carto@github:cartodb/carto#0.15.1-cdb1":
version "0.15.1-cdb1"
resolved "https://codeload.github.com/cartodb/carto/tar.gz/8050ec843f1f32a6469e5d1cf49602773015d398"
dependencies:
mapnik-reference "~6.0.2"
optimist "~0.6.0"
underscore "~1.6.0"
cartocolor@4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/cartocolor/-/cartocolor-4.0.0.tgz#841a3222d8b5b22718d9d545b1e5b972cb26eb36"
@ -2392,9 +2392,9 @@ window-size@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/window-size/-/window-size-0.2.0.tgz#b4315bb4214a3d7058ebeee892e13fa24d98b075"
windshaft@4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/windshaft/-/windshaft-4.1.0.tgz#dc17c8369570c305171d1ab5ca130369bba04d58"
windshaft@cartodb/windshaft#pg-mvt-do-not-filter-columns:
version "4.1.1"
resolved "https://codeload.github.com/cartodb/windshaft/tar.gz/2de4e21b41787acd1630193fdd42c308815bfee5"
dependencies:
abaculus cartodb/abaculus#2.0.3-cdb1
canvas cartodb/node-canvas#1.6.2-cdb2