1use std::collections::HashMap;
2
3#[derive(Debug)]
4pub struct MeasurementSummary {
5 pub epoch: u32,
6 pub val: f64,
7}
8
9#[derive(Debug)]
10pub struct CommitSummary {
11 pub commit: String,
12 pub measurement: Option<MeasurementSummary>,
13}
14
15#[derive(Debug, PartialEq)]
16pub struct MeasurementData {
17 pub epoch: u32,
18 pub name: String,
19 pub timestamp: f64,
20 pub val: f64,
21 pub key_values: HashMap<String, String>,
22}
23
24#[derive(Debug, PartialEq)]
25pub struct Commit {
26 pub commit: String,
27 pub measurements: Vec<MeasurementData>,
28}
29
30impl MeasurementData {
31 pub fn matches_key_values(&self, criteria: &[(String, String)]) -> bool {
35 self.key_values_is_superset_of(criteria)
36 }
37
38 pub fn key_values_is_superset_of(&self, criteria: &[(String, String)]) -> bool {
43 criteria
44 .iter()
45 .all(|(k, v)| self.key_values.get(k).map(|mv| v == mv).unwrap_or(false))
46 }
47}
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52
53 #[test]
54 fn test_matches_key_values() {
55 let mut key_values = HashMap::new();
56 key_values.insert("env".to_string(), "production".to_string());
57 key_values.insert("branch".to_string(), "main".to_string());
58 key_values.insert("cpu".to_string(), "x64".to_string());
59
60 let measurement = MeasurementData {
61 epoch: 1,
62 name: "test_measurement".to_string(),
63 timestamp: 1234567890.0,
64 val: 42.0,
65 key_values,
66 };
67
68 assert!(measurement.matches_key_values(&[]));
70
71 assert!(measurement.matches_key_values(&[("env".to_string(), "production".to_string())]));
73
74 assert!(measurement.matches_key_values(&[
76 ("env".to_string(), "production".to_string()),
77 ("branch".to_string(), "main".to_string()),
78 ]));
79
80 assert!(measurement.matches_key_values(&[
82 ("env".to_string(), "production".to_string()),
83 ("branch".to_string(), "main".to_string()),
84 ("cpu".to_string(), "x64".to_string()),
85 ]));
86
87 assert!(!measurement.matches_key_values(&[("env".to_string(), "staging".to_string())]));
89
90 assert!(!measurement.matches_key_values(&[("os".to_string(), "linux".to_string())]));
92
93 assert!(!measurement.matches_key_values(&[
95 ("env".to_string(), "production".to_string()), ("branch".to_string(), "develop".to_string()), ]));
98 }
99}