extract surrogate key functionality to its own middleware

This commit is contained in:
Daniel García Aubert
2017-10-31 19:49:12 +01:00
parent e06f8fe25e
commit e0ffeb0adc

View File

@@ -274,6 +274,7 @@ MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn
res.locals.layergroup = layergroup;
res.locals.template = mapConfigProvider.template;
res.locals.context = mapConfigProvider.context;
res.locals.templateName = mapConfigProvider.getTemplateName();
const afterLayergroupCreate = self.afterLayergroupCreateBuilder({
useTemplateHash: true
@@ -289,8 +290,6 @@ MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn
const { layergroup } = res.locals;
self.surrogateKeysCache.tag(res, new NamedMapsCacheEntry(cdbuser, mapConfigProvider.getTemplateName()));
res.status(200);
if (req.query && req.query.callback) {
@@ -360,6 +359,10 @@ MapController.prototype.afterLayergroupCreateBuilder = function (options = {}) {
assert.ifError(err);
self.setTurboCartoMetadataToLayergroup(req, res, this);
},
function setSurrogateKeyHeader (err) {
assert.ifError(err);
self.setSurrogateKeyHeader(req, res, this);
},
function finish(err) {
callback(err);
}
@@ -431,15 +434,11 @@ MapController.prototype.getAffectedTables = function (req, res, 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();
@@ -547,6 +546,20 @@ function addContextMetadata(layergroup, mapConfig, context) {
}
}
MapController.prototype.setSurrogateKeyHeader = function (req, res, callback) {
const { affectedTables, user, templateName } = res.locals;
if (req.method === 'GET' && affectedTables.tables && affectedTables.tables.length > 0) {
this.surrogateKeysCache.tag(res, affectedTables);
}
if (templateName) {
this.surrogateKeysCache.tag(res, new NamedMapsCacheEntry(user, templateName));
}
callback();
};
function getLastUpdatedTime(analysesResults, lastUpdateTime) {
if (!Array.isArray(analysesResults)) {
return lastUpdateTime;