#[derive(Debug, Clone)]
pub struct ClickHouseWriterConfig {
pub url: String,
pub database: String,
pub table: String,
pub max_rows: usize,
pub flush_period_ms: u64,
pub max_bytes: usize,
pub channel_capacity: usize,
pub max_retries: u32,
}
impl Default for ClickHouseWriterConfig {
fn default() -> Self {
Self {
url: "http://localhost:8123".to_string(),
database: "opendeviationbar_cache".to_string(),
table: "open_deviation_bars".to_string(),
max_rows: 500,
flush_period_ms: 100,
max_bytes: 5_242_880, channel_capacity: 10_000,
max_retries: 5,
}
}
}
impl ClickHouseWriterConfig {
pub fn from_env() -> Self {
let mut config = Self::default();
if let Ok(hosts) = std::env::var("OPENDEVIATIONBAR_CH_HOSTS") {
let host = hosts.split(',').next().unwrap_or("localhost").trim();
config.url = if host.starts_with("http://") || host.starts_with("https://") {
host.to_string()
} else {
format!("http://{host}:8123")
};
}
if let Ok(val) = std::env::var("OPENDEVIATIONBAR_CH_WRITER_MAX_ROWS")
&& let Ok(n) = val.parse::<usize>()
{
config.max_rows = n;
}
if let Ok(val) = std::env::var("OPENDEVIATIONBAR_CH_WRITER_FLUSH_MS")
&& let Ok(n) = val.parse::<u64>()
{
config.flush_period_ms = n;
}
if let Ok(val) = std::env::var("OPENDEVIATIONBAR_CH_WRITER_MAX_BYTES")
&& let Ok(n) = val.parse::<usize>()
{
config.max_bytes = n;
}
config
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_config_defaults() {
let config = ClickHouseWriterConfig::default();
assert_eq!(config.max_rows, 500);
assert_eq!(config.flush_period_ms, 100);
assert_eq!(config.max_bytes, 5_242_880);
assert_eq!(config.channel_capacity, 10_000);
assert_eq!(config.max_retries, 5);
assert_eq!(config.database, "opendeviationbar_cache");
assert_eq!(config.table, "open_deviation_bars");
assert_eq!(config.url, "http://localhost:8123");
}
#[test]
fn test_config_from_env_defaults() {
let config = ClickHouseWriterConfig::from_env();
assert_eq!(config.max_rows, 500);
assert_eq!(config.flush_period_ms, 100);
assert_eq!(config.max_bytes, 5_242_880);
}
}