From a196a26ab403f03f556897006e7cf6e1db2feeb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Thu, 30 Apr 2020 13:09:12 +0200 Subject: [PATCH] Get templateHash for static tile request and errored named map instantiations --- lib/api/middlewares/metrics.js | 14 +++++++++++++- lib/backends/template-maps.js | 11 +++++++---- .../mapconfig/provider/named-map-provider.js | 10 +++++++++- test/integration/metrics-test.js | 9 +++------ 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/lib/api/middlewares/metrics.js b/lib/api/middlewares/metrics.js index c4b3d570..b51ddba8 100644 --- a/lib/api/middlewares/metrics.js +++ b/lib/api/middlewares/metrics.js @@ -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 }) { diff --git a/lib/backends/template-maps.js b/lib/backends/template-maps.js index 9d09eb9d..f8ff641e 100644 --- a/lib/backends/template-maps.js +++ b/lib/backends/template-maps.js @@ -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]; diff --git a/lib/models/mapconfig/provider/named-map-provider.js b/lib/models/mapconfig/provider/named-map-provider.js index 0818eef2..a131ce18 100644 --- a/lib/models/mapconfig/provider/named-map-provider.js +++ b/lib/models/mapconfig/provider/named-map-provider.js @@ -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) { diff --git a/test/integration/metrics-test.js b/test/integration/metrics-test.js index 53640792..52d345a7 100644 --- a/test/integration/metrics-test.js +++ b/test/integration/metrics-test.js @@ -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();