|
| 1 | +use crate::counter::Counter; |
| 2 | +use crate::gauge::Gauge; |
| 3 | +use crate::label::LabelSet; |
| 4 | +use crate::metric::aggregate::sum::Sum; |
| 5 | +use crate::metric::Metric; |
| 6 | + |
| 7 | +pub trait Avg { |
| 8 | + type Output; |
| 9 | + fn avg(&self, label_set_criteria: &LabelSet) -> Self::Output; |
| 10 | +} |
| 11 | + |
| 12 | +impl Avg for Metric<Counter> { |
| 13 | + type Output = f64; |
| 14 | + |
| 15 | + fn avg(&self, label_set_criteria: &LabelSet) -> Self::Output { |
| 16 | + let matching_samples = self.collect_matching_samples(label_set_criteria); |
| 17 | + |
| 18 | + if matching_samples.is_empty() { |
| 19 | + return 0.0; |
| 20 | + } |
| 21 | + |
| 22 | + let sum = self.sum(label_set_criteria); |
| 23 | + |
| 24 | + #[allow(clippy::cast_precision_loss)] |
| 25 | + (sum as f64 / matching_samples.len() as f64) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl Avg for Metric<Gauge> { |
| 30 | + type Output = f64; |
| 31 | + |
| 32 | + fn avg(&self, label_set_criteria: &LabelSet) -> Self::Output { |
| 33 | + let matching_samples = self.collect_matching_samples(label_set_criteria); |
| 34 | + |
| 35 | + if matching_samples.is_empty() { |
| 36 | + return 0.0; |
| 37 | + } |
| 38 | + |
| 39 | + let sum = self.sum(label_set_criteria); |
| 40 | + |
| 41 | + #[allow(clippy::cast_precision_loss)] |
| 42 | + (sum / matching_samples.len() as f64) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +#[cfg(test)] |
| 47 | +mod tests { |
| 48 | + |
| 49 | + use torrust_tracker_primitives::DurationSinceUnixEpoch; |
| 50 | + |
| 51 | + use crate::counter::Counter; |
| 52 | + use crate::gauge::Gauge; |
| 53 | + use crate::label::LabelSet; |
| 54 | + use crate::metric::aggregate::avg::Avg; |
| 55 | + use crate::metric::{Metric, MetricName}; |
| 56 | + use crate::metric_name; |
| 57 | + use crate::sample::Sample; |
| 58 | + use crate::sample_collection::SampleCollection; |
| 59 | + |
| 60 | + struct MetricBuilder<T> { |
| 61 | + sample_time: DurationSinceUnixEpoch, |
| 62 | + name: MetricName, |
| 63 | + samples: Vec<Sample<T>>, |
| 64 | + } |
| 65 | + |
| 66 | + impl<T> Default for MetricBuilder<T> { |
| 67 | + fn default() -> Self { |
| 68 | + Self { |
| 69 | + sample_time: DurationSinceUnixEpoch::from_secs(1_743_552_000), |
| 70 | + name: metric_name!("test_metric"), |
| 71 | + samples: vec![], |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + impl<T> MetricBuilder<T> { |
| 77 | + fn with_sample(mut self, value: T, label_set: &LabelSet) -> Self { |
| 78 | + let sample = Sample::new(value, self.sample_time, label_set.clone()); |
| 79 | + self.samples.push(sample); |
| 80 | + self |
| 81 | + } |
| 82 | + |
| 83 | + fn build(self) -> Metric<T> { |
| 84 | + Metric::new( |
| 85 | + self.name, |
| 86 | + None, |
| 87 | + None, |
| 88 | + SampleCollection::new(self.samples).expect("invalid samples"), |
| 89 | + ) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + fn counter_cases() -> Vec<(Metric<Counter>, LabelSet, f64)> { |
| 94 | + // (metric, label set criteria, expected_average_value) |
| 95 | + vec![ |
| 96 | + // Metric with one sample without label set |
| 97 | + ( |
| 98 | + MetricBuilder::default().with_sample(1.into(), &LabelSet::empty()).build(), |
| 99 | + LabelSet::empty(), |
| 100 | + 1.0, |
| 101 | + ), |
| 102 | + // Metric with one sample with a label set |
| 103 | + ( |
| 104 | + MetricBuilder::default() |
| 105 | + .with_sample(1.into(), &[("l1", "l1_value")].into()) |
| 106 | + .build(), |
| 107 | + [("l1", "l1_value")].into(), |
| 108 | + 1.0, |
| 109 | + ), |
| 110 | + // Metric with two samples, different label sets, average all |
| 111 | + ( |
| 112 | + MetricBuilder::default() |
| 113 | + .with_sample(1.into(), &[("l1", "l1_value")].into()) |
| 114 | + .with_sample(3.into(), &[("l2", "l2_value")].into()) |
| 115 | + .build(), |
| 116 | + LabelSet::empty(), |
| 117 | + 2.0, // (1 + 3) / 2 = 2.0 |
| 118 | + ), |
| 119 | + // Metric with two samples, different label sets, average one |
| 120 | + ( |
| 121 | + MetricBuilder::default() |
| 122 | + .with_sample(1.into(), &[("l1", "l1_value")].into()) |
| 123 | + .with_sample(2.into(), &[("l2", "l2_value")].into()) |
| 124 | + .build(), |
| 125 | + [("l1", "l1_value")].into(), |
| 126 | + 1.0, |
| 127 | + ), |
| 128 | + // Metric with three samples, same label key, different label values, average by key |
| 129 | + ( |
| 130 | + MetricBuilder::default() |
| 131 | + .with_sample(2.into(), &[("l1", "l1_value"), ("la", "la_value")].into()) |
| 132 | + .with_sample(4.into(), &[("l1", "l1_value"), ("lb", "lb_value")].into()) |
| 133 | + .with_sample(6.into(), &[("l1", "l1_value"), ("lc", "lc_value")].into()) |
| 134 | + .build(), |
| 135 | + [("l1", "l1_value")].into(), |
| 136 | + 4.0, // (2 + 4 + 6) / 3 = 4.0 |
| 137 | + ), |
| 138 | + // Metric with two samples, different label values, average by subkey |
| 139 | + ( |
| 140 | + MetricBuilder::default() |
| 141 | + .with_sample(5.into(), &[("l1", "l1_value"), ("la", "la_value")].into()) |
| 142 | + .with_sample(7.into(), &[("l1", "l1_value"), ("lb", "lb_value")].into()) |
| 143 | + .build(), |
| 144 | + [("la", "la_value")].into(), |
| 145 | + 5.0, |
| 146 | + ), |
| 147 | + // Edge: Metric with no samples at all |
| 148 | + (MetricBuilder::default().build(), LabelSet::empty(), 0.0), |
| 149 | + // Edge: Metric with samples but no matching labels |
| 150 | + ( |
| 151 | + MetricBuilder::default() |
| 152 | + .with_sample(5.into(), &[("foo", "bar")].into()) |
| 153 | + .build(), |
| 154 | + [("not", "present")].into(), |
| 155 | + 0.0, |
| 156 | + ), |
| 157 | + // Edge: Metric with zero value |
| 158 | + ( |
| 159 | + MetricBuilder::default() |
| 160 | + .with_sample(0.into(), &[("l3", "l3_value")].into()) |
| 161 | + .build(), |
| 162 | + [("l3", "l3_value")].into(), |
| 163 | + 0.0, |
| 164 | + ), |
| 165 | + // Edge: Metric with a very large value |
| 166 | + ( |
| 167 | + MetricBuilder::default() |
| 168 | + .with_sample((u64::MAX / 2).into(), &[("edge", "large1")].into()) |
| 169 | + .with_sample((u64::MAX / 2).into(), &[("edge", "large2")].into()) |
| 170 | + .build(), |
| 171 | + LabelSet::empty(), |
| 172 | + #[allow(clippy::cast_precision_loss)] |
| 173 | + (u64::MAX as f64 / 2.0), // Average of (max/2) and (max/2) |
| 174 | + ), |
| 175 | + ] |
| 176 | + } |
| 177 | + |
| 178 | + fn gauge_cases() -> Vec<(Metric<Gauge>, LabelSet, f64)> { |
| 179 | + // (metric, label set criteria, expected_average_value) |
| 180 | + vec![ |
| 181 | + // Metric with one sample without label set |
| 182 | + ( |
| 183 | + MetricBuilder::default().with_sample(1.0.into(), &LabelSet::empty()).build(), |
| 184 | + LabelSet::empty(), |
| 185 | + 1.0, |
| 186 | + ), |
| 187 | + // Metric with one sample with a label set |
| 188 | + ( |
| 189 | + MetricBuilder::default() |
| 190 | + .with_sample(1.0.into(), &[("l1", "l1_value")].into()) |
| 191 | + .build(), |
| 192 | + [("l1", "l1_value")].into(), |
| 193 | + 1.0, |
| 194 | + ), |
| 195 | + // Metric with two samples, different label sets, average all |
| 196 | + ( |
| 197 | + MetricBuilder::default() |
| 198 | + .with_sample(1.0.into(), &[("l1", "l1_value")].into()) |
| 199 | + .with_sample(3.0.into(), &[("l2", "l2_value")].into()) |
| 200 | + .build(), |
| 201 | + LabelSet::empty(), |
| 202 | + 2.0, // (1.0 + 3.0) / 2 = 2.0 |
| 203 | + ), |
| 204 | + // Metric with two samples, different label sets, average one |
| 205 | + ( |
| 206 | + MetricBuilder::default() |
| 207 | + .with_sample(1.0.into(), &[("l1", "l1_value")].into()) |
| 208 | + .with_sample(2.0.into(), &[("l2", "l2_value")].into()) |
| 209 | + .build(), |
| 210 | + [("l1", "l1_value")].into(), |
| 211 | + 1.0, |
| 212 | + ), |
| 213 | + // Metric with three samples, same label key, different label values, average by key |
| 214 | + ( |
| 215 | + MetricBuilder::default() |
| 216 | + .with_sample(2.0.into(), &[("l1", "l1_value"), ("la", "la_value")].into()) |
| 217 | + .with_sample(4.0.into(), &[("l1", "l1_value"), ("lb", "lb_value")].into()) |
| 218 | + .with_sample(6.0.into(), &[("l1", "l1_value"), ("lc", "lc_value")].into()) |
| 219 | + .build(), |
| 220 | + [("l1", "l1_value")].into(), |
| 221 | + 4.0, // (2.0 + 4.0 + 6.0) / 3 = 4.0 |
| 222 | + ), |
| 223 | + // Metric with two samples, different label values, average by subkey |
| 224 | + ( |
| 225 | + MetricBuilder::default() |
| 226 | + .with_sample(5.0.into(), &[("l1", "l1_value"), ("la", "la_value")].into()) |
| 227 | + .with_sample(7.0.into(), &[("l1", "l1_value"), ("lb", "lb_value")].into()) |
| 228 | + .build(), |
| 229 | + [("la", "la_value")].into(), |
| 230 | + 5.0, |
| 231 | + ), |
| 232 | + // Edge: Metric with no samples at all |
| 233 | + (MetricBuilder::default().build(), LabelSet::empty(), 0.0), |
| 234 | + // Edge: Metric with samples but no matching labels |
| 235 | + ( |
| 236 | + MetricBuilder::default() |
| 237 | + .with_sample(5.0.into(), &[("foo", "bar")].into()) |
| 238 | + .build(), |
| 239 | + [("not", "present")].into(), |
| 240 | + 0.0, |
| 241 | + ), |
| 242 | + // Edge: Metric with zero value |
| 243 | + ( |
| 244 | + MetricBuilder::default() |
| 245 | + .with_sample(0.0.into(), &[("l3", "l3_value")].into()) |
| 246 | + .build(), |
| 247 | + [("l3", "l3_value")].into(), |
| 248 | + 0.0, |
| 249 | + ), |
| 250 | + // Edge: Metric with negative values |
| 251 | + ( |
| 252 | + MetricBuilder::default() |
| 253 | + .with_sample((-2.0).into(), &[("l4", "l4_value")].into()) |
| 254 | + .with_sample(4.0.into(), &[("l5", "l5_value")].into()) |
| 255 | + .build(), |
| 256 | + LabelSet::empty(), |
| 257 | + 1.0, // (-2.0 + 4.0) / 2 = 1.0 |
| 258 | + ), |
| 259 | + // Edge: Metric with decimal values |
| 260 | + ( |
| 261 | + MetricBuilder::default() |
| 262 | + .with_sample(1.5.into(), &[("l6", "l6_value")].into()) |
| 263 | + .with_sample(2.5.into(), &[("l7", "l7_value")].into()) |
| 264 | + .build(), |
| 265 | + LabelSet::empty(), |
| 266 | + 2.0, // (1.5 + 2.5) / 2 = 2.0 |
| 267 | + ), |
| 268 | + ] |
| 269 | + } |
| 270 | + |
| 271 | + #[test] |
| 272 | + fn test_counter_cases() { |
| 273 | + for (idx, (metric, criteria, expected_value)) in counter_cases().iter().enumerate() { |
| 274 | + let avg = metric.avg(criteria); |
| 275 | + |
| 276 | + assert!( |
| 277 | + (avg - expected_value).abs() <= f64::EPSILON, |
| 278 | + "at case {idx}, expected avg to be {expected_value}, got {avg}" |
| 279 | + ); |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + #[test] |
| 284 | + fn test_gauge_cases() { |
| 285 | + for (idx, (metric, criteria, expected_value)) in gauge_cases().iter().enumerate() { |
| 286 | + let avg = metric.avg(criteria); |
| 287 | + |
| 288 | + assert!( |
| 289 | + (avg - expected_value).abs() <= f64::EPSILON, |
| 290 | + "at case {idx}, expected avg to be {expected_value}, got {avg}" |
| 291 | + ); |
| 292 | + } |
| 293 | + } |
| 294 | +} |
0 commit comments