mirror of
https://github.com/2e3s/awatcher.git
synced 2025-06-07 12:05:49 +00:00
Add a mock server option
This commit is contained in:
parent
69130e47a8
commit
97a5867df7
@ -3,7 +3,7 @@ mod file_config;
|
|||||||
mod filters;
|
mod filters;
|
||||||
|
|
||||||
use crate::BoxedError;
|
use crate::BoxedError;
|
||||||
use clap::{arg, value_parser, Command};
|
use clap::{arg, value_parser, ArgAction, Command};
|
||||||
use file_config::FileConfig;
|
use file_config::FileConfig;
|
||||||
use std::{path::PathBuf, time::Duration};
|
use std::{path::PathBuf, time::Duration};
|
||||||
|
|
||||||
@ -17,6 +17,7 @@ pub struct Config {
|
|||||||
pub poll_time_window: Duration,
|
pub poll_time_window: Duration,
|
||||||
pub idle_bucket_name: String,
|
pub idle_bucket_name: String,
|
||||||
pub active_window_bucket_name: String,
|
pub active_window_bucket_name: String,
|
||||||
|
pub mock_server: bool,
|
||||||
filters: Vec<Filter>,
|
filters: Vec<Filter>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,6 +43,9 @@ impl Config {
|
|||||||
arg!(--"poll-time-window" <SECONDS> "Period between sending heartbeats to the server for idle activity")
|
arg!(--"poll-time-window" <SECONDS> "Period between sending heartbeats to the server for idle activity")
|
||||||
.value_parser(value_parser!(u32))
|
.value_parser(value_parser!(u32))
|
||||||
.default_value(defaults::poll_time_window_seconds().to_string()),
|
.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();
|
.get_matches();
|
||||||
|
|
||||||
@ -60,6 +64,7 @@ impl Config {
|
|||||||
idle_bucket_name,
|
idle_bucket_name,
|
||||||
active_window_bucket_name,
|
active_window_bucket_name,
|
||||||
filters: config.client.filters,
|
filters: config.client.filters,
|
||||||
|
mock_server: *matches.get_one("mock-server").unwrap(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,10 +106,17 @@ fn main() -> Result<(), BoxedError> {
|
|||||||
let client = ReportClient::new(Config::from_cli()?)?;
|
let client = ReportClient::new(Config::from_cli()?)?;
|
||||||
let client = Arc::new(client);
|
let client = Arc::new(client);
|
||||||
|
|
||||||
|
if client.config.mock_server {
|
||||||
|
warn!(
|
||||||
|
"Not sending to server {}:{}",
|
||||||
|
client.config.host, client.config.port
|
||||||
|
);
|
||||||
|
} else {
|
||||||
info!(
|
info!(
|
||||||
"Sending to server {}:{}",
|
"Sending to server {}:{}",
|
||||||
client.config.host, client.config.port
|
client.config.host, client.config.port
|
||||||
);
|
);
|
||||||
|
}
|
||||||
info!(
|
info!(
|
||||||
"Idle timeout: {} seconds",
|
"Idle timeout: {} seconds",
|
||||||
client.config.idle_timeout.as_secs()
|
client.config.idle_timeout.as_secs()
|
||||||
|
@ -14,8 +14,11 @@ pub struct ReportClient {
|
|||||||
impl ReportClient {
|
impl ReportClient {
|
||||||
pub fn new(config: Config) -> Result<Self, BoxedError> {
|
pub fn new(config: Config) -> Result<Self, BoxedError> {
|
||||||
let client = AwClient::new(&config.host, &config.port.to_string(), "awatcher");
|
let client = AwClient::new(&config.host, &config.port.to_string(), "awatcher");
|
||||||
|
|
||||||
|
if !config.mock_server {
|
||||||
Self::create_bucket(&client, &config.idle_bucket_name, "afkstatus")?;
|
Self::create_bucket(&client, &config.idle_bucket_name, "afkstatus")?;
|
||||||
Self::create_bucket(&client, &config.active_window_bucket_name, "currentwindow")?;
|
Self::create_bucket(&client, &config.active_window_bucket_name, "currentwindow")?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(Self { client, config })
|
Ok(Self { client, config })
|
||||||
}
|
}
|
||||||
@ -39,12 +42,14 @@ impl ReportClient {
|
|||||||
data,
|
data,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if self.config.mock_server {
|
||||||
|
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).as_secs_f64();
|
||||||
self.client
|
self.client
|
||||||
.heartbeat(&self.config.idle_bucket_name, &event, pulsetime)
|
.heartbeat(&self.config.idle_bucket_name, &event, pulsetime)
|
||||||
.map_err(|_| "Failed to send heartbeat")?;
|
.map_err(|_| "Failed to send heartbeat".into())
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_active_window(&self, app_id: &str, title: &str) -> Result<(), BoxedError> {
|
pub fn send_active_window(&self, app_id: &str, title: &str) -> Result<(), BoxedError> {
|
||||||
@ -73,6 +78,10 @@ impl ReportClient {
|
|||||||
data,
|
data,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if self.config.mock_server {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
let interval_margin = self.config.poll_time_idle.as_secs_f64() + 1.0;
|
let interval_margin = self.config.poll_time_idle.as_secs_f64() + 1.0;
|
||||||
self.client
|
self.client
|
||||||
.heartbeat(
|
.heartbeat(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user