// Admin Overview — the Admin area's landing page. An ops/health board (distinct // from the user Dashboard, which is app-centric): it summarises updates, // backups, SSH/security and system health from data we already generate, and // each card links to where you act on it. Renders into #config-section. class AdminOverview { constructor(rootId = 'config-section') { this.rootId = rootId; this.taskManager = (typeof TaskManager !== 'undefined') ? new TaskManager() : null; this._bound = false; } root() { return document.getElementById(this.rootId); } async init() { const r = this.root(); if (r) r.innerHTML = '
Loading…
'; this.bindEvents(); const [upd, backup, ssh, disk, mem, info] = await Promise.all([ this.fetchJson('/data/system/update_status.json'), this.fetchJson('/data/backup/generated/dashboard.json'), this.fetchJson('/data/ssh/access.json'), this.fetchJson('/data/system/disk_usage.json'), this.fetchJson('/data/system/memory_usage.json'), this.fetchJson('/data/system/system_info.json') ]); this.d = { upd, backup, ssh, disk, mem, info }; this.render(); } async fetchJson(url) { try { const r = await fetch(`${url}?t=${Date.now()}`); if (!r.ok) return null; return await r.json(); } catch { return null; } } escape(s) { return String(s ?? '').replace(/[&<>"']/g, c => ( { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c] )); } notify(msg, type) { if (window.notificationSystem) window.notificationSystem.show(msg, type || 'info'); else console.log(`[admin ${type || 'info'}] ${msg}`); } bindEvents() { if (this._bound) return; this._bound = true; document.addEventListener('click', (e) => { const go = e.target.closest('[data-admin-go]'); if (go) { this.go(go.dataset.adminGo); return; } if (e.target.closest('[data-admin-update]')) { this.runUpdate(); return; } }); } go(where) { if (where === 'backup') { window.librePortalSPA?.navigate('/backup', true); } else if (where === 'ssh' || where === 'security' || where === 'system') { const target = where === 'ssh' ? 'ssh-access' : where; window.history.pushState({}, '', window.adminPath(target)); window.configCategory = target; window.configManager?.renderConfig?.(target); } } async runUpdate() { if (!this.taskManager) { this.notify('Task system unavailable', 'error'); return; } if (!confirm('Update LibrePortal now? The WebUI may restart briefly.')) return; try { await this.taskManager.createTask('libreportal update apply', 'update', null); } catch (e) { this.notify(`Failed to start update: ${e.message || e}`, 'error'); } } /* A status card: kind sets the dot colour (ok/warn/none). actionsHtml is the footer (Manage link / button). */ card(title, kind, lines, actionsHtml) { return `
${this.escape(title)}
${lines}
${actionsHtml || ''}
`; } line(label, value) { return `
${this.escape(label)}${this.escape(value)}
`; } render() { const root = this.root(); if (!root) return; const d = this.d || {}; // Updates const upd = d.upd || {}; const updAvail = upd.update_available === true; const updCard = this.card( 'Updates', updAvail ? 'warn' : 'ok', updAvail ? this.line('Status', 'Update available') + this.line('Current → latest', `${upd.current_version || '?'} → ${upd.latest_version || '?'}`) : this.line('Status', 'Up to date') + this.line('Version', upd.current_version || '—'), updAvail && upd.can_update ? `` : `Nothing to do` ); // Backups const b = d.backup || {}; const apps = Array.isArray(b.apps) ? b.apps : []; const locs = Array.isArray(b.locations) ? b.locations : []; const protectedApps = apps.filter(a => a.latest_snapshot).length; const totalSize = locs.reduce((a, r) => a + (parseInt(r.total_size_bytes) || 0), 0); const noBackups = apps.length && protectedApps === 0; const backupCard = this.card( 'Backups', !locs.length ? 'none' : (noBackups ? 'warn' : 'ok'), this.line('Apps protected', `${protectedApps} / ${apps.length}`) + this.line('Locations', String(locs.length)) + this.line('Stored', this.bytes(totalSize)), `` ); // SSH & Security const s = d.ssh || {}; const keyCount = Array.isArray(s.keys) ? s.keys.length : 0; const pwOn = s.password_auth === true; // Warn when password login is on AND no keys (the weakest posture). const sshKind = (pwOn && keyCount === 0) ? 'warn' : 'ok'; const sshCard = this.card( 'SSH & Security', sshKind, this.line('Password login', pwOn ? 'On' : 'Key-only') + this.line('Authorized keys', String(keyCount)) + this.line('Login user', s.user || '—'), `` ); // System health const disk = d.disk?.root || {}; const mem = d.mem || {}; const info = d.info || {}; const diskPct = parseInt(disk.percent) || 0; const sysKind = diskPct >= 90 ? 'warn' : 'ok'; const sysCard = this.card( 'System', sysKind, this.line('Disk', disk.text || `${diskPct}%`) + this.line('Memory', mem.text || '—') + this.line('Uptime', (info.uptime || '—').replace(/^up /, '')) + this.line('OS', info.os || '—'), `` ); root.innerHTML = `
${updCard} ${backupCard} ${sshCard} ${sysCard}
`; } bytes(n) { n = parseInt(n) || 0; const u = ['B', 'KB', 'MB', 'GB', 'TB']; let i = 0; while (n >= 1024 && i < u.length - 1) { n /= 1024; i++; } return `${n.toFixed(i ? 1 : 0)} ${u[i]}`; } } window.AdminOverview = AdminOverview;