import QtQuick 2.0 import Sailfish.Silica 1.0 import "../components" Page { id: page // ── Letture difensive dello snapshot ────────────────────────────────── // Il JSON arriva da un altro processo: ogni campo può mancare. In JS // leggere una proprietà di undefined solleva un'eccezione, quindi si // passa sempre da queste funzioni. function cpuPct() { var d = statusClient.data; return (d && d.cpu && d.cpu.percent !== undefined) ? d.cpu.percent : -1 } function cpuTemp() { var d = statusClient.data; return (d && d.cpu && d.cpu.temp_c !== undefined) ? d.cpu.temp_c : -1 } function cpuCores() { var d = statusClient.data; return (d && d.cpu && d.cpu.cores !== undefined) ? d.cpu.cores : 0 } function memPercent() { var d = statusClient.data; return (d && d.memory && d.memory.percent !== undefined) ? d.memory.percent : -1 } function memText() { var d = statusClient.data if (!d || !d.memory) return "" return statusClient.fmtBytes(d.memory.used_bytes) + " / " + statusClient.fmtBytes(d.memory.total_bytes) } function swapPercent() { var d = statusClient.data if (!d || !d.memory || !d.memory.swap_total_bytes) return 0 return 100 * d.memory.swap_used_bytes / d.memory.swap_total_bytes } function disk() { var d = statusClient.data; return (d && d.disks && d.disks.length > 0) ? d.disks[0] : null } function diskPct() { var x = disk(); return x ? x.percent : -1 } function diskText() { var x = disk(); return x ? statusClient.fmtBytes(x.used_bytes) + " / " + statusClient.fmtBytes(x.total_bytes) : "" } function containers() { var d = statusClient.data; return (d && d.containers && d.containers.running) ? d.containers.running : [] } function containersBad() { var list = containers(), n = 0, i for (i = 0; i < list.length; i++) if (!list[i].ok) n++ return n } function containersText() { var list = containers(), names = [], i for (i = 0; i < list.length; i++) names.push(list[i].name) return names.join(", ") } function backupAge() { var d = statusClient.data; return (d && d.backups && d.backups.age_hours !== null && d.backups.age_hours !== undefined) ? d.backups.age_hours : -1 } function backupResult() { var d = statusClient.data; return (d && d.backups && d.backups.result) ? d.backups.result : "" } // Giorni INTERI: il valore mostrato e il colore devono raccontare la stessa // cosa, quindi si arrotonda all'intero piu' vicino (3,6 g -> 4 = giallo). function backupDays() { var a = backupAge() if (a < 0) return -1 return Math.floor(a / 24 + 0.5) } // Soglie (decisione utente): verde 1-3 giorni, giallo 4-7, rosso da 8 in su. function backupLevel() { if (backupResult() === "") return 1 if (backupResult() !== "success") return 2 var g = backupDays() if (g < 0) return 1 if (g >= 8) return 2 if (g >= 4) return 1 return 0 } function failedServices() { var d = statusClient.data; return (d && d.services && d.services.failed) ? d.services.failed.length : 0 } function sshFailed() { var d = statusClient.data; return (d && d.access && d.access.ssh_failed_24h !== undefined) ? d.access.ssh_failed_24h : 0 } function updatesTotal() { var d = statusClient.data; return (d && d.updates && d.updates.total !== undefined) ? d.updates.total : 0 } function updatesSecurity() { var d = statusClient.data; return (d && d.updates && d.updates.security !== undefined) ? d.updates.security : 0 } function updatesPackages() { var d = statusClient.data return (d && d.updates && d.updates.packages) ? d.updates.packages : [] } // Il dettaglio deve rendere verificabile uno zero: senza la data delle // liste apt "nessun aggiornamento" e' indistinguibile da "non ho guardato". function updatesDetail() { var d = statusClient.data if (!d || !d.updates) return "" var parts = [] if (d.updates.total > 0 && updatesPackages().length > 0) parts.push(updatesPackages().join(", ")) parts.push(qsTr("di sicurezza: ") + updatesSecurity()) if (d.updates.lists_updated_at) parts.push(qsTr("liste apt: ") + statusClient.fmtStamp(d.updates.lists_updated_at)) return parts.join(" · ") } function backupEnough() { return backupAge() >= 0 } function backupDetail() { var d = statusClient.data if (!d || !d.backups) return "" var r = backupResult() var base = r === "" ? qsTr("nessun dato") : "borg · " + r // Senza marcatore né timestamp dell'unit non si conosce l'ora del giro // (dopo un riavvio systemd azzera i suoi timestamp): dirlo, invece di // far sembrare che il backup non sia mai stato fatto. if (!backupEnough()) base += " · " + qsTr("ora del giro non registrata") return base } function ups() { var d = statusClient.data return (d && d.ups && d.ups.available) ? d.ups : null } function upsStatus() { var u = ups() return (u && u.status) ? u.status.toUpperCase() : "" } function upsLabel() { var s = upsStatus() if (s === "") return "—" if (s.indexOf("OB") >= 0) return qsTr("a batteria") if (s.indexOf("OL") >= 0) return qsTr("rete") return s } function upsLevel() { var u = ups() if (!u) return 1 var s = upsStatus() if (s.indexOf("ALARM") >= 0 || s.indexOf("FSD") >= 0 || s.indexOf("LB") >= 0) return 2 if (u.charge_pct !== null && u.charge_pct !== undefined && u.charge_pct < 50) return 2 if (u.load_pct !== null && u.load_pct !== undefined && u.load_pct > 85) return 1 return 0 } function upsDetail() { var u = ups() if (!u) return qsTr("nessun UPS rilevato") var parts = [] if (u.charge_pct !== null && u.charge_pct !== undefined) parts.push(qsTr("carica ") + statusClient.fmtNum(u.charge_pct, 0) + "%") if (u.runtime_s) parts.push(qsTr("autonomia ") + Math.round(u.runtime_s / 60) + " min") if (u.load_pct !== null && u.load_pct !== undefined) parts.push(qsTr("carico ") + statusClient.fmtNum(u.load_pct, 0) + "%") if (u.input_voltage) parts.push(statusClient.fmtNum(u.input_voltage, 0) + " V") return parts.join(" · ") } function vpnText() { var d = statusClient.data if (!d || !d.net || !d.net.interfaces || d.net.interfaces.length === 0) return "nessuna" return d.net.interfaces.join(", ") } function hostText() { var d = statusClient.data if (!d || !d.host) return "" var s = d.host.hostname if (d.host.uptime_s) s = s + " · attivo da " + statusClient.fmtUptime(d.host.uptime_s) return s } function levelFor(percent, warn, bad) { if (percent < 0) return 0 if (percent >= bad) return 2 if (percent >= warn) return 1 return 0 } SilicaFlickable { anchors.fill: parent contentHeight: column.height + Theme.paddingLarge PullDownMenu { MenuItem { text: qsTr("Aggiorna") onClicked: statusClient.refresh() } MenuItem { text: qsTr("Impostazioni") onClicked: pageStack.push(Qt.resolvedUrl("SettingsPage.qml")) } } Column { id: column width: parent.width spacing: Theme.paddingMedium PageHeader { title: qsTr("Stato server") } Label { x: Theme.paddingLarge width: parent.width - 2 * Theme.paddingLarge text: hostText() visible: text !== "" font.pixelSize: Theme.fontSizeExtraSmall color: Theme.secondaryColor truncationMode: TruncationMode.Fade } // Nessun server configurato: un solo comando, nel menu. Label { x: Theme.paddingLarge width: parent.width - 2 * Theme.paddingLarge visible: !statusClient.configured text: qsTr("Nessun server configurato. Menu in alto → Impostazioni.") font.pixelSize: Theme.fontSizeSmall color: Theme.highlightColor wrapMode: Text.WordWrap } // Errore visibile: senza questo la schermata resta vuota e muta. Label { x: Theme.paddingLarge width: parent.width - 2 * Theme.paddingLarge visible: statusClient.error !== "" text: statusClient.error font.pixelSize: Theme.fontSizeSmall color: "#e53935" wrapMode: Text.WordWrap } MetricCard { x: Theme.paddingLarge width: parent.width - 2 * Theme.paddingLarge title: qsTr("CPU") value: cpuPct() < 0 ? "—" : statusClient.fmtNum(cpuPct(), 0) + " %" detail: (cpuTemp() >= 0 ? statusClient.fmtNum(cpuTemp(), 0) + " °C · " : "") + cpuCores() + " core" progress: cpuPct() level: levelFor(cpuPct(), 85, 95) } MetricCard { x: Theme.paddingLarge width: parent.width - 2 * Theme.paddingLarge title: qsTr("Memoria") value: memPercent() < 0 ? "—" : statusClient.fmtNum(memPercent(), 0) + " %" detail: memText() + " · swap " + statusClient.fmtNum(swapPercent(), 0) + " %" progress: memPercent() level: Math.max(levelFor(memPercent(), 85, 95), levelFor(swapPercent(), 80, 95)) } MetricCard { x: Theme.paddingLarge width: parent.width - 2 * Theme.paddingLarge title: qsTr("Disco") + (disk() ? " " + disk().mount : "") value: diskPct() < 0 ? "—" : statusClient.fmtNum(diskPct(), 0) + " %" detail: diskText() progress: diskPct() level: levelFor(diskPct(), 85, 95) } MetricCard { x: Theme.paddingLarge width: parent.width - 2 * Theme.paddingLarge title: qsTr("Container") value: containers().length + " " + qsTr("attivi") detail: containersText() progress: -1 level: containersBad() > 0 ? 2 : 0 } MetricCard { x: Theme.paddingLarge width: parent.width - 2 * Theme.paddingLarge title: qsTr("Backup") value: backupDays() < 0 ? "—" : (backupDays() === 0 ? qsTr("meno di 1 giorno") : (backupDays() === 1 ? qsTr("1 giorno") : backupDays() + " " + qsTr("giorni"))) detail: backupDetail() progress: -1 level: backupLevel() } MetricCard { x: Theme.paddingLarge width: parent.width - 2 * Theme.paddingLarge title: qsTr("UPS") value: upsLabel() detail: upsDetail() progress: -1 level: upsLevel() } MetricCard { x: Theme.paddingLarge width: parent.width - 2 * Theme.paddingLarge title: qsTr("Servizi") value: failedServices() === 0 ? qsTr("tutti attivi") : failedServices() + " " + qsTr("in errore") detail: "" progress: -1 level: failedServices() > 0 ? 2 : 0 } MetricCard { x: Theme.paddingLarge width: parent.width - 2 * Theme.paddingLarge title: qsTr("SSH") value: sshFailed() + " " + qsTr("tentativi") detail: qsTr("password fallite nelle ultime 24 h") progress: -1 level: sshFailed() >= 200 ? 2 : (sshFailed() >= 50 ? 1 : 0) } MetricCard { x: Theme.paddingLarge width: parent.width - 2 * Theme.paddingLarge title: qsTr("Aggiornamenti") value: updatesTotal() === 0 ? qsTr("nessuno") : String(updatesTotal()) detail: updatesDetail() progress: -1 level: updatesSecurity() > 0 ? 1 : 0 } MetricCard { x: Theme.paddingLarge width: parent.width - 2 * Theme.paddingLarge title: qsTr("VPN") value: vpnText() detail: statusClient.fetchedAt !== "" ? qsTr("aggiornato alle ") + statusClient.fetchedAt : "" progress: -1 level: vpnText() === qsTr("nessuna") ? 2 : 0 } } VerticalScrollDecorator { } } // Aggiornamento automatico: lo snapshot sul server è rigenerato ogni 60 s. Timer { interval: 60000 running: true repeat: true onTriggered: statusClient.refresh() } Component.onCompleted: statusClient.refresh() }