Get templateHash for static tile request and errored named map instantiations

remotes/origin/node-12
Daniel García Aubert 4 years ago
parent 8d73571f5b
commit a196a26ab4

@ -96,7 +96,19 @@ function getCacheBuster ({ 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 }) {

@ -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];

@ -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) {

@ -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();

Loading…
Cancel
Save