Skip to content

Commit

Permalink
Extract tracker service and fix reactive timings
Browse files Browse the repository at this point in the history
  • Loading branch information
2e3s committed Jul 7, 2024
1 parent 1b41533 commit ddfe972
Show file tree
Hide file tree
Showing 11 changed files with 383 additions and 228 deletions.
72 changes: 0 additions & 72 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion watchers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ 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 Down
1 change: 0 additions & 1 deletion watchers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ extern crate log;

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

pub use crate::report_client::ReportClient;
Expand Down
30 changes: 20 additions & 10 deletions watchers/src/report_client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::subscriber::IdleSubscriber;
use crate::watchers::idle::Status;

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 @@ -153,10 +152,21 @@ impl ReportClient {
.await
.with_context(|| format!("Failed to create bucket {bucket_name}"))
}
}

#[async_trait]
impl IdleSubscriber for ReportClient {
pub async fn handle_idle_status(&self, status: Status) -> anyhow::Result<()> {
match status {
Status::Idle {
changed,
last_input_time,
duration,
} => self.idle(changed, last_input_time, duration).await,
Status::Active {
changed,
last_input_time,
} => self.non_idle(changed, last_input_time).await,
}
}

async fn idle(
&self,
changed: bool,
Expand Down Expand Up @@ -191,14 +201,14 @@ impl IdleSubscriber for ReportClient {
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),
true,
last_input_time - TimeDelta::milliseconds(1),
TimeDelta::zero(),
)
.await
.await?;

self.ping(false, last_input_time, TimeDelta::zero()).await
} else {
trace!(
"Reporting as not idle at {}",
Expand Down
14 changes: 0 additions & 14 deletions watchers/src/subscriber.rs

This file was deleted.

2 changes: 1 addition & 1 deletion watchers/src/watchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod gnome_idle;
mod gnome_wayland;
#[cfg(feature = "gnome")]
mod gnome_window;
mod idle;
pub mod idle;
#[cfg(feature = "kwin_window")]
mod kwin_window;
mod wl_connection;
Expand Down
13 changes: 7 additions & 6 deletions watchers/src/watchers/gnome_idle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use super::{gnome_wayland::load_watcher, idle, Watcher};
use crate::report_client::ReportClient;
use anyhow::Context;
use async_trait::async_trait;
use chrono::Utc;
use std::sync::Arc;
use zbus::Connection;

pub struct IdleWatcher {
dbus_connection: Connection,
idle_state: idle::State,
idle_state: idle::Tracker,
}

impl IdleWatcher {
Expand Down Expand Up @@ -35,18 +36,18 @@ impl Watcher for IdleWatcher {
load_watcher(|| async move {
let mut watcher = Self {
dbus_connection: Connection::session().await?,
idle_state: idle::State::new(duration, client.clone()),
idle_state: idle::Tracker::new(Utc::now(), duration),
};
watcher.seconds_since_input().await?;
Ok(watcher)
})
.await
}

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

Ok(())
client
.handle_idle_status(self.idle_state.get_with_last_input(Utc::now(), seconds)?)
.await
}
}
Loading

0 comments on commit ddfe972

Please sign in to comment.