From 22e5536b9fd63bd1e29678a793f0d0ad1672271a Mon Sep 17 00:00:00 2001 From: Dylann Batisse Date: Fri, 27 Feb 2026 19:40:12 +0100 Subject: [PATCH] Fix idle conditions fail when both enabled --- rustatio-core/src/faker.rs | 25 +++++++++++++++++++++++++ rustatio-desktop/src/commands/faker.rs | 4 +--- rustatio-desktop/src/commands/grid.rs | 4 ---- rustatio-server/src/services/state.rs | 14 -------------- 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/rustatio-core/src/faker.rs b/rustatio-core/src/faker.rs index 5f71214..254f03c 100644 --- a/rustatio-core/src/faker.rs +++ b/rustatio-core/src/faker.rs @@ -834,6 +834,31 @@ impl RatioFaker { .map_err(|e| FakerError::ConfigError(e.to_string()))?; } + // Recompute left/torrent_completion from the new completion_percent. + // This ensures that changing completion_percent in the UI takes effect + // without having to recreate the faker. + let completion = config.completion_percent.clamp(0.0, 100.0) / 100.0; + let torrent_downloaded = (self.torrent.total_size as f64 * completion) as u64; + let new_left = self.torrent.total_size.saturating_sub(torrent_downloaded); + let new_torrent_completion = if self.torrent.total_size > 0 { + ((self.torrent.total_size - new_left) as f64 / self.torrent.total_size as f64) * 100.0 + } else { + 100.0 + }; + + #[cfg(not(target_arch = "wasm32"))] + if let Ok(mut stats) = self.stats.try_write() { + stats.left = new_left; + stats.torrent_completion = new_torrent_completion; + } + + #[cfg(target_arch = "wasm32")] + { + let mut stats = self.stats.borrow_mut(); + stats.left = new_left; + stats.torrent_completion = new_torrent_completion; + } + self.config = config; Ok(()) } diff --git a/rustatio-desktop/src/commands/faker.rs b/rustatio-desktop/src/commands/faker.rs index 850e856..9949a02 100644 --- a/rustatio-desktop/src/commands/faker.rs +++ b/rustatio-desktop/src/commands/faker.rs @@ -48,8 +48,6 @@ pub async fn start_faker( if existing.torrent.info_hash == torrent_info_hash { config_with_cumulative.initial_uploaded = existing.cumulative_uploaded; config_with_cumulative.initial_downloaded = existing.cumulative_downloaded; - let existing_stats = existing.faker.read().await.get_stats().await; - config_with_cumulative.completion_percent = existing_stats.torrent_completion; log_and_emit!( &app, instance_id, @@ -57,7 +55,7 @@ pub async fn start_faker( "Same torrent detected - continuing with cumulative stats: uploaded={} bytes, downloaded={} bytes, completion={:.1}%", existing.cumulative_uploaded, existing.cumulative_downloaded, - existing_stats.torrent_completion + config_with_cumulative.completion_percent ); } else { log_and_emit!( diff --git a/rustatio-desktop/src/commands/grid.rs b/rustatio-desktop/src/commands/grid.rs index d484279..d83a118 100644 --- a/rustatio-desktop/src/commands/grid.rs +++ b/rustatio-desktop/src/commands/grid.rs @@ -499,8 +499,6 @@ pub async fn grid_update_config( let mut new_config = faker_config.clone(); new_config.initial_uploaded = instance.cumulative_uploaded; new_config.initial_downloaded = instance.cumulative_downloaded; - let existing_stats = instance.faker.read().await.get_stats().await; - new_config.completion_percent = existing_stats.torrent_completion; let result = instance .faker @@ -551,8 +549,6 @@ pub async fn bulk_update_configs( let mut new_config = entry.config.clone(); new_config.initial_uploaded = instance.cumulative_uploaded; new_config.initial_downloaded = instance.cumulative_downloaded; - let existing_stats = instance.faker.read().await.get_stats().await; - new_config.completion_percent = existing_stats.torrent_completion; let result = instance .faker diff --git a/rustatio-server/src/services/state.rs b/rustatio-server/src/services/state.rs index da06730..cbddbc2 100644 --- a/rustatio-server/src/services/state.rs +++ b/rustatio-server/src/services/state.rs @@ -222,13 +222,6 @@ impl AppState { let mut faker_config = config.clone(); faker_config.initial_uploaded = instance.cumulative_uploaded; faker_config.initial_downloaded = instance.cumulative_downloaded; - let existing_stats = instance - .faker - .read() - .await - .stats_snapshot() - .unwrap_or_else(|| RatioFaker::stats_from_config(&instance.config)); - faker_config.completion_percent = existing_stats.torrent_completion; instance .faker @@ -274,13 +267,6 @@ impl AppState { let mut faker_config = config.clone(); faker_config.initial_uploaded = instance.cumulative_uploaded; faker_config.initial_downloaded = instance.cumulative_downloaded; - let existing_stats = instance - .faker - .read() - .await - .stats_snapshot() - .unwrap_or_else(|| RatioFaker::stats_from_config(&instance.config)); - faker_config.completion_percent = existing_stats.torrent_completion; let result = instance .faker