Added a progress counter

This commit is contained in:
daladim 2021-12-24 15:36:34 +01:00
parent 7b13d74edc
commit 7cc1e778bb
2 changed files with 36 additions and 5 deletions

View file

@ -186,8 +186,10 @@ where
let cal_name = cal_local.name().to_string(); let cal_name = cal_local.name().to_string();
progress.info(&format!("Syncing calendar {}", cal_name)); progress.info(&format!("Syncing calendar {}", cal_name));
progress.reset_counter();
progress.feedback(SyncEvent::InProgress{ progress.feedback(SyncEvent::InProgress{
calendar: cal_name.clone(), calendar: cal_name.clone(),
items_done_already: 0,
details: "started".to_string() details: "started".to_string()
}); });
@ -203,6 +205,7 @@ where
let remote_items = cal_remote.get_item_version_tags().await?; let remote_items = cal_remote.get_item_version_tags().await?;
progress.feedback(SyncEvent::InProgress{ progress.feedback(SyncEvent::InProgress{
calendar: cal_name.clone(), calendar: cal_name.clone(),
items_done_already: 0,
details: format!("{} remote items", remote_items.len()), details: format!("{} remote items", remote_items.len()),
}); });
@ -298,10 +301,13 @@ where
progress.trace("Committing changes..."); progress.trace("Committing changes...");
for url_del in local_del { for url_del in local_del {
progress.debug(&format!("> Pushing local deletion {} to the server", url_del)); progress.debug(&format!("> Pushing local deletion {} to the server", url_del));
progress.increment_counter(1);
progress.feedback(SyncEvent::InProgress{ progress.feedback(SyncEvent::InProgress{
calendar: cal_name.clone(), calendar: cal_name.clone(),
items_done_already: progress.counter(),
details: Self::item_name(&cal_local, &url_del).await, details: Self::item_name(&cal_local, &url_del).await,
}); });
match cal_remote.delete_item(&url_del).await { match cal_remote.delete_item(&url_del).await {
Err(err) => { Err(err) => {
progress.warn(&format!("Unable to delete remote item {}: {}", url_del, err)); progress.warn(&format!("Unable to delete remote item {}: {}", url_del, err));
@ -317,8 +323,10 @@ where
for url_del in remote_del { for url_del in remote_del {
progress.debug(&format!("> Applying remote deletion {} locally", url_del)); progress.debug(&format!("> Applying remote deletion {} locally", url_del));
progress.increment_counter(1);
progress.feedback(SyncEvent::InProgress{ progress.feedback(SyncEvent::InProgress{
calendar: cal_name.clone(), calendar: cal_name.clone(),
items_done_already: progress.counter(),
details: Self::item_name(&cal_local, &url_del).await, details: Self::item_name(&cal_local, &url_del).await,
}); });
if let Err(err) = cal_local.immediately_delete_item(&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 { for url_add in local_additions {
progress.debug(&format!("> Pushing local addition {} to the server", url_add)); progress.debug(&format!("> Pushing local addition {} to the server", url_add));
progress.increment_counter(1);
progress.feedback(SyncEvent::InProgress{ progress.feedback(SyncEvent::InProgress{
calendar: cal_name.clone(), calendar: cal_name.clone(),
items_done_already: progress.counter(),
details: Self::item_name(&cal_local, &url_add).await, details: Self::item_name(&cal_local, &url_add).await,
}); });
match cal_local.get_item_by_url_mut(&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 { for url_change in local_changes {
progress.debug(&format!("> Pushing local change {} to the server", url_change)); progress.debug(&format!("> Pushing local change {} to the server", url_change));
progress.increment_counter(1);
progress.feedback(SyncEvent::InProgress{ progress.feedback(SyncEvent::InProgress{
calendar: cal_name.clone(), calendar: cal_name.clone(),
items_done_already: progress.counter(),
details: Self::item_name(&cal_local, &url_change).await, details: Self::item_name(&cal_local, &url_change).await,
}); });
match cal_local.get_item_by_url_mut(&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, Some(url) => Self::item_name(&cal_local, &url).await,
None => String::from("<unable to get the name of the first batched item>"), None => String::from("<unable to get the name of the first batched item>"),
}; };
progress.increment_counter(list_of_additions.len());
progress.feedback(SyncEvent::InProgress{ progress.feedback(SyncEvent::InProgress{
calendar: cal_name.to_string(), calendar: cal_name.to_string(),
items_done_already: progress.counter(),
details: one_item_name, details: one_item_name,
}); });
}, },

View file

@ -10,7 +10,7 @@ pub enum SyncEvent {
/// Sync has just started but no calendar is handled yet /// Sync has just started but no calendar is handled yet
Started, Started,
/// Sync is in progress. /// Sync is in progress.
InProgress{ calendar: String, details: String}, InProgress{ calendar: String, items_done_already: usize, details: String},
/// Sync is finished /// Sync is finished
Finished{ success: bool }, Finished{ success: bool },
} }
@ -20,7 +20,7 @@ impl Display for SyncEvent {
match self { match self {
SyncEvent::NotStarted => write!(f, "Not started"), SyncEvent::NotStarted => write!(f, "Not started"),
SyncEvent::Started => write!(f, "Sync has 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 { SyncEvent::Finished{success} => match success {
true => write!(f, "Sync successfully finished"), true => write!(f, "Sync successfully finished"),
false => write!(f, "Sync finished with errors"), 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 /// A structure that tracks the progression and the errors that happen during a sync
pub struct SyncProgress { pub struct SyncProgress {
n_errors: u32, n_errors: u32,
feedback_channel: Option<FeedbackSender> feedback_channel: Option<FeedbackSender>,
counter: usize,
} }
impl SyncProgress { impl SyncProgress {
pub fn new() -> Self { 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 { 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 { pub fn is_success(&self) -> bool {
self.n_errors == 0 self.n_errors == 0