refactor: const/let modernisation and eliminate duplicate date-parse logic
Lint / Python (flake8) (push) Successful in 47s
Lint / JS (eslint) (push) Successful in 9s
Security / Python Security (bandit) (push) Successful in 1m7s
Test / Python Tests (pytest) (push) Successful in 58s
Lint / Notify on failure (push) Has been skipped
Lint / Deploy (push) Successful in 4s

- Replace all var declarations in base.html, index.html scripts with
  const/let (const for bindings that are never reassigned, let otherwise)
- Add _toIso() helper to links.html script block and replace the two
  inline .replace(' UTC','Z').replace(' ','T') patterns with it
- Replace var with const in links.html _linksInterval

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-10 23:37:32 -04:00
parent 68f59c49a2
commit 7ab85cd055
3 changed files with 37 additions and 36 deletions
+29 -29
View File
@@ -324,7 +324,7 @@
lt.init({ bootName: 'GANDALF' }); lt.init({ bootName: 'GANDALF' });
// Theme toggle // Theme toggle
var themeBtn = document.getElementById('lt-theme-btn'); const themeBtn = document.getElementById('lt-theme-btn');
if (themeBtn) themeBtn.addEventListener('click', function() { lt.theme.toggle(); }); if (themeBtn) themeBtn.addEventListener('click', function() { lt.theme.toggle(); });
// Command palette // Command palette
@@ -343,9 +343,9 @@
// ── Global footer + key actions ─────────────────────────────────────── // ── Global footer + key actions ───────────────────────────────────────
document.addEventListener('click', function(e) { document.addEventListener('click', function(e) {
var btn = e.target.closest('[data-action]'); const btn = e.target.closest('[data-action]');
if (!btn) return; if (!btn) return;
var action = btn.getAttribute('data-action'); const action = btn.getAttribute('data-action');
if (action === 'show-keyboard-help' && window.lt) lt.modal.open('lt-keys-help'); if (action === 'show-keyboard-help' && window.lt) lt.modal.open('lt-keys-help');
if (action === 'open-settings' && window.lt) lt.modal.open('lt-settings-modal'); if (action === 'open-settings' && window.lt) lt.modal.open('lt-settings-modal');
}); });
@@ -366,8 +366,8 @@
// ── Settings modal ──────────────────────────────────────────────────── // ── Settings modal ────────────────────────────────────────────────────
(function() { (function() {
var LS_KEY = 'gandalf_settings'; const LS_KEY = 'gandalf_settings';
var DEFAULT = { refreshInterval: 30 }; const DEFAULT = { refreshInterval: 30 };
function loadSettings() { function loadSettings() {
try { return Object.assign({}, DEFAULT, JSON.parse(localStorage.getItem(LS_KEY) || '{}')); } try { return Object.assign({}, DEFAULT, JSON.parse(localStorage.getItem(LS_KEY) || '{}')); }
@@ -381,11 +381,11 @@
function applyRefreshPillUI(interval) { function applyRefreshPillUI(interval) {
document.querySelectorAll('#settings-refresh-pills .pill').forEach(function(p) { document.querySelectorAll('#settings-refresh-pills .pill').forEach(function(p) {
var isActive = parseInt(p.dataset.refreshInterval) === interval; const isActive = parseInt(p.dataset.refreshInterval) === interval;
p.classList.toggle('active', isActive); p.classList.toggle('active', isActive);
p.setAttribute('aria-pressed', isActive ? 'true' : 'false'); p.setAttribute('aria-pressed', isActive ? 'true' : 'false');
}); });
var hint = document.getElementById('settings-refresh-hint'); const hint = document.getElementById('settings-refresh-hint');
if (hint) { if (hint) {
if (interval === 0) hint.textContent = 'Auto-refresh disabled.'; if (interval === 0) hint.textContent = 'Auto-refresh disabled.';
else if (interval < 60) hint.textContent = 'Refreshes every ' + interval + ' seconds.'; else if (interval < 60) hint.textContent = 'Refreshes every ' + interval + ' seconds.';
@@ -394,16 +394,16 @@
} }
// Init pill UI from saved settings // Init pill UI from saved settings
var _settings = loadSettings(); const _settings = loadSettings();
applyRefreshPillUI(_settings.refreshInterval); applyRefreshPillUI(_settings.refreshInterval);
// Expose for pages that need to read it (e.g. index.html for autoRefresh) // Expose for pages that need to read it (e.g. index.html for autoRefresh)
window.gandalfSettings = _settings; window.gandalfSettings = _settings;
document.addEventListener('click', function(e) { document.addEventListener('click', function(e) {
var pill = e.target.closest('#settings-refresh-pills .pill[data-refresh-interval]'); const pill = e.target.closest('#settings-refresh-pills .pill[data-refresh-interval]');
if (!pill) return; if (!pill) return;
var interval = parseInt(pill.dataset.refreshInterval); const interval = parseInt(pill.dataset.refreshInterval);
_settings.refreshInterval = interval; _settings.refreshInterval = interval;
saveSettings(_settings); saveSettings(_settings);
applyRefreshPillUI(interval); applyRefreshPillUI(interval);
@@ -412,16 +412,16 @@
// ── Notification Bell — shows active monitoring alerts ──────────────── // ── Notification Bell — shows active monitoring alerts ────────────────
(function() { (function() {
var bell = document.getElementById('lt-notif-bell'); const bell = document.getElementById('lt-notif-bell');
var panel = document.getElementById('lt-notif-panel'); const panel = document.getElementById('lt-notif-panel');
var list = document.getElementById('lt-notif-list'); const list = document.getElementById('lt-notif-list');
var clearBtn = document.getElementById('lt-notif-clear-btn'); const clearBtn = document.getElementById('lt-notif-clear-btn');
var wrapEl = document.getElementById('lt-notif-wrap'); const wrapEl = document.getElementById('lt-notif-wrap');
if (!bell || !panel) return; if (!bell || !panel) return;
var _open = false; let _open = false;
var _lastEvents = []; let _lastEvents = [];
var LS_READ_KEY = 'gandalf_notif_read_before'; const LS_READ_KEY = 'gandalf_notif_read_before';
function getReadBefore() { function getReadBefore() {
try { return parseInt(localStorage.getItem(LS_READ_KEY) || '0'); } catch(_) { return 0; } try { return parseInt(localStorage.getItem(LS_READ_KEY) || '0'); } catch(_) { return 0; }
@@ -440,20 +440,20 @@
} }
function fmtAgo(dateStr) { function fmtAgo(dateStr) {
var diff = Math.floor((Date.now() - toMs(dateStr)) / 1000); const diff = Math.floor((Date.now() - toMs(dateStr)) / 1000);
if (diff < 60) return diff + 's ago'; if (diff < 60) return diff + 's ago';
if (diff < 3600) return Math.floor(diff/60) + 'm ago'; if (diff < 3600) return Math.floor(diff/60) + 'm ago';
if (diff < 86400) return Math.floor(diff/3600) + 'h ago'; if (diff < 86400) return Math.floor(diff/3600) + 'h ago';
return Math.floor(diff/86400) + 'd ago'; return Math.floor(diff/86400) + 'd ago';
} }
var SEV_DOT = { critical: 'var(--red)', warning: 'var(--amber)' }; const SEV_DOT = { critical: 'var(--red)', warning: 'var(--amber)' };
function renderAlerts(events) { function renderAlerts(events) {
_lastEvents = events || []; _lastEvents = events || [];
var readBefore = getReadBefore(); const readBefore = getReadBefore();
var active = _lastEvents.filter(function(e) { return e.severity !== 'info'; }); const active = _lastEvents.filter(function(e) { return e.severity !== 'info'; });
var unreadCount = active.filter(function(e) { return toMs(e.last_seen) > readBefore; }).length; const unreadCount = active.filter(function(e) { return toMs(e.last_seen) > readBefore; }).length;
lt.notif.set(bell, unreadCount); lt.notif.set(bell, unreadCount);
if (!active.length) { if (!active.length) {
@@ -461,8 +461,8 @@
return; return;
} }
list.innerHTML = active.slice(0, 25).map(function(e) { list.innerHTML = active.slice(0, 25).map(function(e) {
var isUnread = toMs(e.last_seen) > readBefore; const isUnread = toMs(e.last_seen) > readBefore;
var dotColor = SEV_DOT[e.severity] || 'var(--text-muted)'; const dotColor = SEV_DOT[e.severity] || 'var(--text-muted)';
return '<div class="lt-notif-item' + (isUnread ? ' lt-notif-item--unread' : '') + '">' + return '<div class="lt-notif-item' + (isUnread ? ' lt-notif-item--unread' : '') + '">' +
'<div class="lt-notif-dot' + (isUnread ? '' : ' lt-notif-dot--read') + '" style="background:' + dotColor + '"></div>' + '<div class="lt-notif-dot' + (isUnread ? '' : ' lt-notif-dot--read') + '" style="background:' + dotColor + '"></div>' +
'<div class="lt-notif-item-body">' + '<div class="lt-notif-item-body">' +
@@ -476,14 +476,14 @@
fetch('/api/status', { credentials: 'same-origin' }) fetch('/api/status', { credentials: 'same-origin' })
.then(function(r) { return r.json(); }) .then(function(r) { return r.json(); })
.then(function(data) { .then(function(data) {
var events = data.events || []; const events = data.events || [];
if (andRender) { if (andRender) {
renderAlerts(events); renderAlerts(events);
} else { } else {
_lastEvents = events; _lastEvents = events;
var readBefore = getReadBefore(); const readBefore = getReadBefore();
var active = events.filter(function(e) { return e.severity !== 'info'; }); const active = events.filter(function(e) { return e.severity !== 'info'; });
var unread = active.filter(function(e) { return toMs(e.last_seen) > readBefore; }).length; const unread = active.filter(function(e) { return toMs(e.last_seen) > readBefore; }).length;
lt.notif.set(bell, unread); lt.notif.set(bell, unread);
} }
}) })
+5 -5
View File
@@ -466,7 +466,7 @@
{% block scripts %} {% block scripts %}
<script> <script>
// Start auto-refresh using saved settings interval (default 30 s) // Start auto-refresh using saved settings interval (default 30 s)
var _savedInterval = (window.gandalfSettings && window.gandalfSettings.refreshInterval) || 30; const _savedInterval = (window.gandalfSettings && window.gandalfSettings.refreshInterval) || 30;
if (_savedInterval > 0) lt.autoRefresh.start(refreshAll, _savedInterval * 1000); if (_savedInterval > 0) lt.autoRefresh.start(refreshAll, _savedInterval * 1000);
// When settings change, restart auto-refresh with new interval // When settings change, restart auto-refresh with new interval
@@ -477,9 +477,9 @@
// ── Topology collapse toggle ─────────────────────────────────── // ── Topology collapse toggle ───────────────────────────────────
(function() { (function() {
var LS_KEY = 'gandalf_topo_collapsed'; const LS_KEY = 'gandalf_topo_collapsed';
var btn = document.getElementById('topo-toggle-btn'); const btn = document.getElementById('topo-toggle-btn');
var wrap = document.getElementById('topo-collapsible-wrap'); const wrap = document.getElementById('topo-collapsible-wrap');
if (!btn || !wrap) return; if (!btn || !wrap) return;
function setCollapsed(v) { function setCollapsed(v) {
@@ -489,7 +489,7 @@
try { localStorage.setItem(LS_KEY, v ? '1' : '0'); } catch(_) {} try { localStorage.setItem(LS_KEY, v ? '1' : '0'); } catch(_) {}
} }
var saved = false; let saved = false;
try { saved = localStorage.getItem(LS_KEY) === '1'; } catch(_) {} try { saved = localStorage.getItem(LS_KEY) === '1'; } catch(_) {}
setCollapsed(saved); setCollapsed(saved);
+3 -2
View File
@@ -36,6 +36,7 @@
{% block scripts %} {% block scripts %}
<script> <script>
const escHtml = s => lt.escHtml(s); const escHtml = s => lt.escHtml(s);
const _toIso = s => s ? s.replace(' UTC', 'Z').replace(' ', 'T') : s;
// ── Formatting helpers ──────────────────────────────────────────── // ── Formatting helpers ────────────────────────────────────────────
function fmtRate(bytesPerSec) { function fmtRate(bytesPerSec) {
@@ -326,7 +327,7 @@ function renderPortCard(portName, d) {
function renderUnifiSwitches(unifiSwitches, dataUpdated) { function renderUnifiSwitches(unifiSwitches, dataUpdated) {
if (!unifiSwitches || !Object.keys(unifiSwitches).length) return ''; if (!unifiSwitches || !Object.keys(unifiSwitches).length) return '';
const updStr = dataUpdated const updStr = dataUpdated
? new Date(dataUpdated.replace(' UTC', 'Z').replace(' ', 'T')).toLocaleTimeString() ? new Date(_toIso(dataUpdated)).toLocaleTimeString()
: ''; : '';
const html = Object.entries(unifiSwitches).map(([swName, sw]) => { const html = Object.entries(unifiSwitches).map(([swName, sw]) => {
const ports = sw.ports || {}; const ports = sw.ports || {};
@@ -558,7 +559,7 @@ async function loadLinks() {
} }
loadLinks(); loadLinks();
var _linksInterval = (window.gandalfSettings && window.gandalfSettings.refreshInterval) || 60; const _linksInterval = (window.gandalfSettings && window.gandalfSettings.refreshInterval) || 60;
if (_linksInterval > 0) lt.autoRefresh.start(loadLinks, Math.max(_linksInterval, 15) * 1000); if (_linksInterval > 0) lt.autoRefresh.start(loadLinks, Math.max(_linksInterval, 15) * 1000);
window.onGandalfSettingsChanged = function(s) { window.onGandalfSettingsChanged = function(s) {