From b81e5a27bf0059e7a7951886aeaf02905ef50a66 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Sat, 9 Nov 2024 20:00:06 +0100 Subject: [PATCH] fix(main): retain current movement when tracking for another time --- src/main.rs | 12 ++++++------ src/tasks.rs | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index b796825..37b96cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ use std::fs; use std::fs::File; use std::io::{BufRead, BufReader, Write}; use std::iter::once; -use std::ops::Sub; +use std::ops::{Add, Sub}; use std::path::PathBuf; use std::str::FromStr; use std::time::Duration; @@ -81,11 +81,12 @@ impl EventSender { } } + // TODO this direly needs testing fn submit(&self, event_builder: EventBuilder) -> Result { + let min = Timestamp::now().sub(UNDO_DELAY); { // Always flush if oldest event older than a minute or newer than now let borrow = self.queue.borrow(); - let min = Timestamp::now().sub(UNDO_DELAY); if borrow.iter().any(|e| e.created_at < min || e.created_at > Timestamp::now()) { drop(borrow); debug!("Flushing event queue because it is older than a minute"); @@ -94,10 +95,9 @@ impl EventSender { } let mut queue = self.queue.borrow_mut(); Ok(event_builder.to_event(&self.keys).inspect(|event| { - if event.kind == TRACKING_KIND { - queue.retain(|e| { - e.kind != TRACKING_KIND - }); + if event.kind == TRACKING_KIND && event.created_at > min && event.created_at < tasks::now() { + // Do not send redundant movements + queue.retain(|e| e.kind != TRACKING_KIND); } queue.push(event.clone()); })?) diff --git a/src/tasks.rs b/src/tasks.rs index b23dbd3..d23ccdc 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -24,7 +24,7 @@ pub const HIGH_PRIO: Prio = 85; /// Amount of seconds to treat as "now" const MAX_OFFSET: u64 = 9; -fn now() -> Timestamp { +pub(crate) fn now() -> Timestamp { Timestamp::now() + MAX_OFFSET }