From 856a7afee66f89f754c2aa6367b3541c82c7b257 Mon Sep 17 00:00:00 2001 From: Demmie <2e3s19@gmail.com> Date: Sun, 7 May 2023 23:07:21 -0400 Subject: [PATCH] Simplify config without bucket names --- Cargo.lock | 2 +- Cargo.toml | 1 - src/config.rs | 5 ----- watchers/Cargo.toml | 1 + watchers/src/config.rs | 2 -- watchers/src/report_client.rs | 24 +++++++++++++++--------- 6 files changed, 17 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 96a6dc5..ba198f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -226,7 +226,6 @@ dependencies = [ "chrono", "clap", "fern", - "gethostname 0.4.1", "log", "toml", "watchers", @@ -2060,6 +2059,7 @@ dependencies = [ "aw-client-rust", "chrono", "dirs 5.0.0", + "gethostname 0.4.1", "log", "regex", "serde", diff --git a/Cargo.toml b/Cargo.toml index 47c46d3..51d60ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,6 @@ log = { version = "0.4.17", features = ["std"] } [dependencies] watchers = { path = "./watchers", default-features = false } -gethostname = "0.4.1" chrono = "0.4.24" toml = "0.7.3" clap = { version = "4.2.1", features = ["string"] } diff --git a/src/config.rs b/src/config.rs index fdd2054..0cf3fa1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -70,9 +70,6 @@ pub fn from_cli() -> anyhow::Result { let config = new_with_cli(&matches)?; - let hostname = gethostname::gethostname().into_string().unwrap(); - let idle_bucket_name = format!("aw-watcher-afk_{hostname}"); - let active_window_bucket_name = format!("aw-watcher-window_{hostname}"); let verbosity = match matches.get_count("verbosity") { 0 => LevelFilter::Error, 1 => LevelFilter::Warn, @@ -88,8 +85,6 @@ pub fn from_cli() -> anyhow::Result { idle_timeout: config.client.get_idle_timeout(), poll_time_idle: config.client.get_poll_time_idle(), poll_time_window: config.client.get_poll_time_window(), - idle_bucket_name, - active_window_bucket_name, filters: config.client.filters, no_server: *matches.get_one("no-server").unwrap(), }, diff --git a/watchers/Cargo.toml b/watchers/Cargo.toml index 4960f68..ad8a20c 100644 --- a/watchers/Cargo.toml +++ b/watchers/Cargo.toml @@ -23,6 +23,7 @@ serde = { version = "1.0.160", features = ["derive"] } serde_default = "0.1.0" serde_json = "1.0.95" regex = "1.8.1" +gethostname = "0.4.1" log = { workspace = true } anyhow = { workspace = true } diff --git a/watchers/src/config.rs b/watchers/src/config.rs index 7d0b608..6e5e776 100644 --- a/watchers/src/config.rs +++ b/watchers/src/config.rs @@ -12,8 +12,6 @@ pub struct Config { pub idle_timeout: Duration, pub poll_time_idle: Duration, pub poll_time_window: Duration, - pub idle_bucket_name: String, - pub active_window_bucket_name: String, pub no_server: bool, pub filters: Vec, } diff --git a/watchers/src/report_client.rs b/watchers/src/report_client.rs index 23276ea..defe758 100644 --- a/watchers/src/report_client.rs +++ b/watchers/src/report_client.rs @@ -7,18 +7,28 @@ use serde_json::{Map, Value}; pub struct ReportClient { pub client: AwClient, pub config: Config, + idle_bucket_name: String, + active_window_bucket_name: String, } impl ReportClient { pub fn new(config: Config) -> anyhow::Result { let client = AwClient::new(&config.host, &config.port.to_string(), "awatcher"); + let hostname = gethostname::gethostname().into_string().unwrap(); + let idle_bucket_name = format!("aw-watcher-afk_{hostname}"); + let active_window_bucket_name = format!("aw-watcher-window_{hostname}"); if !config.no_server { - Self::create_bucket(&client, &config.idle_bucket_name, "afkstatus")?; - Self::create_bucket(&client, &config.active_window_bucket_name, "currentwindow")?; + Self::create_bucket(&client, &idle_bucket_name, "afkstatus")?; + Self::create_bucket(&client, &active_window_bucket_name, "currentwindow")?; } - Ok(Self { client, config }) + Ok(Self { + client, + config, + idle_bucket_name, + active_window_bucket_name, + }) } pub fn ping( @@ -46,7 +56,7 @@ impl ReportClient { let pulsetime = (self.config.idle_timeout + self.config.poll_time_idle).as_secs_f64(); self.client - .heartbeat(&self.config.idle_bucket_name, &event, pulsetime) + .heartbeat(&self.idle_bucket_name, &event, pulsetime) .with_context(|| "Failed to send heartbeat") } @@ -82,11 +92,7 @@ impl ReportClient { let interval_margin = self.config.poll_time_idle.as_secs_f64() + 1.0; self.client - .heartbeat( - &self.config.active_window_bucket_name, - &event, - interval_margin, - ) + .heartbeat(&self.active_window_bucket_name, &event, interval_margin) .with_context(|| "Failed to send heartbeat for active window") }