Skip to content

Commit

Permalink
feat: add progress bars to sync and store init (#1684)
Browse files Browse the repository at this point in the history
Replace lots of logging with some progress bars. This looks much nicer

I'd like to move it out of the atuin-client crate and into the atuin
crate. But first, I want to decouple a lot of the record moving, so it
can wait until that's done.
  • Loading branch information
ellie committed Feb 8, 2024
1 parent 1993653 commit 8460210
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atuin-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ urlencoding = { version = "2.1.0", optional = true }
reqwest = { workspace = true, optional = true }
hex = { version = "0.4", optional = true }
sha2 = { version = "0.10", optional = true }
indicatif = "0.17.7"

[dev-dependencies]
tokio = { version = "1", features = ["full"] }
Expand Down
17 changes: 14 additions & 3 deletions atuin-client/src/history/store.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashSet;
use std::{collections::HashSet, fmt::Write};

use eyre::{bail, eyre, Result};
use indicatif::{ProgressBar, ProgressState, ProgressStyle};
use rmp::decode::Bytes;

use crate::{
Expand Down Expand Up @@ -263,11 +264,17 @@ impl HistoryStore {
println!("Fetching history already in store");
let store_ids = self.history_ids().await?;

let pb = ProgressBar::new(history.len() as u64);
pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {human_pos}/{human_len} ({eta})")
.unwrap()
.with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap())
.progress_chars("#>-"));

for i in history {
println!("loaded {}", i.id);
debug!("loaded {}", i.id);

if store_ids.contains(&i.id) {
println!("skipping {} - already exists", i.id);
debug!("skipping {} - already exists", i.id);
continue;
}

Expand All @@ -277,8 +284,12 @@ impl HistoryStore {
} else {
self.push(i).await?;
}

pb.inc(1);
}

pb.finish_with_message("Import complete");

Ok(())
}
}
Expand Down
34 changes: 20 additions & 14 deletions atuin-client/src/record/sync.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// do a sync :O
use std::cmp::Ordering;
use std::{cmp::Ordering, fmt::Write};

use eyre::Result;
use thiserror::Error;
Expand All @@ -8,6 +8,7 @@ use super::store::Store;
use crate::{api_client::Client, settings::Settings};

use atuin_common::record::{Diff, HostId, RecordId, RecordIdx, RecordStatus};
use indicatif::{ProgressBar, ProgressState, ProgressStyle};

#[derive(Error, Debug)]
pub enum SyncError {
Expand Down Expand Up @@ -165,6 +166,12 @@ async fn sync_upload(
let upload_page_size = 100;
let mut progress = 0;

let pb = ProgressBar::new(expected);
pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {human_pos}/{human_len} ({eta})")
.unwrap()
.with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap())
.progress_chars("#>-"));

println!(
"Uploading {} records to {}/{}",
expected,
Expand All @@ -189,19 +196,16 @@ async fn sync_upload(
SyncError::RemoteRequestError { msg: e.to_string() }
})?;

println!(
"uploaded {} to remote, progress {}/{}",
page.len(),
progress,
expected
);
pb.set_position(progress);
progress += page.len() as u64;

if progress >= expected {
break;
}
}

pb.finish_with_message("Uploaded records");

Ok(progress as i64)
}

Expand All @@ -226,6 +230,12 @@ async fn sync_download(
tag
);

let pb = ProgressBar::new(expected);
pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {human_pos}/{human_len} ({eta})")
.unwrap()
.with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap())
.progress_chars("#>-"));

// preload with the first entry if remote does not know of this store
loop {
let page = client
Expand All @@ -238,22 +248,18 @@ async fn sync_download(
.await
.map_err(|e| SyncError::LocalStoreError { msg: e.to_string() })?;

println!(
"downloaded {} records from remote, progress {}/{}",
page.len(),
progress,
expected
);

ret.extend(page.iter().map(|f| f.id));

pb.set_position(progress);
progress += page.len() as u64;

if progress >= expected {
break;
}
}

pb.finish_with_message("Downloaded records");

Ok(ret)
}

Expand Down

0 comments on commit 8460210

Please sign in to comment.