Extract checkLayerAggregationMetadata method

This commit is contained in:
Daniel García Aubert 2017-12-11 19:06:53 +01:00
parent 446449bbde
commit cc68b84212

View File

@ -138,47 +138,64 @@ module.exports = class AggregationMapConfigAdapter {
_adaptLayer (connection, layer, index, isVectorOnlyMapConfig, mapConfig) {
return new Promise((resolve, reject) => {
if (!isVectorOnlyMapConfig && !this._hasLayerAggregation(layer)) {
return resolve({ layer, index, adapted: false });
this._checkLayerAggregationMetadata(connection, isVectorOnlyMapConfig, layer, index, (err, shouldAdapt) => {
if (err) {
return reject(err);
}
if (shouldAdapt) {
const aggregation = new AggregationProxy(mapConfig, layer.options.sql, layer.options.aggregation);
const sqlQueryWrap = layer.options.sql_wrap;
let aggregationSql = aggregation.sql(layer.options);
if (sqlQueryWrap) {
layer.options.sql_raw = aggregationSql;
aggregationSql = sqlQueryWrap.replace(/<%=\s*sql\s*%>/g, aggregationSql);
}
layer.options.sql = aggregationSql;
}
return resolve({ layer, index, adapted: shouldAdapt });
});
});
}
_checkLayerAggregationMetadata (connection, isVectorOnlyMapConfig, layer, index, callback) {
let shouldAdapt = false;
if (!isVectorOnlyMapConfig && !this._hasLayerAggregation(layer)) {
return callback(null, shouldAdapt);
}
const aggregationMetadata = queryUtils.getAggregationMetadata({ query: layer.options.sql });
connection.query(aggregationMetadata, (err, res) => {
if (err) {
return callback(null, shouldAdapt);
}
const aggregationMetadata = queryUtils.getAggregationMetadata({ query: layer.options.sql });
const estimatedFeatureCount = res.rows[0].count;
connection.query(aggregationMetadata, (err, res) => {
if (err) {
return resolve({ layer, index, adapted: false });
}
const threshold = layer.options.aggregation && layer.options.aggregation.threshold ?
layer.options.aggregation.threshold :
1e5;
const estimatedFeatureCount = res.rows[0].count;
const threshold = layer.options.aggregation && layer.options.aggregation.threshold ?
layer.options.aggregation.threshold :
1e5;
if (estimatedFeatureCount < threshold) {
return callback(null, shouldAdapt);
}
const geometryType = res.rows[0].type;
const geometryType = res.rows[0].type;
if (estimatedFeatureCount < threshold) {
return resolve({ layer, index, adapted: false });
}
if (geometryType !== 'ST_Point') {
return callback(new Error(unsupportedGeometryTypeErrorMessage({ geometryType })));
}
if (geometryType !== 'ST_Point') {
return reject(new Error(unsupportedGeometryTypeErrorMessage({ geometryType })));
}
shouldAdapt = true;
const aggregation = new AggregationProxy(mapConfig, layer.options.sql, layer.options.aggregation);
const sqlQueryWrap = layer.options.sql_wrap;
let aggregationSql = aggregation.sql(layer.options);
if (sqlQueryWrap) {
layer.options.sql_raw = aggregationSql;
aggregationSql = sqlQueryWrap.replace(/<%=\s*sql\s*%>/g, aggregationSql);
}
layer.options.sql = aggregationSql;
return resolve({ layer, index, adapted: true });
});
callback(null, shouldAdapt);
});
}