use std::collections::HashMap;
#[derive(Debug, Clone, Copy)]
pub struct MeasurementSummary {
pub epoch: u32,
pub val: f64,
}
#[derive(Debug)]
pub struct CommitSummary {
pub commit: String,
pub measurement: Option<MeasurementSummary>,
}
#[derive(Debug, PartialEq, Clone)]
pub struct MeasurementData {
pub epoch: u32,
pub name: String,
pub timestamp: f64,
pub val: f64,
pub key_values: HashMap<String, String>,
}
#[derive(Debug, PartialEq, Clone)]
pub struct Commit {
pub commit: String,
pub title: String,
pub author: String,
pub measurements: Vec<MeasurementData>,
}
impl MeasurementData {
#[must_use]
pub fn matches_key_values(&self, criteria: &[(String, String)]) -> bool {
self.key_values_is_superset_of(criteria)
}
#[must_use]
pub fn key_values_is_superset_of(&self, criteria: &[(String, String)]) -> bool {
criteria
.iter()
.all(|(k, v)| self.key_values.get(k).map(|mv| v == mv).unwrap_or(false))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_matches_key_values() {
let mut key_values = HashMap::new();
key_values.insert("env".to_string(), "production".to_string());
key_values.insert("branch".to_string(), "main".to_string());
key_values.insert("cpu".to_string(), "x64".to_string());
let measurement = MeasurementData {
epoch: 1,
name: "test_measurement".to_string(),
timestamp: 1234567890.0,
val: 42.0,
key_values,
};
assert!(measurement.matches_key_values(&[]));
assert!(measurement.matches_key_values(&[("env".to_string(), "production".to_string())]));
assert!(measurement.matches_key_values(&[
("env".to_string(), "production".to_string()),
("branch".to_string(), "main".to_string()),
]));
assert!(measurement.matches_key_values(&[
("env".to_string(), "production".to_string()),
("branch".to_string(), "main".to_string()),
("cpu".to_string(), "x64".to_string()),
]));
assert!(!measurement.matches_key_values(&[("env".to_string(), "staging".to_string())]));
assert!(!measurement.matches_key_values(&[("os".to_string(), "linux".to_string())]));
assert!(!measurement.matches_key_values(&[
("env".to_string(), "production".to_string()), ("branch".to_string(), "develop".to_string()), ]));
}
}