2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2017-12-19 02:06:01 +08:00
|
|
|
const MapConfig = require('windshaft').model.MapConfig;
|
|
|
|
const aggregationQuery = require('./aggregation-query');
|
2017-12-19 02:17:43 +08:00
|
|
|
const aggregationValidator = require('./aggregation-validator');
|
|
|
|
const {
|
2017-12-19 02:51:55 +08:00
|
|
|
createPositiveNumberValidator,
|
2017-12-19 03:42:26 +08:00
|
|
|
createIncludesValueValidator,
|
2018-03-22 00:01:32 +08:00
|
|
|
createAggregationColumnsValidator,
|
|
|
|
createAggregationFiltersValidator
|
2017-12-19 17:54:20 +08:00
|
|
|
} = aggregationValidator;
|
2017-12-19 02:06:01 +08:00
|
|
|
|
2018-06-18 19:15:45 +08:00
|
|
|
const queryUtils = require('../../utils/query-utils');
|
2017-12-28 03:08:43 +08:00
|
|
|
|
2017-12-28 20:11:32 +08:00
|
|
|
const removeDuplicates = arr => [...new Set(arr)];
|
|
|
|
|
2017-12-19 02:06:01 +08:00
|
|
|
module.exports = class AggregationMapConfig extends MapConfig {
|
2017-12-20 19:42:29 +08:00
|
|
|
static get AGGREGATIONS () {
|
|
|
|
return aggregationQuery.SUPPORTED_AGGREGATE_FUNCTIONS;
|
|
|
|
}
|
|
|
|
|
2017-12-19 02:06:01 +08:00
|
|
|
static get PLACEMENTS () {
|
2017-12-20 19:42:29 +08:00
|
|
|
return aggregationQuery.SUPPORTED_PLACEMENTS;
|
2017-12-19 02:06:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static get THRESHOLD () {
|
|
|
|
return 1e5; // 100K
|
|
|
|
}
|
|
|
|
|
|
|
|
static get RESOLUTION () {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static get SUPPORTED_GEOMETRY_TYPES () {
|
|
|
|
return [
|
|
|
|
'ST_Point'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-03-22 00:01:32 +08:00
|
|
|
static get FILTER_PARAMETERS () {
|
|
|
|
return [
|
|
|
|
// TODO: valid combinations of parameters:
|
|
|
|
// * Except for less/greater params, only one parameter allowed per filter.
|
|
|
|
// * Any less parameter can be combined with one of the greater paramters. (to define a range)
|
|
|
|
'less_than', 'less_than_or_equal_to',
|
|
|
|
'greater_than', 'greater_than_or_equal_to',
|
|
|
|
'equal', 'not_equal',
|
|
|
|
'between', 'in', 'not_in'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-03-07 01:14:15 +08:00
|
|
|
static get HAS_AGGREGATION_DISABLED () {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-12-19 02:06:01 +08:00
|
|
|
static supportsGeometryType(geometryType) {
|
|
|
|
return AggregationMapConfig.SUPPORTED_GEOMETRY_TYPES.includes(geometryType);
|
|
|
|
}
|
|
|
|
|
2018-01-29 22:48:35 +08:00
|
|
|
static getAggregationGeometryColumn() {
|
|
|
|
return aggregationQuery.GEOMETRY_COLUMN;
|
|
|
|
}
|
|
|
|
|
2017-12-23 01:19:57 +08:00
|
|
|
constructor (user, config, connection, datasource) {
|
2017-12-19 02:06:01 +08:00
|
|
|
super(config, datasource);
|
|
|
|
|
2017-12-19 02:19:02 +08:00
|
|
|
const validate = aggregationValidator(this);
|
2017-12-19 03:42:26 +08:00
|
|
|
const positiveNumberValidator = createPositiveNumberValidator(this);
|
2017-12-19 02:19:02 +08:00
|
|
|
const includesValidPlacementsValidator = createIncludesValueValidator(this, AggregationMapConfig.PLACEMENTS);
|
2017-12-20 19:42:29 +08:00
|
|
|
const aggregationColumnsValidator = createAggregationColumnsValidator(this, AggregationMapConfig.AGGREGATIONS);
|
2018-03-22 00:01:32 +08:00
|
|
|
const aggregationFiltersValidator = createAggregationFiltersValidator(
|
|
|
|
this, AggregationMapConfig.FILTER_PARAMETERS
|
|
|
|
);
|
2017-12-19 02:19:02 +08:00
|
|
|
|
2017-12-19 03:42:26 +08:00
|
|
|
validate('resolution', positiveNumberValidator);
|
2017-12-19 02:19:02 +08:00
|
|
|
validate('placement', includesValidPlacementsValidator);
|
2017-12-19 03:42:26 +08:00
|
|
|
validate('threshold', positiveNumberValidator);
|
|
|
|
validate('columns', aggregationColumnsValidator);
|
2018-03-22 00:01:32 +08:00
|
|
|
validate('filters', aggregationFiltersValidator);
|
2017-12-22 20:43:30 +08:00
|
|
|
|
2017-12-23 01:19:57 +08:00
|
|
|
this.user = user;
|
|
|
|
this.pgConnection = connection;
|
2017-12-19 02:06:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getAggregatedQuery (index) {
|
|
|
|
const { sql_raw, sql } = this.getLayer(index).options;
|
|
|
|
const {
|
2017-12-22 18:31:33 +08:00
|
|
|
// The default aggregation has no placement, columns or dimensions;
|
|
|
|
// this enables the special "full-sample" aggregation.
|
2017-12-19 02:06:01 +08:00
|
|
|
resolution = AggregationMapConfig.RESOLUTION,
|
|
|
|
threshold = AggregationMapConfig.THRESHOLD,
|
2017-12-27 19:48:06 +08:00
|
|
|
placement,
|
2017-12-19 02:06:01 +08:00
|
|
|
columns = {},
|
2018-03-22 00:01:32 +08:00
|
|
|
dimensions = {},
|
|
|
|
filters = {}
|
2017-12-19 02:06:01 +08:00
|
|
|
} = this.getAggregation(index);
|
|
|
|
|
|
|
|
return aggregationQuery({
|
|
|
|
query: sql_raw || sql,
|
|
|
|
resolution,
|
|
|
|
threshold,
|
|
|
|
placement,
|
|
|
|
columns,
|
2017-12-28 20:50:59 +08:00
|
|
|
dimensions,
|
2018-03-22 00:01:32 +08:00
|
|
|
filters,
|
2017-12-28 20:50:59 +08:00
|
|
|
isDefaultAggregation: this._isDefaultLayerAggregation(index)
|
2017-12-19 02:06:01 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
isAggregationLayer (index) {
|
2019-03-07 01:14:15 +08:00
|
|
|
let hasAggregation = this.hasLayerAggregation(index);
|
|
|
|
// for vector-only MapConfig are aggregated unless explicitly disabled
|
2019-03-07 01:32:17 +08:00
|
|
|
return hasAggregation || (
|
|
|
|
this.isVectorOnlyMapConfig() && hasAggregation !== AggregationMapConfig.HAS_AGGREGATION_DISABLED
|
|
|
|
);
|
2017-12-19 02:06:01 +08:00
|
|
|
}
|
|
|
|
|
2019-03-07 01:14:15 +08:00
|
|
|
isAggregationMapConfig () {
|
2017-12-19 02:06:01 +08:00
|
|
|
const layers = this.getLayers();
|
|
|
|
|
|
|
|
for (let index = 0; index < layers.length; index++) {
|
2019-03-07 01:14:15 +08:00
|
|
|
if (this.isAggregationLayer(index)) {
|
2017-12-19 02:06:01 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-07 01:14:15 +08:00
|
|
|
/* Three possible return values:
|
|
|
|
* * true: explicit aggregation ({}, true)
|
|
|
|
* * false: no aggregation (undefined)
|
|
|
|
* * AggregationMapConfig.HAS_AGGREGATION_DISABLED: explicitly disabled aggregation (false)
|
|
|
|
*/
|
2017-12-19 02:06:01 +08:00
|
|
|
hasLayerAggregation (index) {
|
|
|
|
const layer = this.getLayer(index);
|
|
|
|
const { aggregation } = layer.options;
|
2019-03-07 01:14:15 +08:00
|
|
|
if (aggregation !== undefined && (typeof aggregation === 'object' || typeof aggregation === 'boolean')) {
|
|
|
|
if (aggregation === false) {
|
|
|
|
return AggregationMapConfig.HAS_AGGREGATION_DISABLED;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2017-12-19 02:06:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getAggregation (index) {
|
|
|
|
const { aggregation } = this.getLayer(index).options;
|
|
|
|
|
2019-03-07 01:14:15 +08:00
|
|
|
if (this.isAggregationLayer(index)) {
|
|
|
|
if (typeof aggregation === 'boolean' || !aggregation) {
|
|
|
|
return {};
|
|
|
|
}
|
2017-12-19 02:06:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return aggregation;
|
|
|
|
}
|
|
|
|
|
2017-12-28 20:11:32 +08:00
|
|
|
getLayerAggregationColumns (index, callback) {
|
|
|
|
if (this._isDefaultLayerAggregation(index)) {
|
|
|
|
const skipGeoms = true;
|
|
|
|
return this.getLayerColumns(index, skipGeoms, (err, columns) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, columns);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const columns = this._getLayerAggregationRequiredColumns(index);
|
|
|
|
|
|
|
|
return callback(null, columns);
|
|
|
|
}
|
|
|
|
|
|
|
|
_getLayerAggregationRequiredColumns (index) {
|
|
|
|
const { columns, dimensions } = this.getAggregation(index);
|
|
|
|
|
2018-04-04 23:18:56 +08:00
|
|
|
let finalColumns = ['cartodb_id', '_cdb_feature_count'];
|
|
|
|
|
2017-12-28 20:11:32 +08:00
|
|
|
let aggregatedColumns = [];
|
|
|
|
if (columns) {
|
2018-04-04 21:25:08 +08:00
|
|
|
aggregatedColumns = Object.keys(columns);
|
2017-12-28 20:11:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let dimensionsColumns = [];
|
|
|
|
if (dimensions) {
|
2018-04-04 21:25:08 +08:00
|
|
|
dimensionsColumns = Object.keys(dimensions);
|
2017-12-28 20:11:32 +08:00
|
|
|
}
|
|
|
|
|
2018-04-04 23:18:56 +08:00
|
|
|
return removeDuplicates(finalColumns.concat(aggregatedColumns).concat(dimensionsColumns));
|
2017-12-28 20:11:32 +08:00
|
|
|
}
|
|
|
|
|
2017-12-19 02:06:01 +08:00
|
|
|
doesLayerReachThreshold(index, featureCount) {
|
|
|
|
const threshold = this.getAggregation(index) && this.getAggregation(index).threshold ?
|
|
|
|
this.getAggregation(index).threshold :
|
|
|
|
AggregationMapConfig.THRESHOLD;
|
|
|
|
|
|
|
|
return featureCount >= threshold;
|
|
|
|
}
|
2017-12-22 20:43:30 +08:00
|
|
|
|
|
|
|
getLayerColumns (index, skipGeoms, callback) {
|
|
|
|
const geomColumns = ['the_geom', 'the_geom_webmercator'];
|
2019-06-27 00:03:02 +08:00
|
|
|
const limitedQuery = ctx => `SELECT * FROM (${ctx.query}) __cdb_aggregation_schema LIMIT 0`;
|
2017-12-22 20:43:30 +08:00
|
|
|
const layer = this.getLayer(index);
|
|
|
|
|
2017-12-23 01:19:57 +08:00
|
|
|
this.pgConnection.getConnection(this.user, (err, connection) => {
|
2017-12-22 20:43:30 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2017-12-28 03:08:43 +08:00
|
|
|
const sql = limitedQuery({
|
2018-06-18 19:15:45 +08:00
|
|
|
query: queryUtils.substituteDummyTokens(layer.options.sql)
|
2017-12-28 03:08:43 +08:00
|
|
|
});
|
2017-12-22 20:43:30 +08:00
|
|
|
|
2017-12-23 01:19:57 +08:00
|
|
|
connection.query(sql, (err, result) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2017-12-22 20:43:30 +08:00
|
|
|
|
2017-12-23 01:19:57 +08:00
|
|
|
let columns = result.fields || [];
|
|
|
|
|
2017-12-28 03:08:43 +08:00
|
|
|
columns = columns.map(({ name }) => name);
|
2017-12-22 20:43:30 +08:00
|
|
|
|
2017-12-23 01:19:57 +08:00
|
|
|
if (skipGeoms) {
|
|
|
|
columns = columns.filter((column) => !geomColumns.includes(column));
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(err, columns);
|
|
|
|
});
|
2017-12-22 20:43:30 +08:00
|
|
|
});
|
|
|
|
}
|
2017-12-23 01:19:57 +08:00
|
|
|
|
2017-12-28 20:11:32 +08:00
|
|
|
_isDefaultLayerAggregation (index) {
|
2017-12-23 01:19:57 +08:00
|
|
|
const aggregation = this.getAggregation(index);
|
2019-03-07 01:14:15 +08:00
|
|
|
return aggregation && this._isDefaultAggregation(aggregation);
|
2017-12-28 03:08:43 +08:00
|
|
|
}
|
|
|
|
|
2017-12-30 01:25:08 +08:00
|
|
|
_isDefaultAggregation (aggregation) {
|
2017-12-28 03:08:43 +08:00
|
|
|
return aggregation.placement === undefined &&
|
|
|
|
aggregation.columns === undefined &&
|
2018-03-22 00:01:32 +08:00
|
|
|
this._isEmptyParameter(aggregation.dimensions) &&
|
|
|
|
this._isEmptyParameter(aggregation.filters);
|
2017-12-28 03:08:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_isEmptyParameter(parameter) {
|
|
|
|
return parameter === undefined || parameter === null || this._isEmptyObject(parameter);
|
2017-12-23 01:19:57 +08:00
|
|
|
}
|
|
|
|
|
2017-12-28 03:08:43 +08:00
|
|
|
_isEmptyObject (parameter) {
|
|
|
|
return typeof parameter === 'object' && Object.keys(parameter).length === 0;
|
2017-12-23 01:19:57 +08:00
|
|
|
}
|
2017-12-19 02:06:01 +08:00
|
|
|
};
|