diff --git a/.github/workflows/verify.yml b/.github/workflows/verify.yml index dd5024d..1e20ba3 100644 --- a/.github/workflows/verify.yml +++ b/.github/workflows/verify.yml @@ -30,7 +30,6 @@ jobs: components: clippy - uses: Swatinem/rust-cache@v2 - run: cargo clippy --locked --all-targets --all-features --workspace -- -D warnings - - run: cargo clippy --locked --all-targets --workspace -- -D warnings clippy: runs-on: ubuntu-latest env: diff --git a/watchers/src/watchers/idle.rs b/watchers/src/watchers/idle.rs index c259abc..7f4f426 100644 --- a/watchers/src/watchers/idle.rs +++ b/watchers/src/watchers/idle.rs @@ -8,6 +8,9 @@ pub struct State { is_idle: bool, is_changed: bool, idle_timeout: Duration, + + idle_start: Option>, + idle_end: Option>, } impl State { @@ -18,6 +21,8 @@ impl State { is_idle: false, is_changed: false, idle_timeout, + idle_start: None, + idle_end: None, } } @@ -30,10 +35,14 @@ impl State { pub fn mark_not_idle(&mut self) { self.last_input_time = Utc::now(); self.set_idle(false, self.last_input_time); + + self.idle_end = self.changed_time.into(); } pub fn mark_idle(&mut self) { self.set_idle(true, Utc::now()); + + self.idle_start = self.changed_time.into(); } // The logic is rewritten from the original Python code: @@ -67,6 +76,18 @@ impl State { let now = Utc::now(); if !self.is_idle { self.last_input_time = max(now - self.idle_timeout, self.changed_time); + if let (Some(idle_start), Some(idle_end)) = (self.idle_start, self.idle_end) { + if !self.is_changed + && idle_start <= self.last_input_time + && self.last_input_time <= idle_end + { + warn!("Active time may not be accounted for."); + + // TODO: send the correct timings. + // After idle_end there is some active time for idle_timeout which may be accounted as idle time if it becomes idle soon. + return Ok(()); + } + } } self.send_ping(now, client).await @@ -79,7 +100,11 @@ impl State { ) -> anyhow::Result<()> { if self.is_changed { let result = if self.is_idle { - debug!("Reporting as changed to idle"); + debug!( + "Reporting as changed to idle for {} seconds since {}", + (now - self.last_input_time).num_seconds(), + self.last_input_time.format("%Y-%m-%d %H:%M:%S"), + ); client .ping(false, self.last_input_time, Duration::zero()) .await?; @@ -90,7 +115,10 @@ impl State { .ping(true, self.last_input_time, now - self.last_input_time) .await } else { - debug!("Reporting as no longer idle"); + debug!( + "Reporting as no longer idle at {}", + self.last_input_time.format("%Y-%m-%d %H:%M:%S") + ); client .ping(true, self.last_input_time, Duration::zero()) @@ -107,12 +135,19 @@ impl State { self.is_changed = false; result } else if self.is_idle { - trace!("Reporting as idle"); + trace!( + "Reporting as idle for {} seconds since {}", + (now - self.last_input_time).num_seconds(), + self.last_input_time.format("%Y-%m-%d %H:%M:%S"), + ); client .ping(true, self.last_input_time, now - self.last_input_time) .await } else { - trace!("Reporting as not idle"); + trace!( + "Reporting as not idle at {}", + self.last_input_time.format("%Y-%m-%d %H:%M:%S") + ); client .ping(false, self.last_input_time, Duration::zero()) .await