diff --git a/index.js b/index.js index 20e3b8c..c6612c8 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ const fs = require('fs'); const path = require('path'); const app = express(); app.use(express.json()); -const PORT = 3000; +const PORT = 3010; // ─── Persistence ────────────────────────────────────────────────────────────── @@ -584,6 +584,69 @@ app.get('/', async (req, res) => { } .btn-add-url:hover { border-color: #6366f1; color: #a5b4fc; } .modal { max-width: 540px !important; max-height: 90vh; overflow-y: auto; } + + /* ── Last-Hour History Bar ── */ + .history-bar-wrap { + background: #1e293b; + border: 1px solid #334155; + border-radius: 14px; + padding: 1rem 1.5rem; + margin-bottom: 1rem; + } + .history-bar-header { + display: flex; + justify-content: space-between; + align-items: center; + margin-bottom: 0.5rem; + } + .history-bar-title { + font-size: 0.7rem; + font-weight: 600; + text-transform: uppercase; + letter-spacing: 0.07em; + color: #64748b; + } + .history-bar-legend { + display: flex; + gap: 0.85rem; + align-items: center; + font-size: 0.7rem; + color: #64748b; + } + .history-bar-legend span { display: flex; align-items: center; gap: 0.3rem; } + .legend-dot { width: 8px; height: 8px; border-radius: 2px; display: inline-block; } + .history-bar { + width: 100%; + height: 28px; + border-radius: 6px; + overflow: hidden; + display: flex; + background: #334155; + } + .history-bar-seg--up { background: #4ade80; height: 100%; } + .history-bar-seg--down { background: #f87171; height: 100%; } + .history-bar-seg--unknown { background: #334155; height: 100%; } + .history-bar-timestamps { + display: flex; + justify-content: space-between; + margin-top: 0.35rem; + font-size: 0.68rem; + color: #475569; + } + .history-range-btns { display: flex; gap: 0.25rem; } + .history-range-btn { + background: #0f172a; + border: 1px solid #334155; + color: #64748b; + border-radius: 6px; + padding: 0.2rem 0.55rem; + font-size: 0.72rem; + font-weight: 600; + cursor: pointer; + transition: background 0.12s, color 0.12s, border-color 0.12s; + } + .history-range-btn:hover { background: #1e3a5f; color: #93c5fd; } + .history-range-btn.active { background: #1e3a5f; border-color: #3b82f6; color: #3b82f6; } @@ -607,6 +670,29 @@ app.get('/', async (req, res) => { + +
+
+ Connectivity History +
+ + + + +
+
+ Online + Offline + No data +
+
+
+
+ + +
+
+
@@ -882,6 +968,9 @@ app.get('/', async (req, res) => { 'Last updated ' + new Date().toLocaleTimeString() + ' \xb7 checks every ' + (checkIntervalMs / 1000) + 's'; + // Last-hour history bar + renderHistoryBar(d.targets); + // IP History tables // Update current adapter/network in Network Info panel if (d.localIpHistory && d.localIpHistory.length > 0) { @@ -899,6 +988,82 @@ app.get('/', async (req, res) => { } } + let historyBarRange = 3600000; // ms; null = all time + + function setHistoryRange(ms) { + historyBarRange = ms; + renderHistoryBar(lastTargets); + document.querySelectorAll('.history-range-btn').forEach(btn => { + const val = btn.dataset.range === 'null' ? null : Number(btn.dataset.range); + btn.classList.toggle('active', val === ms); + }); + } + + function renderHistoryBar(targets) { + const now = Date.now(); + + // Determine earliest monitor start (data before this is "unknown") + const dataStart = targets.length > 0 + ? Math.min(...targets.map(t => t.monitorStartTime)) + : now; + + // rangeStart: beginning of the window to display + const rangeStart = historyBarRange === null ? dataStart : now - historyBarRange; + + // Collect down intervals overlapping the selected range from all targets + const downIntervals = []; + targets.forEach(t => { + t.outages.forEach(o => { + const s = Math.max(o.start, rangeStart); + const e = Math.min(o.end || now, now); + if (e > s) downIntervals.push({ start: s, end: e }); + }); + if (!t.isOnline && t.currentOutageStart) { + const s = Math.max(t.currentOutageStart, rangeStart); + if (now > s) downIntervals.push({ start: s, end: now }); + } + }); + + // Sort and merge overlapping down intervals + downIntervals.sort((a, b) => a.start - b.start); + const merged = []; + for (const iv of downIntervals) { + if (!merged.length || iv.start > merged[merged.length - 1].end) { + merged.push({ start: iv.start, end: iv.end }); + } else { + merged[merged.length - 1].end = Math.max(merged[merged.length - 1].end, iv.end); + } + } + + // Build segments: unknown -> up/down alternation + const segments = []; + const knownStart = Math.max(rangeStart, dataStart); + + // Gray "no data" segment before monitoring began + if (dataStart > rangeStart) { + segments.push({ type: 'unknown', start: rangeStart, end: knownStart }); + } + + let cursor = knownStart; + for (const iv of merged) { + if (iv.start > cursor) segments.push({ type: 'up', start: cursor, end: iv.start }); + segments.push({ type: 'down', start: iv.start, end: iv.end }); + cursor = iv.end; + } + if (cursor < now) segments.push({ type: 'up', start: cursor, end: now }); + + const totalMs = now - rangeStart; + const bar = document.getElementById('history-bar'); + bar.innerHTML = segments.map(s => { + const pct = (s.end - s.start) / totalMs * 100; + return '
'; + }).join(''); + + document.getElementById('history-bar-start').textContent = fmtDate(rangeStart); + document.getElementById('history-bar-end').textContent = fmtDate(now); + } + refresh(); setInterval(refresh, 1000);