diff options
author | Suchita Lad | 2025-03-28 12:45:35 +0530 |
---|---|---|
committer | Suchita Lad | 2025-03-28 12:45:35 +0530 |
commit | a068b1fd9f936675f81e90f83589923f39c6dc10 (patch) | |
tree | 7d6fdd8cb7bb02b3845dd5b945f926eba64b5e92 | |
parent | fc774d0e4e111edac525ff545177d1a48c399e8d (diff) | |
download | Common-Interface-Project-a068b1fd9f936675f81e90f83589923f39c6dc10.tar.gz Common-Interface-Project-a068b1fd9f936675f81e90f83589923f39c6dc10.tar.bz2 Common-Interface-Project-a068b1fd9f936675f81e90f83589923f39c6dc10.zip |
Updated style of execute script window
-rw-r--r-- | blocks/eda-frontend/src/components/SchematicEditor/ToolbarExtension.js | 222 | ||||
-rw-r--r-- | blocks/simulationAPI/helpers/scilab_manager.py | 20 | ||||
-rw-r--r-- | blocks/simulationAPI/tasks.py | 12 | ||||
-rw-r--r-- | blocks/simulationAPI/views.py | 14 |
4 files changed, 227 insertions, 41 deletions
diff --git a/blocks/eda-frontend/src/components/SchematicEditor/ToolbarExtension.js b/blocks/eda-frontend/src/components/SchematicEditor/ToolbarExtension.js index 4cf64bf6..e5f20e54 100644 --- a/blocks/eda-frontend/src/components/SchematicEditor/ToolbarExtension.js +++ b/blocks/eda-frontend/src/components/SchematicEditor/ToolbarExtension.js @@ -339,11 +339,28 @@ export function ScriptScreen ({ isOpen, onClose }) { const scriptDump = useSelector(state => state.saveSchematicReducer.scriptDump) const title = useSelector(state => state.netlistReducer.title) const dispatch = useDispatch() + const [result, setResult] = useState('No output yet...') + const [variables, setVariables] = useState([]) const scriptHandler = (e) => { dispatch(setSchScriptDump(e.target.value)) } - const [result, setResult] = useState('') + const fetchScriptOutput = async (taskId) => { + console.log('Fetching script output for task:', taskId) + try { + const response = await api.get(`simulation/get_script_output/${taskId}`) + const data = response.data + setResult(data.output || 'No output available.') + + if (data.variables && Array.isArray(data.variables)) { + console.log('Variables:', data.variables) + setVariables(data.variables) + } + } catch (error) { + console.error('Error fetching script output:', error) + setResult('Error fetching script output.') + } + } const prepareScriptNetlist = (scriptDump) => { const titleA = title.split(' ')[1] @@ -361,13 +378,24 @@ export function ScriptScreen ({ isOpen, onClose }) { const res = response.data const taskId = res.details.task_id dispatch(setScriptTaskId(taskId)) + + + setTimeout(() => { + fetchScriptOutput(taskId) + .then((response) => { + if (response.status === 200) { + const data = response.data + dispatch(setResult(data.output, data.variables)) + } + }) + }, 3000) }) .catch(function (error) { console.error(error) }) } - function netlistConfig (file, type) { + async function netlistConfig (file, type) { const formData = new FormData() formData.append('app_name', process.env.REACT_APP_NAME) @@ -379,7 +407,7 @@ export function ScriptScreen ({ isOpen, onClose }) { 'content-type': 'multipart/form-data' } } - return api.post('simulation/upload', formData, config) + return await api.post('simulation/upload', formData, config) } const executeScript = () => { @@ -389,7 +417,9 @@ export function ScriptScreen ({ isOpen, onClose }) { const resetCode = () => { dispatch(setSchScriptDump('')) - setResult('') + setResult('No output yet...') + setVariables('') + } return ( @@ -410,50 +440,190 @@ export function ScriptScreen ({ isOpen, onClose }) { <Box sx={{ p: 4 }}> {/* Code and Result Sections */} - <Box sx={{ display: 'grid', gridTemplateColumns: { xs: '1fr', md: '1fr 1fr' }, gap: 4, alignItems: 'stretch' }}> - {/* Scilab Code Input */} - <Box sx={{ p: 2, bgcolor: 'white', boxShadow: 2, borderRadius: 2, display: 'flex', flexDirection: 'column', height: '100%' }}> - <Typography variant='subtitle1' sx={{ fontWeight: 'bold', mb: 1 }}> + <Box + sx={{ + display: 'grid', + gridTemplateColumns: { xs: '1fr', md: '1fr 1fr' }, + gap: 4, + alignItems: 'stretch', + }} + > + <Box + sx={{ + p: 2, + boxShadow: 2, + borderRadius: 2, + display: 'flex', + flexDirection: 'column', + flexGrow: 1, + }} + > + <Typography variant="subtitle1" style={{ fontWeight: 'bold', mb: 1 }}> Scilab Code: </Typography> - <TextField - value={scriptDump} - onChange={scriptHandler} - multiline - minRows={12} - variant='outlined' - fullWidth - sx={{ fontFamily: 'Courier New, monospace', fontSize: '14px', flexGrow: 1 }} - /> + + <Box + sx={{ + height: '500px', + overflowY: 'scroll', + border: '1px solid #ccc', + borderRadius: 1, + '&::-webkit-scrollbar': { + width: '8px', + }, + '&::-webkit-scrollbar-track': { + backgroundColor: '#f1f1f1', + }, + '&::-webkit-scrollbar-thumb': { + backgroundColor: '#888', + borderRadius: '4px', + }, + '&::-webkit-scrollbar-thumb:hover': { + backgroundColor: '#555', + }, + }} + > + <TextField + value={scriptDump} + onChange={scriptHandler} + multiline + variant="outlined" + fullWidth + InputProps={{ + disableUnderline: true, + sx: { + fontFamily: 'Courier New, monospace', + fontSize: '14px', + }, + }} + /> + </Box> </Box> - {/* Execution Result */} - <Box sx={{ p: 2, bgcolor: 'white', boxShadow: 2, borderRadius: 2, display: 'flex', flexDirection: 'column', height: '100%' }}> - <Typography variant='subtitle1' sx={{ fontWeight: 'bold', mb: 1 }}> + <Box + sx={{ + p: 2, + boxShadow: 2, + borderRadius: 2, + display: 'flex', + flexDirection: 'column', + height: '100%', + }} + > + <Typography variant="subtitle1" style={{ fontWeight: 'bold', mb: 1 }}> Result: </Typography> <Box sx={{ flexGrow: 1, width: '100%', - height: '200px', p: 2, border: '1px solid gray', borderRadius: 1, overflowY: 'auto', display: 'flex', - alignItems: 'center', - justifyContent: 'center', - minHeight: '200px' + height: '500px', + whiteSpace: 'pre-wrap', // Keep line breaks + '&::-webkit-scrollbar': { + width: '8px', + }, + '&::-webkit-scrollbar-track': { + backgroundColor: '#f1f1f1', + }, + '&::-webkit-scrollbar-thumb': { + backgroundColor: '#888', + borderRadius: '4px', + }, + '&::-webkit-scrollbar-thumb:hover': { + backgroundColor: '#555', + }, + }} + > + {result} + </Box> + + <Typography variant="subtitle1" style={{ fontWeight: "bold", marginTop: 16 }}> + Variable Browser : + </Typography> + <Box + style={{ + flexGrow: 1, + padding: 8, + border: "1px solid gray", + borderRadius: 4 }} > - {result || 'No output yet...'} + <TableContainer + component={Paper} + elevation={0} + style={{ maxHeight: 150 }} + > + <Table size="small" stickyHeader> + <TableHead> + <TableRow style={{ backgroundColor: "#e0e0e0" }}> + <TableCell + style={{ + border: "1px solid gray", + fontWeight: "bold", + padding: "4px 8px", + }} + > + Name + </TableCell> + <TableCell + style={{ + border: "1px solid gray", + fontWeight: "bold", + padding: "4px 8px", + }} + > + Value + </TableCell> + <TableCell + style={{ + border: "1px solid gray", + fontWeight: "bold", + padding: "4px 8px", + }} + > + Type + </TableCell> + </TableRow> + </TableHead> + <TableBody> + {variables.length > 0 ? ( + variables.map((variable, index) => ( + <TableRow key={index}> + <TableCell style={{ border: "1px solid gray", padding: "4px 8px" }}> + {variable.name} + </TableCell> + <TableCell style={{ border: "1px solid gray", padding: "4px 8px" }}> + {variable.value} + </TableCell> + <TableCell style={{ border: "1px solid gray", padding: "4px 8px" }}> + {variable.type} + </TableCell> + </TableRow> + )) + ) : ( + <TableRow> + <TableCell colSpan={3} align="center"> + No variables available. + </TableCell> + </TableRow> + )} + </TableBody> + </Table> + </TableContainer> </Box> </Box> + </Box> + + {/* Action Buttons */} - <Box sx={{ mt: 4, display: 'flex', gap: 2 }}> + <Box sx={{ mt: 4, display: 'flex', gap: 4 }}> <Button onClick={executeScript} color='primary' variant='contained'> Execute </Button> diff --git a/blocks/simulationAPI/helpers/scilab_manager.py b/blocks/simulationAPI/helpers/scilab_manager.py index 6e40bc3f..d0b7d8ec 100644 --- a/blocks/simulationAPI/helpers/scilab_manager.py +++ b/blocks/simulationAPI/helpers/scilab_manager.py @@ -232,6 +232,18 @@ class UserData: self.diagramlock = RLock() self.timestamp = time() + def __str__(self): + return (f"UserData(sessiondir={self.sessiondir}, " + f"diagrams={len(self.diagrams)}, " + f"datafiles={len(self.datafiles)}, " + f"scripts={list(self.scripts.keys())}, " + f"scriptcount={self.scriptcount}, " + f"scifile={self.scifile}, " + f"timestamp={self.timestamp})") + + def __repr__(self): + return self.__str__() + def getscriptcount(self): with self.diagramlock: rv = self.scriptcount @@ -624,7 +636,7 @@ def run_scilab(command, base, createlogfile=False, timeout=1800): return None logger.info('Scilab instance log file: %s', instance.log_name) - cmd = command + SCILAB_END + '\n' + cmd = 'try;' + command + SCILAB_END + '\n' logger.info('running command %s', cmd) instance.proc.stdin.write(cmd) instance.proc.stdin.flush() @@ -691,7 +703,7 @@ def uploadscript(session, task): return rv fname = join(sessiondir, SCRIPT_FILES_FOLDER, - script.script_id + '_script.sce') + f"{script.script_id}_script.sce") # file.save(fname) with open(fname, 'wb+') as destination: for chunk in file.chunks(): @@ -706,9 +718,9 @@ def uploadscript(session, task): return rv wfname = join(sessiondir, WORKSPACE_FILES_FOLDER, - script.script_id + '_script_workspace.dat') + f"{script.script_id}_script_workspace.dat") script.workspace_filename = wfname - command = "try;exec('%s');save('%s');" % (fname, wfname) + command = "exec('%s');save('%s');" % (fname, wfname) script.instance = run_scilab(command, script) diff --git a/blocks/simulationAPI/tasks.py b/blocks/simulationAPI/tasks.py index fe3d32b8..f41ec7de 100644 --- a/blocks/simulationAPI/tasks.py +++ b/blocks/simulationAPI/tasks.py @@ -8,7 +8,7 @@ import traceback from blocks.celery_tasks import app from simulationAPI.helpers.ngspice_helper import ExecXml, update_task_status from simulationAPI.models import Task -from simulationAPI.helpers.scilab_manager import uploadscript +from simulationAPI.helpers.scilab_manager import uploadscript, getscriptoutput logger = get_task_logger(__name__) @@ -45,9 +45,6 @@ def process_task(self, task_id): current_process = 'Processed Script, Streaming Output' else: output = ExecXml(task, self.name) - # Set default values - # state = 'FAILED' - # current_process = 'Unknown error occurred' if output == "Streaming": state = 'STREAMING' current_process = 'Processed Xml, Streaming Output' @@ -70,3 +67,10 @@ def process_task(self, task_id): finally: release_lock(lock) # Ensure lock is always released + + +@shared_task +def process_task_script(task_id): + task = Task.objects.get(task_id=task_id) + session = task.session + return getscriptoutput(session, task) diff --git a/blocks/simulationAPI/views.py b/blocks/simulationAPI/views.py index e5ffe344..09541c75 100644 --- a/blocks/simulationAPI/views.py +++ b/blocks/simulationAPI/views.py @@ -16,9 +16,9 @@ from blocks.celery_tasks import app from simulationAPI.models import Task, Session from simulationAPI.negotiation import IgnoreClientContentNegotiation from simulationAPI.serializers import TaskSerializer -from simulationAPI.tasks import process_task +from simulationAPI.tasks import process_task, process_task_script from simulationAPI.helpers.ngspice_helper import CreateXcos -from simulationAPI.helpers.scilab_manager import getscriptoutput + SCILAB_INSTANCE_TIMEOUT_INTERVAL = 300 @@ -360,14 +360,14 @@ class GetScriptOutputView(APIView): """ def get(self, request, task_id, *args, **kwargs): - - task = Task.objects.get(task_id=task_id) - session = task.session + try: - result = getscriptoutput(session, task) + celery_task = process_task_script.apply_async(kwargs={'task_id': str(task_id)}, task_id=str(uuid.uuid4())) + result = celery_task.get(timeout=30) + print("RESULT:", result) return Response(result, status=status.HTTP_200_OK) except Exception as e: - logger.error(f"Error calling getscriptoutput (Session: {session}): {str(e)}") + logger.error(f"Error calling getscriptoutput (Task_id: {task_id}): {str(e)}") return Response({"error": "Internal server error"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) |