Skip to content

Commit

Permalink
Refactor demo notebook and SparkModel.js for improved Spark session h…
Browse files Browse the repository at this point in the history
…andling

- Removed outdated code cells from the demo notebook to enhance clarity and usability.
- Updated SparkModel.js to improve validation of Spark application IDs, ensuring they start with 'app-' and are correctly extracted from the HTML.
- Simplified the logic for storing Spark session information in the Notebook component, enhancing overall session management.
  • Loading branch information
xuwenyihust committed Dec 10, 2024
1 parent 9e345e6 commit 5d1bfce
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 40 deletions.
33 changes: 0 additions & 33 deletions examples/[email protected]/demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,6 @@
"- This is just a demo notebook\n",
"- For testing only"
]
},
{
"cell_type": "code",
"isExecuted": true,
"lastExecutionResult": "success",
"lastExecutionTime": "2024-12-10 08:03:15",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <div style=\"border: 1px solid #e8e8e8; padding: 10px;\">\n",
" <h3>Spark Session Information</h3>\n",
" <p><strong>Config:</strong> {'spark.driver.memory': '1g', 'spark.driver.cores': 1, 'spark.executor.memory': '1g', 'spark.executor.cores': 1, 'spark.executor.instances': 1, 'spark.dynamicAllocation.enabled': False}</p>\n",
" <p><strong>Application ID:</strong> app-20241210080310-0003</p>\n",
" <p><strong>Spark UI:</strong> <a href=\"http://localhost:18080/history/app-20241210080310-0003\">http://localhost:18080/history/app-20241210080310-0003</a></p>\n",
" </div>\n",
" "
],
"text/plain": [
"Custom Spark Session (App ID: app-20241210080310-0003) - UI: http://0edb0a63b2fb:4040"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spark = create_spark(\"work/[email protected]/demo.ipynb\")\n",
"spark"
]
}
],
"metadata": {
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/components/notebook/Notebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ function Notebook({
// Check if contains a spark app id
if (result[0] && result[0].data && result[0].data['text/html'] && SparkModel.isSparkInfo(result[0].data['text/html'])) {
setSparkAppId(SparkModel.extractSparkAppId(result[0].data['text/html']));
SparkModel.storeSparkInfo(SparkModel.extractSparkAppId(result[0].data['text/html']), notebook.path)
SparkModel.storeSparkInfo(sparkAppId, notebook.path)
}
console.log('Spark app id:', sparkAppId);

Expand Down
29 changes: 23 additions & 6 deletions webapp/src/models/SparkModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,22 @@ class SparkModel {
static isSparkInfo(html) {
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const sparkInfo = doc.querySelector('h3');
console.log('sparkInfo:', sparkInfo);
return sparkInfo && sparkInfo.textContent === 'Spark Session Information';

// Check if it's a Spark info div
const sparkInfo = doc.querySelector('div');
if (!sparkInfo || !sparkInfo.textContent.includes('Spark Session Information')) {
return false;
}

// Verify it has an Application ID that starts with 'app-'
const appIdElement = Array.from(doc.querySelectorAll('p'))
.find(p => p.textContent.includes('Application ID:'));
if (!appIdElement) {
return false;
}

const appId = appIdElement.textContent.split(': ')[1];
return appId && appId.startsWith('app-');
}

static async storeSparkInfo(sparkAppId, notebookPath) {
Expand Down Expand Up @@ -48,9 +61,13 @@ class SparkModel {
static extractSparkAppId(html) {
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const pTags = Array.from(doc.querySelectorAll('p'));
const appIdTag = pTags.find(p => p.textContent.includes('Application ID:'));
return appIdTag ? appIdTag.textContent.split(': ')[1] : null;
const appIdTag = Array.from(doc.querySelectorAll('p'))
.find(p => p.textContent.includes('Application ID:'));

if (!appIdTag) return null;

const appId = appIdTag.textContent.split(': ')[1];
return appId && appId.startsWith('app-') ? appId : null;
}

static async createSparkSession(notebookPath) {
Expand Down

0 comments on commit 5d1bfce

Please sign in to comment.