Add a mock server option

This commit is contained in:
Demmie 2023-04-22 19:27:06 -04:00
parent 69130e47a8
commit 97a5867df7
No known key found for this signature in database
GPG Key ID: B06DAA3D432C6E9A
3 changed files with 31 additions and 10 deletions

View File

@ -3,7 +3,7 @@ mod file_config;
mod filters;
use crate::BoxedError;
use clap::{arg, value_parser, Command};
use clap::{arg, value_parser, ArgAction, Command};
use file_config::FileConfig;
use std::{path::PathBuf, time::Duration};
@ -17,6 +17,7 @@ pub struct Config {
pub poll_time_window: Duration,
pub idle_bucket_name: String,
pub active_window_bucket_name: String,
pub mock_server: bool,
filters: Vec<Filter>,
}
@ -42,6 +43,9 @@ impl Config {
arg!(--"poll-time-window" <SECONDS> "Period between sending heartbeats to the server for idle activity")
.value_parser(value_parser!(u32))
.default_value(defaults::poll_time_window_seconds().to_string()),
arg!(--"mock-server" "Don't communicate to the ActivityWatch server")
.value_parser(value_parser!(bool))
.action(ArgAction::SetTrue),
])
.get_matches();
@ -60,6 +64,7 @@ impl Config {
idle_bucket_name,
active_window_bucket_name,
filters: config.client.filters,
mock_server: *matches.get_one("mock-server").unwrap(),
})
}

View File

@ -106,10 +106,17 @@ fn main() -> Result<(), BoxedError> {
let client = ReportClient::new(Config::from_cli()?)?;
let client = Arc::new(client);
info!(
"Sending to server {}:{}",
client.config.host, client.config.port
);
if client.config.mock_server {
warn!(
"Not sending to server {}:{}",
client.config.host, client.config.port
);
} else {
info!(
"Sending to server {}:{}",
client.config.host, client.config.port
);
}
info!(
"Idle timeout: {} seconds",
client.config.idle_timeout.as_secs()

View File

@ -14,8 +14,11 @@ pub struct ReportClient {
impl ReportClient {
pub fn new(config: Config) -> Result<Self, BoxedError> {
let client = AwClient::new(&config.host, &config.port.to_string(), "awatcher");
Self::create_bucket(&client, &config.idle_bucket_name, "afkstatus")?;
Self::create_bucket(&client, &config.active_window_bucket_name, "currentwindow")?;
if !config.mock_server {
Self::create_bucket(&client, &config.idle_bucket_name, "afkstatus")?;
Self::create_bucket(&client, &config.active_window_bucket_name, "currentwindow")?;
}
Ok(Self { client, config })
}
@ -39,12 +42,14 @@ impl ReportClient {
data,
};
if self.config.mock_server {
return Ok(());
}
let pulsetime = (self.config.idle_timeout + self.config.poll_time_idle).as_secs_f64();
self.client
.heartbeat(&self.config.idle_bucket_name, &event, pulsetime)
.map_err(|_| "Failed to send heartbeat")?;
Ok(())
.map_err(|_| "Failed to send heartbeat".into())
}
pub fn send_active_window(&self, app_id: &str, title: &str) -> Result<(), BoxedError> {
@ -73,6 +78,10 @@ impl ReportClient {
data,
};
if self.config.mock_server {
return Ok(());
}
let interval_margin = self.config.poll_time_idle.as_secs_f64() + 1.0;
self.client
.heartbeat(