datafold 0.1.55

A personal database for data sovereignty with AI-powered ingestion
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Configuration management for the logging system
//!
//! This module handles loading and managing logging configuration from TOML files,
//! environment variables, and runtime updates.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;

/// Main logging configuration structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogConfig {
    /// General logging settings
    pub general: GeneralConfig,
    /// Output-specific configurations
    pub outputs: OutputsConfig,
    /// Feature-specific log levels
    pub features: HashMap<String, String>,
}

/// General logging configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GeneralConfig {
    /// Default log level for all modules
    pub default_level: String,
    /// Enable colored output
    pub enable_colors: bool,
    /// Enable correlation IDs for request tracking
    pub enable_correlation_ids: bool,
    /// Maximum correlation ID length
    pub max_correlation_id_length: usize,
    /// Default application/user ID
    pub app_id: Option<String>,
}

/// Configuration for all output types
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct OutputsConfig {
    /// Console output configuration
    pub console: ConsoleConfig,
    /// File output configuration
    pub file: FileConfig,
    /// Web streaming output configuration
    pub web: WebConfig,
    /// Structured JSON output configuration
    pub structured: StructuredConfig,
    /// DynamoDB output configuration
    #[cfg(feature = "aws-backend")]
    pub dynamodb: DynamoConfig,
}

/// Console output configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsoleConfig {
    /// Enable console output
    pub enabled: bool,
    /// Log level for console output
    pub level: String,
    /// Enable colors in console output
    pub colors: bool,
    /// Include timestamps
    pub include_timestamp: bool,
    /// Include module path
    pub include_module: bool,
    /// Include thread information
    pub include_thread: bool,
}

/// File output configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileConfig {
    /// Enable file output
    pub enabled: bool,
    /// Log file path
    pub path: String,
    /// Log level for file output
    pub level: String,
    /// Maximum file size before rotation (e.g., "10MB")
    pub max_size: String,
    /// Maximum number of log files to keep
    pub max_files: u32,
    /// Include timestamps
    pub include_timestamp: bool,
    /// Include module path
    pub include_module: bool,
    /// Include thread information
    pub include_thread: bool,
}

/// Web streaming output configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebConfig {
    /// Enable web streaming output
    pub enabled: bool,
    /// Log level for web output
    pub level: String,
    /// Buffer size for web streaming
    pub buffer_size: usize,
    /// Enable filtering in web interface
    pub enable_filtering: bool,
    /// Maximum number of logs to keep in memory
    pub max_logs: usize,
}

/// Structured JSON output configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StructuredConfig {
    /// Enable structured JSON output
    pub enabled: bool,
    /// Log level for structured output
    pub level: String,
    /// Output file path for structured logs
    pub path: Option<String>,
    /// Include additional context fields
    pub include_context: bool,
    /// Include performance metrics
    pub include_metrics: bool,
}

/// DynamoDB output configuration
#[cfg(feature = "aws-backend")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DynamoConfig {
    /// Enable DynamoDB output
    pub enabled: bool,
    /// Log level for DynamoDB output
    pub level: String,
    /// DynamoDB table name
    pub table_name: String,
    /// DynamoDB region (optional)
    pub region: Option<String>,
}

impl Default for LogConfig {
    fn default() -> Self {
        Self {
            general: GeneralConfig::default(),
            outputs: OutputsConfig::default(),
            features: Self::default_features(),
        }
    }
}

impl Default for GeneralConfig {
    fn default() -> Self {
        Self {
            default_level: "INFO".to_string(),
            enable_colors: true,
            enable_correlation_ids: true,
            max_correlation_id_length: 64,
            app_id: None,
        }
    }
}

impl Default for ConsoleConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            level: "INFO".to_string(),
            colors: true,
            include_timestamp: true,
            include_module: true,
            include_thread: false,
        }
    }
}

impl Default for FileConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            path: "logs/datafold.log".to_string(),
            level: "DEBUG".to_string(),
            max_size: "10MB".to_string(),
            max_files: 5,
            include_timestamp: true,
            include_module: true,
            include_thread: true,
        }
    }
}

impl Default for WebConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            level: "INFO".to_string(),
            buffer_size: 1000,
            enable_filtering: true,
            max_logs: 5000,
        }
    }
}

impl Default for StructuredConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            level: "DEBUG".to_string(),
            path: Some("logs/datafold-structured.json".to_string()),
            include_context: true,
            include_metrics: false,
        }
    }
}

#[cfg(feature = "aws-backend")]
impl Default for DynamoConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            level: "INFO".to_string(),
            table_name: "datafold-logs".to_string(),
            region: None,
        }
    }
}

impl LogConfig {
    /// Load configuration from a TOML file
    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, ConfigError> {
        let content = std::fs::read_to_string(path.as_ref()).map_err(ConfigError::Io)?;

        let mut config: LogConfig =
            toml::from_str(&content).map_err(|e| ConfigError::Parse(e.to_string()))?;

        // Apply environment variable overrides
        config.apply_env_overrides()?;

        Ok(config)
    }

    /// Load configuration from environment variables only
    pub fn from_env() -> Result<Self, ConfigError> {
        let mut config = Self::default();
        config.apply_env_overrides()?;
        Ok(config)
    }

    /// Apply environment variable overrides to the configuration
    pub fn apply_env_overrides(&mut self) -> Result<(), ConfigError> {
        // General settings
        if let Ok(level) = std::env::var("DATAFOLD_LOG_LEVEL") {
            self.general.default_level = level;
        }
        if let Ok(colors) = std::env::var("DATAFOLD_LOG_COLORS") {
            self.general.enable_colors = colors.parse().unwrap_or(true);
        }

        // Console settings
        if let Ok(enabled) = std::env::var("DATAFOLD_LOG_CONSOLE_ENABLED") {
            self.outputs.console.enabled = enabled.parse().unwrap_or(true);
        }
        if let Ok(level) = std::env::var("DATAFOLD_LOG_CONSOLE_LEVEL") {
            self.outputs.console.level = level;
        }

        // File settings
        if let Ok(enabled) = std::env::var("DATAFOLD_LOG_FILE_ENABLED") {
            self.outputs.file.enabled = enabled.parse().unwrap_or(false);
        }
        if let Ok(path) = std::env::var("DATAFOLD_LOG_FILE_PATH") {
            self.outputs.file.path = path;
        }
        if let Ok(level) = std::env::var("DATAFOLD_LOG_FILE_LEVEL") {
            self.outputs.file.level = level;
        }

        // Web settings
        if let Ok(enabled) = std::env::var("DATAFOLD_LOG_WEB_ENABLED") {
            self.outputs.web.enabled = enabled.parse().unwrap_or(true);
        }
        if let Ok(level) = std::env::var("DATAFOLD_LOG_WEB_LEVEL") {
            self.outputs.web.level = level;
        }

        // DynamoDB settings
        #[cfg(feature = "aws-backend")]
        {
            if let Ok(enabled) = std::env::var("DATAFOLD_LOG_DYNAMODB_ENABLED") {
                self.outputs.dynamodb.enabled = enabled.parse().unwrap_or(false);
            }
            if let Ok(level) = std::env::var("DATAFOLD_LOG_DYNAMODB_LEVEL") {
                self.outputs.dynamodb.level = level;
            }
            if let Ok(table) = std::env::var("DATAFOLD_LOG_DYNAMODB_TABLE") {
                self.outputs.dynamodb.table_name = table;
            }
            if let Ok(region) = std::env::var("DATAFOLD_LOG_DYNAMODB_REGION") {
                self.outputs.dynamodb.region = Some(region);
            }
        }

        // Feature-specific overrides
        for (key, value) in std::env::vars() {
            if let Some(feature) = key.strip_prefix("DATAFOLD_LOG_FEATURE_") {
                let feature_name = feature.to_lowercase();
                self.features.insert(feature_name, value);
            }
        }

        Ok(())
    }

