Adopt rustfmt and clean up clippy warnings
Formats the whole backend to rustfmt defaults and fixes all 23 outstanding clippy warnings (collapsible ifs, clone-to-slice-ref, type-complexity, single-arm match, unnecessary is_none check), then wires cargo fmt --check and -D warnings into make lint so both stay enforced going forward. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TeyhRfMYjMditek6geDRoQ
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
2cc8ac8138
commit
c3108677ce
+40
-24
@@ -2,8 +2,8 @@ use std::collections::BTreeMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::consts::{METRICS_POLL_INTERVAL, STDIN_POLL_INTERVAL as POLL_INTERVAL};
|
||||
use crate::engine::{EngineHandle, EngineMessage, IngestedLine, MetricSample};
|
||||
use crate::engine::timestamp::Timestamp;
|
||||
use crate::engine::{EngineHandle, EngineMessage, IngestedLine, MetricSample};
|
||||
use tokio::fs::File;
|
||||
use tokio::io::{AsyncBufReadExt, BufReader};
|
||||
|
||||
@@ -67,15 +67,12 @@ pub async fn run_file(engine: EngineHandle, path: PathBuf) {
|
||||
let mut chunk = String::new();
|
||||
match reader.read_line(&mut chunk).await {
|
||||
Ok(0) => {
|
||||
match tokio::fs::metadata(&path).await {
|
||||
Ok(meta) => {
|
||||
let new_id = file_id(&meta);
|
||||
if new_id != id || meta.len() < offset {
|
||||
continue 'reopen;
|
||||
}
|
||||
id = new_id;
|
||||
if let Ok(meta) = tokio::fs::metadata(&path).await {
|
||||
let new_id = file_id(&meta);
|
||||
if new_id != id || meta.len() < offset {
|
||||
continue 'reopen;
|
||||
}
|
||||
Err(_) => {}
|
||||
id = new_id;
|
||||
}
|
||||
tokio::time::sleep(POLL_INTERVAL).await;
|
||||
}
|
||||
@@ -83,9 +80,7 @@ pub async fn run_file(engine: EngineHandle, path: PathBuf) {
|
||||
offset += n as u64;
|
||||
pending.push_str(&chunk);
|
||||
if pending.ends_with('\n') {
|
||||
let text = pending
|
||||
.trim_end_matches(['\n', '\r'])
|
||||
.to_string();
|
||||
let text = pending.trim_end_matches(['\n', '\r']).to_string();
|
||||
pending.clear();
|
||||
|
||||
let line = IngestedLine {
|
||||
@@ -128,7 +123,12 @@ pub async fn run_metrics_poll(engine: EngineHandle, url: String) {
|
||||
Ok(body) => {
|
||||
let now = Timestamp::now();
|
||||
for sample in parse_openmetrics(&body, now) {
|
||||
if engine.tx.send(EngineMessage::IngestMetricSample(sample)).await.is_err() {
|
||||
if engine
|
||||
.tx
|
||||
.send(EngineMessage::IngestMetricSample(sample))
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -140,7 +140,9 @@ pub async fn run_metrics_poll(engine: EngineHandle, url: String) {
|
||||
}
|
||||
|
||||
fn parse_openmetrics(body: &str, now: Timestamp) -> Vec<MetricSample> {
|
||||
body.lines().filter_map(|line| parse_openmetrics_line(line, now)).collect()
|
||||
body.lines()
|
||||
.filter_map(|line| parse_openmetrics_line(line, now))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn parse_openmetrics_line(line: &str, now: Timestamp) -> Option<MetricSample> {
|
||||
@@ -161,7 +163,10 @@ fn parse_openmetrics_line(line: &str, now: Timestamp) -> Option<MetricSample> {
|
||||
let (metric_name, labels) = match name_and_labels.find('{') {
|
||||
Some(open) => {
|
||||
let close = name_and_labels.rfind('}')?;
|
||||
(name_and_labels[..open].to_string(), parse_openmetrics_labels(&name_and_labels[open + 1..close]))
|
||||
(
|
||||
name_and_labels[..open].to_string(),
|
||||
parse_openmetrics_labels(&name_and_labels[open + 1..close]),
|
||||
)
|
||||
}
|
||||
None => (name_and_labels.to_string(), BTreeMap::new()),
|
||||
};
|
||||
@@ -169,13 +174,23 @@ fn parse_openmetrics_line(line: &str, now: Timestamp) -> Option<MetricSample> {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(MetricSample { metric_name, labels, value, timestamp })
|
||||
Some(MetricSample {
|
||||
metric_name,
|
||||
labels,
|
||||
value,
|
||||
timestamp,
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_openmetrics_labels(raw: &str) -> BTreeMap<String, String> {
|
||||
raw.split(',')
|
||||
.filter_map(|pair| pair.split_once('='))
|
||||
.map(|(name, value)| (name.trim().to_string(), value.trim().trim_matches('"').to_string()))
|
||||
.map(|(name, value)| {
|
||||
(
|
||||
name.trim().to_string(),
|
||||
value.trim().trim_matches('"').to_string(),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -197,10 +212,8 @@ mod openmetrics_tests {
|
||||
#[test]
|
||||
fn parses_labels_and_explicit_timestamp() {
|
||||
let now = Timestamp::from_millis(1000);
|
||||
let samples = parse_openmetrics(
|
||||
r#"http_requests_total{method="GET",path="/a"} 7 5000"#,
|
||||
now,
|
||||
);
|
||||
let samples =
|
||||
parse_openmetrics(r#"http_requests_total{method="GET",path="/a"} 7 5000"#, now);
|
||||
assert_eq!(samples.len(), 1);
|
||||
let sample = &samples[0];
|
||||
assert_eq!(sample.metric_name, "http_requests_total");
|
||||
@@ -223,9 +236,9 @@ mod openmetrics_tests {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::time::Duration;
|
||||
use std::io::Write;
|
||||
use crate::engine::test_handle;
|
||||
use std::io::Write;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
async fn recv_text(rx: &mut mpsc::Receiver<EngineMessage>) -> String {
|
||||
@@ -250,7 +263,10 @@ mod tests {
|
||||
|
||||
assert_eq!(recv_text(&mut rx).await, "first");
|
||||
|
||||
let mut f = std::fs::OpenOptions::new().append(true).open(&path).unwrap();
|
||||
let mut f = std::fs::OpenOptions::new()
|
||||
.append(true)
|
||||
.open(&path)
|
||||
.unwrap();
|
||||
writeln!(f, "second").unwrap();
|
||||
assert_eq!(recv_text(&mut rx).await, "second");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user