diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index aed91641..7f9d51d9 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -163,7 +163,7 @@ MapController.prototype.create = function(req, res, prepareConfigFn) { // ignore } } - self.analysisMapConfigAdapter.getLayers(analysisConfiguration, requestMapConfig, filters, this); + self.analysisMapConfigAdapter.getMapConfig(analysisConfiguration, requestMapConfig, filters, this); }, function beforeLayergroupCreate(err, requestMapConfig, _analysesResults) { assert.ifError(err); @@ -293,7 +293,9 @@ MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn var templateHash = self.templateMaps.fingerPrint(mapConfigProvider.template).substring(0, 8); layergroup.layergroupid = cdbuser + '@' + templateHash + '@' + layergroup.layergroupid; - addWidgetsUrl(req.context.user, layergroup); + addWidgetsUrl(cdbuser, layergroup); + addDataviewsUrls(cdbuser, layergroup, mapConfig.obj()); + addAnalysesMetadata(cdbuser, layergroup, mapConfigProvider.analysesResults); res.set('X-Layergroup-Id', layergroup.layergroupid); self.surrogateKeysCache.tag(res, new NamedMapsCacheEntry(cdbuser, mapConfigProvider.getTemplateName())); @@ -371,7 +373,7 @@ MapController.prototype.afterLayergroupCreate = function(req, res, mapconfig, an // TODO this should take into account several URL patterns addWidgetsUrl(username, layergroup); addDataviewsUrls(username, layergroup, mapconfig.obj()); - addAnalysesMetadata(username, layergroup, analysesResults); + addAnalysesMetadata(username, layergroup, analysesResults, true); if (req.method === 'GET') { var ttl = global.environment.varnish.layergroupTtl || 86400; res.set('Cache-Control', 'public,max-age='+ttl+',must-revalidate'); @@ -390,9 +392,10 @@ MapController.prototype.afterLayergroupCreate = function(req, res, mapconfig, an ); }; -function addAnalysesMetadata(username, layergroup, analysesResults) { +function addAnalysesMetadata(username, layergroup, analysesResults, includeQuery) { + includeQuery = includeQuery || false; analysesResults = analysesResults || []; - layergroup.metadata.analyses = layergroup.metadata.analyses || []; + layergroup.metadata.analyses = []; analysesResults.forEach(function(analysis) { var nodes = analysis.getSortedNodes(); @@ -402,9 +405,11 @@ function addAnalysesMetadata(username, layergroup, analysesResults) { var nodeResource = layergroup.layergroupid + '/analysis/node/' + node.id(); nodesIdMap[node.params.id] = { status: node.getStatus(), - query: node.getQuery(), url: getUrls(username, nodeResource) }; + if (includeQuery) { + nodesIdMap[node.params.id].query = node.getQuery(); + } } return nodesIdMap; diff --git a/lib/cartodb/models/analysis-mapconfig-adapter.js b/lib/cartodb/models/analysis-mapconfig-adapter.js index 935179ea..d2bf15bf 100644 --- a/lib/cartodb/models/analysis-mapconfig-adapter.js +++ b/lib/cartodb/models/analysis-mapconfig-adapter.js @@ -66,7 +66,7 @@ function getFilter(dataview, params) { }; } -AnalysisMapConfigAdapter.prototype.getLayers = function(analysisConfiguration, requestMapConfig, filters, callback) { +AnalysisMapConfigAdapter.prototype.getMapConfig = function(analysisConfiguration, requestMapConfig, filters, callback) { // jshint maxcomplexity:7 var self = this; filters = filters || {}; diff --git a/lib/cartodb/models/mapconfig/named_map_provider.js b/lib/cartodb/models/mapconfig/named_map_provider.js index b1eaba8b..2c3c3b48 100644 --- a/lib/cartodb/models/mapconfig/named_map_provider.js +++ b/lib/cartodb/models/mapconfig/named_map_provider.js @@ -41,6 +41,7 @@ function NamedMapMapConfigProvider(templateMaps, pgConnection, metadataBackend, this.mapConfig = null; this.rendererParams = null; this.context = {}; + this.analysesResults = []; } module.exports = NamedMapMapConfigProvider; @@ -118,11 +119,12 @@ NamedMapMapConfigProvider.prototype.getMapConfig = function(callback) { // ignore } } - self.analysisMapConfigAdapter.getLayers(analysisConfiguration, requestMapConfig, filters, this); + self.analysisMapConfigAdapter.getMapConfig(analysisConfiguration, requestMapConfig, filters, this); }, - function prepareLayergroup(err, _mapConfig) { + function prepareLayergroup(err, _mapConfig, analysesResults) { assert.ifError(err); var next = this; + self.analysesResults = analysesResults || []; self.namedLayersAdapter.getLayers(self.owner, _mapConfig.layers, self.pgConnection, function(err, layers, datasource) { if (err) { diff --git a/test/acceptance/analysis/analysis-layers.js b/test/acceptance/analysis/analysis-layers.js index f2a3569f..6d86f410 100644 --- a/test/acceptance/analysis/analysis-layers.js +++ b/test/acceptance/analysis/analysis-layers.js @@ -184,4 +184,33 @@ describe('analysis-layers', function() { testClient.drain(done); }); }); + + it('should retrieve enough metadata about analyses', function(done) { + var useCase = useCases[1]; + + // No API key here + var testClient = new TestClient(useCase.mapConfig, 1234); + + testClient.getLayergroup(function(err, layergroupResult) { + assert.ok(!err, err); + + assert.ok( + Array.isArray(layergroupResult.metadata.analyses), + 'Missing "analyses" array metadata from: ' + JSON.stringify(layergroupResult) + ); + var analyses = layergroupResult.metadata.analyses; + assert.equal(analyses.length, 1, 'Invalid number of analyses in metadata'); + var nodes = analyses[0].nodes; + var nodesIds = Object.keys(nodes); + assert.deepEqual(nodesIds, ['2570e105-7b37-40d2-bdf4-1af889598745', 'HEAD']); + nodesIds.forEach(function(nodeId) { + var node = nodes[nodeId]; + assert.ok(node.hasOwnProperty('url'), 'Missing "url" attribute in node'); + assert.ok(node.hasOwnProperty('status'), 'Missing "status" attribute in node'); + assert.ok(node.hasOwnProperty('query'), 'Missing "status" attribute in node'); + }); + + testClient.drain(done); + }); + }); }); diff --git a/test/acceptance/analysis/named-maps.js b/test/acceptance/analysis/named-maps.js index e022999c..13d84120 100644 --- a/test/acceptance/analysis/named-maps.js +++ b/test/acceptance/analysis/named-maps.js @@ -126,6 +126,22 @@ describe('named-maps analysis', function() { assert.ok(layergroup.hasOwnProperty('layergroupid'), "Missing 'layergroupid' from: " + res.body); layergroupid = layergroup.layergroupid; + assert.ok( + Array.isArray(layergroup.metadata.analyses), + 'Missing "analyses" array metadata from: ' + res.body + ); + var analyses = layergroup.metadata.analyses; + assert.equal(analyses.length, 1, 'Invalid number of analyses in metadata'); + var nodes = analyses[0].nodes; + var nodesIds = Object.keys(nodes); + assert.deepEqual(nodesIds, ['2570e105-7b37-40d2-bdf4-1af889598745', 'HEAD']); + nodesIds.forEach(function(nodeId) { + var node = nodes[nodeId]; + assert.ok(node.hasOwnProperty('url'), 'Missing "url" attribute in node'); + assert.ok(node.hasOwnProperty('status'), 'Missing "status" attribute in node'); + assert.ok(!node.hasOwnProperty('query'), 'Unexpected "query" attribute in node'); + }); + keysToDelete['map_cfg|' + LayergroupToken.parse(layergroup.layergroupid).token] = 0; keysToDelete['user:localhost:mapviews:global'] = 5;