oxigdal-ml 0.1.7

Machine learning capabilities for OxiGDAL - ONNX Runtime integration for geospatial ML workflows
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
//! Model serving and deployment utilities
//!
//! This module provides production-ready model serving capabilities including
//! model versioning, A/B testing, canary deployments, and load balancing.

use crate::error::{MlError, Result};
// use crate::models::Model;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
use tracing::{debug, info};

/// Model version information
#[derive(Debug, Clone)]
pub struct ModelVersion {
    /// Version identifier
    pub version: String,
    /// Model file path
    pub path: PathBuf,
    /// Deployment timestamp
    pub deployed_at: std::time::SystemTime,
    /// Model metadata
    pub metadata: HashMap<String, String>,
    /// Performance metrics
    pub metrics: VersionMetrics,
}

/// Performance metrics for a model version
#[derive(Debug, Clone, Default)]
pub struct VersionMetrics {
    /// Total requests served
    pub requests: u64,
    /// Average latency in milliseconds
    pub avg_latency_ms: f32,
    /// Success rate (0.0 to 1.0)
    pub success_rate: f32,
    /// Average CPU usage percentage
    pub avg_cpu_usage: f32,
    /// Average memory usage in MB
    pub avg_memory_mb: f32,
}

/// Deployment strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeploymentStrategy {
    /// Direct replacement
    Replace,
    /// Blue-green deployment
    BlueGreen,
    /// Canary deployment with gradual rollout
    Canary {
        /// Initial traffic percentage (0-100)
        initial_percent: u8,
        /// Step size for traffic increase
        step_percent: u8,
    },
    /// A/B testing
    ABTest {
        /// Traffic split percentage for new version
        split_percent: u8,
    },
    /// Shadow mode: the version is recorded as deployed for observation while
    /// the stable version keeps serving all user-facing traffic. The shadow
    /// version never returns user-facing results.
    Shadow,
}

/// Model server configuration
#[derive(Debug, Clone)]
pub struct ServerConfig {
    /// Maximum concurrent requests
    pub max_concurrent: usize,
    /// Request timeout in milliseconds
    pub timeout_ms: u64,
    /// Enable request queuing
    pub enable_queue: bool,
    /// Queue size limit
    pub queue_size: usize,
    /// Enable health checks
    pub health_check: bool,
    /// Health check interval in seconds
    pub health_check_interval_s: u64,
}

impl Default for ServerConfig {
    fn default() -> Self {
        Self {
            max_concurrent: 100,
            timeout_ms: 30000,
            enable_queue: true,
            queue_size: 1000,
            health_check: true,
            health_check_interval_s: 30,
        }
    }
}

/// Model server for production deployment
pub struct ModelServer {
    config: ServerConfig,
    versions: Arc<RwLock<HashMap<String, ModelVersion>>>,
    active_version: Arc<RwLock<String>>,
    routing: Arc<RwLock<RoutingStrategy>>,
}

/// Traffic routing strategy
#[derive(Debug, Clone)]
enum RoutingStrategy {
    /// Single version
    Single {
        /// Version ID
        version: String,
    },
    /// Weighted routing
    Weighted {
        /// Version weights (version -> percentage)
        weights: HashMap<String, u8>,
    },
    /// Canary routing
    Canary {
        /// Stable version
        stable: String,
        /// Canary version
        canary: String,
        /// Canary traffic percentage
        canary_percent: u8,
    },
    /// Shadow deployment: `stable` serves all user-facing traffic while `shadow`
    /// is recorded as deployed for observation only. This records deployment
    /// intent/state; it does not itself mirror live requests (this module has no
    /// request-serving path).
    Shadow {
        /// User-facing stable version
        stable: String,
        /// Shadow version (never serves user-facing results)
        shadow: String,
    },
}

impl ModelServer {
    /// Creates a new model server
    #[must_use]
    pub fn new(config: ServerConfig) -> Self {
        info!("Initializing model server");
        Self {
            config,
            versions: Arc::new(RwLock::new(HashMap::new())),
            active_version: Arc::new(RwLock::new(String::new())),
            routing: Arc::new(RwLock::new(RoutingStrategy::Single {
                version: String::new(),
            })),
        }
    }

