Skip to content

Commit

Permalink
Refine workspace data collection strategy
Browse files Browse the repository at this point in the history
Automatically collect workspace data after execution, only when new changes are detected to avoid redundant data collection.
  • Loading branch information
simosathan9 committed Sep 16, 2024
1 parent 3e32926 commit a89e3a2
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 67 deletions.
Binary file modified blockly_unix_database.db
Binary file not shown.
1 change: 0 additions & 1 deletion public/blocks/cutBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ var cutBlock = {
text: ''
}
],

style: 'Text Processing',
previousStatement: 'Action',
nextStatement: 'Action',
Expand Down
2 changes: 1 addition & 1 deletion public/blocks/headBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var headBlock = {
type: 'field_dropdown',
name: 'metric_type',
options: [
['elements', 'elements'],
['lines', 'lines'],
['bytes', 'bytes']
]
}
Expand Down
4 changes: 2 additions & 2 deletions public/js/el.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Blockly.Msg['GZIP_CHANGE_SUFFIX'] = 'Aλλαγή κατάληξης απο .gz
Blockly.Msg['GZIP_VERBOSE'] = 'Εμφάνισε πληροφορίες %1';
Blockly.Msg['GZIP_TOOLTIP'] = 'Συμπίεση/αποσυμπίεση αρχείου';
Blockly.Msg['GZIP_HELPURL'] = 'https://www.google.com/';
Blockly.Msg['HEAD_MESSAGE'] = 'Επικεφαλίδα αρχείου';
Blockly.Msg['HEAD_MESSAGE'] = 'Πρώτα στοιχεία αρχείου';
Blockly.Msg['HEAD_METRIC'] = 'Μετρική %1';
Blockly.Msg['HEAD_NUMBER_OF'] = 'Αριθμός %1';
Blockly.Msg['HEAD_TOOLTIP'] = 'Εμφάνιση του πρώτου μέρους των αρχείων';
Expand Down Expand Up @@ -867,7 +867,7 @@ Blockly.Msg['SORT_TOOLTIP'] =
'Αναφορά ή φιλτράρισμα επαναλαμβανόμενων γραμμών σε ένα αρχείο';
Blockly.Msg['SORT_HELPURL'] = 'https://www.google.com/';

Blockly.Msg['TAIL'] = 'Ουρά του αρχείου';
Blockly.Msg['TAIL'] = 'Τελευταία στοιχεία αρχείου';
Blockly.Msg['TAIL_METRIC'] = 'Μετρική %1';
Blockly.Msg['TAIL_NUMBER_OF'] = 'Αριθμός %1';
Blockly.Msg['TAIL_DESC'] = 'Φθίνουσα σειρά %1';
Expand Down
4 changes: 2 additions & 2 deletions public/js/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Blockly.Msg['GZIP_CHANGE_SUFFIX'] = 'Change suffix from .gz to';
Blockly.Msg['GZIP_VERBOSE'] = 'Τurn on verbose mode %1';
Blockly.Msg['GZIP_TOOLTIP'] = 'File compress/decompress';
Blockly.Msg['GZIP_HELPURL'] = 'https://www.google.com/';
Blockly.Msg['HEAD_MESSAGE'] = 'Head of file';
Blockly.Msg['HEAD_MESSAGE'] = 'First elements of file';
Blockly.Msg['HEAD_METRIC'] = 'Metric %1';
Blockly.Msg['HEAD_NUMBER_OF'] = 'Number of %1';
Blockly.Msg['HEAD_TOOLTIP'] = 'Output the first part of files';
Expand Down Expand Up @@ -840,7 +840,7 @@ Blockly.Msg['SORT_IGNORE_LEADING_BLANKS'] = 'Ignore leading blanks %1';
Blockly.Msg['SORT_TOOLTIP'] = 'Report or filter out repeated lines in a file';
Blockly.Msg['SORT_HELPURL'] = 'https://www.google.com/';

Blockly.Msg['TAIL'] = 'Tail of file';
Blockly.Msg['TAIL'] = 'Last elements of file';
Blockly.Msg['TAIL_METRIC'] = 'Metric %1';
Blockly.Msg['TAIL_NUMBER_OF'] = 'Number of %1';
Blockly.Msg['TAIL_DESC'] = 'Descending order %1';
Expand Down
78 changes: 36 additions & 42 deletions public/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Blockly.JavaScript.init(workspace);
Blockly.JavaScript = new Blockly.Generator('JavaScript');
var generator = javascript.javascriptGenerator;
let workspaceChangedAfterExecution = false; // Flag to track if the workspace has changed after execution
let workspaceChangedAfterLastExecution = false; // Flag to track if the workspace has changed after execution
let hasExecuted = false;
let executedWorkspace = '';

