From 02f93f3a14ae85bcb26c9db734a7c3900714f2b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 9 Mar 2018 12:58:05 +0100 Subject: [PATCH] Extract layergroup-metadata class --- lib/cartodb/controllers/map.js | 193 +++-------------------- lib/cartodb/utils/layergroup-metadata.js | 168 ++++++++++++++++++++ 2 files changed, 186 insertions(+), 175 deletions(-) create mode 100644 lib/cartodb/utils/layergroup-metadata.js diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index 94e74657..01fe461b 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -15,6 +15,7 @@ var NamedMapsCacheEntry = require('../cache/model/named_maps_entry'); var NamedMapMapConfigProvider = require('../models/mapconfig/provider/named-map-provider'); var CreateLayergroupMapConfigProvider = require('../models/mapconfig/provider/create-layergroup-provider'); +const LayergroupMetadata = require('../utils/layergroup-metadata'); /** * @param {AuthApi} authApi @@ -41,7 +42,8 @@ function MapController(prepareContext, pgConnection, templateMaps, mapBackend, m this.layergroupAffectedTables = layergroupAffectedTables; this.mapConfigAdapter = mapConfigAdapter; - this.resourceLocator = new ResourceLocator(global.environment); + const resourceLocator = new ResourceLocator(global.environment); + this.layergroupMetadata = new LayergroupMetadata(resourceLocator); this.statsBackend = statsBackend; this.prepareContext = prepareContext; @@ -98,11 +100,11 @@ MapController.prototype.composeCreateMapMiddleware = function (useTemplate = fal setCacheControl(), setLayerStats(this.pgConnection, this.statsBackend), setLayergroupIdHeader(this.templateMaps ,useTemplateHash), - setDataviewsAndWidgetsUrlsToLayergroupMetadata(this.resourceLocator), - setAnalysesMetadataToLayergroup(this.resourceLocator, includeQuery), - setTurboCartoMetadataToLayergroup(), - setAggregationMetadataToLayergroup(), - setTilejsonMetadataToLayergroup(this.resourceLocator), + setDataviewsAndWidgetsUrlsToLayergroupMetadata(this.layergroupMetadata), + setAnalysesMetadataToLayergroup(this.layergroupMetadata, includeQuery), + setTurboCartoMetadataToLayergroup(this.layergroupMetadata), + setAggregationMetadataToLayergroup(this.layergroupMetadata), + setTilejsonMetadataToLayergroup(this.layergroupMetadata), setSurrogateKeyHeader(this.surrogateKeysCache), sendResponse(), augmentError({ label, addContext }) @@ -326,23 +328,6 @@ function augmentLayergroupData () { }; } -function getTemplateUrl(url) { - return url.https || url.http; -} - -function getTilejson(tiles, grids) { - const tilejson = { - tilejson: '2.2.0', - tiles: tiles.https || tiles.http - }; - - if (grids) { - tilejson.grids = grids.https || grids.http; - } - - return tilejson; -} - function getAffectedTables (pgConnection, layergroupAffectedTables) { return function getAffectedTablesMiddleware (req, res, next) { const { dbname, layergroup, user, mapconfig } = res.locals; @@ -482,193 +467,51 @@ function setLayergroupIdHeader (templateMaps, useTemplateHash) { }; } -function setDataviewsAndWidgetsUrlsToLayergroupMetadata (resourceLocator) { +function setDataviewsAndWidgetsUrlsToLayergroupMetadata (layergroupMetadata) { return function setDataviewsAndWidgetsUrlsToLayergroupMetadataMiddleware (req, res, next) { const { layergroup, user, mapconfig } = res.locals; - addDataviewsAndWidgetsUrls(resourceLocator, user, layergroup, mapconfig.obj()); + layergroupMetadata.addDataviewsAndWidgetsUrls(user, layergroup, mapconfig.obj()); next(); }; -}; - -// TODO this should take into account several URL patterns -function addDataviewsAndWidgetsUrls (resourceLocator, username, layergroup, mapConfig) { - addDataviewsUrls(resourceLocator, username, layergroup, mapConfig); - addWidgetsUrl(resourceLocator, username, layergroup, mapConfig); } - -function addDataviewsUrls (resourceLocator, username, layergroup, mapConfig) { - layergroup.metadata.dataviews = layergroup.metadata.dataviews || {}; - var dataviews = mapConfig.dataviews || {}; - - Object.keys(dataviews).forEach(function(dataviewName) { - var resource = layergroup.layergroupid + '/dataview/' + dataviewName; - layergroup.metadata.dataviews[dataviewName] = { - url: resourceLocator.getUrls(username, resource) - }; - }); -} - -function addWidgetsUrl (resourceLocator, username, layergroup, mapConfig) { - if (layergroup.metadata && Array.isArray(layergroup.metadata.layers) && Array.isArray(mapConfig.layers)) { - layergroup.metadata.layers = layergroup.metadata.layers.map(function(layer, layerIndex) { - var mapConfigLayer = mapConfig.layers[layerIndex]; - if (mapConfigLayer.options && mapConfigLayer.options.widgets) { - layer.widgets = layer.widgets || {}; - Object.keys(mapConfigLayer.options.widgets).forEach(function(widgetName) { - var resource = layergroup.layergroupid + '/' + layerIndex + '/widget/' + widgetName; - layer.widgets[widgetName] = { - type: mapConfigLayer.options.widgets[widgetName].type, - url: resourceLocator.getUrls(username, resource) - }; - }); - } - return layer; - }); - } -} - -function setAnalysesMetadataToLayergroup (resourceLocator, includeQuery) { +function setAnalysesMetadataToLayergroup (layergroupMetadata, includeQuery) { return function setAnalysesMetadataToLayergroupMiddleware (req, res, next) { const { layergroup, user, analysesResults = [] } = res.locals; - addAnalysesMetadata(resourceLocator, user, layergroup, analysesResults, includeQuery); + layergroupMetadata.addAnalysesMetadata(user, layergroup, analysesResults, includeQuery); next(); }; } -function addAnalysesMetadata (resourceLocator, username, layergroup, analysesResults, includeQuery) { - includeQuery = includeQuery || false; - analysesResults = analysesResults || []; - layergroup.metadata.analyses = []; - - analysesResults.forEach(function(analysis) { - var nodes = analysis.getNodes(); - layergroup.metadata.analyses.push({ - nodes: nodes.reduce(function(nodesIdMap, node) { - if (node.params.id) { - var nodeResource = layergroup.layergroupid + '/analysis/node/' + node.id(); - var nodeRepr = { - status: node.getStatus(), - url: resourceLocator.getUrls(username, nodeResource) - }; - if (includeQuery) { - nodeRepr.query = node.getQuery(); - } - if (node.getStatus() === 'failed') { - nodeRepr.error_message = node.getErrorMessage(); - } - nodesIdMap[node.params.id] = nodeRepr; - } - - return nodesIdMap; - }, {}) - }); - }); -} - -function setTurboCartoMetadataToLayergroup () { +function setTurboCartoMetadataToLayergroup (layergroupMetadata) { return function setTurboCartoMetadataToLayergroupMiddleware (req, res, next) { const { layergroup, mapconfig, context } = res.locals; - addTurboCartoContextMetadata(layergroup, mapconfig.obj(), context); + layergroupMetadata.addTurboCartoContextMetadata(layergroup, mapconfig.obj(), context); next(); }; } -function addTurboCartoContextMetadata(layergroup, mapConfig, context) { - if (layergroup.metadata && Array.isArray(layergroup.metadata.layers) && Array.isArray(mapConfig.layers)) { - layergroup.metadata.layers = layergroup.metadata.layers.map(function(layer, layerIndex) { - if (context.turboCarto && Array.isArray(context.turboCarto.layers)) { - layer.meta.cartocss_meta = context.turboCarto.layers[layerIndex]; - } - return layer; - }); - } -} - -// TODO: see how evolve this function, it's a good candidate to be refactored -function setAggregationMetadataToLayergroup () { +function setAggregationMetadataToLayergroup (layergroupMetadata) { return function setAggregationMetadataToLayergroupMiddleware (req, res, next) { const { layergroup, mapconfig, context } = res.locals; - addAggregationContextMetadata(layergroup, mapconfig.obj(), context); + layergroupMetadata.addAggregationContextMetadata(layergroup, mapconfig.obj(), context); next(); }; } -function addAggregationContextMetadata(layergroup, mapConfig, context) { - if (layergroup.metadata && Array.isArray(layergroup.metadata.layers) && Array.isArray(mapConfig.layers)) { - layergroup.metadata.layers = layergroup.metadata.layers.map(function(layer, layerIndex) { - if (context.aggregation && Array.isArray(context.aggregation.layers)) { - layer.meta.aggregation = context.aggregation.layers[layerIndex]; - } - return layer; - }); - } -} - -function setTilejsonMetadataToLayergroup (resourceLocator) { +function setTilejsonMetadataToLayergroup (layergroupMetadata) { return function augmentLayergroupTilejsonMiddleware (req, res, next) { const { layergroup, user, mapconfig } = res.locals; - const isVectorOnlyMapConfig = mapconfig.isVectorOnlyMapConfig(); - let hasMapnikLayers = false; - layergroup.metadata.layers.forEach((layerMetadata, index) => { - const layerId = mapconfig.getLayerId(index); - const rasterResource = `${layergroup.layergroupid}/${layerId}/{z}/{x}/{y}.png`; - if (mapconfig.layerType(index) === 'mapnik') { - hasMapnikLayers = true; - const vectorResource = `${layergroup.layergroupid}/${layerId}/{z}/{x}/{y}.mvt`; - const layerTilejson = { - vector: getTilejson(resourceLocator.getTileUrls(user, vectorResource)) - }; - if (!isVectorOnlyMapConfig) { - let grids = null; - const layer = mapconfig.getLayer(index); - if (layer.options.interactivity) { - const gridResource = `${layergroup.layergroupid}/${layerId}/{z}/{x}/{y}.grid.json`; - grids = resourceLocator.getTileUrls(user, gridResource); - } - layerTilejson.raster = getTilejson( - resourceLocator.getTileUrls(user, rasterResource), - grids - ); - } - layerMetadata.tilejson = layerTilejson; - } else { - layerMetadata.tilejson = { - raster: getTilejson(resourceLocator.getTileUrls(user, rasterResource)) - }; - } - }); - - const tilejson = {}; - const url = {}; - - if (hasMapnikLayers) { - const vectorResource = `${layergroup.layergroupid}/{z}/{x}/{y}.mvt`; - tilejson.vector = getTilejson( - resourceLocator.getTileUrls(user, vectorResource) - ); - url.vector = getTemplateUrl(resourceLocator.getTemplateUrls(user, vectorResource)); - - if (!isVectorOnlyMapConfig) { - const rasterResource = `${layergroup.layergroupid}/{z}/{x}/{y}.png`; - tilejson.raster = getTilejson( - resourceLocator.getTileUrls(user, rasterResource) - ); - url.raster = getTemplateUrl(resourceLocator.getTemplateUrls(user, rasterResource)); - } - } - - layergroup.metadata.tilejson = tilejson; - layergroup.metadata.url = url; + layergroupMetadata.addTileJsonMetadata(layergroup, user, mapconfig); next(); }; diff --git a/lib/cartodb/utils/layergroup-metadata.js b/lib/cartodb/utils/layergroup-metadata.js new file mode 100644 index 00000000..0e5d702a --- /dev/null +++ b/lib/cartodb/utils/layergroup-metadata.js @@ -0,0 +1,168 @@ +module.exports = class LayergroupMetadata { + constructor (resourceLocator) { + this.resourceLocator = resourceLocator; + } + + // TODO this should take into account several URL patterns + addDataviewsAndWidgetsUrls (username, layergroup, mapConfig) { + this._addDataviewsUrls(username, layergroup, mapConfig); + this._addWidgetsUrl(username, layergroup, mapConfig); + } + + _addDataviewsUrls (username, layergroup, mapConfig) { + layergroup.metadata.dataviews = layergroup.metadata.dataviews || {}; + var dataviews = mapConfig.dataviews || {}; + + Object.keys(dataviews).forEach((dataviewName) => { + var resource = layergroup.layergroupid + '/dataview/' + dataviewName; + layergroup.metadata.dataviews[dataviewName] = { + url: this.resourceLocator.getUrls(username, resource) + }; + }); + } + + _addWidgetsUrl (username, layergroup, mapConfig) { + if (layergroup.metadata && Array.isArray(layergroup.metadata.layers) && Array.isArray(mapConfig.layers)) { + layergroup.metadata.layers = layergroup.metadata.layers.map((layer, layerIndex) => { + var mapConfigLayer = mapConfig.layers[layerIndex]; + if (mapConfigLayer.options && mapConfigLayer.options.widgets) { + layer.widgets = layer.widgets || {}; + Object.keys(mapConfigLayer.options.widgets).forEach((widgetName) => { + var resource = layergroup.layergroupid + '/' + layerIndex + '/widget/' + widgetName; + layer.widgets[widgetName] = { + type: mapConfigLayer.options.widgets[widgetName].type, + url: this.resourceLocator.getUrls(username, resource) + }; + }); + } + + return layer; + }); + } + } + + addAnalysesMetadata (username, layergroup, analysesResults, includeQuery) { + includeQuery = includeQuery || false; + analysesResults = analysesResults || []; + layergroup.metadata.analyses = []; + + analysesResults.forEach((analysis) => { + var nodes = analysis.getNodes(); + layergroup.metadata.analyses.push({ + nodes: nodes.reduce((nodesIdMap, node) => { + if (node.params.id) { + var nodeResource = layergroup.layergroupid + '/analysis/node/' + node.id(); + var nodeRepr = { + status: node.getStatus(), + url: this.resourceLocator.getUrls(username, nodeResource) + }; + if (includeQuery) { + nodeRepr.query = node.getQuery(); + } + if (node.getStatus() === 'failed') { + nodeRepr.error_message = node.getErrorMessage(); + } + nodesIdMap[node.params.id] = nodeRepr; + } + + return nodesIdMap; + }, {}) + }); + }); + } + + addAggregationContextMetadata (layergroup, mapConfig, context) { + if (layergroup.metadata && Array.isArray(layergroup.metadata.layers) && Array.isArray(mapConfig.layers)) { + layergroup.metadata.layers = layergroup.metadata.layers.map(function(layer, layerIndex) { + if (context.aggregation && Array.isArray(context.aggregation.layers)) { + layer.meta.aggregation = context.aggregation.layers[layerIndex]; + } + return layer; + }); + } + } + + addTileJsonMetadata (layergroup, user, mapconfig) { + const isVectorOnlyMapConfig = mapconfig.isVectorOnlyMapConfig(); + let hasMapnikLayers = false; + + layergroup.metadata.layers.forEach((layerMetadata, index) => { + const layerId = mapconfig.getLayerId(index); + const rasterResource = `${layergroup.layergroupid}/${layerId}/{z}/{x}/{y}.png`; + if (mapconfig.layerType(index) === 'mapnik') { + hasMapnikLayers = true; + const vectorResource = `${layergroup.layergroupid}/${layerId}/{z}/{x}/{y}.mvt`; + const layerTilejson = { + vector: this._getTilejson(this.resourceLocator.getTileUrls(user, vectorResource)) + }; + if (!isVectorOnlyMapConfig) { + let grids = null; + const layer = mapconfig.getLayer(index); + if (layer.options.interactivity) { + const gridResource = `${layergroup.layergroupid}/${layerId}/{z}/{x}/{y}.grid.json`; + grids = this.resourceLocator.getTileUrls(user, gridResource); + } + layerTilejson.raster = this._getTilejson( + this.resourceLocator.getTileUrls(user, rasterResource), + grids + ); + } + layerMetadata.tilejson = layerTilejson; + } else { + layerMetadata.tilejson = { + raster: this._getTilejson(this.resourceLocator.getTileUrls(user, rasterResource)) + }; + } + }); + + const tilejson = {}; + const url = {}; + + if (hasMapnikLayers) { + const vectorResource = `${layergroup.layergroupid}/{z}/{x}/{y}.mvt`; + tilejson.vector = this._getTilejson( + this.resourceLocator.getTileUrls(user, vectorResource) + ); + url.vector = this._getTemplateUrl(this.resourceLocator.getTemplateUrls(user, vectorResource)); + + if (!isVectorOnlyMapConfig) { + const rasterResource = `${layergroup.layergroupid}/{z}/{x}/{y}.png`; + tilejson.raster = this._getTilejson( + this.resourceLocator.getTileUrls(user, rasterResource) + ); + url.raster = this._getTemplateUrl(this.resourceLocator.getTemplateUrls(user, rasterResource)); + } + } + + layergroup.metadata.tilejson = tilejson; + layergroup.metadata.url = url; + } + + _getTilejson(tiles, grids) { + const tilejson = { + tilejson: '2.2.0', + tiles: tiles.https || tiles.http + }; + + if (grids) { + tilejson.grids = grids.https || grids.http; + } + + return tilejson; + } + + _getTemplateUrl(url) { + return url.https || url.http; + } + + addTurboCartoContextMetadata(layergroup, mapConfig, context) { + if (layergroup.metadata && Array.isArray(layergroup.metadata.layers) && Array.isArray(mapConfig.layers)) { + layergroup.metadata.layers = layergroup.metadata.layers.map(function(layer, layerIndex) { + if (context.turboCarto && Array.isArray(context.turboCarto.layers)) { + layer.meta.cartocss_meta = context.turboCarto.layers[layerIndex]; + } + return layer; + }); + } + } +};