Skip to content

Commit

Permalink
found a desent place to sync scoutbot classes
Browse files Browse the repository at this point in the history
  • Loading branch information
erinz2020 committed Dec 4, 2024
1 parent 90d18e0 commit 854d1f2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
49 changes: 49 additions & 0 deletions api/models/Labels.js
Original file line number Diff line number Diff line change
@@ -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 },
Expand All @@ -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 = {};
[
Expand Down
6 changes: 6 additions & 0 deletions config/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 854d1f2

Please sign in to comment.