Skip to content

Commit 72f122f

Browse files
committed
Add progress printer to print the steps of a snapshot download in json
1 parent 88945b5 commit 72f122f

File tree

2 files changed

+56
-11
lines changed

2 files changed

+56
-11
lines changed

mithril-client/src/services/snapshot.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ use mithril_common::{
2525
use crate::{
2626
aggregator_client::{AggregatorHTTPClientError, CertificateClient, SnapshotClient},
2727
utils::{
28-
DownloadProgressReporter, ProgressOutputType, SnapshotUnpacker, SnapshotUnpackerError,
28+
DownloadProgressReporter, ProgressOutputType, ProgressPrinter, SnapshotUnpacker,
29+
SnapshotUnpackerError,
2930
},
3031
};
3132

@@ -217,15 +218,15 @@ impl SnapshotService for MithrilClientSnapshotService {
217218
debug!("Snapshot service: download.");
218219

219220
let db_dir = download_dir.join("db");
220-
let progress_bar = MultiProgress::with_draw_target(progress_output_type.into());
221-
progress_bar.println("1/7 - Checking local disk info…")?;
221+
let progress_bar = ProgressPrinter::new(progress_output_type, 7);
222+
progress_bar.report_step(1, "Checking local disk info…")?;
222223
let unpacker = SnapshotUnpacker;
223224

224225
if let Err(e) = unpacker.check_prerequisites(&db_dir, snapshot_entity.artifact.size) {
225226
self.check_disk_space_error(e)?;
226227
}
227228

228-
progress_bar.println("2/7 - Fetching the certificate's information…")?;
229+
progress_bar.report_step(2, "Fetching the certificate's information…")?;
229230
let certificate = self
230231
.certificate_client
231232
.get(&snapshot_entity.certificate_id)
@@ -236,11 +237,11 @@ impl SnapshotService for MithrilClientSnapshotService {
236237
)
237238
})?;
238239

239-
progress_bar.println("3/7 - Verifying the certificate chain…")?;
240+
progress_bar.report_step(3, "Verifying the certificate chain…")?;
240241
let verifier = self.verify_certificate_chain(genesis_verification_key, &certificate);
241242
self.wait_spinner(&progress_bar, verifier).await?;
242243

243-
progress_bar.println("4/7 - Downloading the snapshot…")?;
244+
progress_bar.report_step(4, "Downloading the snapshot…")?;
244245
let pb = progress_bar.add(ProgressBar::new(snapshot_entity.artifact.size));
245246
pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})")
246247
.unwrap()
@@ -256,7 +257,7 @@ impl SnapshotService for MithrilClientSnapshotService {
256257
.await
257258
.with_context(|| format!("Could not download file in '{}'", download_dir.display()))?;
258259

259-
progress_bar.println("5/7 - Unpacking the snapshot…")?;
260+
progress_bar.report_step(5, "Unpacking the snapshot…")?;
260261
let unpacker = unpacker.unpack_snapshot(&snapshot_path, &db_dir);
261262
self.wait_spinner(&progress_bar, unpacker).await?;
262263

@@ -268,14 +269,14 @@ impl SnapshotService for MithrilClientSnapshotService {
268269
);
269270
};
270271

271-
progress_bar.println("6/7 - Computing the snapshot digest…")?;
272+
progress_bar.report_step(6, "Computing the snapshot digest…")?;
272273
let unpacked_snapshot_digest = self
273274
.immutable_digester
274275
.compute_digest(&db_dir, &certificate.beacon)
275276
.await
276277
.with_context(|| format!("Could not compute digest in '{}'", db_dir.display()))?;
277278

278-
progress_bar.println("7/7 - Verifying the snapshot signature…")?;
279+
progress_bar.report_step(7, "Verifying the snapshot signature…")?;
279280
let expected_message = {
280281
let mut protocol_message = certificate.protocol_message.clone();
281282
protocol_message.set_message_part(

mithril-client/src/utils/progress_reporter.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
use indicatif::{ProgressBar, ProgressDrawTarget};
1+
use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget};
2+
use mithril_common::StdResult;
23
use slog_scope::warn;
34
use std::{
5+
ops::Deref,
46
sync::RwLock,
57
time::{Duration, Instant},
68
};
79

810
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9-
/// Output type of a [ProgressReporter]
11+
/// Output type of a [ProgressPrinter] or a [DownloadProgressReporter]
1012
pub enum ProgressOutputType {
1113
/// Output to json
1214
JsonReporter,
@@ -26,6 +28,48 @@ impl From<ProgressOutputType> for ProgressDrawTarget {
2628
}
2729
}
2830

31+
/// Wrapper of a indicatif [MultiProgress] to allow reporting to json.
32+
pub struct ProgressPrinter {
33+
multi_progress: MultiProgress,
34+
output_type: ProgressOutputType,
35+
number_of_steps: u16,
36+
}
37+
38+
impl ProgressPrinter {
39+
/// Instanciate a new progress printer
40+
pub fn new(output_type: ProgressOutputType, number_of_steps: u16) -> Self {
41+
Self {
42+
multi_progress: MultiProgress::with_draw_target(output_type.into()),
43+
output_type,
44+
number_of_steps,
45+
}
46+
}
47+
48+
/// Report the current step
49+
pub fn report_step(&self, step_number: u16, text: &str) -> StdResult<()> {
50+
match self.output_type {
51+
ProgressOutputType::JsonReporter => println!(
52+
r#"{{"step_num": {step_number}, "total_steps": {}, "message": "{text}"}}"#,
53+
self.number_of_steps
54+
),
55+
ProgressOutputType::TTY => self
56+
.multi_progress
57+
.println(format!("{step_number}/{} - {text}", self.number_of_steps))?,
58+
ProgressOutputType::Hidden => (),
59+
};
60+
61+
Ok(())
62+
}
63+
}
64+
65+
impl Deref for ProgressPrinter {
66+
type Target = MultiProgress;
67+
68+
fn deref(&self) -> &Self::Target {
69+
&self.multi_progress
70+
}
71+
}
72+
2973
/// Wrapper of a indicatif [ProgressBar] to allow reporting to json.
3074
pub struct DownloadProgressReporter {
3175
progress_bar: ProgressBar,

0 commit comments

Comments
 (0)