|
| 1 | +require(["base/js/namespace", "base/js/events"], (Jupyter, events) => { |
| 2 | + const parseLifetimeToMs = (str) => { |
| 3 | + const parts = str.split(":").map(Number); |
| 4 | + if (parts.length !== 3 || parts.some(isNaN)) { |
| 5 | + return null; |
| 6 | + } |
| 7 | + const [h, m, s] = parts; |
| 8 | + return ((h * 60 + m) * 60 + s) * 1000; |
| 9 | + }; |
| 10 | + |
| 11 | + const insertCountdown = (remainingMs) => { |
| 12 | + if (document.getElementById("culling-countdown")) { |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + const banner = document.createElement("div"); |
| 17 | + banner.id = "culling-countdown"; |
| 18 | + |
| 19 | + const shutdownWarning = document.createElement("div"); |
| 20 | + shutdownWarning.id = "shutdown-warning"; |
| 21 | + shutdownWarning.innerHTML = "⚠️ Shutdown imminent! ⚠️"; |
| 22 | + banner.appendChild(shutdownWarning); |
| 23 | + |
| 24 | + const countdown = document.createElement("div"); |
| 25 | + countdown.id = "countdown"; |
| 26 | + countdown.innerHTML = `Session time remaining: `; |
| 27 | + const timer = document.createElement("span"); |
| 28 | + timer.id = "culling-timer"; |
| 29 | + timer.innerHTML = "Calculating..."; |
| 30 | + countdown.appendChild(timer); |
| 31 | + banner.appendChild(countdown); |
| 32 | + |
| 33 | + const saveInfo = document.createElement("div"); |
| 34 | + saveInfo.id = "save-info"; |
| 35 | + saveInfo.innerHTML = ` |
| 36 | + Consider saving your work using the <b>File Manager</b> or the <b>Terminal</b> |
| 37 | + `; |
| 38 | + banner.appendChild(saveInfo); |
| 39 | + |
| 40 | + const endTime = new Date(Date.now() + remainingMs); |
| 41 | + |
| 42 | + const formatTime = (seconds) => { |
| 43 | + const hrs = `${Math.floor(seconds / 3600)}`.padStart(2, "0"); |
| 44 | + const mins = `${Math.floor((seconds % 3600) / 60)}`.padStart(2, "0"); |
| 45 | + const secs = `${Math.floor(seconds % 60)}`.padStart(2, "0"); |
| 46 | + return `${hrs}:${mins}:${secs}`; |
| 47 | + }; |
| 48 | + |
| 49 | + const updateTimer = () => { |
| 50 | + const now = new Date(); |
| 51 | + const timeLeft = (endTime - now) / 1000; |
| 52 | + if (timeLeft < 0) { |
| 53 | + clearInterval(interval); |
| 54 | + return; |
| 55 | + } |
| 56 | + if (timeLeft < 1800) { |
| 57 | + banner.style.backgroundColor = "#DAA801"; |
| 58 | + saveInfo.style.display = "block"; |
| 59 | + } |
| 60 | + if (timeLeft < 300) { |
| 61 | + banner.style.backgroundColor = "red"; |
| 62 | + shutdownWarning.style.display = "block"; |
| 63 | + shutdownWarning.innerHTML = "⚠️ Shutdown imminent ⚠️"; |
| 64 | + } |
| 65 | + timer.innerHTML = formatTime(timeLeft); |
| 66 | + }; |
| 67 | + |
| 68 | + updateTimer(); |
| 69 | + const interval = setInterval(updateTimer, 1000); |
| 70 | + |
| 71 | + const container = document.getElementById("header"); |
| 72 | + if (container) { |
| 73 | + container.parentNode.insertBefore(banner, container); |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + loadCountdown = async () => { |
| 78 | + try { |
| 79 | + const response = await fetch("/static/custom/config.json"); |
| 80 | + const config = await response.json(); |
| 81 | + |
| 82 | + // Opt-in point for deployments |
| 83 | + if (!config.ephemeral) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + if (!config.expiry) { |
| 88 | + console.warn("Missing `expiry` in config file"); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + const expiry = new Date(config.expiry).getTime(); |
| 93 | + const remaining = expiry - Date.now(); |
| 94 | + |
| 95 | + insertCountdown(Math.max(0, remaining)); |
| 96 | + } catch (err) { |
| 97 | + console.error("Countdown init failed:", err); |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + events.on("app_initialized.NotebookApp", loadCountdown); |
| 102 | + loadCountdown(); |
| 103 | +}); |
0 commit comments