From dcf333353bc6a444b80599daf42cc47390c42483 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Sat, 10 Aug 2024 21:17:07 +0300 Subject: [PATCH] fix(tasks): ignore future stamps in time-tracking summation --- src/tasks.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/tasks.rs b/src/tasks.rs index 44f9354..64ac3c9 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -840,15 +840,19 @@ fn timestamps<'a>(events: impl Iterator, ids: &'a Vec) .skip_while(|element| element.1 == None) } +/// Iterates Events to accumulate times tracked +/// Expects a sorted iterator struct TimesTracked<'a> { events: Box + 'a>, ids: &'a Vec, + threshold: Option, } impl TimesTracked<'_> { fn from<'b>(events: impl IntoIterator + 'b, ids: &'b Vec) -> TimesTracked<'b> { TimesTracked { events: Box::new(events.into_iter()), ids, + threshold: Some(Timestamp::now()), } } } @@ -860,6 +864,9 @@ impl Iterator for TimesTracked<'_> { let mut start: Option = None; while let Some(event) = self.events.next() { if matching_tag_id(event, self.ids).is_some() { + if self.threshold.is_some_and(|th| event.created_at > th) { + continue; + } start = start.or(Some(event.created_at.as_u64())) } else { if let Some(stamp) = start { @@ -867,7 +874,8 @@ impl Iterator for TimesTracked<'_> { } } } - return start.map(|stamp| Duration::from_secs(Timestamp::now().as_u64() - stamp)); + let now = self.threshold.unwrap_or(Timestamp::now()).as_u64(); + return start.filter(|t| t < &now).map(|stamp| Duration::from_secs(now.saturating_sub(stamp))); } }