This allows for more elegant code to aggregate multiple
{Dynamic,Static}ServiceStats, e.g., for building service bundles.

Signed-off-by: Daniel Kral <[email protected]>
---
 proxmox-resource-scheduling/src/pve_dynamic.rs | 17 ++++++++++++++++-
 proxmox-resource-scheduling/src/pve_static.rs  | 15 ++++++++++++++-
 2 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/proxmox-resource-scheduling/src/pve_dynamic.rs 
b/proxmox-resource-scheduling/src/pve_dynamic.rs
index 4f480612..21a15e81 100644
--- a/proxmox-resource-scheduling/src/pve_dynamic.rs
+++ b/proxmox-resource-scheduling/src/pve_dynamic.rs
@@ -1,8 +1,10 @@
 use serde::{Deserialize, Serialize};
 
+use std::ops::Add;
+
 use crate::scheduler::{NodeStats, ServiceStats};
 
-#[derive(Clone, Copy, Serialize, Deserialize)]
+#[derive(Clone, Copy, Default, Serialize, Deserialize)]
 #[serde(rename_all = "kebab-case")]
 /// Dynamic usage stats of a node.
 pub struct DynamicNodeStats {
@@ -51,3 +53,16 @@ impl From<DynamicServiceStats> for ServiceStats {
         }
     }
 }
+
+impl Add for DynamicServiceStats {
+    type Output = Self;
+
+    fn add(self, rhs: Self) -> Self {
+        Self {
+            cpu: self.cpu + rhs.cpu,
+            maxcpu: self.maxcpu + rhs.maxcpu,
+            mem: self.mem + rhs.mem,
+            maxmem: self.maxmem + rhs.maxmem,
+        }
+    }
+}
diff --git a/proxmox-resource-scheduling/src/pve_static.rs 
b/proxmox-resource-scheduling/src/pve_static.rs
index b269c44f..c96da100 100644
--- a/proxmox-resource-scheduling/src/pve_static.rs
+++ b/proxmox-resource-scheduling/src/pve_static.rs
@@ -1,6 +1,8 @@
 use anyhow::Error;
 use serde::{Deserialize, Serialize};
 
+use std::ops::Add;
+
 use crate::scheduler::{ClusterUsage, NodeStats, NodeUsage, ServiceStats};
 
 #[derive(Clone, Serialize, Deserialize)]
@@ -59,7 +61,7 @@ fn add_cpu_usage(old: f64, max: f64, add: f64) -> f64 {
     }
 }
 
-#[derive(Clone, Copy, Serialize, Deserialize)]
+#[derive(Clone, Copy, Default, Serialize, Deserialize)]
 #[serde(rename_all = "kebab-case")]
 /// Static usage information of an HA resource.
 pub struct StaticServiceUsage {
@@ -80,6 +82,17 @@ impl From<StaticServiceUsage> for ServiceStats {
     }
 }
 
+impl Add for StaticServiceUsage {
+    type Output = Self;
+
+    fn add(self, rhs: Self) -> Self::Output {
+        Self {
+            maxcpu: self.maxcpu + rhs.maxcpu,
+            maxmem: self.maxmem + rhs.maxmem,
+        }
+    }
+}
+
 /// Scores candidate `nodes` to start a `service` on. Scoring is done 
according to the static memory
 /// and CPU usages of the nodes as if the service would already be running on 
each.
 ///
-- 
2.47.3




Reply via email to