Simplify config without bucket names

This commit is contained in:
Demmie 2023-05-07 23:07:21 -04:00
parent 53a5c1adce
commit 856a7afee6
No known key found for this signature in database
GPG Key ID: B06DAA3D432C6E9A
6 changed files with 17 additions and 18 deletions

2
Cargo.lock generated
View File

@ -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",

View File

@ -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"] }

View File

@ -70,9 +70,6 @@ pub fn from_cli() -> anyhow::Result<Config> {
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<Config> {
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(),
},

View File

@ -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 }

View File

@ -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<Filter>,
}

View File

@ -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<Self> {
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")
}