Skip to main content

align

Function align 

Source
pub fn align(
    samples: &[Sample],
    interval: Interval,
    rules: &Rules,
) -> Result<BTreeMap<String, Reading>, AlignError>
Expand description

Samples of one interval, as one number per metric.

A metric with no sample at all is absent from the map rather than Missing: nothing was expected of it here, and an expectation is the caller’s to hold. The map is a BTreeMap, so the same input always yields the same iteration order.

A metric’s kind is taken from its last sample in caller order; a caller that mixes kinds for one metric is a caller to fix.

§Errors

Returns AlignError::SampleOutsideInterval when a sample’s timestamp lies outside [interval.start, interval.end), and AlignError::CounterWentBackwards when a counter metric that passed the coverage gate falls.

use interval_align::{align, Duration, Interval, Kind, Reading, Rules, Sample};

// Synthetic example data.
let start = 1_772_949_600;
let interval = Interval { start, length: Duration::seconds(900).expect("positive") };
let samples = [
    Sample::new("hall.kw", start, 100.0, Kind::Power).expect("finite"),
    Sample::new("hall.kw", start + 300, 200.0, Kind::Power).expect("finite"),
    Sample::new("hall.kw", start + 720, 100.0, Kind::Power).expect("finite"),
];
let aligned = align(&samples, interval, &Rules { min_coverage_pct: 80 })
    .expect("all samples inside the interval");
assert!(matches!(aligned["hall.kw"], Reading::Value { coverage_pct: 80, .. }));