burncloud_service_models/
runtime.rs1use serde::{Deserialize, Serialize};
2use uuid::Uuid;
3use chrono::{DateTime, Utc};
4use std::collections::HashMap;
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct RuntimeConfig {
9 pub max_context_length: Option<u32>,
11 pub temperature: Option<f32>,
13 pub top_p: Option<f32>,
15 pub top_k: Option<u32>,
17 pub max_tokens: Option<u32>,
19 pub stop_sequences: Vec<String>,
21 pub batch_size: Option<u32>,
23 pub max_concurrent_requests: Option<u32>,
25 pub gpu_device_ids: Vec<u32>,
27 pub memory_limit_mb: Option<u64>,
29 pub enable_streaming: bool,
31 pub custom_params: HashMap<String, serde_json::Value>,
33}
34
35#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct ModelRuntime {
38 pub id: Uuid,
40 pub model_id: Uuid,
42 pub name: String,
44 pub port: u16,
46 pub process_id: Option<u32>,
48 pub config: RuntimeConfig,
50 pub started_at: Option<DateTime<Utc>>,
52 pub stopped_at: Option<DateTime<Utc>>,
54 pub status: crate::ModelStatus,
56 pub health_endpoint: String,
58 pub api_endpoint: String,
60 pub log_file: Option<String>,
62 pub environment: HashMap<String, String>,
64}
65
66#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct RuntimeMetrics {
69 pub runtime_id: Uuid,
71 pub timestamp: DateTime<Utc>,
73 pub cpu_usage_percent: f32,
75 pub memory_usage_mb: u64,
77 pub gpu_usage_percent: Option<f32>,
79 pub gpu_memory_usage_mb: Option<u64>,
81 pub active_connections: u32,
83 pub total_requests: u64,
85 pub successful_requests: u64,
87 pub failed_requests: u64,
89 pub avg_response_time_ms: f32,
91 pub throughput_rps: f32,
93 pub queue_length: u32,
95}
96
97#[derive(Debug, Clone, Serialize, Deserialize)]
99pub struct RuntimeEvent {
100 pub id: Uuid,
102 pub runtime_id: Uuid,
104 pub event_type: RuntimeEventType,
106 pub timestamp: DateTime<Utc>,
108 pub message: String,
110 pub details: Option<serde_json::Value>,
112 pub severity: EventSeverity,
114}
115
116#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
118pub enum RuntimeEventType {
119 Started,
121 Stopped,
123 Restarted,
125 ConfigUpdated,
127 HealthCheckFailed,
129 MemoryWarning,
131 Error,
133 RequestProcessed,
135 PerformanceWarning,
137}
138
139#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
141pub enum EventSeverity {
142 Info,
144 Warning,
146 Error,
148 Critical,
150}
151
152impl Default for RuntimeConfig {
153 fn default() -> Self {
154 Self {
155 max_context_length: Some(4096),
156 temperature: Some(0.7),
157 top_p: Some(0.9),
158 top_k: Some(50),
159 max_tokens: Some(2048),
160 stop_sequences: vec!["</s>".to_string(), "<|endoftext|>".to_string()],
161 batch_size: Some(1),
162 max_concurrent_requests: Some(10),
163 gpu_device_ids: vec![0],
164 memory_limit_mb: None,
165 enable_streaming: true,
166 custom_params: HashMap::new(),
167 }
168 }
169}
170
171impl ModelRuntime {
172 pub fn new(model_id: Uuid, name: String, port: u16) -> Self {
174 Self {
175 id: Uuid::new_v4(),
176 model_id,
177 name,
178 port,
179 process_id: None,
180 config: RuntimeConfig::default(),
181 started_at: None,
182 stopped_at: None,
183 status: crate::ModelStatus::Stopped,
184 health_endpoint: format!("http://localhost:{}/health", port),
185 api_endpoint: format!("http://localhost:{}/v1", port),
186 log_file: None,
187 environment: HashMap::new(),
188 }
189 }
190
191 pub fn mark_started(&mut self, process_id: u32) {
193 self.process_id = Some(process_id);
194 self.started_at = Some(Utc::now());
195 self.status = crate::ModelStatus::Running;
196 self.stopped_at = None;
197 }
198
199 pub fn mark_stopped(&mut self) {
201 self.process_id = None;
202 self.stopped_at = Some(Utc::now());
203 self.status = crate::ModelStatus::Stopped;
204 }
205
206 pub fn uptime_seconds(&self) -> Option<i64> {
208 self.started_at.map(|start| {
209 if self.status == crate::ModelStatus::Running {
210 Utc::now().timestamp() - start.timestamp()
211 } else {
212 self.stopped_at.unwrap_or(Utc::now()).timestamp() - start.timestamp()
213 }
214 })
215 }
216
217 pub fn is_healthy(&self) -> bool {
219 self.status == crate::ModelStatus::Running && self.process_id.is_some()
220 }
221}
222
223impl RuntimeEvent {
224 pub fn new(
226 runtime_id: Uuid,
227 event_type: RuntimeEventType,
228 message: String,
229 severity: EventSeverity,
230 ) -> Self {
231 Self {
232 id: Uuid::new_v4(),
233 runtime_id,
234 event_type,
235 timestamp: Utc::now(),
236 message,
237 details: None,
238 severity,
239 }
240 }
241
242 pub fn with_details(
244 runtime_id: Uuid,
245 event_type: RuntimeEventType,
246 message: String,
247 severity: EventSeverity,
248 details: serde_json::Value,
249 ) -> Self {
250 Self {
251 id: Uuid::new_v4(),
252 runtime_id,
253 event_type,
254 timestamp: Utc::now(),
255 message,
256 details: Some(details),
257 severity,
258 }
259 }
260}