From df6f1b2d3298d63c3bf2f68a11fba4b5f8be24bb Mon Sep 17 00:00:00 2001 From: "D. Berge" Date: Fri, 22 Aug 2025 00:04:46 +0200 Subject: [PATCH] Add script to update comparison groups. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This should be run at regular intervals (via cron or so) to keep the comparisons up to date. It is not necessarily a good idea to run this as part of the runner.sh script as it will delay other tasks trying to update the active project every time. Probably OK to put it on a cronjbo every 2‒24 hours. If two copies are running concurrently that should not break anything but it will increase the server load. --- bin/update_comparisons.js | 60 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 bin/update_comparisons.js diff --git a/bin/update_comparisons.js b/bin/update_comparisons.js new file mode 100755 index 0000000..cc28e1c --- /dev/null +++ b/bin/update_comparisons.js @@ -0,0 +1,60 @@ +#!/usr/bin/node + +const cmp = require('../lib/www/server/lib/comparisons'); + + +async function main () { + console.log("Retrieving project groups"); + const groups = await cmp.groups(); + + if (!Object.keys(groups??{})?.length) { + console.log("No groups found"); + return 0; + } + + console.log(`Found ${groups.length} groups: ${Object.keys(groups).join(", ")}`); + + for (const groupName of Object.keys(groups)) { + const projects = groups[groupName]; + + console.log(`Fetching saved comparisons for ${groupName}`); + + const comparisons = await cmp.getGroup(groupName); + + // Check if there are any projects that have been modified since last comparison + // or if there are any pairs that are no longer part of the group + + const outdated = comparisons.filter( c => { + const baseline_tstamp = projects.find( p => p.pid === c.baseline_pid )?.tstamp; + const monitor_tstamp = projects.find( p => p.pid === c.monitor_pid )?.tstamp; + return (c.tstamp < baseline_tstamp) || (c.tstamp < monitor_tstamp) || + baseline_tstamp == null || monitor_tstamp == null; + }); + + for (const comparison of outdated) { + console.log(`Removing stale comparison: ${comparison.baseline_pid} → ${comparison.monitor_pid}`); + await cmp.remove(comparison.baseline_pid, comparison.monitor_pid); + } + + if (projects?.length < 2) { + console.log(`Group ${groupName} has less than two projects. No comparisons are possible`); + continue; + } + + // Re-run the comparisons that are not in the database. They may + // be missing either beacause they were not there to start with + // or because we just removed them due to being stale + + console.log(`Recalculating group ${groupName}`); + await cmp.saveGroup(groupName); + } + + console.log("Comparisons update done"); + return 0; +} + +if (require.main === module) { + main(); +} else { + module.exports = main; +}