Analysis layers adapter skips analysis if there is only source nodes

This commit is contained in:
Raul Ochoa 2016-03-17 12:50:42 +01:00
parent ed84ed8475
commit 5bd30b6b5f
3 changed files with 95 additions and 8 deletions

View File

@ -1,4 +1,5 @@
var queue = require('queue-async'); var queue = require('queue-async');
var debug = require('debug')('windshaft:analysis');
var camshaft = require('camshaft'); var camshaft = require('camshaft');
var dot = require('dot'); var dot = require('dot');
@ -29,6 +30,32 @@ function layerQuery(query, columnNames) {
return layerQueryTemplate({ _query: query, _columns: skipColumns(columnNames).join(', ') }); return layerQueryTemplate({ _query: query, _columns: skipColumns(columnNames).join(', ') });
} }
function replaceSourceRootQueries(requestMapConfig) {
var analysisToRemove = {};
var analysisSourceRootsIds = requestMapConfig.analyses.reduce(function(sourceRootsIds, analysis, analysisIndex) {
if (analysis.type === 'source' && !!analysis.id) {
sourceRootsIds[analysis.id] = analysis;
analysisToRemove[analysisIndex] = true;
}
return sourceRootsIds;
}, {});
requestMapConfig.analyses = requestMapConfig.analyses.filter(function(analysis, index) {
return !analysisToRemove.hasOwnProperty(index);
});
requestMapConfig.layers = requestMapConfig.layers.map(function(layer) {
var sourceId = layer.options && layer.options.source && layer.options.source.id;
if (sourceId && analysisSourceRootsIds.hasOwnProperty(sourceId)) {
delete layer.options.source;
layer.options.sql = analysisSourceRootsIds[sourceId].params.query;
}
return layer;
});
return requestMapConfig;
}
function shouldAdaptLayers(requestMapConfig) { function shouldAdaptLayers(requestMapConfig) {
return Array.isArray(requestMapConfig.layers) && return Array.isArray(requestMapConfig.layers) &&
Array.isArray(requestMapConfig.analyses) && requestMapConfig.analyses.length > 0; Array.isArray(requestMapConfig.analyses) && requestMapConfig.analyses.length > 0;
@ -36,7 +63,14 @@ function shouldAdaptLayers(requestMapConfig) {
AnalysisMapConfigAdapter.prototype.getLayers = function(analysisConfiguration, requestMapConfig, callback) { AnalysisMapConfigAdapter.prototype.getLayers = function(analysisConfiguration, requestMapConfig, callback) {
debug('mapconfig input', JSON.stringify(requestMapConfig, null, 4));
if (Array.isArray(requestMapConfig.analyses)) {
requestMapConfig = replaceSourceRootQueries(requestMapConfig);
}
if (!shouldAdaptLayers(requestMapConfig)) { if (!shouldAdaptLayers(requestMapConfig)) {
debug('mapconfig output', JSON.stringify(requestMapConfig, null, 4));
return callback(null, requestMapConfig); return callback(null, requestMapConfig);
} }
@ -77,6 +111,8 @@ AnalysisMapConfigAdapter.prototype.getLayers = function(analysisConfiguration, r
return layer; return layer;
}); });
debug('mapconfig output', JSON.stringify(requestMapConfig, null, 4));
return callback(null, requestMapConfig); return callback(null, requestMapConfig);
}); });
}; };

View File

@ -53,11 +53,12 @@ describe('analysis-layers', function() {
var DEFAULT_MULTITYPE_STYLE = cartocss(); var DEFAULT_MULTITYPE_STYLE = cartocss();
var TILE_ANALYSIS_TABLES = { z: 14, x: 8023, y: 6177 }; var TILE_ANALYSIS_TABLES = { z: 0, x: 0, y: 0 };
var useCases = [ var useCases = [
{ {
desc: 'basic source-id mapnik layer', desc: 'basic source-id mapnik layer',
fixture: 'basic-source-id-mapnik-layer.png',
mapConfig: mapConfig( mapConfig: mapConfig(
[ [
{ {
@ -67,8 +68,7 @@ describe('analysis-layers', function() {
"id": "2570e105-7b37-40d2-bdf4-1af889598745" "id": "2570e105-7b37-40d2-bdf4-1af889598745"
}, },
"cartocss": DEFAULT_MULTITYPE_STYLE, "cartocss": DEFAULT_MULTITYPE_STYLE,
"cartocss_version": "2.1.1", "cartocss_version": "2.3.0"
"interactivity": []
} }
} }
], ],
@ -82,8 +82,44 @@ describe('analysis-layers', function() {
} }
} }
] ]
), )
tile: { z: 0, x: 0, y: 0 } },
{
desc: 'buffer over source',
fixture: 'buffer-over-source.png',
tile: { z: 7, x: 61, y: 47 },
mapConfig: mapConfig(
[
{
"type": "cartodb",
"options": {
"source": {
"id": "HEAD"
},
"cartocss": DEFAULT_MULTITYPE_STYLE,
"cartocss_version": "2.3.0"
}
}
],
{},
[
{
"id": "HEAD",
"type": "buffer",
"params": {
"source": {
"id": "2570e105-7b37-40d2-bdf4-1af889598745",
"type": "source",
"params": {
"query": "select * from populated_places_simple_reduced"
}
},
"radio": 50000
}
}
]
)
} }
]; ];
@ -100,7 +136,7 @@ describe('analysis-layers', function() {
// To generate images use: // To generate images use:
// image.save('/tmp/' + useCase.desc.replace(/\s/g, '-') + '.png'); // image.save('/tmp/' + useCase.desc.replace(/\s/g, '-') + '.png');
var fixturePath = './test/fixtures/analysis/basic-source-id-mapnik-layer.png'; var fixturePath = './test/fixtures/analysis/' + useCase.fixture;
assert.imageIsSimilarToFile(image, fixturePath, IMAGE_TOLERANCE_PER_MIL, function(err) { assert.imageIsSimilarToFile(image, fixturePath, IMAGE_TOLERANCE_PER_MIL, function(err) {
assert.ok(!err, err); assert.ok(!err, err);
@ -110,12 +146,27 @@ describe('analysis-layers', function() {
}); });
}); });
it('should fail for non-authenticated requests', function(done) { it('should NOT fail for non-authenticated requests when it is just source', function(done) {
var useCase = useCases[0]; var useCase = useCases[0];
// No API key here // No API key here
var testClient = new TestClient(useCase.mapConfig); var testClient = new TestClient(useCase.mapConfig);
testClient.getLayergroup(function(err, layergroupResult) {
assert.ok(!err, err);
assert.equal(layergroupResult.metadata.layers.length, 1);
testClient.drain(done);
});
});
it('should fail for non-authenticated requests that has a node other than "source"', function(done) {
var useCase = useCases[1];
// No API key here
var testClient = new TestClient(useCase.mapConfig);
var PERMISSION_DENIED_RESPONSE = { var PERMISSION_DENIED_RESPONSE = {
status: 403, status: 403,
headers: { headers: {
@ -128,7 +179,7 @@ describe('analysis-layers', function() {
assert.deepEqual(layergroupResult.errors, ["permission denied for relation cdb_tablemetadata"]); assert.deepEqual(layergroupResult.errors, ["permission denied for relation cdb_tablemetadata"]);
done(); testClient.drain(done);
}); });
}); });
}); });

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB