Skip to content

Commit

Permalink
Update template use tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dbauszus-glx committed Sep 26, 2024
1 parent 4572362 commit f957a92
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 55 deletions.
116 changes: 67 additions & 49 deletions mod/workspace/_workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

})
}
5 changes: 2 additions & 3 deletions mod/workspace/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion mod/workspace/getTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions mod/workspace/mergeTemplates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down

0 comments on commit f957a92

Please sign in to comment.