From f957a92b1dcf5c1940900d528bf0d79df9963c86 Mon Sep 17 00:00:00 2001 From: dbauszus-glx Date: Thu, 26 Sep 2024 14:15:51 +0100 Subject: [PATCH] Update template use tests --- mod/workspace/_workspace.js | 116 ++++++++++++++++++-------------- mod/workspace/cache.js | 5 +- mod/workspace/getTemplate.js | 2 +- mod/workspace/mergeTemplates.js | 4 +- 4 files changed, 72 insertions(+), 55 deletions(-) diff --git a/mod/workspace/_workspace.js b/mod/workspace/_workspace.js index 32d9ce5d0f..4aca777954 100644 --- a/mod/workspace/_workspace.js +++ b/mod/workspace/_workspace.js @@ -307,93 +307,111 @@ async function test(req, res) { return } - const testResults = {}; - const errArr = [] - let custom_templates = {}; - let templateUsage = {}; + const test = { + results: {}, + errArr: [], + unused_templates: new Set(), + used_templates: [], + properties: new Set(['template', 'templates', 'query']) + } for (const localeKey of Object.keys(workspace.locales)) { // Will get layer and assignTemplates to workspace. const locale = await getLocale({ locale: localeKey, user: req.params.user }) - //We get the initial custom templates from the workspace.templates as we want to see usage of templates before we merge templates from layers. - custom_templates = { - ...Object.fromEntries( - Object.entries(workspace.templates).filter(([key, value]) => value._type === 'workspace_template') - ) - }; + Object.entries(workspace.templates) + .filter(([key, value]) => value._type === 'workspace') + .forEach(([key, value]) => test.unused_templates.add(key)) for (const layerKey of Object.keys(locale.layers)) { // Will get layer and assignTemplates to workspace. const layer = await getLayer({ locale: localeKey, layer: layerKey, user: req.params.user }) - if (layer.template) { - update_templateUsage(layer.template, custom_templates, templateUsage); - } - - layer.templates?.forEach(template => { - update_templateUsage(template, custom_templates, templateUsage); - }); - - if (layer.err) errArr.push(`${layerKey}: ${layer.err}`) + if (layer.err) test.errArr.push(`${layerKey}: ${layer.err}`) } + // Test locale and all of its layers as nested object. + templateUse(locale, test); } - //Finding the unused templates from the custom_template where we don't see any count in the templateUsage object. - const unused_templates = Object.keys(custom_templates).filter(template => !Object.keys(templateUsage).includes(template)) - - //Adding the results to the testResults object. - testResults.usage = templateUsage; - testResults.unused_templates = unused_templates; - // From here on its 🐢 Templates all the way down. for (const key of Object.keys(workspace.templates)) { const template = await getTemplate(key) - if (template.err) errArr.push(`${key}: ${template.err.path}`) + if (template.err) test.errArr.push(`${key}: ${template.err.path}`) } - testResults.errors = errArr.flat(); + test.results.errors = test.errArr.flat(); + + test.results.unused_templates = Array.from(test.unused_templates); + + // Reduce the test.used_templates array to count the occurance of each template. + test.results.usage = Object.fromEntries(test.used_templates + .reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map())); res.setHeader('content-type', 'application/json'); - res.send(JSON.stringify(testResults)); + res.send(JSON.stringify(test.results)); } /** -@function update_templateUsage +@function templateUse @description -Checks how many times a custom_template is used by layers. - -@param {string||object} template The template maybe a string or an object. -@param {Object} custom_templates An object of _type='workspace_template' templates. -@param {Object} templateUsage Also an object of _type='workspace_template' templates with an added count property. +Iterates through all nested object properties. +Test properties found in the test.properties Set. +Removes template keys from test.unused_templates Set. +Add template keys to test.used_templates Array. + +@param {Object} obj The object to test. +@param {Object} test The test config object. +@property {Set} test.properties Set of properties to test ['template', 'templates', 'query'] +@property {Set} test.unused_templates Set of templates not (yet) used. +@property {Arry} test.used_templates Array of template keys for each usage. */ -function update_templateUsage(template, custom_templates, templateUsage) { +function templateUse(obj, test) { + + if (obj === null) return; - // We set a template_key based on if the template is a string or an object. - const template_key = typeof template === 'string' ? template : template.key; + if (obj instanceof Object && !Object.keys(obj)) return; - // Get the template from the workspace.templates - const workspace_template = custom_templates[template_key]; + Object.entries(obj).forEach(entry => { - // If we get the template and have a key, then we will increase the usage counter. - if (template_key && workspace_template) { + // entry key === ['template', 'templates', 'query'] + if (test.properties.has(entry[0])) { - if (templateUsage[template_key]) { + if (typeof entry[1] === 'string') { - // Increase counter for existing templateKey in templateUsage. - templateUsage[template_key].count++; + test.unused_templates.delete(entry[1]) + test.used_templates.push(entry[1]) + } - } else { + if (Array.isArray(entry[1])) { - // Create templateKey in templateUsage with count 1. - templateUsage[template_key] = { count: 1 }; + entry[1] + .filter(item => typeof item === 'string') + .forEach(item => { + test.unused_templates.delete(item) + test.used_templates.push(item) + }) + } } - } + + // Iterate through each array, eg. infoj + if (Array.isArray(entry[1])) { + + entry[1].forEach(entry => templateUse(entry, test)) + return; + } + + // Iterate through nested objects eg. layers + if (entry[1] instanceof Object) { + + Object.values(entry[1])?.forEach(value => templateUse(value, test)) + } + + }) } diff --git a/mod/workspace/cache.js b/mod/workspace/cache.js index 6df7324ae1..7cf9f794da 100644 --- a/mod/workspace/cache.js +++ b/mod/workspace/cache.js @@ -107,7 +107,6 @@ async function cacheWorkspace() { */ function mark_template(templates_object, type) { - // custom_templates may be undefined. if (!templates_object) return; return Object.fromEntries( @@ -124,10 +123,10 @@ async function cacheWorkspace() { ...mark_template(msg_templates, 'core'), ...mark_template(query_templates, 'core'), - ...mark_template(custom_templates, 'custom_templates'), + ...mark_template(custom_templates, 'custom'), // Default templates can be overridden by assigning a template with the same key. - ...mark_template(workspace.templates, 'workspace_template') + ...mark_template(workspace.templates, 'workspace') } // A workspace must have a default locale [template] diff --git a/mod/workspace/getTemplate.js b/mod/workspace/getTemplate.js index 9c53227e12..58c3d86c8b 100644 --- a/mod/workspace/getTemplate.js +++ b/mod/workspace/getTemplate.js @@ -20,7 +20,7 @@ const envReplace = require('../utils/envReplace') /** @global @typedef {Object} template A template is an object property of the workspace.templates -@property {Object} _type The _type property distinguish the origin of a template. 'core' templates are added from the /mod/workspace/templates directory. A 'custom_template' is added from a custom_template JSON file defined in the process.env. A 'workspace_template' is added from the workspace itself. +@property {Object} _type The _type property distinguish the origin of a template. 'core' templates are added from the /mod/workspace/templates directory. A 'custom' is added from a custom_template JSON file defined in the process.env. A 'workspace' is added from the workspace itself. A _type='template' object is assigned in the [assignWorkspaceTemplates]{@link module:/workspace/mergeTemplates~assignWorkspaceTemplates} method. @property {String} src The source is a location from which a template object is loaded when required. Once loaded the template will be cached. @property {Object} cached The cached template. @property {String} template The string representation of a template, eg. html, sql. diff --git a/mod/workspace/mergeTemplates.js b/mod/workspace/mergeTemplates.js index 3043c1a916..646038f48e 100644 --- a/mod/workspace/mergeTemplates.js +++ b/mod/workspace/mergeTemplates.js @@ -119,7 +119,7 @@ module.exports = async function mergeTemplates(obj) { @description The method parses an object for a template object property. The template property value will be assigned to the workspace.templates{} object matching the template key value. -The template._type property will be set to 'workspace_template' indicating that the templates origin is in the workspace. It is possible to overassign _type:'core' templates which are loaded from the /mod/workspace/templates directory. +The template._type property will be set to 'template' indicating that the templates origin is in the workspace. It is possible to overassign _type:'core' templates which are loaded from the /mod/workspace/templates directory. The method will call itself for nested objects. @@ -135,7 +135,7 @@ function assignWorkspaceTemplates(obj) { if (entry[0] === 'template' && entry[1].key) { - entry[1]._type = 'workspace_template'; + entry[1]._type = 'template'; workspace.templates[entry[1].key] = Object.assign(workspace.templates[entry[1].key] || {}, entry[1]) return;