style: switch rustfmt to hard tabs; document tests-separate rule in TODO

- rustfmt.toml: hard_tabs = true.
- TODO §0.7: tests live in tests/, no #[cfg(test)] modules in code files.
This commit is contained in:
Loki 2026-11-15 22:34:29 +01:00
parent d8393e0f39
commit a90f0da226
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
96 changed files with 6542 additions and 6537 deletions

View file

@ -10,80 +10,80 @@ const FLUSH_INTERVAL: Duration = Duration::from_secs(1);
#[derive(Debug, Clone, Serialize)]
pub struct ClickHouseEvent {
pub timestamp: DateTime<Utc>,
pub event_type: String,
pub ip: String,
pub data_float: f64,
pub data_int: i64,
pub data_string: String,
pub timestamp: DateTime<Utc>,
pub event_type: String,
pub ip: String,
pub data_float: f64,
pub data_int: i64,
pub data_string: String,
}
pub struct ClickHouseWriter {
url: String,
client: reqwest::Client,
buffer: Vec<ClickHouseEvent>,
url: String,
client: reqwest::Client,
buffer: Vec<ClickHouseEvent>,
}
impl ClickHouseWriter {
pub fn new(url: &str) -> Self {
Self {
url: url.to_string(),
client: reqwest::Client::new(),
buffer: Vec::with_capacity(BATCH_SIZE),
}
}
pub fn new(url: &str) -> Self {
Self {
url: url.to_string(),
client: reqwest::Client::new(),
buffer: Vec::with_capacity(BATCH_SIZE),
}
}
pub async fn push(&mut self, event: ClickHouseEvent) -> anyhow::Result<()> {
self.buffer.push(event);
if self.buffer.len() >= BATCH_SIZE {
self.flush().await?;
}
Ok(())
}
pub async fn push(&mut self, event: ClickHouseEvent) -> anyhow::Result<()> {
self.buffer.push(event);
if self.buffer.len() >= BATCH_SIZE {
self.flush().await?;
}
Ok(())
}
pub async fn flush(&mut self) -> anyhow::Result<()> {
if self.buffer.is_empty() {
return Ok(());
}
let events = std::mem::take(&mut self.buffer);
let json = serde_json::to_string(&events)?;
let response = self
.client
.post(&self.url)
.query(&[("query", "INSERT INTO rampart_events FORMAT JSONEachRow")])
.header("Content-Type", "application/json")
.body(json)
.send()
.await?;
let status = response.status();
if !status.is_success() {
let text = response.text().await?;
anyhow::bail!("clickhouse insert failed ({}): {}", status, text);
}
tracing::debug!("flushed {} events to clickhouse", events.len());
Ok(())
}
pub async fn flush(&mut self) -> anyhow::Result<()> {
if self.buffer.is_empty() {
return Ok(());
}
let events = std::mem::take(&mut self.buffer);
let json = serde_json::to_string(&events)?;
let response = self
.client
.post(&self.url)
.query(&[("query", "INSERT INTO rampart_events FORMAT JSONEachRow")])
.header("Content-Type", "application/json")
.body(json)
.send()
.await?;
let status = response.status();
if !status.is_success() {
let text = response.text().await?;
anyhow::bail!("clickhouse insert failed ({}): {}", status, text);
}
tracing::debug!("flushed {} events to clickhouse", events.len());
Ok(())
}
}
pub fn start_flush_task(writer: Arc<Mutex<ClickHouseWriter>>, mut shutdown: watch::Receiver<bool>) {
tokio::spawn(async move {
loop {
tokio::select! {
_ = tokio::time::sleep(FLUSH_INTERVAL) => {
if let Err(e) = writer.lock().await.flush().await {
tracing::error!("clickhouse flush error: {e}");
}
}
_ = shutdown.changed() => {
if *shutdown.borrow() {
tracing::info!("flushing clickhouse on shutdown");
if let Err(e) = writer.lock().await.flush().await {
tracing::error!("clickhouse final flush error: {e}");
}
return;
}
}
}
}
});
tokio::spawn(async move {
loop {
tokio::select! {
_ = tokio::time::sleep(FLUSH_INTERVAL) => {
if let Err(e) = writer.lock().await.flush().await {
tracing::error!("clickhouse flush error: {e}");
}
}
_ = shutdown.changed() => {
if *shutdown.borrow() {
tracing::info!("flushing clickhouse on shutdown");
if let Err(e) = writer.lock().await.flush().await {
tracing::error!("clickhouse final flush error: {e}");
}
return;
}
}
}
}
});
}