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();
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("<unable to get the name of the first batched item>"),
};
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,
});
},

View file

@ -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<FeedbackSender>
feedback_channel: Option<FeedbackSender>,
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