use relay_protocol::{Annotated, Array, Empty, FromValue, IntoValue, Object};
use crate::processor::ProcessValue;
pub type MetricSummaryMapping = Object<Array<MetricSummary>>;
#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
pub struct MetricsSummary(pub MetricSummaryMapping);
impl MetricsSummary {
pub fn update_value<F: FnOnce(MetricSummaryMapping) -> MetricSummaryMapping>(&mut self, f: F) {
self.0 = f(std::mem::take(&mut self.0));
}
}
#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
pub struct MetricSummary {
pub min: Annotated<f64>,
pub max: Annotated<f64>,
pub sum: Annotated<f64>,
pub count: Annotated<u64>,
pub tags: Annotated<Object<String>>,
}
impl MetricSummary {
pub fn merge(&mut self, other: MetricSummary) {
self.min.merge(other.min, |l, r| *l = l.min(r));
self.max.merge(other.max, |l, r| *l = l.max(r));
self.sum.merge(other.sum, |l, r| *l += r);
self.count.merge(other.count, |l, r| *l += r);
}
}