From 0359de37c84f9ad268727b77590203706709a737 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Mon, 24 Feb 2025 22:34:02 -0500 Subject: [PATCH] Fix next_nth sorting bug --- src/api/cluster/volume.rs | 4 +++- src/api/entity/nodes.rs | 25 ++++++++++++++----------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/api/cluster/volume.rs b/src/api/cluster/volume.rs index c9362d9..b543f43 100644 --- a/src/api/cluster/volume.rs +++ b/src/api/cluster/volume.rs @@ -455,7 +455,9 @@ async fn transfer_directly( let storage = &svc.config.pve_storage_pool; let name_offset = storage.len() + 1; // 1 for the colon (:) let disk_name = mount[name_offset..].split(",").next().unwrap(); -debug!("transfer_directly: mount {mount} | name_offset {name_offset} | disk_name {disk_name}"); + + debug!("transfer_directly: mount {mount} | name_offset {name_offset} | disk_name {disk_name}"); + // Persist the volume let mut vol = vol.into_active_model(); vol.pve_node_id = Set(to_node.pve_id); diff --git a/src/api/entity/nodes.rs b/src/api/entity/nodes.rs index 72538a2..e695a94 100644 --- a/src/api/entity/nodes.rs +++ b/src/api/entity/nodes.rs @@ -96,6 +96,7 @@ pub async fn try_lock_first_available<'a>( /** The PVE config file for an LXC container (i.e. /etc/pve/lxc/XYZ.conf) */ +#[derive(Debug)] pub struct PveConfig { lines: Vec, } @@ -167,19 +168,21 @@ impl PveConfig { * Using that example, next_nth("volume") would return 2. */ pub fn next_nth(&self, prefix: &str) -> u32 { - let res = self.lines - .iter() - .filter(|line| line.starts_with(prefix)) + let mut res = self.lines // list of lines + .iter() // as an iterator + .filter(|line| line.starts_with(prefix)) // only lines starting with "mp" .map(|line| - line[prefix.len()..] - .split(":") - .next() - .unwrap() - .parse::()) - .filter_map(|idx| idx.ok()) - .next(); + line[prefix.len()..] // string after the prefix ("1: ") + .split(":") // ["1", " "] + .next() // "1" + .unwrap() // "1" + .parse::()) // 1 + .filter_map(|idx| idx.ok()) // [1, 2] + .collect::>(); // [1, 2] - if let Some(idx) = res { + res.sort_by(|a, z| z.cmp(a)); // [2, 1] + + if let Some(idx) = res.first() { return idx + 1; }