Skip to main content

codetether_agent/telemetry/provider/
stats.rs

1//! Pure helpers used by [`super::ProviderMetrics::all_snapshots`].
2//!
3//! Kept in their own file so the orchestration code in `metrics.rs` stays
4//! small and readable.
5
6/// Return the value at the `(n * pct) as usize` index of `sorted_values`,
7/// or `0.0` if out of bounds. Inputs must be pre-sorted ascending.
8///
9/// # Examples
10///
11/// ```rust
12/// use codetether_agent::telemetry::provider::stats::percentile;
13///
14/// let xs = [1.0, 2.0, 3.0, 4.0, 5.0];
15/// assert_eq!(percentile(&xs, 0.50), 3.0);
16/// assert_eq!(percentile(&xs, 0.95), 5.0);
17/// assert_eq!(percentile(&[], 0.50), 0.0);
18/// ```
19pub fn percentile(sorted_values: &[f64], pct: f64) -> f64 {
20    if sorted_values.is_empty() {
21        return 0.0;
22    }
23    let idx = (sorted_values.len() as f64 * pct) as usize;
24    sorted_values.get(idx).copied().unwrap_or(0.0)
25}
26
27/// Sort a `Vec<f64>` ascending, treating NaN as equal. Pure utility so the
28/// unwrap-on-ordering pattern only appears here.
29pub fn sort_f64(values: &mut [f64]) {
30    values.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
31}
32
33/// Arithmetic mean of `values`. Returns `0.0` for empty input.
34///
35/// # Examples
36///
37/// ```rust
38/// use codetether_agent::telemetry::provider::stats::mean;
39///
40/// assert_eq!(mean(&[2.0, 4.0, 6.0]), 4.0);
41/// assert_eq!(mean(&[]), 0.0);
42/// ```
43pub fn mean(values: &[f64]) -> f64 {
44    if values.is_empty() {
45        return 0.0;
46    }
47    values.iter().sum::<f64>() / values.len() as f64
48}