/home/jgrusewski/Work/foxhunt/config/src/storage_config.rs
Line | Count | Source |
1 | | //! Model storage and metadata configuration structures. |
2 | | //! |
3 | | //! This module defines configuration structures for managing ML model metadata, |
4 | | //! training metrics, and architectural information. Used for model versioning, |
5 | | //! performance tracking, and deployment management in the Foxhunt trading system. |
6 | | |
7 | | use chrono::{DateTime, Utc}; |
8 | | use serde::{Deserialize, Serialize}; |
9 | | use std::path::PathBuf; |
10 | | use uuid::Uuid; |
11 | | |
12 | | /// Comprehensive metadata for ML model storage and tracking. |
13 | | /// |
14 | | /// Contains all information necessary for model identification, versioning, |
15 | | /// and performance tracking. Used for model lifecycle management and |
16 | | /// deployment coordination across the trading system. |
17 | | #[derive(Debug, Clone, Serialize, Deserialize)] |
18 | | pub struct ModelMetadata { |
19 | | /// Unique identifier for this model instance |
20 | | pub id: Uuid, |
21 | | /// Human-readable model name (e.g., "mamba2-price-prediction") |
22 | | pub name: String, |
23 | | /// Semantic version string (e.g., "1.2.3") |
24 | | pub version: String, |
25 | | /// Timestamp when this model was created/trained |
26 | | pub created_at: DateTime<Utc>, |
27 | | /// Timestamp when this model metadata was last updated |
28 | | pub updated_at: DateTime<Utc>, |
29 | | /// Training performance metrics for model evaluation |
30 | | pub training_metrics: TrainingMetrics, |
31 | | /// Model architecture and hyperparameter configuration |
32 | | pub architecture: ModelArchitecture, |
33 | | } |
34 | | |
35 | | /// Training performance metrics for model evaluation. |
36 | | /// |
37 | | /// Captures key performance indicators from model training to enable |
38 | | /// comparison between different model versions and architectures. |
39 | | /// Essential for model selection and performance monitoring. |
40 | | #[derive(Debug, Clone, Serialize, Deserialize)] |
41 | | pub struct TrainingMetrics { |
42 | | /// Final training accuracy (0.0 to 1.0) |
43 | | pub accuracy: f64, |
44 | | /// Final training loss value |
45 | | pub loss: f64, |
46 | | /// Final validation accuracy (0.0 to 1.0) |
47 | | pub validation_accuracy: f64, |
48 | | /// Final validation loss value |
49 | | pub validation_loss: f64, |
50 | | /// Number of training epochs completed |
51 | | pub epochs: u32, |
52 | | /// Total training time in seconds |
53 | | pub training_time_seconds: f64, |
54 | | } |
55 | | |
56 | | /// Model architecture and hyperparameter specification. |
57 | | /// |
58 | | /// Defines the structural configuration of ML models including layer |
59 | | /// dimensions, activation functions, and optimization parameters. |
60 | | /// Used for model reconstruction and hyperparameter tracking. |
61 | | #[derive(Debug, Clone, Serialize, Deserialize)] |
62 | | pub struct ModelArchitecture { |
63 | | /// Model type identifier (e.g., "mamba2", "transformer", "dqn") |
64 | | pub model_type: String, |
65 | | /// Input feature dimension size |
66 | | pub input_dim: usize, |
67 | | /// Output prediction dimension size |
68 | | pub output_dim: usize, |
69 | | /// Hidden layer sizes in order from input to output |
70 | | pub hidden_layers: Vec<usize>, |
71 | | /// Activation function name (e.g., "relu", "gelu", "swish") |
72 | | pub activation: String, |
73 | | /// Optimizer type (e.g., "adam", "sgd", "adamw") |
74 | | pub optimizer: String, |
75 | | /// Learning rate used during training |
76 | | pub learning_rate: f64, |
77 | | } |
78 | | |
79 | | /// Storage configuration for model artifacts |
80 | | #[derive(Debug, Clone, Serialize, Deserialize)] |
81 | | pub struct StorageConfig { |
82 | | /// Storage type (e.g., "local", "s3") |
83 | | pub storage_type: String, |
84 | | /// Local base path for file storage (required for "local" storage type) |
85 | | pub local_base_path: Option<PathBuf>, |
86 | | /// Enable compression for stored models |
87 | | pub enable_compression: bool, |
88 | | } |
89 | | |
90 | | impl Default for StorageConfig { |
91 | 0 | fn default() -> Self { |
92 | 0 | Self { |
93 | 0 | storage_type: "local".to_string(), |
94 | 0 | local_base_path: Some(PathBuf::from("/tmp/foxhunt/models")), |
95 | 0 | enable_compression: false, |
96 | 0 | } |
97 | 0 | } |
98 | | } |
99 | | |
100 | | impl StorageConfig { |
101 | | /// Create StorageConfig from environment variables |
102 | 0 | pub fn from_env() -> Result<Self, Box<dyn std::error::Error>> { |
103 | 0 | let storage_type = std::env::var("STORAGE_TYPE").unwrap_or_else(|_| "local".to_string()); |
104 | 0 | let local_base_path = std::env::var("STORAGE_LOCAL_PATH") |
105 | 0 | .ok() |
106 | 0 | .map(PathBuf::from) |
107 | 0 | .or_else(|| Some(PathBuf::from("/tmp/foxhunt/models"))); |
108 | 0 | let enable_compression = std::env::var("STORAGE_ENABLE_COMPRESSION") |
109 | 0 | .ok() |
110 | 0 | .and_then(|v| v.parse().ok()) |
111 | 0 | .unwrap_or(false); |
112 | | |
113 | 0 | Ok(Self { |
114 | 0 | storage_type, |
115 | 0 | local_base_path, |
116 | 0 | enable_compression, |
117 | 0 | }) |
118 | 0 | } |
119 | | } |