From 7cc1e778bbfff8700584b64f0a00b810bf163d04 Mon Sep 17 00:00:00 2001 From: daladim Date: Fri, 24 Dec 2021 15:36:34 +0100 Subject: [PATCH] Added a progress counter --- src/provider/mod.rs | 14 ++++++++++++++ src/provider/sync_progress.rs | 27 ++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/provider/mod.rs b/src/provider/mod.rs index 85d208c..c73b503 100644 --- a/src/provider/mod.rs +++ b/src/provider/mod.rs @@ -186,8 +186,10 @@ where let cal_name = cal_local.name().to_string(); progress.info(&format!("Syncing calendar {}", cal_name)); + progress.reset_counter(); progress.feedback(SyncEvent::InProgress{ calendar: cal_name.clone(), + items_done_already: 0, details: "started".to_string() }); @@ -203,6 +205,7 @@ where let remote_items = cal_remote.get_item_version_tags().await?; progress.feedback(SyncEvent::InProgress{ calendar: cal_name.clone(), + items_done_already: 0, details: format!("{} remote items", remote_items.len()), }); @@ -298,10 +301,13 @@ where progress.trace("Committing changes..."); for url_del in local_del { progress.debug(&format!("> Pushing local deletion {} to the server", url_del)); + progress.increment_counter(1); progress.feedback(SyncEvent::InProgress{ calendar: cal_name.clone(), + items_done_already: progress.counter(), details: Self::item_name(&cal_local, &url_del).await, }); + match cal_remote.delete_item(&url_del).await { Err(err) => { progress.warn(&format!("Unable to delete remote item {}: {}", url_del, err)); @@ -317,8 +323,10 @@ where for url_del in remote_del { progress.debug(&format!("> Applying remote deletion {} locally", url_del)); + progress.increment_counter(1); progress.feedback(SyncEvent::InProgress{ calendar: cal_name.clone(), + items_done_already: progress.counter(), details: Self::item_name(&cal_local, &url_del).await, }); if let Err(err) = cal_local.immediately_delete_item(&url_del).await { @@ -345,8 +353,10 @@ where for url_add in local_additions { progress.debug(&format!("> Pushing local addition {} to the server", url_add)); + progress.increment_counter(1); progress.feedback(SyncEvent::InProgress{ calendar: cal_name.clone(), + items_done_already: progress.counter(), details: Self::item_name(&cal_local, &url_add).await, }); match cal_local.get_item_by_url_mut(&url_add).await { @@ -368,8 +378,10 @@ where for url_change in local_changes { progress.debug(&format!("> Pushing local change {} to the server", url_change)); + progress.increment_counter(1); progress.feedback(SyncEvent::InProgress{ calendar: cal_name.clone(), + items_done_already: progress.counter(), details: Self::item_name(&cal_local, &url_change).await, }); match cal_local.get_item_by_url_mut(&url_change).await { @@ -460,8 +472,10 @@ where Some(url) => Self::item_name(&cal_local, &url).await, None => String::from(""), }; + progress.increment_counter(list_of_additions.len()); progress.feedback(SyncEvent::InProgress{ calendar: cal_name.to_string(), + items_done_already: progress.counter(), details: one_item_name, }); }, diff --git a/src/provider/sync_progress.rs b/src/provider/sync_progress.rs index d27c51e..5cd1a11 100644 --- a/src/provider/sync_progress.rs +++ b/src/provider/sync_progress.rs @@ -10,7 +10,7 @@ pub enum SyncEvent { /// Sync has just started but no calendar is handled yet Started, /// Sync is in progress. - InProgress{ calendar: String, details: String}, + InProgress{ calendar: String, items_done_already: usize, details: String}, /// Sync is finished Finished{ success: bool }, } @@ -20,7 +20,7 @@ impl Display for SyncEvent { match self { SyncEvent::NotStarted => write!(f, "Not started"), SyncEvent::Started => write!(f, "Sync has started..."), - SyncEvent::InProgress{calendar, details} => write!(f, "[{}] {}...", calendar, details), + SyncEvent::InProgress{calendar, items_done_already, details} => write!(f, "{} [{}/?] {}...", calendar, items_done_already, details), SyncEvent::Finished{success} => match success { true => write!(f, "Sync successfully finished"), false => write!(f, "Sync finished with errors"), @@ -53,16 +53,33 @@ pub fn feedback_channel() -> (FeedbackSender, FeedbackReceiver) { /// A structure that tracks the progression and the errors that happen during a sync pub struct SyncProgress { n_errors: u32, - feedback_channel: Option + feedback_channel: Option, + counter: usize, } impl SyncProgress { pub fn new() -> Self { - Self { n_errors: 0, feedback_channel: None } + Self { n_errors: 0, feedback_channel: None, counter: 0 } } pub fn new_with_feedback_channel(channel: FeedbackSender) -> Self { - Self { n_errors: 0, feedback_channel: Some(channel) } + Self { n_errors: 0, feedback_channel: Some(channel), counter: 0 } } + /// Reset the user-info counter + pub fn reset_counter(&mut self) { + self.counter = 0; + } + /// Increments the user-info counter. + pub fn increment_counter(&mut self, increment: usize) { + self.counter += increment; + } + /// Retrieves the current user-info counter. + /// This counts "arbitrary things", that's provided as a convenience but it is not used internally + /// (e.g. that can be used to keep track of the items handled for the current calendar) + pub fn counter(&self) -> usize { + self.counter + } + + pub fn is_success(&self) -> bool { self.n_errors == 0