From db3d14bf4a19698cd504be567ae8b06459d7a916 Mon Sep 17 00:00:00 2001 From: Demmie <2e3s19@gmail.com> Date: Mon, 24 Jun 2024 01:42:56 -0400 Subject: [PATCH] Replace Duration by TimeDelta for Chrono This simplifies the code. --- src/main.rs | 9 ++++++--- watchers/src/config.rs | 2 +- watchers/src/config/file_config.rs | 15 +++++++------- watchers/src/report_client.rs | 20 ++++++++++--------- watchers/src/watchers.rs | 4 ++-- watchers/src/watchers/gnome_idle.rs | 3 +-- watchers/src/watchers/idle.rs | 20 +++++++++---------- watchers/src/watchers/wl_ext_idle_notify.rs | 8 ++++---- watchers/src/watchers/wl_kwin_idle.rs | 8 ++++---- watchers/src/watchers/x11_screensaver_idle.rs | 5 +---- 10 files changed, 48 insertions(+), 46 deletions(-) diff --git a/src/main.rs b/src/main.rs index 82fab75..bf2b944 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,14 +29,17 @@ async fn main() -> anyhow::Result<(), Box> { } else { info!("Sending to server {}:{}", config.host, config.port); } - info!("Idle timeout: {} seconds", config.idle_timeout.as_secs()); + info!( + "Idle timeout: {} seconds", + config.idle_timeout.num_seconds() + ); info!( "Idle polling period: {} seconds", - config.poll_time_idle.as_secs() + config.poll_time_idle.num_seconds() ); info!( "Window polling period: {} seconds", - config.poll_time_window.as_secs() + config.poll_time_window.num_seconds() ); #[cfg(feature = "bundle")] let (shutdown_send, mut shutdown_recv) = mpsc::unbounded_channel(); diff --git a/watchers/src/config.rs b/watchers/src/config.rs index 0ebe7cb..715d078 100644 --- a/watchers/src/config.rs +++ b/watchers/src/config.rs @@ -3,8 +3,8 @@ mod file_config; mod filters; use self::filters::{Filter, Replacement}; +use chrono::Duration; pub use file_config::FileConfig; -use std::time::Duration; pub struct Config { pub port: u16, diff --git a/watchers/src/config/file_config.rs b/watchers/src/config/file_config.rs index 7554509..464f9d2 100644 --- a/watchers/src/config/file_config.rs +++ b/watchers/src/config/file_config.rs @@ -1,7 +1,8 @@ use anyhow::{anyhow, Context}; +use chrono::TimeDelta; use serde::Deserialize; use serde_default::DefaultFromSerde; -use std::{fs, io::ErrorKind, path::PathBuf, time::Duration}; +use std::{fs, io::ErrorKind, path::PathBuf}; use crate::config::defaults; @@ -71,16 +72,16 @@ pub struct ClientConfig { } impl ClientConfig { - pub fn get_idle_timeout(&self) -> Duration { - Duration::from_secs(u64::from(self.idle_timeout_seconds)) + pub fn get_idle_timeout(&self) -> TimeDelta { + TimeDelta::seconds(self.idle_timeout_seconds.into()) } - pub fn get_poll_time_idle(&self) -> Duration { - Duration::from_secs(u64::from(self.poll_time_idle_seconds)) + pub fn get_poll_time_idle(&self) -> TimeDelta { + TimeDelta::seconds(self.poll_time_idle_seconds.into()) } - pub fn get_poll_time_window(&self) -> Duration { - Duration::from_secs(u64::from(self.poll_time_window_seconds)) + pub fn get_poll_time_window(&self) -> TimeDelta { + TimeDelta::seconds(self.poll_time_window_seconds.into()) } } diff --git a/watchers/src/report_client.rs b/watchers/src/report_client.rs index e80ce53..4858b3b 100644 --- a/watchers/src/report_client.rs +++ b/watchers/src/report_client.rs @@ -1,7 +1,7 @@ use super::config::Config; use anyhow::Context; use aw_client_rust::{AwClient, Event as AwEvent}; -use chrono::{DateTime, Duration, Utc}; +use chrono::{DateTime, TimeDelta, Utc}; use serde_json::{Map, Value}; use std::error::Error; use std::future::Future; @@ -61,7 +61,7 @@ impl ReportClient { &self, is_idle: bool, timestamp: DateTime, - duration: Duration, + duration: TimeDelta, ) -> anyhow::Result<()> { let mut data = Map::new(); data.insert( @@ -80,11 +80,10 @@ impl ReportClient { return Ok(()); } - let pulsetime = (self.config.idle_timeout + self.config.poll_time_idle).as_secs_f64(); - + let pulsetime = (self.config.idle_timeout + self.config.poll_time_idle).num_seconds(); let request = || { self.client - .heartbeat(&self.idle_bucket_name, &event, pulsetime) + .heartbeat(&self.idle_bucket_name, &event, pulsetime as f64) }; Self::run_with_retries(request) @@ -118,7 +117,7 @@ impl ReportClient { let event = AwEvent { id: None, timestamp: Utc::now(), - duration: Duration::zero(), + duration: TimeDelta::zero(), data, }; @@ -126,10 +125,13 @@ impl ReportClient { return Ok(()); } - let interval_margin = self.config.poll_time_window.as_secs_f64() + 1.0; + let interval_margin = self.config.poll_time_window.num_seconds() + 1; let request = || { - self.client - .heartbeat(&self.active_window_bucket_name, &event, interval_margin) + self.client.heartbeat( + &self.active_window_bucket_name, + &event, + interval_margin as f64, + ) }; Self::run_with_retries(request) diff --git a/watchers/src/watchers.rs b/watchers/src/watchers.rs index c1137ce..b99328d 100644 --- a/watchers/src/watchers.rs +++ b/watchers/src/watchers.rs @@ -28,8 +28,8 @@ pub enum WatcherType { impl WatcherType { fn sleep_time(&self, config: &Config) -> Duration { match self { - WatcherType::Idle => config.poll_time_idle, - WatcherType::ActiveWindow => config.poll_time_window, + WatcherType::Idle => config.poll_time_idle.to_std().unwrap(), + WatcherType::ActiveWindow => config.poll_time_window.to_std().unwrap(), } } } diff --git a/watchers/src/watchers/gnome_idle.rs b/watchers/src/watchers/gnome_idle.rs index 7e0b014..3d0c81f 100644 --- a/watchers/src/watchers/gnome_idle.rs +++ b/watchers/src/watchers/gnome_idle.rs @@ -2,7 +2,6 @@ use super::{gnome_wayland::load_watcher, idle, Watcher}; use crate::report_client::ReportClient; use anyhow::Context; use async_trait::async_trait; -use chrono::Duration; use std::sync::Arc; use zbus::Connection; @@ -32,7 +31,7 @@ impl IdleWatcher { #[async_trait] impl Watcher for IdleWatcher { async fn new(client: &Arc) -> anyhow::Result { - let duration = Duration::from_std(client.config.idle_timeout).unwrap(); + let duration = client.config.idle_timeout; load_watcher(|| async move { let mut watcher = Self { dbus_connection: Connection::session().await?, diff --git a/watchers/src/watchers/idle.rs b/watchers/src/watchers/idle.rs index 7f4f426..7da218b 100644 --- a/watchers/src/watchers/idle.rs +++ b/watchers/src/watchers/idle.rs @@ -1,5 +1,5 @@ use crate::report_client::ReportClient; -use chrono::{DateTime, Duration, Utc}; +use chrono::{DateTime, TimeDelta, Utc}; use std::{cmp::max, sync::Arc}; pub struct State { @@ -7,14 +7,14 @@ pub struct State { changed_time: DateTime, is_idle: bool, is_changed: bool, - idle_timeout: Duration, + idle_timeout: TimeDelta, idle_start: Option>, idle_end: Option>, } impl State { - pub fn new(idle_timeout: Duration) -> Self { + pub fn new(idle_timeout: TimeDelta) -> Self { Self { last_input_time: Utc::now(), changed_time: Utc::now(), @@ -53,7 +53,7 @@ impl State { client: &Arc, ) -> anyhow::Result<()> { let now = Utc::now(); - let time_since_input = Duration::seconds(i64::from(seconds_since_input)); + let time_since_input = TimeDelta::seconds(i64::from(seconds_since_input)); self.last_input_time = now - time_since_input; @@ -106,11 +106,11 @@ impl State { self.last_input_time.format("%Y-%m-%d %H:%M:%S"), ); client - .ping(false, self.last_input_time, Duration::zero()) + .ping(false, self.last_input_time, TimeDelta::zero()) .await?; // ping with timestamp+1ms with the next event (to ensure the latest event gets retrieved by get_event) - self.last_input_time += Duration::milliseconds(1); + self.last_input_time += TimeDelta::milliseconds(1); client .ping(true, self.last_input_time, now - self.last_input_time) .await @@ -121,14 +121,14 @@ impl State { ); client - .ping(true, self.last_input_time, Duration::zero()) + .ping(true, self.last_input_time, TimeDelta::zero()) .await?; client .ping( false, - self.last_input_time + Duration::milliseconds(1), - Duration::zero(), + self.last_input_time + TimeDelta::milliseconds(1), + TimeDelta::zero(), ) .await }; @@ -149,7 +149,7 @@ impl State { self.last_input_time.format("%Y-%m-%d %H:%M:%S") ); client - .ping(false, self.last_input_time, Duration::zero()) + .ping(false, self.last_input_time, TimeDelta::zero()) .await } } diff --git a/watchers/src/watchers/wl_ext_idle_notify.rs b/watchers/src/watchers/wl_ext_idle_notify.rs index c923b59..5e41a6a 100644 --- a/watchers/src/watchers/wl_ext_idle_notify.rs +++ b/watchers/src/watchers/wl_ext_idle_notify.rs @@ -4,7 +4,7 @@ use super::Watcher; use crate::report_client::ReportClient; use anyhow::anyhow; use async_trait::async_trait; -use chrono::Duration; +use chrono::TimeDelta; use std::sync::Arc; use wayland_client::{ globals::GlobalListContents, @@ -28,7 +28,7 @@ impl Drop for WatcherState { } impl WatcherState { - fn new(idle_notification: ExtIdleNotificationV1, idle_timeout: Duration) -> Self { + fn new(idle_notification: ExtIdleNotificationV1, idle_timeout: TimeDelta) -> Self { Self { idle_notification, idle_state: idle::State::new(idle_timeout), @@ -79,12 +79,12 @@ impl Watcher for IdleWatcher { let mut connection: WlEventConnection = WlEventConnection::connect()?; connection.get_ext_idle()?; - let timeout = u32::try_from(client.config.idle_timeout.as_secs() * 1000); + let timeout = u32::try_from(client.config.idle_timeout.num_milliseconds()); let mut watcher_state = WatcherState::new( connection .get_ext_idle_notification(timeout.unwrap()) .unwrap(), - Duration::from_std(client.config.idle_timeout).unwrap(), + client.config.idle_timeout, ); connection .event_queue diff --git a/watchers/src/watchers/wl_kwin_idle.rs b/watchers/src/watchers/wl_kwin_idle.rs index 551a288..b6ac3e1 100644 --- a/watchers/src/watchers/wl_kwin_idle.rs +++ b/watchers/src/watchers/wl_kwin_idle.rs @@ -4,7 +4,7 @@ use super::Watcher; use crate::report_client::ReportClient; use anyhow::anyhow; use async_trait::async_trait; -use chrono::Duration; +use chrono::TimeDelta; use std::sync::Arc; use wayland_client::{ globals::GlobalListContents, @@ -29,7 +29,7 @@ impl Drop for WatcherState { } impl WatcherState { - fn new(kwin_idle_timeout: OrgKdeKwinIdleTimeout, idle_timeout: Duration) -> Self { + fn new(kwin_idle_timeout: OrgKdeKwinIdleTimeout, idle_timeout: TimeDelta) -> Self { Self { kwin_idle_timeout, idle_state: idle::State::new(idle_timeout), @@ -80,10 +80,10 @@ impl Watcher for IdleWatcher { let mut connection: WlEventConnection = WlEventConnection::connect()?; connection.get_kwin_idle()?; - let timeout = u32::try_from(client.config.idle_timeout.as_secs() * 1000); + let timeout = u32::try_from(client.config.idle_timeout.num_milliseconds()); let mut watcher_state = WatcherState::new( connection.get_kwin_idle_timeout(timeout.unwrap()).unwrap(), - Duration::from_std(client.config.idle_timeout).unwrap(), + client.config.idle_timeout, ); connection .event_queue diff --git a/watchers/src/watchers/x11_screensaver_idle.rs b/watchers/src/watchers/x11_screensaver_idle.rs index ea3d748..36b2cda 100644 --- a/watchers/src/watchers/x11_screensaver_idle.rs +++ b/watchers/src/watchers/x11_screensaver_idle.rs @@ -1,5 +1,4 @@ use async_trait::async_trait; -use chrono::Duration; use super::{idle, x11_connection::X11Client, Watcher}; use crate::report_client::ReportClient; @@ -26,9 +25,7 @@ impl Watcher for IdleWatcher { Ok(IdleWatcher { client, - idle_state: idle::State::new( - Duration::from_std(report_client.config.idle_timeout).unwrap(), - ), + idle_state: idle::State::new(report_client.config.idle_timeout), }) }