2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-02-13 01:38:06 +08:00
|
|
|
var queue = require('queue-async');
|
2016-03-17 19:50:42 +08:00
|
|
|
var debug = require('debug')('windshaft:analysis');
|
2016-02-13 01:38:06 +08:00
|
|
|
|
|
|
|
var camshaft = require('camshaft');
|
2019-11-25 20:36:56 +08:00
|
|
|
const queryUtils = require('../../../../lib/utils/query-utils');
|
2016-02-13 01:38:06 +08:00
|
|
|
var dot = require('dot');
|
|
|
|
dot.templateSettings.strip = false;
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function AnalysisMapConfigAdapter (analysisBackend) {
|
2016-04-14 23:09:07 +08:00
|
|
|
this.analysisBackend = analysisBackend;
|
2016-02-13 01:38:06 +08:00
|
|
|
}
|
|
|
|
|
2016-03-14 18:50:52 +08:00
|
|
|
module.exports = AnalysisMapConfigAdapter;
|
2016-02-13 01:38:06 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
AnalysisMapConfigAdapter.prototype.getMapConfig = function (user, requestMapConfig, params, context, callback) {
|
2016-04-14 23:09:07 +08:00
|
|
|
var self = this;
|
2016-04-20 21:40:14 +08:00
|
|
|
|
|
|
|
if (!shouldAdaptLayers(requestMapConfig)) {
|
|
|
|
return callback(null, requestMapConfig);
|
|
|
|
}
|
|
|
|
|
2016-05-23 22:44:14 +08:00
|
|
|
var analysisConfiguration = context.analysisConfiguration;
|
|
|
|
|
|
|
|
var filters = {};
|
|
|
|
if (params.filters) {
|
|
|
|
try {
|
|
|
|
filters = JSON.parse(params.filters);
|
|
|
|
} catch (e) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-19 00:22:02 +08:00
|
|
|
var dataviewsFilters = filters.dataviews || {};
|
|
|
|
debug(dataviewsFilters);
|
|
|
|
var dataviews = requestMapConfig.dataviews || {};
|
|
|
|
|
2016-04-21 22:16:00 +08:00
|
|
|
var errors = getDataviewsErrors(dataviews);
|
|
|
|
if (errors.length > 0) {
|
|
|
|
return callback(errors);
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
var dataviewsFiltersBySourceId = Object.keys(dataviewsFilters).reduce(function (bySourceId, dataviewName) {
|
2016-03-19 00:22:02 +08:00
|
|
|
var dataview = dataviews[dataviewName];
|
|
|
|
if (dataview) {
|
|
|
|
var sourceId = dataview.source.id;
|
2019-10-22 05:33:27 +08:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(bySourceId, sourceId)) {
|
2016-03-19 00:22:02 +08:00
|
|
|
bySourceId[sourceId] = {};
|
|
|
|
}
|
|
|
|
|
2016-03-22 19:22:04 +08:00
|
|
|
bySourceId[sourceId][dataviewName] = getFilter(dataview, dataviewsFilters[dataviewName]);
|
2016-03-19 00:22:02 +08:00
|
|
|
}
|
|
|
|
return bySourceId;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
debug(dataviewsFiltersBySourceId);
|
2016-02-20 00:13:28 +08:00
|
|
|
|
2016-03-17 19:50:42 +08:00
|
|
|
debug('mapconfig input', JSON.stringify(requestMapConfig, null, 4));
|
|
|
|
|
2016-03-22 01:02:19 +08:00
|
|
|
requestMapConfig = appendFiltersToNodes(requestMapConfig, dataviewsFiltersBySourceId);
|
2016-03-19 00:28:36 +08:00
|
|
|
|
2017-10-03 01:16:44 +08:00
|
|
|
// Expected format for analyses filters
|
|
|
|
// filters = {analyses: {
|
|
|
|
// a1: [{min, max}, {accept, reject}],
|
|
|
|
// b1: [{range, column, min, max}, {category, column, accept, reject}]
|
|
|
|
// }}
|
|
|
|
requestMapConfig = appendFiltersToNodes(requestMapConfig, filters.analyses);
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function createAnalysis (analysisDefinition, done) {
|
2016-06-13 18:20:56 +08:00
|
|
|
self.analysisBackend.create(analysisConfiguration, analysisDefinition, function (err, analysis) {
|
|
|
|
if (err) {
|
2016-06-22 00:26:10 +08:00
|
|
|
var error = new Error(err.message);
|
|
|
|
error.type = 'analysis';
|
2016-06-27 00:43:04 +08:00
|
|
|
error.analysis = {
|
2016-06-22 00:26:10 +08:00
|
|
|
id: analysisDefinition.id,
|
2016-07-11 21:48:26 +08:00
|
|
|
node_id: err.node_id,
|
2016-06-22 00:26:10 +08:00
|
|
|
type: analysisDefinition.type
|
2016-06-13 18:20:56 +08:00
|
|
|
};
|
2016-06-22 00:26:10 +08:00
|
|
|
return done(error);
|
2016-06-13 18:20:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
done(null, analysis);
|
|
|
|
});
|
2016-02-13 01:38:06 +08:00
|
|
|
}
|
|
|
|
|
2016-07-07 06:36:59 +08:00
|
|
|
var analysesQueue = queue(1);
|
2019-10-22 01:07:24 +08:00
|
|
|
requestMapConfig.analyses.forEach(function (analysis) {
|
2016-06-27 00:43:04 +08:00
|
|
|
analysesQueue.defer(createAnalysis, analysis);
|
2016-03-10 00:39:20 +08:00
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
analysesQueue.awaitAll(function (err, analysesResults) {
|
2016-02-13 01:38:06 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
var sourceId2Node = analysesResults.reduce(function (sourceId2Query, analysis) {
|
2016-03-10 00:39:20 +08:00
|
|
|
var rootNode = analysis.getRoot();
|
|
|
|
if (rootNode.params && rootNode.params.id) {
|
|
|
|
sourceId2Query[rootNode.params.id] = rootNode;
|
|
|
|
}
|
2016-02-13 01:38:06 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
analysis.getNodes().forEach(function (node) {
|
2016-03-10 00:39:20 +08:00
|
|
|
if (node.params && node.params.id) {
|
|
|
|
sourceId2Query[node.params.id] = node;
|
|
|
|
}
|
|
|
|
});
|
2016-02-13 01:38:06 +08:00
|
|
|
|
2016-03-10 00:39:20 +08:00
|
|
|
return sourceId2Query;
|
|
|
|
}, {});
|
2016-02-13 01:38:06 +08:00
|
|
|
|
2018-10-22 21:26:59 +08:00
|
|
|
var analysesErrors = [];
|
2016-04-21 23:24:25 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
requestMapConfig.layers = requestMapConfig.layers.map(function (layer, layerIndex) {
|
2016-04-28 00:30:05 +08:00
|
|
|
if (getLayerSourceId(layer)) {
|
|
|
|
var layerSourceId = getLayerSourceId(layer);
|
2016-04-21 23:24:25 +08:00
|
|
|
var layerNode = sourceId2Node[layerSourceId];
|
|
|
|
if (layerNode) {
|
2018-10-22 21:26:59 +08:00
|
|
|
try {
|
|
|
|
var analysisSql;
|
|
|
|
|
|
|
|
// it might throw error: Range filter from camshaft, for instance.
|
|
|
|
analysisSql = layerQuery(layerNode);
|
|
|
|
|
|
|
|
var sqlQueryWrap = layer.options.sql_wrap;
|
|
|
|
if (sqlQueryWrap) {
|
|
|
|
layer.options.sql_raw = analysisSql;
|
|
|
|
analysisSql = sqlQueryWrap.replace(/<%=\s*sql\s*%>/g, analysisSql);
|
|
|
|
}
|
|
|
|
layer.options.sql = analysisSql;
|
|
|
|
layer.options.columns = getDataviewsColumns(getLayerDataviews(layer, dataviews));
|
|
|
|
layer.options.affected_tables = getAllAffectedTablesFromSourceNodes(layerNode);
|
|
|
|
} catch (error) {
|
|
|
|
analysesErrors.push(error);
|
2016-05-06 22:37:52 +08:00
|
|
|
}
|
2016-04-21 23:24:25 +08:00
|
|
|
} else {
|
2018-10-22 21:26:59 +08:00
|
|
|
analysesErrors.push(
|
2019-10-22 01:07:24 +08:00
|
|
|
new Error('Missing analysis node.id="' + layerSourceId + '" for layer=' + layerIndex)
|
2016-04-21 23:24:25 +08:00
|
|
|
);
|
|
|
|
}
|
2016-03-10 00:39:20 +08:00
|
|
|
}
|
|
|
|
return layer;
|
2016-02-13 01:38:06 +08:00
|
|
|
});
|
2016-03-10 00:39:20 +08:00
|
|
|
|
2016-06-21 23:25:28 +08:00
|
|
|
var missingDataviewsNodesErrors = getMissingDataviewsSourceIds(dataviews, sourceId2Node);
|
2018-10-22 21:26:59 +08:00
|
|
|
if (analysesErrors.length > 0 || missingDataviewsNodesErrors.length > 0) {
|
|
|
|
return callback(analysesErrors.concat(missingDataviewsNodesErrors));
|
2016-04-21 23:24:25 +08:00
|
|
|
}
|
|
|
|
|
2016-07-07 04:30:24 +08:00
|
|
|
// Augment dataviews with sql from analyses
|
2019-10-22 01:07:24 +08:00
|
|
|
Object.keys(dataviews).forEach(function (dataviewName) {
|
2016-07-07 04:30:24 +08:00
|
|
|
var dataview = requestMapConfig.dataviews[dataviewName];
|
|
|
|
var dataviewSourceId = dataview.source.id;
|
|
|
|
var dataviewNode = sourceId2Node[dataviewSourceId];
|
|
|
|
dataview.node = {
|
|
|
|
type: dataviewNode.type,
|
|
|
|
filters: dataviewNode.getFilters()
|
|
|
|
};
|
|
|
|
dataview.sql = {
|
|
|
|
own_filter_on: dataviewQuery(dataviewNode, dataviewName, true),
|
|
|
|
own_filter_off: dataviewQuery(dataviewNode, dataviewName, false),
|
|
|
|
no_filters: dataviewNode.getQuery(Object.keys(dataviewNode.getFilters())
|
2019-10-22 01:07:24 +08:00
|
|
|
.reduce(function (applyFilters, filterId) {
|
2016-07-07 04:30:24 +08:00
|
|
|
applyFilters[filterId] = false;
|
|
|
|
return applyFilters;
|
|
|
|
}, {})
|
|
|
|
)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
if (Object.keys(dataviews).length > 0) {
|
|
|
|
requestMapConfig.dataviews = dataviews;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug('mapconfig output', JSON.stringify(requestMapConfig, null, 4));
|
|
|
|
|
2016-05-24 00:59:23 +08:00
|
|
|
context.analysesResults = analysesResults;
|
|
|
|
|
|
|
|
return callback(null, requestMapConfig);
|
2016-03-10 00:39:20 +08:00
|
|
|
});
|
2016-02-13 01:38:06 +08:00
|
|
|
};
|
2016-04-21 22:16:00 +08:00
|
|
|
|
2016-05-25 23:24:28 +08:00
|
|
|
var SKIP_COLUMNS = {
|
2019-10-22 01:07:24 +08:00
|
|
|
the_geom: true,
|
|
|
|
the_geom_webmercator: true
|
2016-05-25 23:24:28 +08:00
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function skipColumns (columnNames) {
|
2016-05-25 23:24:28 +08:00
|
|
|
return columnNames
|
2019-11-25 20:36:56 +08:00
|
|
|
.filter(function (columnName) { return !SKIP_COLUMNS[queryUtils.stripQuotes(columnName)]; });
|
2016-05-25 23:24:28 +08:00
|
|
|
}
|
|
|
|
|
2016-07-07 04:30:24 +08:00
|
|
|
var wrappedQueryTpl = dot.template([
|
2016-05-25 23:24:28 +08:00
|
|
|
'SELECT {{=it._columns}}',
|
|
|
|
'FROM ({{=it._query}}) _cdb_analysis_query'
|
|
|
|
].join('\n'));
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function layerQuery (node) {
|
2016-05-25 23:24:28 +08:00
|
|
|
if (node.type === 'source') {
|
|
|
|
return node.getQuery();
|
|
|
|
}
|
|
|
|
var _columns = ['ST_Transform(the_geom, 3857) the_geom_webmercator'].concat(skipColumns(node.getColumns()));
|
2016-07-07 04:30:24 +08:00
|
|
|
return wrappedQueryTpl({ _query: node.getQuery(), _columns: _columns.join(', ') });
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function dataviewQuery (node, dataviewName, ownFilter) {
|
2016-07-07 04:30:24 +08:00
|
|
|
var applyFilters = {};
|
|
|
|
if (!ownFilter) {
|
|
|
|
applyFilters[dataviewName] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node.type === 'source') {
|
|
|
|
return node.getQuery(applyFilters);
|
|
|
|
}
|
|
|
|
var _columns = ['ST_Transform(the_geom, 3857) the_geom_webmercator'].concat(skipColumns(node.getColumns()));
|
|
|
|
return wrappedQueryTpl({ _query: node.getQuery(applyFilters), _columns: _columns.join(', ') });
|
2016-05-25 23:24:28 +08:00
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function appendFiltersToNodes (requestMapConfig, dataviewsFiltersBySourceId) {
|
2016-05-25 23:24:28 +08:00
|
|
|
var analyses = requestMapConfig.analyses || [];
|
2017-10-03 01:16:44 +08:00
|
|
|
dataviewsFiltersBySourceId = dataviewsFiltersBySourceId || {};
|
2016-05-25 23:24:28 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
requestMapConfig.analyses = analyses.map(function (analysisDefinition) {
|
2016-05-25 23:24:28 +08:00
|
|
|
var analysisGraph = new camshaft.reference.AnalysisGraph(analysisDefinition);
|
|
|
|
var definition = analysisDefinition;
|
2019-10-22 01:07:24 +08:00
|
|
|
Object.keys(dataviewsFiltersBySourceId).forEach(function (sourceId) {
|
|
|
|
definition = analysisGraph.getDefinitionWith(sourceId, { filters: dataviewsFiltersBySourceId[sourceId] });
|
2016-05-25 23:24:28 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return definition;
|
|
|
|
});
|
|
|
|
|
|
|
|
return requestMapConfig;
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function shouldAdaptLayers (requestMapConfig) {
|
2019-11-06 20:56:59 +08:00
|
|
|
return (Array.isArray(requestMapConfig.layers) && requestMapConfig.layers.some(getLayerSourceId)) ||
|
2016-06-21 23:25:28 +08:00
|
|
|
(Array.isArray(requestMapConfig.analyses) && requestMapConfig.analyses.length > 0) ||
|
|
|
|
requestMapConfig.dataviews;
|
2016-05-25 23:24:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var DATAVIEW_TYPE_2_FILTER_TYPE = {
|
|
|
|
aggregation: 'category',
|
|
|
|
histogram: 'range'
|
|
|
|
};
|
2019-10-22 01:07:24 +08:00
|
|
|
function getFilter (dataview, params) {
|
2016-05-25 23:24:28 +08:00
|
|
|
var type = dataview.type;
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: DATAVIEW_TYPE_2_FILTER_TYPE[type],
|
|
|
|
column: dataview.options.column,
|
|
|
|
params: params
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function getLayerSourceId (layer) {
|
2016-04-28 00:30:05 +08:00
|
|
|
return layer.options.source && layer.options.source.id;
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function getDataviewSourceId (dataview) {
|
2016-04-28 00:30:05 +08:00
|
|
|
return dataview.source && dataview.source.id;
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function getLayerDataviews (layer, dataviews) {
|
2016-04-28 00:30:05 +08:00
|
|
|
var layerDataviews = [];
|
|
|
|
|
|
|
|
var layerSourceId = getLayerSourceId(layer);
|
|
|
|
if (layerSourceId) {
|
|
|
|
var dataviewsList = getDataviewsList(dataviews);
|
2019-10-22 01:07:24 +08:00
|
|
|
dataviewsList.forEach(function (dataview) {
|
2016-04-28 00:30:05 +08:00
|
|
|
if (getDataviewSourceId(dataview) === layerSourceId) {
|
|
|
|
layerDataviews.push(dataview);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return layerDataviews;
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function getDataviewsColumns (dataviews) {
|
|
|
|
return Object.keys(dataviews.reduce(function (columnsDict, dataview) {
|
|
|
|
getDataviewColumns(dataview).forEach(function (columnName) {
|
|
|
|
if (columnName) {
|
2016-06-06 21:58:16 +08:00
|
|
|
columnsDict[columnName] = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return columnsDict;
|
|
|
|
}, {}));
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function getDataviewColumns (dataview) {
|
2016-04-28 00:30:05 +08:00
|
|
|
var columns = [];
|
|
|
|
var options = dataview.options;
|
2019-10-22 01:07:24 +08:00
|
|
|
['column', 'aggregationColumn'].forEach(function (opt) {
|
2019-10-22 05:33:27 +08:00
|
|
|
if (Object.prototype.hasOwnProperty.call(options, opt) && !!options[opt]) {
|
2016-04-28 00:30:05 +08:00
|
|
|
columns.push(options[opt]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return columns;
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function getDataviewsList (dataviews) {
|
|
|
|
return Object.keys(dataviews).map(function (dataviewKey) { return dataviews[dataviewKey]; });
|
2016-04-28 00:30:05 +08:00
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function getDataviewsErrors (dataviews) {
|
2016-06-10 00:13:54 +08:00
|
|
|
var dataviewType = typeof dataviews;
|
|
|
|
if (dataviewType !== 'object') {
|
|
|
|
return [new Error('"dataviews" must be a valid JSON object: "' + dataviewType + '" type found')];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(dataviews)) {
|
|
|
|
return [new Error('"dataviews" must be a valid JSON object: "array" type found')];
|
|
|
|
}
|
|
|
|
|
2016-04-21 22:16:00 +08:00
|
|
|
var errors = [];
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
Object.keys(dataviews).forEach(function (dataviewName) {
|
2016-04-21 22:16:00 +08:00
|
|
|
var dataview = dataviews[dataviewName];
|
2019-10-22 05:33:27 +08:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(dataview, 'source') || !dataview.source.id) {
|
2016-04-21 22:16:00 +08:00
|
|
|
errors.push(new Error('Dataview "' + dataviewName + '" is missing `source.id` attribute'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dataview.type) {
|
|
|
|
errors.push(new Error('Dataview "' + dataviewName + '" is missing `type` attribute'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
}
|
2016-06-21 23:25:28 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function getMissingDataviewsSourceIds (dataviews, sourceId2Node) {
|
2016-06-21 23:25:28 +08:00
|
|
|
var missingDataviewsSourceIds = [];
|
2019-10-22 01:07:24 +08:00
|
|
|
Object.keys(dataviews).forEach(function (dataviewName) {
|
2016-06-21 23:25:28 +08:00
|
|
|
var dataview = dataviews[dataviewName];
|
|
|
|
var dataviewSourceId = getDataviewSourceId(dataview);
|
2019-10-22 05:33:27 +08:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(sourceId2Node, dataviewSourceId)) {
|
2019-10-22 01:07:24 +08:00
|
|
|
missingDataviewsSourceIds.push(new AnalysisError('Node with `source.id="' + dataviewSourceId + '"`' +
|
2016-06-21 23:25:28 +08:00
|
|
|
' not found in analyses for dataview "' + dataviewName + '"'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return missingDataviewsSourceIds;
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function AnalysisError (message) {
|
2016-06-21 23:25:28 +08:00
|
|
|
Error.captureStackTrace(this, this.constructor);
|
|
|
|
this.name = this.constructor.name;
|
|
|
|
this.type = 'analysis';
|
|
|
|
this.message = message;
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function getAllAffectedTablesFromSourceNodes (node) {
|
2017-03-10 18:40:48 +08:00
|
|
|
var affectedTables = node.getAllInputNodes(function (node) {
|
|
|
|
return node.getType() === 'source';
|
2019-10-22 01:07:24 +08:00
|
|
|
}).reduce(function (list, node) {
|
2017-03-14 01:36:13 +08:00
|
|
|
return list.concat(node.getAffectedTables());
|
2019-10-22 01:07:24 +08:00
|
|
|
}, []);
|
2017-03-10 18:40:48 +08:00
|
|
|
return affectedTables;
|
|
|
|
}
|
|
|
|
|
2016-06-22 00:26:10 +08:00
|
|
|
require('util').inherits(AnalysisError, Error);
|