Skip to content

Commit

Permalink
Enhance notebook loading process with Spark integration
Browse files Browse the repository at this point in the history
- Added SparkModel import to App.js for improved Spark session handling.
- Refactored handleExistingNotebookClick to use async/await for fetching notebook data and Spark application details concurrently.
- Implemented error handling for fetching notebook and Spark app, ensuring better user feedback and session management.
- Introduced a new method in SparkModel.js to retrieve Spark app details by notebook path, enhancing integration with the backend API.
  • Loading branch information
xuwenyihust committed Dec 10, 2024
1 parent 3b2dca7 commit 1ae461f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
41 changes: 27 additions & 14 deletions webapp/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createTheme, ThemeProvider } from '@mui/material/styles';
import config from './config';
import NotebookModel from './models/NotebookModel';
import DirectoryModel from './models/DirectoryModel';
import SparkModel from './models/SparkModel';

const theme = createTheme({
components: {
Expand Down Expand Up @@ -128,22 +129,34 @@ const App = () => {
}
};

const handleExistingNotebookClick = (path) => {
const handleExistingNotebookClick = async (path) => {
if (handleUnsavedChanges()) {
NotebookModel.fetchNotebook(`${path}`).then((data) => {
if (data.message == 'Token has expired') {
console.error('Token has expired, please log in again');
logout();
} else {
console.log('Fetched notebook:', data);
setNotebook(data);
setShowHistoryServer(false);
setShowScheduler(false);
setShowNotebook(true);
try {
const [notebookData, sparkApp] = await Promise.all([
NotebookModel.fetchNotebook(`${path}`),
SparkModel.getSparkAppByNotebookPath(path)
]);

if (notebookData.message === 'Token has expired') {
console.error('Token has expired, please log in again');
logout();
return;
}

console.log('Fetched notebook:', notebookData);
setNotebook(notebookData);

// Update Spark badge if there's an active Spark app
if (sparkApp) {
setSparkAppId(sparkApp.spark_app_id);
}

setShowHistoryServer(false);
setShowScheduler(false);
setShowNotebook(true);
} catch (error) {
console.error('Failed to fetch notebook or Spark app:', error);
}
}).catch((error) => {
console.error('Failed to fetch notebook:', error);
});
}
}

Expand Down
22 changes: 22 additions & 0 deletions webapp/src/models/SparkModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ spark`;
throw error;
}
}

static async getSparkAppByNotebookPath(notebookPath) {
const token = sessionStorage.getItem('token');
try {
const response = await fetch(`${config.serverBaseUrl}/notebook/spark_app/${notebookPath}`, {
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
});

if (!response.ok) {
return null;
}

const sparkApps = await response.json();
return sparkApps.length > 0 ? sparkApps[0] : null;
} catch (error) {
console.error('Failed to fetch Spark app:', error);
return null;
}
}
}

export default SparkModel;
Expand Down

0 comments on commit 1ae461f

Please sign in to comment.