Expand Down Expand Up @@ -161,16 +161,16 @@ async function updateWorkspaces(userId) {

// Populate dropdown with the user's saved workspaces
workspaces.forEach((workspace) => {
if (workspace.workspaceName !== '__autosave__') {
if (
workspace.workspaceName !== '__autosave__' &&
workspace.workspaceName !== 'executedWorkspace'
) {
const option = document.createElement('option');
option.value = workspace.id; // use the workspace ID as the option value
option.text = workspace.workspaceName; // display the workspace name
savedWorkspacesSelect.appendChild(option);
}
});

// Optionally, show a success message or silently update
console.log('Workspaces updated successfully.');
} catch (error) {
console.error('Error updating workspaces:', error);
}
Expand Down Expand Up @@ -384,26 +384,14 @@ function onWorkspaceChange(event) {
.then((response) => response.json())
.then((data) => {
const token = data.authToken;
workspaceChangedAfterLastExecution = true;
hasExecuted = false;
if (token) {
autoSaveWorkspace();
} else {
saveWorkspace();
}
});
if (hasExecuted) {
// Compare the current workspace state with the executed workspace state
const currentWorkspaceState =
Blockly.serialization.workspaces.save(workspace);
const jsonCurrentState = JSON.stringify(currentWorkspaceState);

if (jsonCurrentState !== executedWorkspace) {
workspaceChangedAfterExecution = true;
console.log('Workspace has changed since execution.');
} else {
workspaceChangedAfterExecution = false;
console.log('Workspace has not changed since execution.');
}
}
}
Blockly.getMainWorkspace().addChangeListener(onWorkspaceChange);

Expand All @@ -422,35 +410,19 @@ function saveGuestWorkspaceData() {
.then((response) => response.json())
.then((data) => {
const token = data.authToken;
if (!token && hasExecuted && jsonState !== executedWorkspace) {
workspaceChangedAfterExecution = true;
}
if (!token) {
fetch('/saveGuestWorkspace', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
workspaceData: jsonState,
executionStatus: hasExecuted,
changesAfterExecution: workspaceChangedAfterExecution
workspaceData: jsonState
})
})
.then((response) => response.json())
.then((result) => {
if (result.error) {
console.error('Error saving guest workspace:', result.error);
alert('Failed to save guest workspace. Please try again.');
}
})
.catch((error) => {
alert('Failed to save guest workspace. Please try again.');
});
});
}
});
}
window.addEventListener('beforeunload', saveGuestWorkspaceData);
//window.addEventListener('beforeunload', autoSaveWorkspace);

// Global replacement map
Expand Down Expand Up @@ -576,14 +548,36 @@ document
fetch('/auth-token')
.then((response) => response.json())
.then((data) => {
const token = data.authToken;
if (!token) {
hasExecuted = true;
console.log('It got executed');
if (workspaceChangedAfterLastExecution) {
const state = Blockly.serialization.workspaces.save(workspace);
executedWorkspace = JSON.stringify(state);
const token = data.authToken;
const user = data.user;
hasExecuted = true;
workspaceChangedAfterLastExecution = false;
if (token) {
if (executedWorkspace) {
// Use AJAX to send the data to the server without redirecting
fetch('/saveWorkspace', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
workspaceData: executedWorkspace,
userId: user.id,
workspaceName: 'executedWorkspace'
})
});
}
} else {
console.log(
'Workspace has changed since execution ' +
workspaceChangedAfterLastExecution
);
saveGuestWorkspaceData();
}
}
console.log('Has executed ', hasExecuted); // Now inside the .then block
});
} else {
console.log('Has executed ', hasExecuted); // Still log if no fetch occurs
Expand Down
28 changes: 11 additions & 17 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,27 +264,21 @@ app.post('/saveWorkspace', (req, res) => {

app.post('/saveGuestWorkspace', (req, res) => {
// Retrieve the workspace data and user ID from the request body
const { workspaceData, executionStatus, changesAfterExecution } = req.body;
const { workspaceData } = req.body;
if (!workspaceData) {
return res.status(400).json({ error: 'Missing workspace data.' });
}
const query = `INSERT INTO guestsWorkspaces (workspaceData, executionStatus, changesAfterExecution) VALUES (?, ?, ?)`;
db.run(
query,
[workspaceData, executionStatus, changesAfterExecution],
function (err) {
if (err) {
console.error('Error inserting workspace data:', err.message);
return res
.status(500)
.json({ error: 'Failed to save workspace data.' });
}
res.status(200).json({
message: 'Workspace data saved successfully.',
workspaceId: this.lastID // Return the ID of the inserted workspace
});
const query = `INSERT INTO guestsWorkspaces (workspaceData) VALUES (?)`;
db.run(query, [workspaceData], function (err) {
if (err) {
console.error('Error inserting workspace data:', err.message);
return res.status(500).json({ error: 'Failed to save workspace data.' });
}
);
res.status(200).json({
message: 'Workspace data saved successfully.',
workspaceId: this.lastID // Return the ID of the inserted workspace
});
});
});

app.post('/autoSaveWorkspace', (req, res) => {
Expand Down
2 changes: 0 additions & 2 deletions table_creation_queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ CREATE TABLE IF NOT EXISTS workspaces (
CREATE TABLE IF NOT EXISTS guestsWorkspaces (
id INTEGER PRIMARY KEY AUTOINCREMENT,
workspaceData TEXT NOT NULL,
executionStatus BOOLEAN DEFAULT 0,
changesAfterExecution BOOLEAN DEFAULT 0,
created_at DATETIME DEFAULT (datetime('now', 'localtime'))
);

Expand Down

0 comments on commit a89e3a2

Please sign in to comment.