    /// Save configuration to a TOML file
    pub fn save_to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), ConfigError> {
        let content =
            toml::to_string_pretty(self).map_err(|e| ConfigError::Serialize(e.to_string()))?;

        // Create parent directories if they don't exist
        if let Some(parent) = path.as_ref().parent() {
            std::fs::create_dir_all(parent).map_err(ConfigError::Io)?;
        }

        std::fs::write(path, content).map_err(ConfigError::Io)?;
        Ok(())
    }

    /// Get default feature-specific log levels
    fn default_features() -> HashMap<String, String> {
        let mut features = HashMap::new();
        features.insert("query".to_string(), "INFO".to_string());
        features.insert("mutation".to_string(), "INFO".to_string());
        features.insert("schema".to_string(), "INFO".to_string());
        features.insert("ingestion".to_string(), "INFO".to_string());
        features.insert("transform".to_string(), "DEBUG".to_string());
        features.insert("network".to_string(), "INFO".to_string());
        features.insert("permissions".to_string(), "INFO".to_string());
        features.insert("http_server".to_string(), "DEBUG".to_string());
        features.insert("tcp_server".to_string(), "INFO".to_string());
        features.insert("database".to_string(), "WARN".to_string());
        features
    }

    /// Validate the configuration
    pub fn validate(&self) -> Result<(), ConfigError> {
        // Validate log levels
        let valid_levels = ["TRACE", "DEBUG", "INFO", "WARN", "ERROR"];

        if !valid_levels.contains(&self.general.default_level.as_str()) {
            return Err(ConfigError::InvalidLevel(
                self.general.default_level.clone(),
            ));
        }

        if !valid_levels.contains(&self.outputs.console.level.as_str()) {
            return Err(ConfigError::InvalidLevel(
                self.outputs.console.level.clone(),
            ));
        }

        if !valid_levels.contains(&self.outputs.file.level.as_str()) {
            return Err(ConfigError::InvalidLevel(self.outputs.file.level.clone()));
        }

        if !valid_levels.contains(&self.outputs.web.level.as_str()) {
            return Err(ConfigError::InvalidLevel(self.outputs.web.level.clone()));
        }

        if !valid_levels.contains(&self.outputs.structured.level.as_str()) {
            return Err(ConfigError::InvalidLevel(
                self.outputs.structured.level.clone(),
            ));
        }

        #[cfg(feature = "aws-backend")]
        if !valid_levels.contains(&self.outputs.dynamodb.level.as_str()) {
            return Err(ConfigError::InvalidLevel(
                self.outputs.dynamodb.level.clone(),
            ));
        }

        // Validate feature levels
        for (feature, level) in &self.features {
            if !valid_levels.contains(&level.as_str()) {
                return Err(ConfigError::InvalidFeatureLevel(
                    feature.clone(),
                    level.clone(),
                ));
            }
        }

        // Validate file size format
        if self.outputs.file.enabled {
            self.parse_file_size(&self.outputs.file.max_size)?;
        }

        Ok(())
    }

    /// Parse file size string (e.g., "10MB", "1GB") to bytes
    fn parse_file_size(&self, size_str: &str) -> Result<u64, ConfigError> {
        let size_str = size_str.to_uppercase();

        if let Some(num_str) = size_str.strip_suffix("GB") {
            let num: u64 = num_str
                .parse()
                .map_err(|_| ConfigError::InvalidFileSize(size_str.clone()))?;
            Ok(num * 1024 * 1024 * 1024)
        } else if let Some(num_str) = size_str.strip_suffix("MB") {
            let num: u64 = num_str
                .parse()
                .map_err(|_| ConfigError::InvalidFileSize(size_str.clone()))?;
            Ok(num * 1024 * 1024)
        } else if let Some(num_str) = size_str.strip_suffix("KB") {
            let num: u64 = num_str
                .parse()
                .map_err(|_| ConfigError::InvalidFileSize(size_str.clone()))?;
            Ok(num * 1024)
        } else if let Some(num_str) = size_str.strip_suffix("B") {
            num_str
                .parse()
                .map_err(|_| ConfigError::InvalidFileSize(size_str.clone()))
        } else {
            // Default to bytes if no suffix
            size_str
                .parse()
                .map_err(|_| ConfigError::InvalidFileSize(size_str.clone()))
        }
    }
}

/// Configuration errors
#[derive(Debug, thiserror::Error)]
pub enum ConfigError {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
    #[error("Failed to parse configuration: {0}")]
    Parse(String),
    #[error("Failed to serialize configuration: {0}")]
    Serialize(String),
    #[error("Invalid log level: {0}")]
    InvalidLevel(String),
    #[error("Invalid log level for feature '{0}': {1}")]
    InvalidFeatureLevel(String, String),
    #[error("Invalid file size format: {0}")]
    InvalidFileSize(String),
}