Files
harbour-server/qml/pages/StatusPage.qml
T

249 lines
10 KiB
QML

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 : "" }
function backupLevel() {
if (backupResult() === "") return 1
if (backupResult() !== "success") return 2
var age = backupAge()
if (age < 0) return 1
if (age > 72) return 2
if (age > 36) 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 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: backupAge() < 0 ? "—" : statusClient.fmtAge(backupAge())
detail: backupResult() === "" ? qsTr("nessun dato") : "borg · " + backupResult()
progress: -1
level: backupLevel()
}
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()
}