From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 7AE071FF191 for ; Tue, 23 Sep 2025 11:51:28 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id A067F9EE5; Tue, 23 Sep 2025 11:51:58 +0200 (CEST) From: Dominik Csapak To: pdm-devel@lists.proxmox.com Date: Tue, 23 Sep 2025 11:51:11 +0200 Message-ID: <20250923095124.1679038-9-d.csapak@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250923095124.1679038-1-d.csapak@proxmox.com> References: <20250923095124.1679038-1-d.csapak@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.027 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pdm-devel] [PATCH yew-comp 8/8] add `node_info` helper to render a consistent view of the NodeStatus X-BeenThere: pdm-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Datacenter Manager development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox Datacenter Manager development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pdm-devel-bounces@lists.proxmox.com Sender: "pdm-devel" since we want this for the pve and pbs NodeStatus, we introduce a small enum type that can hold a reference to either and dynamically extract the information we need. That way we can easily use the helper for both PVE and PBS node panels. The content itself should be the same as the one we have in the native PVE and PBS interface, minus the subscription info. Signed-off-by: Dominik Csapak --- Cargo.toml | 3 + src/lib.rs | 3 + src/node_info.rs | 222 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 228 insertions(+) create mode 100644 src/node_info.rs diff --git a/Cargo.toml b/Cargo.toml index 11680c7..d6002a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -82,6 +82,9 @@ proxmox-access-control = "1.1" proxmox-dns-api = { version = "1", optional = true } proxmox-network-api = { version = "1", optional = true } +pve-api-types = "8" +pbs-api-types = "1" + [features] default = [] apt = [ diff --git a/src/lib.rs b/src/lib.rs index ca34e67..492326a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -80,6 +80,9 @@ pub use loadable_component::{ LoadableComponent, LoadableComponentContext, LoadableComponentLink, LoadableComponentMaster, }; +mod node_info; +pub use node_info::{node_info, NodeStatus}; + mod notes_view; pub use notes_view::{NotesView, NotesWithDigest, ProxmoxNotesView}; diff --git a/src/node_info.rs b/src/node_info.rs new file mode 100644 index 0000000..19a1ada --- /dev/null +++ b/src/node_info.rs @@ -0,0 +1,222 @@ +use proxmox_human_byte::HumanByte; +use pwt::{prelude::*, widget::Container}; + +use crate::{MeterLabel, StatusRow}; + +/// Type that holds either a PVE NodeStatus or a PBS NodeStatus +pub enum NodeStatus<'a> { + Pve(&'a pve_api_types::NodeStatus), + Pbs(&'a pbs_api_types::NodeStatus), +} + +impl<'a> From<&'a pve_api_types::NodeStatus> for NodeStatus<'a> { + fn from(value: &'a pve_api_types::NodeStatus) -> Self { + NodeStatus::Pve(value) + } +} + +impl<'a> From<&'a pbs_api_types::NodeStatus> for NodeStatus<'a> { + fn from(value: &'a pbs_api_types::NodeStatus) -> Self { + NodeStatus::Pbs(value) + } +} + +/// Renders the NodeInfo panel content +// TODO: add repository status +// NOTE: if we need internal state or the tree get's too big, we should convert this +// into a proper component +pub fn node_info(data: Option) -> Container { + let (cpu, cpus_total) = match data { + Some(NodeStatus::Pve(node_status)) => (node_status.cpu, node_status.cpuinfo.cpus as u64), + Some(NodeStatus::Pbs(node_status)) => (node_status.cpu, node_status.cpuinfo.cpus as u64), + None => (0.0, 1), + }; + + let wait = match data { + Some(NodeStatus::Pve(node_status)) => node_status + .additional_properties + .get("wait") + .and_then(|wait| wait.as_f64()) + .unwrap_or_default(), + Some(NodeStatus::Pbs(node_status)) => node_status.wait, + None => 0.0, + }; + + let (memory_used, memory_total) = match data { + Some(NodeStatus::Pve(node_status)) => ( + node_status.memory.used as u64, + node_status.memory.total as u64, + ), + Some(NodeStatus::Pbs(node_status)) => (node_status.memory.used, node_status.memory.total), + None => (0, 1), + }; + + let loadavg = match data { + Some(NodeStatus::Pve(node_status)) => node_status.loadavg.join(" "), + Some(NodeStatus::Pbs(node_status)) => format!( + "{:.2} {:.2} {:.2}", + node_status.loadavg[0], node_status.loadavg[1], node_status.loadavg[2] + ), + None => tr!("N/A"), + }; + + let (root_used, root_total) = match data { + Some(NodeStatus::Pve(node_status)) => ( + node_status.rootfs.used as u64, + node_status.rootfs.total as u64, + ), + Some(NodeStatus::Pbs(node_status)) => (node_status.root.used, node_status.root.total), + None => (0, 1), + }; + + let (swap_used, swap_total) = match data { + Some(NodeStatus::Pve(node_status)) => { + if let Some(swap) = node_status + .additional_properties + .get("swap") + .and_then(|swap| swap.as_object()) + { + let used = swap + .get("used") + .and_then(|used| used.as_u64()) + .unwrap_or_default(); + let total = swap + .get("total") + .and_then(|used| used.as_u64()) + .unwrap_or(0); + (used, total) + } else { + (0, 0) + } + } + Some(NodeStatus::Pbs(node_status)) => (node_status.swap.used, node_status.swap.total), + None => (0, 1), + }; + + let (model, sockets) = match data { + Some(NodeStatus::Pve(node_status)) => ( + node_status.cpuinfo.model.clone(), + node_status.cpuinfo.sockets as u64, + ), + Some(NodeStatus::Pbs(node_status)) => ( + node_status.cpuinfo.model.clone(), + node_status.cpuinfo.sockets as u64, + ), + None => (String::new(), 1), + }; + + let version = match data { + Some(NodeStatus::Pve(node_status)) => Some(node_status.pveversion.clone()), + _ => None, + }; + + let (k_sysname, k_release, k_version) = match data { + Some(NodeStatus::Pve(node_status)) => ( + node_status.current_kernel.sysname.clone(), + node_status.current_kernel.release.clone(), + node_status.current_kernel.version.clone(), + ), + Some(NodeStatus::Pbs(node_status)) => ( + node_status.current_kernel.sysname.clone(), + node_status.current_kernel.release.clone(), + node_status.current_kernel.version.clone(), + ), + None => (String::new(), String::new(), String::new()), + }; + + Container::new() + .class("pwt-d-grid pwt-gap-2 pwt-align-items-center") + .style("grid-template-columns", "1fr 20px 1fr") + .style("height", "fit-content") + .padding(4) + .with_child( + MeterLabel::with_zero_optimum(tr!("CPU Usage")) + .animated(true) + .icon_class("fa fa-fw fa-cpu") + .value(cpu as f32) + .status(format!("{:.2}% of {} CPU(s)", cpu * 100.0, cpus_total)), + ) + .with_child( + MeterLabel::with_zero_optimum(tr!("IO delay")) + .animated(true) + .style("grid-column", "3") + .icon_class("fa fa-fw fa-clock-o") + .value(wait as f32), + ) + .with_child(Container::new().padding(2).style("grid-column", "1/-1")) + .with_child({ + let fraction = ((memory_used as f64) / (memory_total as f64)) as f32; + MeterLabel::with_zero_optimum(tr!("RAM Usage")) + .animated(true) + .icon_class("fa fa-fw fa-memory") + .value(fraction) + .status(format!( + "{:.2}% ({} of {})", + fraction * 100.0, + HumanByte::from(memory_used), + HumanByte::from(memory_total), + )) + }) + .with_child( + StatusRow::new(tr!("Load Average")) + .icon_class("fa fa-fw fa-tasks") + .status(loadavg) + .style("grid-column", "3"), + ) + .with_child({ + let fraction = ((root_used as f64) / (root_total as f64)) as f32; + MeterLabel::with_zero_optimum(tr!("HD space (root)")) + .animated(true) + .icon_class("fa fa-fw fa-hdd-o") + .value(fraction) + .status(format!( + "{:.2}% ({} of {})", + fraction * 100.0, + HumanByte::new_decimal(root_used as f64), + HumanByte::new_decimal(root_total as f64), + )) + }) + .with_child({ + let (fraction, status) = if swap_total > 0 { + let fraction = ((swap_used as f64) / (swap_total as f64)) as f32; + let status = format!( + "{:.2}% ({} of {})", + fraction * 100.0, + HumanByte::from(swap_used), + HumanByte::from(swap_total), + ); + (Some(fraction), status) + } else { + (None, tr!("N/A")) + }; + MeterLabel::with_zero_optimum(tr!("SWAP usage")) + .animated(true) + .style("grid-column", "3") + .icon_class("fa fa-fw fa-refresh") + .value(fraction) + .animated(true) + .status(status) + }) + .with_child(Container::new().padding(2).style("grid-column", "1/-1")) + .with_child({ + let cpu_model_text = format!( + "{} x {} ({})", + cpus_total, + model, + ngettext!("1 Socket", "{n} Sockets", sockets), + ); + StatusRow::new(tr!("CPU(s)")) + .style("grid-column", "1/-1") + .status(cpu_model_text) + }) + .with_optional_child(version.map(|version| { + StatusRow::new(tr!("Version")) + .style("grid-column", "1/-1") + .status(version) + })) + .with_child( + StatusRow::new(tr!("Kernel Version")) + .style("grid-column", "1/-1") + .status(format!("{} {} {}", k_sysname, k_release, k_version)), + ) +} -- 2.47.3 _______________________________________________ pdm-devel mailing list pdm-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel