tsdb/
meta.rs

1use serde::{Deserialize, Serialize};
2use std::cmp::{Eq, PartialEq};
3use std::{fs::read_to_string, path::Path};
4
5#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
6pub struct BlockStats {
7    #[serde(rename = "numSamples")]
8    pub num_samples: u64,
9    #[serde(rename = "numSeries")]
10    pub num_series: u64,
11    #[serde(rename = "numChunks")]
12    pub num_chunks: u64,
13}
14
15#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
16pub struct BlockCompaction {
17    pub level: u8,
18    pub sources: Vec<String>,
19}
20
21#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
22pub struct MetaData {
23    pub version: u8,
24    pub ulid: String, // TODO use ulid type
25    #[serde(rename = "minTime")]
26    pub min_time: u64,
27    #[serde(rename = "maxTime")]
28    pub max_time: u64,
29    pub stats: BlockStats,
30    pub compaction: BlockCompaction,
31}
32
33impl MetaData {
34    pub fn new(path: &Path) -> Self {
35        let content =
36            read_to_string(path).expect("Something went wrong reading the test meta.json file");
37
38        let m: MetaData = serde_json::from_str(&content).expect("Failed to deserialize meta.json");
39
40        m
41    }
42}
43
44#[cfg(test)]
45mod test {
46    use super::*;
47
48    #[test]
49    fn load_meta_data() {
50        let meta = MetaData::new(Path::new("testdata/testblock/meta.json"));
51
52        let expected = MetaData {
53            version: 1,
54            ulid: String::from("01G0P9P6T0VSA5Z484BRGH2N68"),
55            min_time: 1650005003777,
56            max_time: 1650009600000,
57            stats: BlockStats {
58                num_samples: 5551013,
59                num_series: 35354,
60                num_chunks: 37020,
61            },
62            compaction: BlockCompaction {
63                level: 1,
64                sources: [String::from("01G0P9P6T0VSA5Z484BRGH2N68")].to_vec(),
65            },
66        };
67
68        assert_eq!(meta, expected);
69
70        let ser = serde_json::to_string_pretty(&meta).unwrap();
71        let expected = r#"{
72  "version": 1,
73  "ulid": "01G0P9P6T0VSA5Z484BRGH2N68",
74  "minTime": 1650005003777,
75  "maxTime": 1650009600000,
76  "stats": {
77    "numSamples": 5551013,
78    "numSeries": 35354,
79    "numChunks": 37020
80  },
81  "compaction": {
82    "level": 1,
83    "sources": [
84      "01G0P9P6T0VSA5Z484BRGH2N68"
85    ]
86  }
87}"#;
88
89        assert_eq!(expected, ser);
90    }
91}