-
Notifications
You must be signed in to change notification settings - Fork 15
Implement opt-in countdown feature and add to image recipe #524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
stack/lab/before-notebook.d/70_prepare_countdown_config.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #!/bin/bash | ||
| set -e | ||
|
|
||
| CUSTOM_DIR="${CONDA_DIR}/lib/python${PYTHON_MINOR_VERSION}/site-packages/notebook/static/custom" | ||
|
|
||
| if [ "$LIFETIME" ]; then | ||
| EPHEMERAL=true | ||
|
|
||
| # Convert LIFETIME from HH:MM:SS to seconds | ||
| IFS=: read -r H M S <<<"$LIFETIME" | ||
| LIFETIME_SEC=$((10#$H * 3600 + 10#$M * 60 + 10#$S)) | ||
|
|
||
| # Calculate expiry timestamp in UTC | ||
| EXPIRY=$(date -u -d "+${LIFETIME_SEC} seconds" +"%Y-%m-%dT%H:%M:%SZ") | ||
| export EXPIRY | ||
| else | ||
| EPHEMERAL=false | ||
| fi | ||
|
|
||
| export EPHEMERAL | ||
| envsubst <"${CUSTOM_DIR}/config.json.template" >"$CUSTOM_DIR/config.json" | ||
| rm "${CUSTOM_DIR}/config.json.template" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "ephemeral": ${EPHEMERAL}, | ||
| "expiry": "${EXPIRY}" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #culling-countdown { | ||
| position: sticky; | ||
| top: 0; | ||
| left: 0; | ||
| width: 100%; | ||
| background-color: #0078d4; | ||
| color: white; | ||
| text-align: center; | ||
| padding: 8px; | ||
| font-size: 18px; | ||
| font-weight: bold; | ||
| z-index: 9999; | ||
| } | ||
|
|
||
| #shutdown-warning, | ||
| #save-info { | ||
| display: none; | ||
| } | ||
|
|
||
| #shutdown-warning { | ||
| font-size: 20px; | ||
| } | ||
|
|
||
| #save-info { | ||
| font-size: 16px; | ||
| text-align: center; | ||
| font-weight: normal; | ||
| font-size: 18px; | ||
| } | ||
|
|
||
| #culling-timer { | ||
| margin-left: 5px; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| require(["base/js/namespace", "base/js/events"], (Jupyter, events) => { | ||
| const parseLifetimeToMs = (str) => { | ||
| const parts = str.split(":").map(Number); | ||
| if (parts.length !== 3 || parts.some(isNaN)) { | ||
| return null; | ||
| } | ||
| const [h, m, s] = parts; | ||
| return ((h * 60 + m) * 60 + s) * 1000; | ||
| }; | ||
|
|
||
| const insertCountdown = (remainingMs) => { | ||
| if (document.getElementById("culling-countdown")) { | ||
| return; | ||
| } | ||
|
|
||
| const banner = document.createElement("div"); | ||
| banner.id = "culling-countdown"; | ||
|
|
||
| const shutdownWarning = document.createElement("div"); | ||
| shutdownWarning.id = "shutdown-warning"; | ||
| shutdownWarning.innerHTML = "⚠️ Shutdown imminent! ⚠️"; | ||
| banner.appendChild(shutdownWarning); | ||
|
|
||
| const countdown = document.createElement("div"); | ||
| countdown.id = "countdown"; | ||
| countdown.innerHTML = `Session time remaining: `; | ||
| const timer = document.createElement("span"); | ||
| timer.id = "culling-timer"; | ||
| timer.innerHTML = "Calculating..."; | ||
| countdown.appendChild(timer); | ||
| banner.appendChild(countdown); | ||
|
|
||
| const saveInfo = document.createElement("div"); | ||
| saveInfo.id = "save-info"; | ||
| saveInfo.innerHTML = ` | ||
| Consider saving your work using the <b>File Manager</b> or the <b>Terminal</b> | ||
| `; | ||
| banner.appendChild(saveInfo); | ||
|
|
||
| const endTime = new Date(Date.now() + remainingMs); | ||
|
|
||
| const formatTime = (seconds) => { | ||
| const hrs = `${Math.floor(seconds / 3600)}`.padStart(2, "0"); | ||
| const mins = `${Math.floor((seconds % 3600) / 60)}`.padStart(2, "0"); | ||
| const secs = `${Math.floor(seconds % 60)}`.padStart(2, "0"); | ||
| return `${hrs}:${mins}:${secs}`; | ||
| }; | ||
|
|
||
| const updateTimer = () => { | ||
| const now = new Date(); | ||
| const timeLeft = (endTime - now) / 1000; | ||
| if (timeLeft < 0) { | ||
| clearInterval(interval); | ||
| return; | ||
| } | ||
| if (timeLeft < 1800) { | ||
| banner.style.backgroundColor = "#DAA801"; | ||
| saveInfo.style.display = "block"; | ||
| } | ||
| if (timeLeft < 300) { | ||
| banner.style.backgroundColor = "red"; | ||
| shutdownWarning.style.display = "block"; | ||
| shutdownWarning.innerHTML = "⚠️ Shutdown imminent ⚠️"; | ||
| } | ||
| timer.innerHTML = formatTime(timeLeft); | ||
| }; | ||
|
|
||
| updateTimer(); | ||
| const interval = setInterval(updateTimer, 1000); | ||
|
|
||
| const container = document.getElementById("header"); | ||
| if (container) { | ||
| container.parentNode.insertBefore(banner, container); | ||
| } | ||
| }; | ||
|
|
||
| loadCountdown = async () => { | ||
| try { | ||
| const response = await fetch("/static/custom/config.json"); | ||
| const config = await response.json(); | ||
|
|
||
| // Opt-in point for deployments | ||
| if (!config.ephemeral) { | ||
| return; | ||
| } | ||
|
|
||
| if (!config.expiry) { | ||
| console.warn("Missing `expiry` in config file"); | ||
| return; | ||
| } | ||
|
|
||
| const expiry = new Date(config.expiry).getTime(); | ||
| const remaining = expiry - Date.now(); | ||
|
|
||
| insertCountdown(Math.max(0, remaining)); | ||
| } catch (err) { | ||
| console.error("Countdown init failed:", err); | ||
| } | ||
| }; | ||
|
|
||
| events.on("app_initialized.NotebookApp", loadCountdown); | ||
| loadCountdown(); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These lines were moved to here when we debug it together. Can you move it to where the logo.png is copied, and see if it still work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't work due to the order of events I believe