diff --git a/api/models/Labels.js b/api/models/Labels.js index d86390a..93b013b 100644 --- a/api/models/Labels.js +++ b/api/models/Labels.js @@ -1,3 +1,6 @@ +const util = require('util'); +const exec = util.promisify(require('child_process').exec); + module.exports = { attributes: { name: { type: 'string', required: true, unique: true }, @@ -10,6 +13,52 @@ module.exports = { return labels; }, + /** + * Synchronize labels with scoutbot get_classes + */ + addMlLabelsAndReturnAllLabels: async function () { + try { + const checkCommand = `command -v scoutbot`; + const { stdout: checkScoutbot } = await exec(checkCommand); + + if (!checkScoutbot.trim()) { + console.warn('Scoutbot command is not available in this environment. Skipping label synchronization.'); + return { success: false, message: 'Scoutbot command not found. Label synchronization skipped.' }; + } + + // Fetch all existing labels from the database + const existingLabels = await Labels.find({}); + const existingLabelNames = existingLabels.map(label => label.name); + + const getClassesCommand = `scoutbot get_classes`; + const { stdout: classesStdout } = await exec(getClassesCommand); + + const cleanedJson = classesStdout + .replace(/'/g, '"') + .replace(/,\s*]/, ']') + .trim(); + + const returnData = JSON.parse(cleanedJson).sort(); + + // Filter out already existing labels + const newLabels = returnData.filter(className => !existingLabelNames.includes(className)); + + // Insert new labels into the database + for (const label of newLabels) { + await Labels.create({ name: label, source: 'ml' }); + } + + return { success: true, message: 'Label synchronization completed.', labels: await Labels.find() }; + } catch (error) { + console.error('Error synchronizing labels:', error.message); + return { success: false, message: `Error: ${error.message}` }; + } + }, + + + /** + * Create predefined ML labels + */ createMLLabel: async function() { let newLabel = {}; [ diff --git a/config/bootstrap.js b/config/bootstrap.js index fb1c17f..8262170 100644 --- a/config/bootstrap.js +++ b/config/bootstrap.js @@ -11,6 +11,12 @@ module.exports.bootstrap = async function() { + try { + Labels.addMlLabelsAndReturnAllLabels(); + }catch (e) { + console.log("error",e); + } + // By convention, this is a good place to set up fake data during development. // // For example: