Merge pull request #790 from CartoDB/fix-issue-maps-controller-refactor

Fix issue maps controller refactor
This commit is contained in:
Daniel
2017-11-13 13:04:41 +01:00
committed by GitHub
3 changed files with 69 additions and 80 deletions
+29 -33
View File
@@ -386,6 +386,8 @@ LayergroupController.prototype.staticMap = function(req, res, width, height, zoo
};
LayergroupController.prototype.sendResponse = function(req, res, body, status, headers) {
var self = this;
req.profiler.done('res');
res.set('Cache-Control', 'public,max-age=31536000');
@@ -400,48 +402,42 @@ LayergroupController.prototype.sendResponse = function(req, res, body, status, h
}
res.set('Last-Modified', lastUpdated.toUTCString());
this.setCacheChannel(req, res, (err) => {
if (err) {
global.logger.warn('ERROR generating cache channel: ' + err);
}
var dbName = res.locals.dbname;
step(
function getAffectedTables() {
self.getAffectedTables(res.locals.user, dbName, res.locals.token, this);
},
function sendResponse(err, affectedTables) {
req.profiler.done('affectedTables');
if (err) {
global.logger.warn('ERROR generating cache channel: ' + err);
}
if (!!affectedTables) {
res.set('X-Cache-Channel', affectedTables.getCacheChannel());
self.surrogateKeysCache.tag(res, affectedTables);
}
if (headers) {
res.set(headers);
}
if (headers) {
res.set(headers);
}
res.status(status);
res.status(status);
if (!Buffer.isBuffer(body) && typeof body === 'object') {
if (req.query && req.query.callback) {
res.jsonp(body);
if (!Buffer.isBuffer(body) && typeof body === 'object') {
if (req.query && req.query.callback) {
res.jsonp(body);
} else {
res.json(body);
}
} else {
res.json(body);
res.send(body);
}
} else {
res.send(body);
}
});
};
LayergroupController.prototype.setCacheChannel = function(req, res, callback) {
const { dbname, user, token } = res.locals;
this.getAffectedTables(user, dbname, token, (err, affectedTables) => {
req.profiler.done('affectedTables');
if (err) {
return callback(err);
}
if (!!affectedTables) {
res.set('X-Cache-Channel', affectedTables.getCacheChannel());
this.surrogateKeysCache.tag(res, affectedTables);
}
callback();
});
);
};
LayergroupController.prototype.getAffectedTables = function(user, dbName, layergroupId, callback) {
if (this.layergroupAffectedTables.hasAffectedTables(dbName, layergroupId)) {
return callback(null, this.layergroupAffectedTables.get(dbName, layergroupId));
}
+8 -2
View File
@@ -98,6 +98,7 @@ MapController.prototype.initProfiler = function (isTemplateInstantiation) {
return function initProfilerMiddleware (req, res, next) {
req.profiler.start(`windshaft-cartodb.${operation}_${req.method.toLowerCase()}`);
req.profiler.done(`${operation}.initProfilerMiddleware`);
next();
};
};
@@ -108,6 +109,8 @@ MapController.prototype.checkJsonContentType = function () {
return next(new Error('POST data must be of type application/json'));
}
req.profiler.done('checkJsonContentTypeMiddleware');
next();
};
};
@@ -130,6 +133,8 @@ MapController.prototype.checkInstantiteLayergroup = function () {
}
}
req.profiler.done('checkInstantiteLayergroup');
return next();
};
};
@@ -150,6 +155,7 @@ MapController.prototype.checkCreateLayergroup = function () {
}
}
req.profiler.done('checkCreateLayergroup');
return next();
};
};
@@ -173,7 +179,7 @@ MapController.prototype.getTemplate = function () {
);
mapconfigProvider.getMapConfig((err, mapconfig, rendererParams) => {
req.profiler.done('getMapConfig');
req.profiler.done('named.getMapConfig');
if (err) {
return next(err);
}
@@ -210,7 +216,7 @@ MapController.prototype.prepareAdapterMapConfig = function () {
};
this.mapConfigAdapter.getMapConfig(user, requestMapConfig, res.locals, context, (err, requestMapConfig) => {
req.profiler.done('getMapConfig');
req.profiler.done('anonymous.getMapConfig');
if (err) {
return next(err);
}
+32 -45
View File
@@ -39,56 +39,43 @@ NamedMapsController.prototype.register = function(app) {
);
};
NamedMapsController.prototype.sendResponse = function(req, res, body, headers) {
const { namedMapProvider } = res.locals;
NamedMapsController.prototype.sendResponse = function(req, res, body, headers, namedMapProvider) {
this.surrogateKeysCache.tag(res, new NamedMapsCacheEntry(res.locals.user, namedMapProvider.getTemplateName()));
res.set('Content-Type', headers['content-type'] || headers['Content-Type'] || 'image/png');
res.set('Cache-Control', 'public,max-age=7200,must-revalidate');
this.setCacheChannel(req, res, (err) => {
if (err) {
global.logger.log('ERROR generating cache channel: ' + err);
}
var self = this;
res.status(200);
res.send(body);
});
};
NamedMapsController.prototype.setCacheChannel = function(req, res, callback) {
const { namedMapProvider } = res.locals;
this.getAffectedTables(namedMapProvider, (err, result) => {
req.profiler.done('affectedTables');
if (err) {
global.logger.log('ERROR generating cache channel: ' + err);
}
if (!result || !!result.tables) {
// we increase cache control as we can invalidate it
res.set('Cache-Control', 'public,max-age=31536000');
var lastModifiedDate;
if (Number.isFinite(result.lastUpdatedTime)) {
lastModifiedDate = new Date(result.getLastUpdatedAt());
} else {
lastModifiedDate = new Date();
step(
function getAffectedTablesAndLastUpdatedTime() {
namedMapProvider.getAffectedTablesAndLastUpdatedTime(this);
},
function sendResponse(err, result) {
req.profiler.done('affectedTables');
if (err) {
global.logger.log('ERROR generating cache channel: ' + err);
}
res.set('Last-Modified', lastModifiedDate.toUTCString());
if (!result || !!result.tables) {
// we increase cache control as we can invalidate it
res.set('Cache-Control', 'public,max-age=31536000');
res.set('X-Cache-Channel', result.getCacheChannel());
if (result.tables.length > 0) {
this.surrogateKeysCache.tag(res, result);
var lastModifiedDate;
if (Number.isFinite(result.lastUpdatedTime)) {
lastModifiedDate = new Date(result.getLastUpdatedAt());
} else {
lastModifiedDate = new Date();
}
res.set('Last-Modified', lastModifiedDate.toUTCString());
res.set('X-Cache-Channel', result.getCacheChannel());
if (result.tables.length > 0) {
self.surrogateKeysCache.tag(res, result);
}
}
res.status(200);
res.send(body);
}
callback();
});
};
NamedMapsController.prototype.getAffectedTables = function(namedMapProvider, callback) {
namedMapProvider.getAffectedTablesAndLastUpdatedTime(callback);
);
};
NamedMapsController.prototype.tile = function(req, res, next) {
@@ -110,7 +97,7 @@ NamedMapsController.prototype.tile = function(req, res, next) {
},
function getTile(err, _namedMapProvider) {
assert.ifError(err);
res.locals.namedMapProvider = namedMapProvider = _namedMapProvider;
namedMapProvider = _namedMapProvider;
self.tileBackend.getTile(namedMapProvider, req.params, this);
},
function handleImage(err, tile, headers, stats) {
@@ -119,7 +106,7 @@ NamedMapsController.prototype.tile = function(req, res, next) {
err.label = 'NAMED_MAP_TILE';
next(err);
} else {
self.sendResponse(req, res, tile, headers);
self.sendResponse(req, res, tile, headers, namedMapProvider);
}
}
);
@@ -149,7 +136,7 @@ NamedMapsController.prototype.staticMap = function(req, res, next) {
function prepareLayerVisibility(err, _namedMapProvider) {
assert.ifError(err);
res.locals.namedMapProvider = namedMapProvider = _namedMapProvider;
namedMapProvider = _namedMapProvider;
self.prepareLayerFilterFromPreviewLayers(cdbUser, req, res.locals, namedMapProvider, this);
},
@@ -192,7 +179,7 @@ NamedMapsController.prototype.staticMap = function(req, res, next) {
err.label = 'STATIC_VIZ_MAP';
next(err);
} else {
self.sendResponse(req, res, image, headers);
self.sendResponse(req, res, image, headers, namedMapProvider);
}
}
);