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; +}