2016-02-13 01:38:06 +08:00
|
|
|
var queue = require('queue-async');
|
|
|
|
|
|
|
|
var camshaft = require('camshaft');
|
|
|
|
var dot = require('dot');
|
|
|
|
dot.templateSettings.strip = false;
|
|
|
|
|
2016-03-14 18:50:52 +08:00
|
|
|
function AnalysisMapConfigAdapter(templateMaps) {
|
2016-02-13 01:38:06 +08:00
|
|
|
this.templateMaps = templateMaps;
|
|
|
|
}
|
|
|
|
|
2016-03-14 18:50:52 +08:00
|
|
|
module.exports = AnalysisMapConfigAdapter;
|
2016-02-13 01:38:06 +08:00
|
|
|
|
2016-03-02 19:42:42 +08:00
|
|
|
var SKIP_COLUMNS = {
|
|
|
|
'the_geom': true,
|
|
|
|
'the_geom_webmercator': true
|
|
|
|
};
|
|
|
|
|
|
|
|
function skipColumns(columnNames) {
|
|
|
|
return columnNames
|
|
|
|
.filter(function(columnName) { return !SKIP_COLUMNS[columnName]; });
|
|
|
|
}
|
2016-02-13 01:38:06 +08:00
|
|
|
|
|
|
|
var layerQueryTemplate = dot.template([
|
2016-03-02 19:42:42 +08:00
|
|
|
'SELECT ST_Transform(the_geom, 3857) the_geom_webmercator, {{=it._columns}}',
|
2016-02-13 01:38:06 +08:00
|
|
|
'FROM ({{=it._query}}) _cdb_analysis_query'
|
|
|
|
].join('\n'));
|
|
|
|
|
2016-03-02 19:42:42 +08:00
|
|
|
function layerQuery(query, columnNames) {
|
|
|
|
return layerQueryTemplate({ _query: query, _columns: skipColumns(columnNames).join(', ') });
|
|
|
|
}
|
|
|
|
|
2016-03-10 00:39:20 +08:00
|
|
|
function shouldAdaptLayers(requestMapConfig) {
|
|
|
|
return Array.isArray(requestMapConfig.layers) &&
|
|
|
|
Array.isArray(requestMapConfig.analyses) && requestMapConfig.analyses.length > 0;
|
2016-03-02 19:42:42 +08:00
|
|
|
}
|
|
|
|
|
2016-03-14 18:50:52 +08:00
|
|
|
AnalysisMapConfigAdapter.prototype.getLayers = function(analysisConfiguration, requestMapConfig, callback) {
|
2016-02-20 00:13:28 +08:00
|
|
|
|
2016-03-10 00:39:20 +08:00
|
|
|
if (!shouldAdaptLayers(requestMapConfig)) {
|
2016-02-20 00:13:28 +08:00
|
|
|
return callback(null, requestMapConfig);
|
|
|
|
}
|
2016-02-13 01:38:06 +08:00
|
|
|
|
2016-03-10 00:39:20 +08:00
|
|
|
function createAnalysis(analysisDefinition, done) {
|
|
|
|
camshaft.create(analysisConfiguration, analysisDefinition, done);
|
2016-02-13 01:38:06 +08:00
|
|
|
}
|
|
|
|
|
2016-03-10 00:39:20 +08:00
|
|
|
var analysesQueue = queue(requestMapConfig.analyses.length);
|
|
|
|
requestMapConfig.analyses.forEach(function(analysis) {
|
|
|
|
analysesQueue.defer(createAnalysis, analysis);
|
|
|
|
});
|
|
|
|
|
|
|
|
analysesQueue.awaitAll(function(err, analysesResults) {
|
2016-02-13 01:38:06 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2016-03-10 00:39:20 +08:00
|
|
|
var sourceId2Node = analysesResults.reduce(function(sourceId2Query, analysis) {
|
|
|
|
var rootNode = analysis.getRoot();
|
|
|
|
if (rootNode.params && rootNode.params.id) {
|
|
|
|
sourceId2Query[rootNode.params.id] = rootNode;
|
|
|
|
}
|
2016-02-13 01:38:06 +08:00
|
|
|
|
2016-03-10 00:39:20 +08:00
|
|
|
analysis.getSortedNodes().forEach(function(node) {
|
|
|
|
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
|
|
|
|
2016-03-10 00:39:20 +08:00
|
|
|
requestMapConfig.layers = requestMapConfig.layers.map(function(layer) {
|
|
|
|
if (layer.options.source && layer.options.source.id) {
|
|
|
|
var layerNode = sourceId2Node[layer.options.source.id];
|
|
|
|
layer.options.sql = layerQuery(layerNode.getQuery(), layerNode.getColumns());
|
|
|
|
}
|
|
|
|
return layer;
|
2016-02-13 01:38:06 +08:00
|
|
|
});
|
2016-03-10 00:39:20 +08:00
|
|
|
|
2016-02-13 01:38:06 +08:00
|
|
|
return callback(null, requestMapConfig);
|
2016-03-10 00:39:20 +08:00
|
|
|
});
|
2016-02-13 01:38:06 +08:00
|
|
|
};
|