lighthouse/
types.rs

1// src/types.rs
2
3use std::collections::HashMap;
4use serde::{Deserialize, Serialize};
5
6/// Unique identifier for a resource (e.g., "web-servers", "database-pool")
7pub type ResourceId = String;
8
9/// A metric value (CPU %, memory usage, request count, etc.)
10pub type MetricValue = f64;
11
12/// Unix timestamp in seconds
13pub type Timestamp = u64;
14
15/// Represents metrics for a specific resource at a point in time
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct ResourceMetrics {
18    /// Unique identifier for this resource
19    pub resource_id: ResourceId,
20    /// Type of resource (e.g., "kubernetes-deployment", "ec2-asg", "database")
21    pub resource_type: String,
22    /// When these metrics were collected
23    pub timestamp: Timestamp,
24    /// Key-value pairs of metric names to values
25    /// Examples: "cpu_percent" -> 75.5, "memory_mb" -> 2048.0
26    pub metrics: HashMap<String, MetricValue>,
27}
28
29/// Direction to scale a resource
30#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
31pub enum ScaleDirection {
32    /// Scale up (add resources)
33    Up,
34    /// Scale down (remove resources)  
35    Down,
36    /// Keep current scale
37    Maintain,
38}
39
40/// A scaling recommendation from the lighthouse engine
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct ScaleAction {
43    /// Which resource to scale
44    pub resource_id: ResourceId,
45    /// Type of resource being scaled
46    pub resource_type: String,
47    /// Direction to scale
48    pub direction: ScaleDirection,
49    /// Specific target (e.g., number of instances)
50    pub target_capacity: Option<u32>,
51    /// Multiplier for scaling (e.g., 1.5x current capacity)
52    pub scale_factor: Option<f64>,
53    /// Human-readable explanation
54    pub reason: String,
55    /// How confident the engine is (0.0 = uncertain, 1.0 = very confident)
56    pub confidence: f64,
57    /// When this recommendation was generated
58    pub timestamp: Timestamp,
59    /// Optional metadata for the scaling action
60    pub metadata: HashMap<String, String>,
61}
62
63/// Defines when and how to scale based on metric thresholds
64#[derive(Debug, Clone, Serialize, Deserialize)]
65pub struct ScalingThreshold {
66    /// The metric to watch (e.g., "cpu_percent")
67    pub metric_name: String,
68    /// Scale up when metric goes above this value
69    pub scale_up_threshold: MetricValue,
70    /// Scale down when metric goes below this value  
71    pub scale_down_threshold: MetricValue,
72    /// How much to scale by (e.g., 1.5 = increase by 50%)
73    pub scale_factor: f64,
74    /// Minimum time between scaling actions (prevents flapping)
75    pub cooldown_seconds: u64,
76}
77
78/// A scaling policy that combines multiple thresholds and rules
79#[derive(Debug, Clone, Serialize, Deserialize)]
80pub struct ScalingPolicy {
81    /// Name of this policy (e.g., "aggressive-cpu-scaling")
82    pub name: String,
83    /// List of thresholds to evaluate
84    pub thresholds: Vec<ScalingThreshold>,
85    /// Minimum number of instances/capacity
86    pub min_capacity: Option<u32>,
87    /// Maximum number of instances/capacity  
88    pub max_capacity: Option<u32>,
89    /// Whether this policy is currently active
90    pub enabled: bool,
91}
92
93/// Configuration for a specific resource type
94#[derive(Debug, Clone, Serialize, Deserialize)]
95pub struct ResourceConfig {
96    /// Type identifier (e.g., "kubernetes-deployment")
97    pub resource_type: String,
98    /// Scaling policies for this resource type
99    pub policies: Vec<ScalingPolicy>,
100    /// Default policy to use if none specified
101    pub default_policy: Option<String>,
102    /// Custom settings specific to this resource type
103    pub settings: HashMap<String, String>,
104}
105
106/// Main configuration for the lighthouse engine
107#[derive(Debug, Clone, Serialize, Deserialize)]
108pub struct LighthouseConfig {
109    /// How often to evaluate scaling decisions (seconds)
110    pub evaluation_interval_seconds: u64,
111    /// Configuration for each resource type
112    pub resource_configs: HashMap<String, ResourceConfig>,
113    /// Global settings that apply to all resources
114    pub global_settings: HashMap<String, String>,
115    /// Whether to log scaling decisions
116    pub enable_logging: bool,
117}
118
119impl Default for LighthouseConfig {
120    fn default() -> Self {
121        Self {
122            evaluation_interval_seconds: 30,
123            resource_configs: HashMap::new(),
124            global_settings: HashMap::new(),
125            enable_logging: true,
126        }
127    }
128}
129
130/// Builder pattern for easy configuration creation
131impl LighthouseConfig {
132    pub fn builder() -> LighthouseConfigBuilder {
133        LighthouseConfigBuilder::new()
134    }
135}
136
137/// Builder for creating lighthouse configurations easily
138#[derive(Debug)]
139pub struct LighthouseConfigBuilder {
140    config: LighthouseConfig,
141}
142
143impl LighthouseConfigBuilder {
144    pub fn new() -> Self {
145        Self {
146            config: LighthouseConfig::default(),
147        }
148    }
149
150    pub fn evaluation_interval(mut self, seconds: u64) -> Self {
151        self.config.evaluation_interval_seconds = seconds;
152        self
153    }
154
155    pub fn add_resource_config(mut self, resource_type: &str, config: ResourceConfig) -> Self {
156        self.config.resource_configs.insert(resource_type.to_string(), config);
157        self
158    }
159
160    pub fn global_setting(mut self, key: &str, value: &str) -> Self {
161        self.config.global_settings.insert(key.to_string(), value.to_string());
162        self
163    }
164
165    pub fn enable_logging(mut self, enabled: bool) -> Self {
166        self.config.enable_logging = enabled;
167        self
168    }
169
170    pub fn build(self) -> LighthouseConfig {
171        self.config
172    }
173}