use serde::Serialize;
use crate::query::Hit;
pub const MATCHED_LEVELS: [&str; 3] = ["PointAddress", "StreetAddress", "StreetInt"];
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Summary {
pub rows: usize,
pub answered: usize,
pub within_150m: usize,
pub share_within_150m: f64,
pub median_m: Option<f64>,
pub p95_m: Option<f64>,
pub match_rate: f64,
pub by_match_level: std::collections::BTreeMap<String, usize>,
}
fn quantile(sorted: &[f64], q: f64) -> Option<f64> {
if sorted.is_empty() {
return None;
}
let rank = (sorted.len() - 1) as f64 * q;
let (lo, hi) = (rank.floor() as usize, rank.ceil() as usize);
Some(sorted[lo] + (rank - lo as f64) * (sorted[hi] - sorted[lo]))
}
pub fn summarize(rows: &[(Option<&Hit>, (f64, f64))]) -> Summary {
let mut dists: Vec<f64> = Vec::new();
let mut by_level = std::collections::BTreeMap::new();
let mut matched = 0;
for (hit, truth) in rows {
match hit {
Some(h) => {
dists.push(crate::codes::haversine_m(*truth, (h.lat, h.lon)));
let level = crate::query::match_level(h).0;
matched += usize::from(MATCHED_LEVELS.contains(&level));
*by_level.entry(level.to_string()).or_insert(0) += 1;
}
None => *by_level.entry("U".to_string()).or_insert(0) += 1,
}
}
let within = dists.iter().filter(|d| **d <= 150.0).count();
dists.sort_by(f64::total_cmp);
let n = rows.len().max(1) as f64;
Summary {
rows: rows.len(),
answered: dists.len(),
within_150m: within,
share_within_150m: within as f64 / n,
median_m: quantile(&dists, 0.5).map(|v| (v * 100.0).round() / 100.0),
p95_m: quantile(&dists, 0.95).map(|v| (v * 100.0).round() / 100.0),
match_rate: matched as f64 / n,
by_match_level: by_level,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_quantile_is_the_linear_inclusive_one_of_the_benchmark_tool() {
let close = |got: Option<f64>, want: f64| got.is_some_and(|g| (g - want).abs() < 1e-9);
assert!(close(quantile(&[0.0, 10.0, 20.0, 30.0], 0.95), 28.5));
assert_eq!(quantile(&[5.0], 0.95), Some(5.0));
assert_eq!(quantile(&[], 0.5), None);
assert!(close(quantile(&[0.0, 10.0, 20.0, 30.0], 0.5), 15.0));
}
}