From: Lukas Wagner <l.wagner@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [PATCH proxmox-yew-comp v4 04/12] rrd: add rrd_value_renderer module
Date: Wed, 15 Apr 2026 15:20:05 +0200 [thread overview]
Message-ID: <20260415132013.440581-5-l.wagner@proxmox.com> (raw)
In-Reply-To: <20260415132013.440581-1-l.wagner@proxmox.com>
This new module houses a couple of helpers that are commonly used
when rendering values in RRD graphs.
Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
src/lib.rs | 2 +-
src/rrd/mod.rs | 2 +
src/rrd/rrd_value_renderer.rs | 84 +++++++++++++++++++++++++++++++++++
3 files changed, 87 insertions(+), 1 deletion(-)
create mode 100644 src/rrd/rrd_value_renderer.rs
diff --git a/src/lib.rs b/src/lib.rs
index 62aa571..2b09bf4 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -124,7 +124,7 @@ pub use role_selector::RoleSelector;
#[cfg(feature = "rrd")]
mod rrd;
#[cfg(feature = "rrd")]
-pub use rrd::{RRDGraph, Series};
+pub use rrd::{rrd_value_renderer, RRDGraph, Series};
#[cfg(feature = "rrd")]
mod rrd_grid;
diff --git a/src/rrd/mod.rs b/src/rrd/mod.rs
index 5bcc9be..031505d 100644
--- a/src/rrd/mod.rs
+++ b/src/rrd/mod.rs
@@ -9,3 +9,5 @@ pub(crate) mod series;
pub use series::Series;
pub(crate) mod units;
+
+pub mod rrd_value_renderer;
diff --git a/src/rrd/rrd_value_renderer.rs b/src/rrd/rrd_value_renderer.rs
new file mode 100644
index 0000000..d7ba3c4
--- /dev/null
+++ b/src/rrd/rrd_value_renderer.rs
@@ -0,0 +1,84 @@
+//! Helpers for rendering values in RRD graphs.
+
+/// Render CPU usage in percent. `v` is multiplied by 100 to get the percent value.
+pub fn render_cpu_usage(v: &f64) -> String {
+ if v.is_finite() {
+ format!("{:.1}%", v * 100.0)
+ } else {
+ v.to_string()
+ }
+}
+
+/// Render server load value.
+pub fn render_load(v: &f64) -> String {
+ if v.is_finite() {
+ format!("{:.2}", v)
+ } else {
+ v.to_string()
+ }
+}
+
+/// Render a byte value.
+pub fn render_bytes(v: &f64) -> String {
+ if v.is_finite() {
+ proxmox_human_byte::HumanByte::from(*v as u64).to_string()
+ } else {
+ v.to_string()
+ }
+}
+
+/// Render bandwidth.
+pub fn render_bandwidth(v: &f64) -> String {
+ if v.is_finite() {
+ let bytes = proxmox_human_byte::HumanByte::from(*v as u64);
+ format!("{bytes}/s")
+ } else {
+ v.to_string()
+ }
+}
+
+/// Render pressure stall value.
+pub fn render_pressure(v: &f64) -> String {
+ if v.is_finite() {
+ format!("{:.1}%", v)
+ } else {
+ v.to_string()
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_render_cpu_usage() {
+ assert_eq!(render_cpu_usage(&0.532), "53.2%");
+ assert_eq!(render_cpu_usage(&f64::NAN), "NaN");
+ }
+
+ #[test]
+ fn test_render_load() {
+ assert_eq!(render_load(&0.538), "0.54");
+ assert_eq!(render_load(&f64::NAN), "NaN");
+ }
+
+ #[test]
+ fn test_render_bytes() {
+ assert_eq!(render_bytes(&(1024f64 * 1024f64)), "1 MiB");
+ assert_eq!(render_bytes(&(1254f64 * 1024f64)), "1.225 MiB");
+ assert_eq!(render_bytes(&f64::NAN), "NaN");
+ }
+
+ #[test]
+ fn test_render_bandwidth() {
+ assert_eq!(render_bandwidth(&(1024f64 * 1024f64)), "1 MiB/s");
+ assert_eq!(render_bandwidth(&(1254f64 * 1024f64)), "1.225 MiB/s");
+ assert_eq!(render_bandwidth(&f64::NAN), "NaN");
+ }
+
+ #[test]
+ fn test_render_pressure() {
+ assert_eq!(render_pressure(&0.538), "0.5%");
+ assert_eq!(render_pressure(&f64::NAN), "NaN");
+ }
+}
--
2.47.3
next prev parent reply other threads:[~2026-04-15 13:21 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-15 13:20 [PATCH datacenter-manager/proxmox-yew-comp v4 00/12] metric collection for the PDM host Lukas Wagner
2026-04-15 13:20 ` [PATCH proxmox-yew-comp v4 01/12] node status panel: add `children` property Lukas Wagner
2026-04-15 13:20 ` [PATCH proxmox-yew-comp v4 02/12] RRDGrid: fix size observer by attaching node reference to rendered container Lukas Wagner
2026-04-15 13:20 ` [PATCH proxmox-yew-comp v4 03/12] RRDGrid: add padding and increase gap between elements Lukas Wagner
2026-04-15 13:20 ` Lukas Wagner [this message]
2026-04-15 13:20 ` [PATCH datacenter-manager v4 05/12] metric collection: clarify naming for remote metric collection Lukas Wagner
2026-04-15 13:20 ` [PATCH datacenter-manager v4 06/12] metric collection: fix minor typo in error message Lukas Wagner
2026-04-15 13:20 ` [PATCH datacenter-manager v4 07/12] metric collection: collect PDM host metrics in a new collection task Lukas Wagner
2026-04-15 13:20 ` [PATCH datacenter-manager v4 08/12] api: fix /nodes/localhost/rrddata endpoint Lukas Wagner
2026-04-15 13:20 ` [PATCH datacenter-manager v4 09/12] pdm: node rrd data: rename 'total-time' to 'metric-collection-total-time' Lukas Wagner
2026-04-15 13:20 ` [PATCH datacenter-manager v4 10/12] pdm-api-types: add PDM host metric fields Lukas Wagner
2026-04-15 13:20 ` [PATCH datacenter-manager v4 11/12] ui: node status: add RRD graphs for PDM host metrics Lukas Wagner
2026-04-15 13:20 ` [PATCH datacenter-manager v4 12/12] ui: lxc/qemu/node: use RRD value render helpers from yew-comp Lukas Wagner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260415132013.440581-5-l.wagner@proxmox.com \
--to=l.wagner@proxmox.com \
--cc=pdm-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox