From 794546168238eca6300a9088204ad59ef917395e Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Mon, 4 Nov 2024 20:26:55 +0100 Subject: fix(yt/download): Only print changed bytes sizes, on changed string 2_000_000_000 and 2_000_000_001 cache sizes are not the same, but will both print out "1.86 GiB". Obviously, notifying the user about this change is rather counter-productive. --- crates/bytes/src/lib.rs | 2 +- yt/src/download/mod.rs | 44 +++++++++++++++++++++++++++++--------------- yt/src/status/mod.rs | 2 +- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/crates/bytes/src/lib.rs b/crates/bytes/src/lib.rs index 6e3f73c..2a9248d 100644 --- a/crates/bytes/src/lib.rs +++ b/crates/bytes/src/lib.rs @@ -34,7 +34,7 @@ const TB: u64 = 1000 * GB; pub mod error; pub mod serde; -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Ord, Eq)] pub struct Bytes(u64); impl Bytes { diff --git a/yt/src/download/mod.rs b/yt/src/download/mod.rs index 5032b0f..8bd8a75 100644 --- a/yt/src/download/mod.rs +++ b/yt/src/download/mod.rs @@ -72,7 +72,7 @@ pub struct Downloader { current_download: Option, video_size_cache: HashMap, printed_warning: bool, - cached_cache_allocation: Option, + cached_cache_allocation: Option, } impl Default for Downloader { @@ -121,30 +121,44 @@ impl Downloader { return Ok(CacheSizeCheck::ExceedsMaxCacheSize); } - if cache_allocation + video_size >= max_cache_size { + if cache_allocation.as_u64() + video_size >= max_cache_size { if !self.printed_warning { warn!( "Can't download video: '{}' ({}) as it's too large for the cache ({} of {} allocated). \ Waiting for cache size reduction..", - next_video.title, Bytes::new(video_size), Bytes::new(cache_allocation), Bytes::new(max_cache_size) + next_video.title, Bytes::new(video_size), &cache_allocation, Bytes::new(max_cache_size) ); self.printed_warning = true; + + // Update this value immediately. + // This avoids printing the "Current cache size has changed .." warning below. self.cached_cache_allocation = Some(cache_allocation); } + if let Some(cca) = self.cached_cache_allocation { if cca != cache_allocation { - warn!( - "Current cache size has changed, it's now: '{}'", - Bytes::new(cache_allocation) + // Only print the warning if the display string has actually changed. + // Otherwise, we might confuse the user + if cca.to_string() != cache_allocation.to_string() { + warn!( + "Current cache size has changed, it's now: '{}'", + cache_allocation + ); + } + debug!( + "Cache size has changed: {} -> {}", + cca.as_u64(), + cache_allocation.as_u64() ); self.cached_cache_allocation = Some(cache_allocation); } } else { - info!( - "Current cache size allocation: '{}'", - Bytes::new(cache_allocation) + unreachable!( + "The `printed_warning` should be false in this case, \ + and thus should have already set the `cached_cache_allocation`." ); - self.cached_cache_allocation = Some(cache_allocation); + // info!("Current cache size allocation: '{}'", cache_allocation); + // self.cached_cache_allocation = Some(cache_allocation); } // Wait and hope, that a large video is deleted from the cache. @@ -213,8 +227,8 @@ impl Downloader { Ok(()) } - pub async fn get_current_cache_allocation(app: &App) -> Result { - fn dir_size(mut dir: fs::ReadDir) -> BoxFuture<'static, Result> { + pub async fn get_current_cache_allocation(app: &App) -> Result { + fn dir_size(mut dir: fs::ReadDir) -> BoxFuture<'static, Result> { async move { let mut acc = 0; while let Some(entry) = dir.next_entry().await? { @@ -223,13 +237,13 @@ impl Downloader { let path = entry.path(); let read_dir = fs::read_dir(path).await?; - dir_size(read_dir).await? + dir_size(read_dir).await?.as_u64() } data => data.len(), }; acc += size; } - Ok(acc) + Ok(Bytes::new(acc)) } .boxed() } @@ -254,7 +268,7 @@ impl Downloader { // The new dir should not contain anything (otherwise we would not have had to // create it) - return Ok(0); + return Ok(Bytes::new(0)); } err => Err(io::Error::from(err)).with_context(|| { format!( diff --git a/yt/src/status/mod.rs b/yt/src/status/mod.rs index 474cab7..2f7db25 100644 --- a/yt/src/status/mod.rs +++ b/yt/src/status/mod.rs @@ -78,7 +78,7 @@ pub async fn show(app: &App) -> Result<()> { let cache_usage_raw = Downloader::get_current_cache_allocation(app) .await .context("Failed to get current cache allocation")?; - let cache_usage = Bytes::new(cache_usage_raw); + let cache_usage: Bytes = cache_usage_raw; println!( "\ Picked Videos: {picked_videos_len} ({picked_videos_changing} changing) -- cgit 1.4.1