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,7 +96,19 @@ function getCacheBuster ({ res }) {
} }
function getTemplateHash ({ res }) { function getTemplateHash ({ res }) {
return res.locals.templateHash; 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 }) { function getStatTag ({ res }) {

View File

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

View File

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

View File

@ -412,8 +412,7 @@ describe('metrics', function () {
assert.strictEqual(attributes.map_type, expectedMapType); assert.strictEqual(attributes.map_type, expectedMapType);
assert.strictEqual(typeof attributes.map_id, 'string'); assert.strictEqual(typeof attributes.map_id, 'string');
assert.strictEqual(typeof attributes.cache_buster, '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); assert.strictEqual(attributes.stat_tag, templateMissingCartoCSSVersion.layergroup.stat_tag);
return done(); return done();
@ -491,8 +490,7 @@ describe('metrics', function () {
assert.strictEqual(attributes.map_type, expectedMapType); assert.strictEqual(attributes.map_type, expectedMapType);
assert.strictEqual(typeof attributes.map_id, 'string'); assert.strictEqual(typeof attributes.map_id, 'string');
assert.strictEqual(typeof attributes.cache_buster, '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); assert.strictEqual(attributes.stat_tag, template.layergroup.stat_tag);
return done(); return done();
@ -543,8 +541,7 @@ describe('metrics', function () {
assert.strictEqual(attributes.map_type, expectedMapType); assert.strictEqual(attributes.map_type, expectedMapType);
assert.strictEqual(typeof attributes.map_id, 'string'); assert.strictEqual(typeof attributes.map_id, 'string');
assert.strictEqual(typeof attributes.cache_buster, '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); assert.strictEqual(attributes.stat_tag, template.layergroup.stat_tag);
return done(); return done();