pragma Singleton import QtQuick import Quickshell import Quickshell.Io Singleton { id: root property int consumers: 0 property var snapshot: ({}) property var history: [] property string error: "" property int retryDelay: 1000 property double nextStart: 0 property int samples: 0 property double lastSample: 0 property bool stale: true readonly property bool wanted: ConfigService.ready && consumers > 0 readonly property string options: ConfigService.ready ? JSON.stringify({ interval: ConfigService.data.metrics.interval, storagePaths: ConfigService.data.metrics.storagePaths, networkInterface: ConfigService.data.metrics.networkInterface }) : "{}" readonly property var cpu: snapshot.cpu || ({}) readonly property var ram: snapshot.ram || ({}) readonly property var network: snapshot.network || ({}) readonly property var gpus: snapshot.gpus || [] readonly property var gpu: (ConfigService.data.metrics.gpu === "auto" ? gpus[0] : gpus.find(g => g.id === ConfigService.data.metrics.gpu)) || ({}) onOptionsChanged: { process.running = false; root.stale = true; root.lastSample = 0; root.history = []; root.nextStart = 0; root.retryDelay = 1000; } // Idle shutdown keeps the last sample: consumers that pause briefly (desktop // widgets under windows) resume with it until it ages past the stale limit. onWantedChanged: if (!wanted) process.running = false function value(n, suffix, digits) { return stale || n === undefined || n === null || !Number.isFinite(n) ? "—" : n.toFixed(digits || 0) + (suffix || ""); } function bytes(n) { if (stale || n === undefined || n === null || !Number.isFinite(n)) return "—"; const units = ["B", "KiB", "MiB", "GiB", "TiB"]; let i = 0; while (n >= 1024 && i < units.length - 1) { n /= 1024; i++; } return n.toFixed(i > 1 ? 1 : 0) + " " + units[i]; } Timer { interval: 1000 repeat: true running: root.wanted triggeredOnStart: true onTriggered: { if (!process.running && Date.now() >= root.nextStart) { process.command = ["python3", Quickshell.shellPath("helpers/metrics.py"), root.options]; process.running = true; } root.stale = !root.lastSample || Date.now() - root.lastSample > Math.max(10000, ConfigService.data.metrics.interval * 3000); } } Process { id: process stdout: SplitParser { onRead: data => { try { const state = JSON.parse(data); if (!state.cpu || !state.ram || !Array.isArray(state.gpus) || !state.network) throw Error("Invalid metrics snapshot"); root.snapshot = state; root.lastSample = Date.now(); root.stale = false; root.error = ""; root.samples++; root.retryDelay = 1000; root.history = root.history.concat([ { cpu: state.cpu.percent, ram: state.ram.percent, down: state.network ? state.network.download : null, up: state.network ? state.network.upload : null } ]).slice(-60); } catch (e) { root.error = String(e); } } } stderr: StdioCollector { id: errors } // Quickshell 0.3.1 metadata omits QProcess::ExitStatus. // qmllint disable signal-handler-parameters onExited: code => { if (root.wanted) { root.stale = true; root.lastSample = 0; root.nextStart = Date.now() + root.retryDelay; root.retryDelay = Math.min(30000, root.retryDelay * 2); root.error = errors.text.trim().slice(0, 300) || "Metrics collector stopped; retrying"; } } } IpcHandler { target: "metrics" function status(): string { return JSON.stringify({ running: process.running, processId: process.processId, retryDelay: root.retryDelay, consumers: root.consumers, samples: root.samples, stale: root.stale, error: root.error, snapshot: root.snapshot }); } } }