Skip to content

Commit

Permalink
tweak: improve formatting ETA and code around
Browse files Browse the repository at this point in the history
  • Loading branch information
brusherru committed Jun 24, 2024
1 parent 0a03869 commit f0888a9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
24 changes: 13 additions & 11 deletions src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::io::{Read, Seek, SeekFrom, Write};
use std::path::Path;
use std::time::Instant;

use crate::eta::Eta;
use crate::read_error_response::read_error_response;
use crate::user_agent::APP_USER_AGENT;
use crate::utils;

pub fn download_file(url: &str, file_path: &Path, redirect_path: &Path) -> Result<()> {
if let Some(dir) = file_path.parent() {
Expand Down Expand Up @@ -58,7 +58,7 @@ pub fn download_file(url: &str, file_path: &Path, redirect_path: &Path) -> Resul

const MEASUREMENT_SIZE: usize = 500;

let mut last_reported_progress: f64 = -1.0;
let mut last_reported_progress: Option<f64> = None;
let start = Instant::now();
let mut measurements = VecDeque::with_capacity(MEASUREMENT_SIZE);
let mut just_downloaded: u64 = 0;
Expand All @@ -81,26 +81,28 @@ pub fn download_file(url: &str, file_path: &Path, redirect_path: &Path) -> Resul
0.0
};
measurements.push_back(speed);
if measurements.len() > 10 {
if measurements.len() > MEASUREMENT_SIZE {
measurements.pop_front();
}
let avg_speed = measurements.iter().sum::<f64>() / measurements.len() as f64;
let eta = if avg_speed > 1.0 {
(total_size as f64 - downloaded as f64) / avg_speed
let eta = if avg_speed > 1.0 && measurements.len() > (MEASUREMENT_SIZE / 2) {
Eta::Seconds((total_size as f64 - downloaded as f64) / avg_speed)
} else {
0.0
Eta::Unknown
};

let progress = utils::to_precision(downloaded as f64 / total_size as f64 * 100.0, 2);
if progress > last_reported_progress {
let progress = downloaded as f64 / total_size as f64;
if last_reported_progress.is_none()
|| last_reported_progress.is_some_and(|x| progress > x + 0.001)
{
println!(
"Downloading... {:.2}% ({:.2} MB/{:.2} MB) ETA: {:.0} sec",
progress,
"Downloading... {:.2}% ({:.2} MB/{:.2} MB) ETA: {}",
progress * 100.0,
downloaded as f64 / 1_024_000.00,
total_size as f64 / 1_024_000.00,
eta
);
last_reported_progress = progress;
last_reported_progress = Some(progress);
}
}
Err(e) => {
Expand Down
13 changes: 13 additions & 0 deletions src/eta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pub enum Eta {
Unknown,
Seconds(f64),
}

impl std::fmt::Display for Eta {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Eta::Unknown => write!(f, "unknown"),
Eta::Seconds(s) => write!(f, "{s:.0} sec"),
}
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use url::Url;

mod checksum;
mod download;
mod eta;
mod go_spacemesh;
mod parsers;
mod read_error_response;
Expand Down
10 changes: 0 additions & 10 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,6 @@ pub fn fetch_latest_available_layer(download_url: &Url, go_version: &str) -> Res
Ok(num)
}

pub fn to_precision(number: f64, precision: u8) -> f64 {
let pow = u32::pow(10, precision as u32);
let mult: f64 = f64::from(pow);
if mult > 1.0 {
return (number * mult).round() / mult;
} else {
return number;
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit f0888a9

Please sign in to comment.