    /// Registers a new model version
    ///
    /// # Errors
    /// Returns an error if version registration fails
    pub fn register_version(
        &mut self,
        version_id: &str,
        model_path: PathBuf,
        metadata: HashMap<String, String>,
    ) -> Result<()> {
        info!("Registering model version: {}", version_id);

        if !model_path.exists() {
            return Err(MlError::InvalidConfig(format!(
                "Model file not found: {}",
                model_path.display()
            )));
        }

        let version = ModelVersion {
            version: version_id.to_string(),
            path: model_path,
            deployed_at: std::time::SystemTime::now(),
            metadata,
            metrics: VersionMetrics::default(),
        };

        if let Ok(mut versions) = self.versions.write() {
            versions.insert(version_id.to_string(), version);
        }

        Ok(())
    }

    /// Deploys a model version using the specified strategy
    ///
    /// # Errors
    /// Returns an error if deployment fails
    pub fn deploy(&mut self, version_id: &str, strategy: DeploymentStrategy) -> Result<()> {
        info!(
            "Deploying version {} with strategy {:?}",
            version_id, strategy
        );

        // Verify version exists
        let version_exists = self
            .versions
            .read()
            .map(|v| v.contains_key(version_id))
            .unwrap_or(false);

        if !version_exists {
            return Err(MlError::InvalidConfig(format!(
                "Version not found: {}",
                version_id
            )));
        }

        match strategy {
            DeploymentStrategy::Replace => self.deploy_replace(version_id),
            DeploymentStrategy::BlueGreen => self.deploy_blue_green(version_id),
            DeploymentStrategy::Canary {
                initial_percent,
                step_percent,
            } => self.deploy_canary(version_id, initial_percent, step_percent),
            DeploymentStrategy::ABTest { split_percent } => {
                self.deploy_ab_test(version_id, split_percent)
            }
            DeploymentStrategy::Shadow => self.deploy_shadow(version_id),
        }
    }

    /// Rolls back to a previous version
    ///
    /// # Errors
    /// Returns an error if rollback fails
    pub fn rollback(&mut self, version_id: &str) -> Result<()> {
        info!("Rolling back to version: {}", version_id);
        self.deploy_replace(version_id)
    }

    /// Returns metrics for all versions
    #[must_use]
    pub fn version_metrics(&self) -> HashMap<String, VersionMetrics> {
        self.versions
            .read()
            .map(|versions| {
                versions
                    .iter()
                    .map(|(k, v)| (k.clone(), v.metrics.clone()))
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Returns the active version
    #[must_use]
    pub fn active_version(&self) -> String {
        self.active_version
            .read()
            .map(|v| v.clone())
            .unwrap_or_default()
    }

    /// Performs health check on active version
    #[must_use]
    pub fn health_check(&self) -> HealthStatus {
        if !self.config.health_check {
            return HealthStatus::Unknown;
        }

        // Check if any model is loaded
        let has_models = self.versions.read().map(|v| !v.is_empty()).unwrap_or(false);

        if !has_models {
            return HealthStatus::Unhealthy;
        }

        // Check if active version exists
        let active_version = self.active_version();
        if active_version.is_empty() {
            return HealthStatus::Degraded;
        }

        // Verify active version is in versions map
        let version_exists = self
            .versions
            .read()
            .map(|v| v.contains_key(&active_version))
            .unwrap_or(false);

        if !version_exists {
            return HealthStatus::Unhealthy;
        }

        // Check live memory pressure. The most severe threshold must be tested
        // first so that > 95% reports Unhealthy rather than being shadowed by the
        // > 90% Degraded branch.
        if let Ok(memory_info) = Self::get_memory_usage() {
            if let Some(status) = Self::memory_pressure_status(memory_info.usage_percent) {
                return status;
            }
        }

        HealthStatus::Healthy
    }

    /// Maps a memory-usage percentage to a degraded/unhealthy health status.
    ///
    /// Returns `None` when usage is within safe limits. Extracted as a pure
    /// function so the degradation thresholds are unit-testable without
    /// fabricating system memory.
    fn memory_pressure_status(usage_percent: f32) -> Option<HealthStatus> {
        if usage_percent > 95.0 {
            Some(HealthStatus::Unhealthy)
        } else if usage_percent > 90.0 {
            Some(HealthStatus::Degraded)
        } else {
            None
        }
    }

    /// Gets current memory usage information
    fn get_memory_usage() -> Result<MemoryInfo> {
        #[cfg(target_os = "linux")]
        {
            Self::get_memory_usage_linux()
        }

        #[cfg(target_os = "macos")]
        {
            Self::get_memory_usage_macos()
        }

        #[cfg(target_os = "windows")]
        {
            Self::get_memory_usage_windows()
        }

        #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
        {
            // Default fallback for unsupported platforms
            Ok(MemoryInfo {
                total_mb: 0,
                used_mb: 0,
                available_mb: 0,
                usage_percent: 0.0,
            })
        }
    }

    #[cfg(target_os = "linux")]
    fn get_memory_usage_linux() -> Result<MemoryInfo> {
        use std::fs;

        let meminfo = fs::read_to_string("/proc/meminfo")
            .map_err(|e| MlError::InvalidConfig(format!("Failed to read meminfo: {}", e)))?;

        let mut total = 0u64;
        let mut available = 0u64;

        for line in meminfo.lines() {
            if let Some(rest) = line.strip_prefix("MemTotal:") {
                total = rest
                    .trim()
                    .split_whitespace()
                    .next()
                    .and_then(|s| s.parse::<u64>().ok())
                    .unwrap_or(0);
            } else if let Some(rest) = line.strip_prefix("MemAvailable:") {
                available = rest
                    .trim()
                    .split_whitespace()
                    .next()
                    .and_then(|s| s.parse::<u64>().ok())
                    .unwrap_or(0);
            }
        }

        let total_mb = total / 1024;
        let available_mb = available / 1024;
        let used_mb = total_mb.saturating_sub(available_mb);
        let usage_percent = if total_mb > 0 {
            (used_mb as f32 / total_mb as f32) * 100.0
        } else {
            0.0
        };

        Ok(MemoryInfo {
            total_mb,
            used_mb,
            available_mb,
            usage_percent,
        })
    }

    #[cfg(target_os = "macos")]
    fn get_memory_usage_macos() -> Result<MemoryInfo> {
        // Query live memory via the Pure-Rust `sysinfo` crate (already a
        // dependency), which reads real system statistics on macOS.
        Self::get_memory_usage_sysinfo()
    }

    #[cfg(target_os = "windows")]
    fn get_memory_usage_windows() -> Result<MemoryInfo> {
        // Query live memory via the Pure-Rust `sysinfo` crate (already a
        // dependency), which wraps GlobalMemoryStatusEx on Windows.
        Self::get_memory_usage_sysinfo()
    }

    /// Reads live memory statistics via the Pure-Rust `sysinfo` crate.
    ///
    /// `sysinfo` reports memory in bytes; values are converted to MB. This is
    /// used on platforms without a bespoke reader (macOS, Windows) so that
    /// `health_check` observes real memory pressure instead of a fabricated
    /// constant.
    #[cfg(any(target_os = "macos", target_os = "windows"))]
    fn get_memory_usage_sysinfo() -> Result<MemoryInfo> {
        use sysinfo::System;

        let mut system = System::new();
        system.refresh_memory();

        // sysinfo returns bytes (>= 0.30).
        let total_bytes = system.total_memory();
        if total_bytes == 0 {
            return Err(MlError::InvalidConfig(
                "sysinfo reported zero total memory".to_string(),
            ));
        }
        let available_bytes = system.available_memory();

        let total_mb = total_bytes / (1024 * 1024);
        let available_mb = available_bytes / (1024 * 1024);
        let used_mb = total_mb.saturating_sub(available_mb);
        let usage_percent = ((total_bytes.saturating_sub(available_bytes)) as f64
            / total_bytes as f64
            * 100.0) as f32;

        Ok(MemoryInfo {
            total_mb,
            used_mb,
            available_mb,
            usage_percent,
        })
    }

    // Private deployment methods

    fn deploy_replace(&mut self, version_id: &str) -> Result<()> {
        debug!("Deploying with replace strategy");

        if let Ok(mut active) = self.active_version.write() {
            *active = version_id.to_string();
        }

        if let Ok(mut routing) = self.routing.write() {
            *routing = RoutingStrategy::Single {
                version: version_id.to_string(),
            };
        }

        info!("Version {} deployed successfully", version_id);
        Ok(())
    }

    fn deploy_blue_green(&mut self, version_id: &str) -> Result<()> {
        debug!("Deploying with blue-green strategy");

        // In blue-green, we prepare the new version first
        // Then switch traffic atomically
        self.deploy_replace(version_id)
    }

    fn deploy_canary(
        &mut self,
        version_id: &str,
        initial_percent: u8,
        _step_percent: u8,
    ) -> Result<()> {
        debug!(
            "Deploying with canary strategy ({}% initial)",
            initial_percent
        );

        let stable_version = self.active_version();

        if let Ok(mut routing) = self.routing.write() {
            *routing = RoutingStrategy::Canary {
                stable: stable_version,
                canary: version_id.to_string(),
                canary_percent: initial_percent,
            };
        }

        info!("Canary deployment started for version {}", version_id);
        Ok(())
    }

    fn deploy_ab_test(&mut self, version_id: &str, split_percent: u8) -> Result<()> {
        debug!("Deploying with A/B test ({}% split)", split_percent);

        let stable_version = self.active_version();
        let mut weights = HashMap::new();
        weights.insert(stable_version, 100 - split_percent);
        weights.insert(version_id.to_string(), split_percent);

        if let Ok(mut routing) = self.routing.write() {
            *routing = RoutingStrategy::Weighted { weights };
        }

        info!("A/B test started for version {}", version_id);
        Ok(())
    }

    fn deploy_shadow(&mut self, version_id: &str) -> Result<()> {
        debug!("Deploying in shadow mode");

        // Shadow mode records the new version as deployed-for-observation while
        // the current active (stable) version keeps serving all user-facing
        // traffic. `active_version` is deliberately NOT changed so the shadow
        // version never becomes user-facing. This records deployment state so it
        // is introspectable and consistent with the other deploy_* methods; it
        // does not mirror live requests (no request-serving path exists here).
        let stable_version = self.active_version();

        if let Ok(mut routing) = self.routing.write() {
            *routing = RoutingStrategy::Shadow {
                stable: stable_version,
                shadow: version_id.to_string(),
            };
        }

        info!("Version {} deployed in shadow mode", version_id);
        Ok(())
    }

    /// Increases canary traffic percentage
    ///
    /// # Errors
    /// Returns an error if not in canary mode
    pub fn increase_canary_traffic(&mut self, increment: u8) -> Result<()> {
        let mut routing = self
            .routing
            .write()
            .map_err(|_| MlError::InvalidConfig("Failed to acquire routing lock".to_string()))?;

        match &mut *routing {
            RoutingStrategy::Canary { canary_percent, .. } => {
                *canary_percent = (*canary_percent + increment).min(100);
                info!("Increased canary traffic to {}%", canary_percent);
                Ok(())
            }
            _ => Err(MlError::InvalidConfig(
                "Not in canary deployment mode".to_string(),
            )),
        }
    }

    /// Promotes canary to stable
    ///
    /// # Errors
    /// Returns an error if not in canary mode
    pub fn promote_canary(&mut self) -> Result<()> {
        let routing = self
            .routing
            .read()
            .map_err(|_| MlError::InvalidConfig("Failed to acquire routing lock".to_string()))?;

        if let RoutingStrategy::Canary { canary, .. } = &*routing {
            let canary_version = canary.clone();
            drop(routing); // Release read lock
            self.deploy_replace(&canary_version)?;
            info!("Canary promoted to stable");
            Ok(())
        } else {
            Err(MlError::InvalidConfig(
                "Not in canary deployment mode".to_string(),
            ))
        }
    }
}

/// Health status
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HealthStatus {
    /// Service is healthy
    Healthy,
    /// Service is degraded but operational
    Degraded,
    /// Service is unhealthy
    Unhealthy,
    /// Health status unknown
    Unknown,
}

/// Memory usage information
#[derive(Debug, Clone)]
struct MemoryInfo {
    /// Total memory in MB
    total_mb: u64,
    /// Used memory in MB
    used_mb: u64,
    /// Available memory in MB
    available_mb: u64,
    /// Memory usage percentage
    usage_percent: f32,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_server_config_default() {
        let config = ServerConfig::default();
        assert_eq!(config.max_concurrent, 100);
        assert_eq!(config.timeout_ms, 30000);
        assert!(config.enable_queue);
    }

    #[test]
    fn test_deployment_strategy_variants() {
        let strategies = vec![
            DeploymentStrategy::Replace,
            DeploymentStrategy::BlueGreen,
            DeploymentStrategy::Canary {
                initial_percent: 10,
                step_percent: 10,
            },
            DeploymentStrategy::ABTest { split_percent: 50 },
            DeploymentStrategy::Shadow,
        ];

        for strategy in strategies {
            // Just verify they can be created
            let _ = format!("{:?}", strategy);
        }
    }

    #[test]
    fn test_model_server_creation() {
        let config = ServerConfig::default();
        let server = ModelServer::new(config);
        assert_eq!(server.active_version(), "");
    }

    #[test]
    fn test_health_status() {
        assert_eq!(HealthStatus::Healthy, HealthStatus::Healthy);
        assert_ne!(HealthStatus::Healthy, HealthStatus::Degraded);
    }

    #[test]
    fn test_memory_pressure_status_thresholds() {
        // Below 90%: healthy (None).
        assert_eq!(ModelServer::memory_pressure_status(50.0), None);
        assert_eq!(ModelServer::memory_pressure_status(90.0), None);
        // Between 90% and 95%: degraded.
        assert_eq!(
            ModelServer::memory_pressure_status(92.0),
            Some(HealthStatus::Degraded)
        );
        // Above 95%: unhealthy (must NOT be shadowed by the degraded branch).
        assert_eq!(
            ModelServer::memory_pressure_status(97.0),
            Some(HealthStatus::Unhealthy)
        );
    }

    #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
    #[test]
    fn test_get_memory_usage_is_live_not_constant() {
        // On supported platforms, memory stats must be real (non-zero total,
        // usage within 0..=100), not the old fabricated 50% / 16384 MB constant.
        let info = ModelServer::get_memory_usage().expect("memory usage query");
        assert!(info.total_mb > 0, "total memory should be positive");
        assert!(
            info.usage_percent >= 0.0 && info.usage_percent <= 100.0,
            "usage_percent out of range: {}",
            info.usage_percent
        );
        assert!(info.available_mb <= info.total_mb);
    }

    #[test]
    fn test_deploy_shadow_records_state() {
        use std::io::Write;

        // register_version requires the model file to exist on disk.
        let dir = std::env::temp_dir();
        let stable_path = dir.join("oxigdal_ml_shadow_stable.onnx");
        let shadow_path = dir.join("oxigdal_ml_shadow_new.onnx");
        for p in [&stable_path, &shadow_path] {
            let mut f = std::fs::File::create(p).expect("create temp model file");
            f.write_all(b"onnx").expect("write temp model");
        }

        let mut server = ModelServer::new(ServerConfig::default());
        server
            .register_version("v1", stable_path.clone(), HashMap::new())
            .expect("register v1");
        server
            .register_version("v2", shadow_path.clone(), HashMap::new())
            .expect("register v2");

        // v1 becomes the active/stable version.
        server
            .deploy("v1", DeploymentStrategy::Replace)
            .expect("deploy v1");
        assert_eq!(server.active_version(), "v1");

        // Deploy v2 in shadow mode: routing state must reflect it, and the active
        // (user-facing) version must stay v1.
        server
            .deploy("v2", DeploymentStrategy::Shadow)
            .expect("deploy v2 shadow");
        assert_eq!(
            server.active_version(),
            "v1",
            "shadow must not become active"
        );

        let routing = server.routing.read().expect("read routing");
        match &*routing {
            RoutingStrategy::Shadow { stable, shadow } => {
                assert_eq!(stable, "v1");
                assert_eq!(shadow, "v2");
            }
            other => panic!("expected Shadow routing, got {:?}", other),
        }
        drop(routing);

        let _ = std::fs::remove_file(stable_path);
        let _ = std::fs::remove_file(shadow_path);
    }

    #[test]
    fn test_version_metrics() {
        let metrics = VersionMetrics {
            requests: 1000,
            avg_latency_ms: 50.0,
            success_rate: 0.99,
            avg_cpu_usage: 45.0,
            avg_memory_mb: 512.0,
        };

        assert_eq!(metrics.requests, 1000);
        assert!((metrics.success_rate - 0.99).abs() < 0.01);
    }
}