Skip to content

Commit

Permalink
Invert client dependency for idle watchers with basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
2e3s committed Jun 25, 2024
1 parent db3d14b commit 1b41533
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 80 deletions.
72 changes: 72 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion watchers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ path = "src/lib.rs"
[dev-dependencies]
rstest = "0.21.0"
tempfile = "3.10.1"
mockall = "0.12.1"

[dependencies]
aw-client-rust = { git = "https://github.com/ActivityWatch/aw-server-rust", rev = "bb787fd" }
Expand All @@ -32,7 +33,7 @@ gethostname = "0.4.3"
log = { workspace = true }
anyhow = { workspace = true }
async-trait = "0.1.80"
tokio = { workspace = true, features = ["time", "sync"] }
tokio = { workspace = true, features = ["time", "sync", "macros"] }

[features]
default = ["gnome", "kwin_window"]
Expand Down
1 change: 1 addition & 0 deletions watchers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate log;

pub mod config;
mod report_client;
mod subscriber;
mod watchers;

pub use crate::report_client::ReportClient;
Expand Down
57 changes: 57 additions & 0 deletions watchers/src/report_client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::subscriber::IdleSubscriber;

use super::config::Config;
use anyhow::Context;
use async_trait::async_trait;
use aw_client_rust::{AwClient, Event as AwEvent};
use chrono::{DateTime, TimeDelta, Utc};
use serde_json::{Map, Value};
Expand Down Expand Up @@ -151,3 +154,57 @@ impl ReportClient {
.with_context(|| format!("Failed to create bucket {bucket_name}"))
}
}

#[async_trait]
impl IdleSubscriber for ReportClient {
async fn idle(
&self,
changed: bool,
last_input_time: DateTime<Utc>,
duration: TimeDelta,
) -> anyhow::Result<()> {
if changed {
debug!(
"Reporting as changed to idle for {} seconds since {}",
duration.num_seconds(),
last_input_time.format("%Y-%m-%d %H:%M:%S"),
);
self.ping(false, 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.ping(true, last_input_time, duration + TimeDelta::milliseconds(1))
.await
} else {
trace!(
"Reporting as idle for {} seconds since {}",
duration.num_seconds(),
last_input_time.format("%Y-%m-%d %H:%M:%S"),
);
self.ping(true, last_input_time, duration).await
}
}

async fn non_idle(&self, changed: bool, last_input_time: DateTime<Utc>) -> anyhow::Result<()> {
if changed {
debug!(
"Reporting as no longer idle at {}",
last_input_time.format("%Y-%m-%d %H:%M:%S")
);

self.ping(true, last_input_time, TimeDelta::zero()).await?;

self.ping(
false,
last_input_time + TimeDelta::milliseconds(1),
TimeDelta::zero(),
)
.await
} else {
trace!(
"Reporting as not idle at {}",
last_input_time.format("%Y-%m-%d %H:%M:%S")
);
self.ping(false, last_input_time, TimeDelta::zero()).await
}
}
}
14 changes: 14 additions & 0 deletions watchers/src/subscriber.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use async_trait::async_trait;
use chrono::{DateTime, TimeDelta, Utc};

#[async_trait]
pub trait IdleSubscriber: Sync + Send {
async fn idle(
&self,
changed: bool,
last_input_time: DateTime<Utc>,
duration: TimeDelta,
) -> anyhow::Result<()>;

async fn non_idle(&self, changed: bool, last_input_time: DateTime<Utc>) -> anyhow::Result<()>;
}
8 changes: 3 additions & 5 deletions watchers/src/watchers/gnome_idle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,17 @@ impl Watcher for IdleWatcher {
load_watcher(|| async move {
let mut watcher = Self {
dbus_connection: Connection::session().await?,
idle_state: idle::State::new(duration),
idle_state: idle::State::new(duration, client.clone()),
};
watcher.seconds_since_input().await?;
Ok(watcher)
})
.await
}

async fn run_iteration(&mut self, client: &Arc<ReportClient>) -> anyhow::Result<()> {
async fn run_iteration(&mut self, _: &Arc<ReportClient>) -> anyhow::Result<()> {
let seconds = self.seconds_since_input().await?;
self.idle_state
.send_with_last_input(seconds, client)
.await?;
self.idle_state.send_with_last_input(seconds).await?;

Ok(())
}
Expand Down
Loading

0 comments on commit 1b41533

Please sign in to comment.