Skip to content

Commit

Permalink
fixes and similfications + refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
Pop John committed Jul 4, 2024
1 parent 8f12f99 commit 204f490
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 237 deletions.
66 changes: 66 additions & 0 deletions backend/src/facades/algorithmExecutionFacade.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const express = require('express');
const { broadcastTerminal } = require('../services/websocketService');
const ALGORITHM_TYPES = require('../constants/algorithmTypesConstants');
const { executeCommand } = require('./commandExecutionFacade');
const { pruningParserInstance } = require('../parsers/pruningParser');
const { quantizationParserInstance } = require('../parsers/quantizationParser');
const { machineUnlearningParserInstance } = require('../parsers/machineUnlearningParser');

class ScriptExecutor {
constructor(type, parser) {
this.type = type;
this.parser = parser;
}

execute(scriptPath, algorithm, args) {
return new Promise((resolve, reject) => {
this.parser.reset();
const cmd = this.buildCommand(scriptPath, algorithm, args);
const pythonCmd = this.buildPythonCommand(scriptPath, algorithm, args);

broadcastTerminal(pythonCmd);

executeCommand(
cmd,
(data) => this.handleOutput(data),
() => resolve(),
(error) => reject(error)
);
});
}

buildCommand(scriptPath, algorithm, args) {
if (this.type === ALGORITHM_TYPES.MULTIFLOW) {
return `source "${process.env.CONDA_SH_PATH}" && cd "${process.env.MACHINE_LEARNING_CORE_PATH}/multiflow" && conda activate modelsmith && python3 ${algorithm} ${args}`;
}
return `source "${process.env.CONDA_SH_PATH}" && conda activate modelsmith && python3 "${scriptPath}${algorithm}" ${args}`;
}

buildPythonCommand(scriptPath, algorithm, args) {
if (this.type === ALGORITHM_TYPES.MULTIFLOW) {
return `python3 ${algorithm} ${args}`;
}
return `python3 "${scriptPath}${algorithm}" ${args}`;
}

handleOutput(data) {
const formattedData = data.toString();
broadcastTerminal(formattedData);
this.parser.parseLine(formattedData);
}
}

const executorFactory = {
[ALGORITHM_TYPES.PRUNING]: new ScriptExecutor(ALGORITHM_TYPES.PRUNING, pruningParserInstance),
[ALGORITHM_TYPES.QUANTIZATION]: new ScriptExecutor(ALGORITHM_TYPES.QUANTIZATION, quantizationParserInstance),
[ALGORITHM_TYPES.MACHINE_UNLEARNING]: new ScriptExecutor(
ALGORITHM_TYPES.MACHINE_UNLEARNING,
machineUnlearningParserInstance
),
[ALGORITHM_TYPES.MULTIFLOW]: new ScriptExecutor(ALGORITHM_TYPES.MULTIFLOW, { reset: () => {}, parseLine: () => {} })
};

module.exports = module.exports = {
ScriptExecutor,
executorFactory
};
File renamed without changes.
2 changes: 1 addition & 1 deletion backend/src/middlewares/checkSshConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

// SPDX-License-Identifier: Apache-2.0

const { getConnectionStatus } = require('../facades/executionFacade');
const { getConnectionStatus } = require('../facades/commandExecutionFacade');

