vortex_metrics/
histogram.rs1use std::sync::Arc;
5
6use parking_lot::RwLock;
7use sketches_ddsketch::DDSketch;
8
9#[derive(Default, Clone)]
14pub struct Histogram(Arc<RwLock<DDSketch>>);
15
16impl std::fmt::Debug for Histogram {
17 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18 f.debug_tuple("Histogram").finish_non_exhaustive()
19 }
20}
21
22impl Histogram {
23 pub(crate) fn new() -> Self {
24 Self(Default::default())
25 }
26
27 pub fn update(&self, value: f64) {
29 self.0.write().add(value);
30 }
31
32 #[expect(clippy::expect_used)]
35 pub fn quantile(&self, quantile: f64) -> Option<f64> {
36 assert!(
37 (0.0..=1.0).contains(&quantile),
38 "quantile must be between 0.0 and 1.0"
39 );
40
41 self.0
42 .read()
43 .quantile(quantile)
44 .expect("quantile range checked")
45 }
46
47 pub fn total(&self) -> f64 {
49 self.0.read().sum().unwrap_or_default()
50 }
51
52 pub fn count(&self) -> usize {
54 self.0.read().count()
55 }
56
57 pub fn is_empty(&self) -> bool {
59 self.0.read().count() == 0
60 }
61}