oxirs_tsdb/
config.rs

1use serde::{Deserialize, Serialize};
2use std::time::Duration;
3
4/// Time-series database configuration
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct TsdbConfig {
7    /// Duration of each time chunk (default: 2 hours)
8    #[serde(with = "humantime_serde")]
9    pub chunk_duration: Duration,
10
11    /// Enable Gorilla compression
12    pub compression_enabled: bool,
13
14    /// In-memory write buffer size (number of data points)
15    pub buffer_size: usize,
16
17    /// Enable Write-Ahead Log for durability
18    pub wal_enabled: bool,
19
20    /// Sync WAL to disk on every write (slower but more durable)
21    pub wal_sync: bool,
22
23    /// Enable background compaction
24    pub compaction_enabled: bool,
25
26    /// Compaction interval
27    #[serde(with = "humantime_serde")]
28    pub compaction_interval: Duration,
29
30    /// Retention policies
31    pub retention_policies: Vec<RetentionPolicy>,
32}
33
34/// Retention policy for automatic data expiration
35#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct RetentionPolicy {
37    /// Policy name
38    pub name: String,
39
40    /// Retention duration
41    #[serde(with = "humantime_serde")]
42    pub duration: Duration,
43
44    /// Optional downsampling configuration
45    pub downsampling: Option<Downsampling>,
46}
47
48/// Downsampling configuration
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct Downsampling {
51    /// Original resolution
52    #[serde(with = "humantime_serde")]
53    pub from_resolution: Duration,
54
55    /// Target resolution after downsampling
56    #[serde(with = "humantime_serde")]
57    pub to_resolution: Duration,
58
59    /// Aggregation function
60    pub aggregation: AggregationFunction,
61}
62
63/// Aggregation functions for downsampling
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
65pub enum AggregationFunction {
66    /// Average value
67    #[serde(rename = "AVG")]
68    Average,
69    /// Minimum value
70    #[serde(rename = "MIN")]
71    Min,
72    /// Maximum value
73    #[serde(rename = "MAX")]
74    Max,
75    /// Sum of values
76    #[serde(rename = "SUM")]
77    Sum,
78    /// Last value (latest timestamp)
79    #[serde(rename = "LAST")]
80    Last,
81    /// First value (earliest timestamp)
82    #[serde(rename = "FIRST")]
83    First,
84}
85
86impl Default for TsdbConfig {
87    fn default() -> Self {
88        Self {
89            chunk_duration: Duration::from_secs(7200), // 2 hours
90            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), // 1 hour
96            retention_policies: vec![RetentionPolicy {
97                name: "raw".to_string(),
98                duration: Duration::from_secs(7 * 24 * 3600), // 7 days
99                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}