From 467bee4c911fddb5e780bffc560916fc49469c25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Tue, 31 Oct 2017 13:13:20 +0100 Subject: [PATCH] Split afterLayergroupCreate method in multiple "pre-middlewares" --- lib/cartodb/controllers/map.js | 157 +++++++++++++++++++++++---------- 1 file changed, 108 insertions(+), 49 deletions(-) diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index f1bbe55e..4603a7b3 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -312,8 +312,6 @@ MapController.prototype.afterLayergroupCreate = function(req, res, mapconfig, layergroup, analysesResults, callback) { var self = this; - var username = res.locals.user; - var tasksleft = 2; // redis key and affectedTables var errors = []; @@ -335,6 +333,7 @@ function(req, res, mapconfig, layergroup, analysesResults, callback) { // Don't wait for the mapview count increment to // take place before proceeding. Error will be logged // asynchronously + var username = res.locals.user; this.metadataBackend.incMapviewCount(username, mapconfig.obj().stat_tag, function(err) { req.profiler.done('incMapviewCount'); if ( err ) { @@ -343,6 +342,40 @@ function(req, res, mapconfig, layergroup, analysesResults, callback) { done(); }); + res.locals.layergroup = layergroup; + res.locals.mapconfig = mapconfig; + res.locals.analysesResults = analysesResults; + + step( + function getAffectedTables () { + self.getAffectedTables(req, res, this); + }, + function setCacheChannel (err) { + assert.ifError(err); + self.setCacheChannel(req, res, this); + }, + function setLastUpdatedTime (err) { + assert.ifError(err); + self.setLastUpdatedTimeToLayergroup(req, res, this); + }, + function setCacheControl (err) { + assert.ifError(err); + self.setCacheControl(req, res, this); + }, + function setLayerStats (err) { + assert.ifError(err); + self.setLayerStats(req, res, this); + }, + function finish(err) { + done(err); + } + ); +}; + +MapController.prototype.getAffectedTables = function (req, res, callback) { + const self = this; + const { dbname, layergroup, user, mapconfig } = res.locals; + var sql = []; mapconfig.getLayers().forEach(function(layer) { sql.push(layer.options.sql); @@ -353,65 +386,91 @@ function(req, res, mapconfig, layergroup, analysesResults, callback) { } }); - var dbName = res.locals.dbname; - var layergroupId = layergroup.layergroupid; - var dbConnection; - step( function getPgConnection() { - self.pgConnection.getConnection(username, this); + self.pgConnection.getConnection(user, this); }, function getAffectedTablesAndLastUpdatedTime(err, connection) { assert.ifError(err); - dbConnection = connection; - QueryTables.getAffectedTablesFromQuery(dbConnection, sql.join(';'), this); + QueryTables.getAffectedTablesFromQuery(connection, sql.join(';'), this); }, - function handleAffectedTablesAndLastUpdatedTime(err, result) { - req.profiler.done('queryTablesAndLastUpdated'); - assert.ifError(err); - // feed affected tables cache so it can be reused from, for instance, layergroup controller - self.layergroupAffectedTables.set(dbName, layergroupId, result); - - var lastUpdateTime = result.getLastUpdatedAt(); - lastUpdateTime = getLastUpdatedTime(analysesResults, lastUpdateTime) || lastUpdateTime; - - // last update for layergroup cache buster - layergroup.layergroupid = layergroup.layergroupid + ':' + lastUpdateTime; - layergroup.last_updated = new Date(lastUpdateTime).toISOString(); - - if (req.method === 'GET') { - var ttl = global.environment.varnish.layergroupTtl || 86400; - res.set('Cache-Control', 'public,max-age='+ttl+',must-revalidate'); - res.set('Last-Modified', (new Date()).toUTCString()); - res.set('X-Cache-Channel', result.getCacheChannel()); - if (result.tables && result.tables.length > 0) { - self.surrogateKeysCache.tag(res, result); - } + function handleAffectedTablesAndLastUpdatedTime(err, affectedTables) { + if (err) { + return callback(err); } - return null; - }, - function fetchLayersStats(err) { - assert.ifError(err); - var next = this; - self.statsBackend.getStats(mapconfig, dbConnection, function(err, layersStats) { - if (err) { - return next(err); - } - if (layersStats.length > 0) { - layergroup.metadata.layers.forEach(function (layer, index) { - layer.meta.stats = layersStats[index]; - }); - } - return next(); - }); - }, - function finish(err) { - done(err); + // feed affected tables cache so it can be reused from, for instance, layergroup controller + self.layergroupAffectedTables.set(dbname, layergroup.layergroupId, affectedTables); + + res.locals.affectedTables = affectedTables; + + callback(); } ); }; +MapController.prototype.setCacheChannel = function (req, res, callback) { + const self = this; + const { affectedTables } = res.locals; + + if (req.method === 'GET') { + res.set('Last-Modified', (new Date()).toUTCString()); + res.set('X-Cache-Channel', affectedTables.getCacheChannel()); + if (affectedTables.tables && affectedTables.tables.length > 0) { + self.surrogateKeysCache.tag(res, affectedTables); + } + } + + callback(); +}; + +MapController.prototype.setLastUpdatedTimeToLayergroup = function (req, res, callback) { + const { affectedTables, layergroup, analysesResults } = res.locals; + + var lastUpdateTime = affectedTables.getLastUpdatedAt(); + + lastUpdateTime = getLastUpdatedTime(analysesResults, lastUpdateTime) || lastUpdateTime; + + // last update for layergroup cache buster + layergroup.layergroupid = layergroup.layergroupid + ':' + lastUpdateTime; + layergroup.last_updated = new Date(lastUpdateTime).toISOString(); + + callback(); +}; + +MapController.prototype.setCacheControl = function (req, res, callback) { + if (req.method === 'GET') { + var ttl = global.environment.varnish.layergroupTtl || 86400; + res.set('Cache-Control', 'public,max-age='+ttl+',must-revalidate'); + } + + callback(); +}; + +MapController.prototype.setLayerStats = function (req, res, callback) { + const { user, mapconfig, layergroup } = res.locals; + + this.pgConnection.getConnection(user, (err, connection) => { + if (err) { + return callback(err); + } + + this.statsBackend.getStats(mapconfig, connection, function(err, layersStats) { + if (err) { + return callback(err); + } + + if (layersStats.length > 0) { + layergroup.metadata.layers.forEach(function (layer, index) { + layer.meta.stats = layersStats[index]; + }); + } + + callback(); + }); + }); +}; + function getLastUpdatedTime(analysesResults, lastUpdateTime) { if (!Array.isArray(analysesResults)) { return lastUpdateTime;