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 #[allow(clippy::expect_used)]
35 #[allow(clippy::unwrap_in_result)]
36 pub fn quantile(&self, quantile: f64) -> Option<f64> {
37 assert!(
38 (0.0..=1.0).contains(&quantile),
39 "quantile must be between 0.0 and 1.0"
40 );
41
42 self.0
43 .read()
44 .quantile(quantile)
45 .expect("quantile range checked")
46 }
47
48 pub fn total(&self) -> f64 {
50 self.0.read().sum().unwrap_or_default()
51 }
52
53 pub fn count(&self) -> usize {
55 self.0.read().count()
56 }
57
58 pub fn is_empty(&self) -> bool {
60 self.0.read().count() == 0
61 }
62}