module.exports = (req, res, next) => {
if (getConnectionStatus() !== 'READY') {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/router/modelsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const router = express.Router();
const getModelsByType = require('../constants/modelsConstants');
const { getActiveScriptDetails, getPreviousScriptDetails } = require('../state/scriptState');
const ALGORITHM_TYPES = require('../constants/algorithmTypesConstants');
const { executeCommand } = require('../facades/executionFacade');
const { executeCommand } = require('../facades/commandExecutionFacade');
const checkSshConnection = require('../middlewares/checkSshConnection');
const { BAD_REQUEST, OK, INTERNAL_SERVER_ERROR, NOT_FOUND } = require('../constants/httpStatusCodes');

Expand Down
2 changes: 1 addition & 1 deletion backend/src/router/runRecordsRouter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const express = require('express');
const router = express.Router();
const ALGORITHM_TYPES = require('../constants/algorithmTypesConstants');
const { executeCommand } = require('../facades/executionFacade');
const { executeCommand } = require('../facades/commandExecutionFacade');
const checkSshConnection = require('../middlewares/checkSshConnection');
const StatelessPruningParser = require('../parsers/statelessPruningParser');
const StatelessQuantizationParser = require('../parsers/statelessQuantizationParser');
Expand Down
103 changes: 24 additions & 79 deletions backend/src/router/scriptsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

// SPDX-License-Identifier: Apache-2.0

const express = require('express');
const router = express.Router();
const { broadcastTerminal, broadcastStatus } = require('../services/websocketService');
Expand All @@ -33,11 +32,8 @@ const {
getPreviousScriptDetails
} = require('../state/scriptState');
const ALGORITHM_PARAMETERS = require('../constants/parametersConstants');
const ALGORITHM_TYPES = require('../constants/algorithmTypesConstants');
const { executeCommand } = require('../facades/executionFacade');
const { pruningParserInstance } = require('../parsers/pruningParser');
const { quantizationParserInstance } = require('../parsers/quantizationParser');
const { machineUnlearningParserInstance } = require('../parsers/machineUnlearningParser');
const { executeCommand } = require('../facades/commandExecutionFacade');
const { executorFactory } = require('../facades/algorithmExecutionFacade');

router.get('/current-or-last-active-script-details', (_, res) => {
let script = getActiveScriptDetails() || getPreviousScriptDetails();
Expand All @@ -56,7 +52,7 @@ router.get('/current-or-last-active-script-details', (_, res) => {
res.status(OK).send(lastActiveScript);
});

router.post('/run-script', checkSshConnection, checkIfScriptRunning, (req, res) => {
router.post('/run-script', checkSshConnection, checkIfScriptRunning, async (req, res) => {
const { alg = '', params = [] } = req.body;

const scriptDetails = ALGORITHMS[alg];
Expand All @@ -74,85 +70,34 @@ router.post('/run-script', checkSshConnection, checkIfScriptRunning, (req, res)

changeAndBroadcastScriptState(ScriptState.RUNNING);

executePythonScript(scriptDetails.path, scriptDetails.fileName, args, scriptDetails.type)
.then(() => {
res.status(OK).send({ message: 'Script execution ended successfully.' });
broadcastTerminal('Script execution ended successfully.', MESSAGE_TYPES.SUCCESS);

setActiveScriptDetails(null);
changeAndBroadcastScriptState(ScriptState.NOT_RUNNING);
})
.catch((error) => {
logger.error(`Error executing command: ${error}`);

setActiveScriptDetails(null);
changeAndBroadcastScriptState(ScriptState.ERROR);

broadcastTerminal(error, MESSAGE_TYPES.ERROR);
const executor = executorFactory[scriptDetails.type];
if (!executor) {
return res.status(BAD_REQUEST).send({ error: 'Unsupported algorithm type.' });
}

res.status(INTERNAL_SERVER_ERROR).send({
error: 'The script has errors and failed to start automatically. Please check the terminal.'
});
try {
await executor.execute(
`${process.env.MACHINE_LEARNING_CORE_PATH}/${scriptDetails.path}`,
scriptDetails.fileName,
args
);
res.status(OK).send({ message: 'Script execution ended successfully.' });
broadcastTerminal('Script execution ended successfully.', MESSAGE_TYPES.SUCCESS);
} catch (error) {
logger.error(`Error executing command: ${error}`);
res.status(INTERNAL_SERVER_ERROR).send({
error: 'The script has errors and failed to start automatically. Please check the terminal.'
});
broadcastTerminal(error, MESSAGE_TYPES.ERROR);
} finally {
setActiveScriptDetails(null);
changeAndBroadcastScriptState(ScriptState.NOT_RUNNING);
}
});

function executePythonScript(path, algorithm, args = '', type) {
return new Promise((resolve, reject) => {
if (type === ALGORITHM_TYPES.PRUNING) {
pruningParserInstance.reset();
} else if (type === ALGORITHM_TYPES.QUANTIZATION) {
quantizationParserInstance.reset();
} else if (type === ALGORITHM_TYPES.MACHINE_UNLEARNING) {
machineUnlearningParserInstance.reset();
}

const scriptPath = `${process.env.MACHINE_LEARNING_CORE_PATH}/${path}`;
let cmd;

if (type === ALGORITHM_TYPES.MULTIFLOW) {
cmd = `source "${process.env.CONDA_SH_PATH}" && cd "${process.env.MACHINE_LEARNING_CORE_PATH}/multiflow" && conda activate modelsmith && python3 ${algorithm} ${args}`;
pythonCmd = `python3 ${algorithm} ${args}`;
} else {
cmd = `source "${process.env.CONDA_SH_PATH}" && conda activate modelsmith && python3 "${scriptPath}${algorithm}" ${args}`;
pythonCmd = `python3 "${scriptPath}${algorithm}" ${args}`;
}

broadcastTerminal(pythonCmd);

executeCommand(
cmd,
(data) => {
const formattedData = data.toString();
broadcastTerminal(formattedData);

switch (type) {
case ALGORITHM_TYPES.PRUNING:
pruningParserInstance.parseLine(formattedData);
break;
case ALGORITHM_TYPES.QUANTIZATION:
quantizationParserInstance.parseLine(formattedData);
break;
case ALGORITHM_TYPES.MACHINE_UNLEARNING:
machineUnlearningParserInstance.parseLine(formattedData);
break;
default:
break;
}
},
() => {
resolve();
},
(error) => {
reject(error);
}
);
});
}

router.get('/script-status', (req, res) => {
const scriptStatus = getScriptState();
const response = { status: scriptStatus };

res.status(OK).send(response);
});

Expand Down
2 changes: 1 addition & 1 deletion backend/src/router/uploadFileRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
const express = require('express');
const router = express.Router();
const multer = require('multer');
const { conn } = require('../facades/executionFacade');
const { conn } = require('../facades/commandExecutionFacade');
const { OK, INTERNAL_SERVER_ERROR } = require('../constants/httpStatusCodes');
const logger = require('../utils/logger');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export class RunDrawerActionsComponent implements OnInit, AfterViewInit {
isXAxisVisible: true,
isXAxisDynamic: true,
areTooltipsEnabled: true,
xAxisInitialLabelValue: 0,
xAxisInitialLabelValue: 1,
xAxisLabelPrefix: 'Step:',
datasetLabelPrefix: 'Test:',
isChartWithCustomColorSettings: true,
Expand Down

This file was deleted.

Loading

0 comments on commit 204f490

Please sign in to comment.