Skip to content

Commit

Permalink
feat: add total progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
antheas committed Nov 25, 2024
1 parent 7c8121a commit 6b4cdaf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
22 changes: 17 additions & 5 deletions lib/src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ async fn handle_layer_progress_print(
mut layers: tokio::sync::mpsc::Receiver<ostree_container::store::ImportProgress>,
mut layer_bytes: tokio::sync::watch::Receiver<Option<ostree_container::store::LayerProgress>>,
n_layers_to_fetch: usize,
download_bytes: u64,
) {
let start = std::time::Instant::now();
let mut total_read = 0u64;
Expand All @@ -150,23 +151,30 @@ async fn handle_layer_progress_print(
n_layers_to_fetch.try_into().unwrap(),
));
let byte_bar = bar.add(indicatif::ProgressBar::new(0));
let total_byte_bar = bar.add(indicatif::ProgressBar::new(download_bytes));
// let byte_bar = indicatif::ProgressBar::new(0);
// byte_bar.set_draw_target(indicatif::ProgressDrawTarget::hidden());
println!("");
layers_bar.set_style(
indicatif::ProgressStyle::default_bar()
.template("{prefix} {bar} {pos}/{len} {wide_msg}")
.template("{prefix} {pos}/{len} {bar:15}")
.unwrap(),
);
layers_bar.set_prefix("Fetching layers");
layers_bar.set_prefix("Fetched Layers");
layers_bar.set_message("");
byte_bar.set_prefix("Fetching");
byte_bar.set_style(
indicatif::ProgressStyle::default_bar()
.template(
" └ {prefix} {bar} {binary_bytes}/{binary_total_bytes} ({binary_bytes_per_sec}) {wide_msg}",
" └ {bar:20} {msg} ({binary_bytes}/{binary_total_bytes})",
)
.unwrap()
);
total_byte_bar.set_prefix("Total");
total_byte_bar.set_style(
indicatif::ProgressStyle::default_bar()
.template("\n{prefix} {bar:30} {binary_bytes}/{binary_total_bytes} ({binary_bytes_per_sec}, {elapsed}/{duration})")
.unwrap(),
);
loop {
tokio::select! {
// Always handle layer changes first.
Expand All @@ -186,6 +194,7 @@ async fn handle_layer_progress_print(
byte_bar.set_position(layer_size);
layers_bar.inc(1);
total_read = total_read.saturating_add(layer_size);
total_byte_bar.set_position(total_read);
}
} else {
// If the receiver is disconnected, then we're done
Expand All @@ -200,6 +209,7 @@ async fn handle_layer_progress_print(
let bytes = layer_bytes.borrow();
if let Some(bytes) = &*bytes {
byte_bar.set_position(bytes.fetched);
total_byte_bar.set_position(total_read + bytes.fetched);
}
}
}
Expand Down Expand Up @@ -250,11 +260,13 @@ pub(crate) async fn pull(
ostree_ext::cli::print_layer_status(&prep);
let layers_to_fetch = prep.layers_to_fetch().collect::<Result<Vec<_>>>()?;
let n_layers_to_fetch = layers_to_fetch.len();
let download_bytes: u64 = layers_to_fetch.iter().map(|(l, _)| l.layer.size()).sum();

let printer = (!quiet).then(|| {
let layer_progress = imp.request_progress();
let layer_byte_progress = imp.request_layer_progress();
tokio::task::spawn(async move {
handle_layer_progress_print(layer_progress, layer_byte_progress, n_layers_to_fetch)
handle_layer_progress_print(layer_progress, layer_byte_progress, n_layers_to_fetch, download_bytes)
.await
})
});
Expand Down
2 changes: 1 addition & 1 deletion ostree-ext/src/container/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub enum PrepareResult {
#[derive(Debug)]
pub struct ManifestLayerState {
/// The underlying layer descriptor.
pub(crate) layer: oci_image::Descriptor,
pub layer: oci_image::Descriptor,
// TODO semver: Make this readonly via an accessor
/// The ostree ref name for this layer.
pub ostree_ref: String,
Expand Down

0 comments on commit 6b4cdaf

Please sign in to comment.