1use serde::{Deserialize, Serialize};
2use std::time::Duration;
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct TsdbConfig {
7 #[serde(with = "humantime_serde")]
9 pub chunk_duration: Duration,
10
11 pub compression_enabled: bool,
13
14 pub buffer_size: usize,
16
17 pub wal_enabled: bool,
19
20 pub wal_sync: bool,
22
23 pub compaction_enabled: bool,
25
26 #[serde(with = "humantime_serde")]
28 pub compaction_interval: Duration,
29
30 pub retention_policies: Vec<RetentionPolicy>,
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct RetentionPolicy {
37 pub name: String,
39
40 #[serde(with = "humantime_serde")]
42 pub duration: Duration,
43
44 pub downsampling: Option<Downsampling>,
46}
47
48#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct Downsampling {
51 #[serde(with = "humantime_serde")]
53 pub from_resolution: Duration,
54
55 #[serde(with = "humantime_serde")]
57 pub to_resolution: Duration,
58
59 pub aggregation: AggregationFunction,
61}
62
63#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
65pub enum AggregationFunction {
66 #[serde(rename = "AVG")]
68 Average,
69 #[serde(rename = "MIN")]
71 Min,
72 #[serde(rename = "MAX")]
74 Max,
75 #[serde(rename = "SUM")]
77 Sum,
78 #[serde(rename = "LAST")]
80 Last,
81 #[serde(rename = "FIRST")]
83 First,
84}
85
86impl Default for TsdbConfig {
87 fn default() -> Self {
88 Self {
89 chunk_duration: Duration::from_secs(7200), compression_enabled: true,
91 buffer_size: 100_000,
92 wal_enabled: true,
93 wal_sync: false,
94 compaction_enabled: true,
95 compaction_interval: Duration::from_secs(3600), retention_policies: vec![RetentionPolicy {
97 name: "raw".to_string(),
98 duration: Duration::from_secs(7 * 24 * 3600), downsampling: None,
100 }],
101 }
102 }
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108
109 #[test]
110 fn test_default_config() {
111 let config = TsdbConfig::default();
112 assert!(config.compression_enabled);
113 assert_eq!(config.buffer_size, 100_000);
114 assert_eq!(config.retention_policies.len(), 1);
115 }
116}