Get templateHash for static tile request and errored named map instantiations

This commit is contained in:
Daniel García Aubert 2020-04-30 13:09:12 +02:00
parent 8d73571f5b
commit a196a26ab4
4 changed files with 32 additions and 12 deletions

View File

@ -96,9 +96,21 @@ function getCacheBuster ({ res }) {
}
function getTemplateHash ({ res }) {
if (res.locals.templateHash) {
return res.locals.templateHash;
}
if (res.locals.mapConfigProvider && res.locals.mapConfigProvider.getTemplateHash) {
let templateHash;
try {
templateHash = res.locals.mapConfigProvider.getTemplateHash();
} catch (e) {}
return templateHash;
}
}
function getStatTag ({ res }) {
if (res.locals.mapConfig) {
return res.locals.mapConfig.obj().stat_tag;

View File

@ -511,12 +511,15 @@ TemplateMaps.prototype.instance = function (template, params) {
// Return a fingerPrint of the object
TemplateMaps.prototype.fingerPrint = function (template) {
return crypto.createHash('md5')
.update(JSON.stringify(template))
.digest('hex')
;
return fingerPrint(template);
};
function fingerPrint (template) {
return crypto.createHash('md5').update(JSON.stringify(template)).digest('hex');
}
module.exports.fingerPrint = fingerPrint;
module.exports.templateName = function templateName (templateId) {
var templateIdTokens = templateId.split('@');
var name = templateIdTokens[0];

View File

@ -4,7 +4,7 @@ const BaseMapConfigProvider = require('./base-mapconfig-adapter');
const crypto = require('crypto');
const dot = require('dot');
const MapConfig = require('windshaft').model.MapConfig;
const templateName = require('../../../backends/template-maps').templateName;
const { templateName, fingerPrint: templateFingerPrint } = require('../../../backends/template-maps');
// Configure bases for cache keys suitable for string interpolation
const baseKey = '{{=it.dbname}}:{{=it.user}}:{{=it.templateName}}';
@ -258,6 +258,14 @@ module.exports = class NamedMapMapConfigProvider extends BaseMapConfigProvider {
getTemplateName () {
return this.templateName;
}
getTemplateHash () {
if (!this.template) {
throw new Error('Missing template, call "getTemplate()" method first');
}
return templateFingerPrint(this.template);
}
};
function createConfigHash (config) {

View File

@ -412,8 +412,7 @@ describe('metrics', function () {
assert.strictEqual(attributes.map_type, expectedMapType);
assert.strictEqual(typeof attributes.map_id, 'string');
assert.strictEqual(typeof attributes.cache_buster, 'string');
// TODO: uncomment this
// assert.strictEqual(typeof attributes.template_hash, 'string');
assert.strictEqual(typeof attributes.template_hash, 'string');
assert.strictEqual(attributes.stat_tag, templateMissingCartoCSSVersion.layergroup.stat_tag);
return done();
@ -491,8 +490,7 @@ describe('metrics', function () {
assert.strictEqual(attributes.map_type, expectedMapType);
assert.strictEqual(typeof attributes.map_id, 'string');
assert.strictEqual(typeof attributes.cache_buster, 'string');
// TODO: uncomment this
// assert.strictEqual(typeof attributes.template_hash, 'string');
assert.strictEqual(typeof attributes.template_hash, 'string');
assert.strictEqual(attributes.stat_tag, template.layergroup.stat_tag);
return done();
@ -543,8 +541,7 @@ describe('metrics', function () {
assert.strictEqual(attributes.map_type, expectedMapType);
assert.strictEqual(typeof attributes.map_id, 'string');
assert.strictEqual(typeof attributes.cache_buster, 'string');
// TODO: uncomment this
// assert.strictEqual(typeof attributes.template_hash, 'string');
assert.strictEqual(typeof attributes.template_hash, 'string');
assert.strictEqual(attributes.stat_tag, template.layergroup.stat_tag);
return done();