oxigdal-edge 0.1.4

Edge computing platform for OxiGDAL with offline-first architecture and minimal footprint
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
//! Resource management for constrained edge devices
//!
//! Monitors and manages CPU, memory, and storage resources to ensure
//! efficient operation on resource-limited devices.

use crate::error::{EdgeError, Result};
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};

/// Resource constraints for edge devices
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceConstraints {
    /// Maximum memory usage in bytes
    pub max_memory_bytes: usize,
    /// Maximum CPU usage percentage (0-100)
    pub max_cpu_percent: f64,
    /// Maximum storage usage in bytes
    pub max_storage_bytes: usize,
    /// Maximum concurrent operations
    pub max_concurrent_ops: usize,
    /// Operation timeout in seconds
    pub operation_timeout_secs: u64,
}

impl Default for ResourceConstraints {
    fn default() -> Self {
        Self {
            max_memory_bytes: crate::MAX_MEMORY_USAGE,
            max_cpu_percent: 80.0,
            max_storage_bytes: 100 * 1024 * 1024, // 100 MB
            max_concurrent_ops: 10,
            operation_timeout_secs: 30,
        }
    }
}

impl ResourceConstraints {
    /// Create constraints for minimal embedded devices
    pub fn minimal() -> Self {
        Self {
            max_memory_bytes: 10 * 1024 * 1024, // 10 MB
            max_cpu_percent: 50.0,
            max_storage_bytes: 10 * 1024 * 1024, // 10 MB
            max_concurrent_ops: 3,
            operation_timeout_secs: 10,
        }
    }

    /// Create constraints for moderate edge devices
    pub fn moderate() -> Self {
        Self {
            max_memory_bytes: 50 * 1024 * 1024, // 50 MB
            max_cpu_percent: 70.0,
            max_storage_bytes: 100 * 1024 * 1024, // 100 MB
            max_concurrent_ops: 10,
            operation_timeout_secs: 30,
        }
    }

    /// Create constraints for powerful edge devices
    pub fn powerful() -> Self {
        Self {
            max_memory_bytes: 200 * 1024 * 1024, // 200 MB
            max_cpu_percent: 90.0,
            max_storage_bytes: 500 * 1024 * 1024, // 500 MB
            max_concurrent_ops: 50,
            operation_timeout_secs: 60,
        }
    }

    /// Validate constraints
    pub fn validate(&self) -> Result<()> {
        if self.max_memory_bytes == 0 {
            return Err(EdgeError::invalid_config("max_memory_bytes must be > 0"));
        }
        if self.max_cpu_percent <= 0.0 || self.max_cpu_percent > 100.0 {
            return Err(EdgeError::invalid_config(
                "max_cpu_percent must be between 0 and 100",
            ));
        }
        if self.max_storage_bytes == 0 {
            return Err(EdgeError::invalid_config("max_storage_bytes must be > 0"));
        }
        if self.max_concurrent_ops == 0 {
            return Err(EdgeError::invalid_config("max_concurrent_ops must be > 0"));
        }
        Ok(())
    }
}

/// Resource usage metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceMetrics {
    /// Current memory usage in bytes
    pub memory_bytes: usize,
    /// Current CPU usage percentage
    pub cpu_percent: f64,
    /// Current storage usage in bytes
    pub storage_bytes: usize,
    /// Number of active operations
    pub active_operations: usize,
    /// Peak memory usage
    pub peak_memory_bytes: usize,
    /// Peak CPU usage
    pub peak_cpu_percent: f64,
    /// Total operations completed
    pub total_operations: u64,
    /// Failed operations count
    pub failed_operations: u64,
}

impl Default for ResourceMetrics {
    fn default() -> Self {
        Self {
            memory_bytes: 0,
            cpu_percent: 0.0,
            storage_bytes: 0,
            active_operations: 0,
            peak_memory_bytes: 0,
            peak_cpu_percent: 0.0,
            total_operations: 0,
            failed_operations: 0,
        }
    }
}

impl ResourceMetrics {
    /// Get memory usage percentage
    pub fn memory_percent(&self, max_memory: usize) -> f64 {
        if max_memory == 0 {
            return 0.0;
        }
        (self.memory_bytes as f64 / max_memory as f64) * 100.0
    }

    /// Get storage usage percentage
    pub fn storage_percent(&self, max_storage: usize) -> f64 {
        if max_storage == 0 {
            return 0.0;
        }
        (self.storage_bytes as f64 / max_storage as f64) * 100.0
    }

    /// Get success rate
    pub fn success_rate(&self) -> f64 {
        if self.total_operations == 0 {
            return 100.0;
        }
        let successful = self.total_operations - self.failed_operations;
        (successful as f64 / self.total_operations as f64) * 100.0
    }
}

/// Resource manager for edge devices
pub struct ResourceManager {
    constraints: ResourceConstraints,
    memory_used: Arc<AtomicUsize>,
    storage_used: Arc<AtomicUsize>,
    active_ops: Arc<AtomicUsize>,
    total_ops: Arc<AtomicU64>,
    failed_ops: Arc<AtomicU64>,
    peak_memory: Arc<AtomicUsize>,
    cpu_samples: Arc<RwLock<Vec<f64>>>,
}

impl ResourceManager {
    /// Create new resource manager
    pub fn new(constraints: ResourceConstraints) -> Result<Self> {
        constraints.validate()?;

        Ok(Self {
            constraints,
            memory_used: Arc::new(AtomicUsize::new(0)),
            storage_used: Arc::new(AtomicUsize::new(0)),
            active_ops: Arc::new(AtomicUsize::new(0)),
            total_ops: Arc::new(AtomicU64::new(0)),
            failed_ops: Arc::new(AtomicU64::new(0)),
            peak_memory: Arc::new(AtomicUsize::new(0)),
            cpu_samples: Arc::new(RwLock::new(Vec::new())),
        })
    }

    /// Check if operation can be started
    pub fn can_start_operation(&self) -> Result<()> {
        // Check concurrent operations limit
        let active = self.active_ops.load(Ordering::Relaxed);
        if active >= self.constraints.max_concurrent_ops {
            return Err(EdgeError::resource_constraint(format!(
                "Maximum concurrent operations ({}) reached",
                self.constraints.max_concurrent_ops
            )));
        }

        // Check memory constraint
        let memory = self.memory_used.load(Ordering::Relaxed);
        if memory >= self.constraints.max_memory_bytes {
            return Err(EdgeError::resource_constraint(format!(
                "Memory limit ({} bytes) exceeded",
                self.constraints.max_memory_bytes
            )));
        }

        Ok(())
    }

    /// Start an operation
    pub fn start_operation(&self) -> Result<OperationGuard> {
        self.can_start_operation()?;

        self.active_ops.fetch_add(1, Ordering::Relaxed);
        self.total_ops.fetch_add(1, Ordering::Relaxed);

        Ok(OperationGuard {
            active_ops: Arc::clone(&self.active_ops),
        })
    }

    /// Record operation failure
    pub fn record_failure(&self) {
        self.failed_ops.fetch_add(1, Ordering::Relaxed);
    }

    /// Allocate memory
    pub fn allocate_memory(&self, bytes: usize) -> Result<MemoryGuard> {
        let current = self.memory_used.load(Ordering::Relaxed);
        let new_total = current.saturating_add(bytes);

        if new_total > self.constraints.max_memory_bytes {
            return Err(EdgeError::resource_constraint(format!(
                "Memory allocation of {} bytes would exceed limit of {} bytes",
                bytes, self.constraints.max_memory_bytes
            )));
        }

        self.memory_used.fetch_add(bytes, Ordering::Relaxed);

        // Update peak memory
        let mut peak = self.peak_memory.load(Ordering::Relaxed);
        while new_total > peak {
            match self.peak_memory.compare_exchange_weak(
                peak,
                new_total,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(current) => peak = current,
            }
        }

        Ok(MemoryGuard {
            bytes,
            memory_used: Arc::clone(&self.memory_used),
        })
    }

    /// Allocate storage
    pub fn allocate_storage(&self, bytes: usize) -> Result<StorageGuard> {
        let current = self.storage_used.load(Ordering::Relaxed);
        let new_total = current.saturating_add(bytes);

        if new_total > self.constraints.max_storage_bytes {
            return Err(EdgeError::resource_constraint(format!(
                "Storage allocation of {} bytes would exceed limit of {} bytes",
                bytes, self.constraints.max_storage_bytes
            )));
        }

        self.storage_used.fetch_add(bytes, Ordering::Relaxed);

        Ok(StorageGuard {
            bytes,
            storage_used: Arc::clone(&self.storage_used),
        })
    }

    /// Record CPU sample
    pub fn record_cpu_sample(&self, cpu_percent: f64) {
        let mut samples = self.cpu_samples.write();
        samples.push(cpu_percent);

        // Keep only last 100 samples
        if samples.len() > 100 {
            samples.remove(0);
        }
    }

    /// Get current CPU usage (averaged over recent samples)
    pub fn current_cpu(&self) -> f64 {
        let samples = self.cpu_samples.read();
        if samples.is_empty() {
            return 0.0;
        }

        let sum: f64 = samples.iter().sum();
        sum / samples.len() as f64
    }

    /// Check if CPU is overloaded
    pub fn is_cpu_overloaded(&self) -> bool {
        self.current_cpu() > self.constraints.max_cpu_percent
    }

    /// Get current metrics
    pub fn metrics(&self) -> ResourceMetrics {
        let memory = self.memory_used.load(Ordering::Relaxed);
        let storage = self.storage_used.load(Ordering::Relaxed);
        let active_ops = self.active_ops.load(Ordering::Relaxed);
        let total_ops = self.total_ops.load(Ordering::Relaxed);
        let failed_ops = self.failed_ops.load(Ordering::Relaxed);
        let peak_memory = self.peak_memory.load(Ordering::Relaxed);

        let cpu_samples = self.cpu_samples.read();
        let (cpu_current, cpu_peak) = if cpu_samples.is_empty() {
            (0.0, 0.0)
        } else {
            let sum: f64 = cpu_samples.iter().sum();
            let avg = sum / cpu_samples.len() as f64;
            let peak = cpu_samples.iter().copied().fold(0.0, f64::max);
            (avg, peak)
        };

        ResourceMetrics {
            memory_bytes: memory,
            cpu_percent: cpu_current,
            storage_bytes: storage,
            active_operations: active_ops,
            peak_memory_bytes: peak_memory,
            peak_cpu_percent: cpu_peak,
            total_operations: total_ops,
            failed_operations: failed_ops,
        }
    }

    /// Get constraints
    pub fn constraints(&self) -> &ResourceConstraints {
        &self.constraints
    }

    /// Reset metrics
    pub fn reset_metrics(&self) {
        self.total_ops.store(0, Ordering::Relaxed);
        self.failed_ops.store(0, Ordering::Relaxed);
        self.peak_memory.store(0, Ordering::Relaxed);
        self.cpu_samples.write().clear();
    }

    /// Check overall health
    pub fn health_check(&self) -> HealthStatus {
        let metrics = self.metrics();

        let memory_ok = metrics.memory_percent(self.constraints.max_memory_bytes) < 90.0;
        let cpu_ok = metrics.cpu_percent < self.constraints.max_cpu_percent * 0.9;
        let storage_ok = metrics.storage_percent(self.constraints.max_storage_bytes) < 90.0;
        let success_ok = metrics.success_rate() > 95.0;

        if memory_ok && cpu_ok && storage_ok && success_ok {
            HealthStatus::Healthy
        } else if !memory_ok || !cpu_ok || !storage_ok {
            HealthStatus::Critical
        } else {
            HealthStatus::Degraded
        }
    }
}

/// Health status of edge device
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum HealthStatus {
    /// All resources operating normally
    Healthy,
    /// Some resources under pressure but operational
    Degraded,
    /// Critical resource constraints
    Critical,
}

/// RAII guard for operations
pub struct OperationGuard {
    active_ops: Arc<AtomicUsize>,
}

impl Drop for OperationGuard {
    fn drop(&mut self) {
        self.active_ops.fetch_sub(1, Ordering::Relaxed);
    }
}

/// RAII guard for memory allocation
pub struct MemoryGuard {
    bytes: usize,
    memory_used: Arc<AtomicUsize>,
}

impl Drop for MemoryGuard {
    fn drop(&mut self) {
        self.memory_used.fetch_sub(self.bytes, Ordering::Relaxed);
    }
}

/// RAII guard for storage allocation
pub struct StorageGuard {
    bytes: usize,
    storage_used: Arc<AtomicUsize>,
}

impl Drop for StorageGuard {
    fn drop(&mut self) {
        self.storage_used.fetch_sub(self.bytes, Ordering::Relaxed);
    }
}

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

    #[test]
    fn test_constraints_creation() {
        let constraints = ResourceConstraints::default();
        assert!(constraints.validate().is_ok());

        let minimal = ResourceConstraints::minimal();
        assert!(minimal.validate().is_ok());
        assert!(minimal.max_memory_bytes < constraints.max_memory_bytes);
    }

    #[test]
    fn test_constraints_validation() {
        let mut invalid = ResourceConstraints {
            max_memory_bytes: 0,
            ..Default::default()
        };
        assert!(invalid.validate().is_err());

        invalid.max_memory_bytes = 1024;
        invalid.max_cpu_percent = 150.0;
        assert!(invalid.validate().is_err());
    }

    #[test]
    fn test_resource_manager_creation() {
        let constraints = ResourceConstraints::minimal();
        let manager = ResourceManager::new(constraints);
        assert!(manager.is_ok());
    }

    #[test]
    fn test_operation_guard() -> Result<()> {
        let constraints = ResourceConstraints::minimal();
        let manager = ResourceManager::new(constraints)?;

        let metrics_before = manager.metrics();
        assert_eq!(metrics_before.active_operations, 0);

        {
            let _guard = manager.start_operation()?;
            let metrics_during = manager.metrics();
            assert_eq!(metrics_during.active_operations, 1);
        }

        let metrics_after = manager.metrics();
        assert_eq!(metrics_after.active_operations, 0);

        Ok(())
    }

    #[test]
    fn test_memory_allocation() -> Result<()> {
        let constraints = ResourceConstraints::minimal();
        let manager = ResourceManager::new(constraints)?;

        let _guard = manager.allocate_memory(1024)?;
        let metrics = manager.metrics();
        assert_eq!(metrics.memory_bytes, 1024);

        Ok(())
    }

    #[test]
    fn test_memory_limit() -> Result<()> {
        let mut constraints = ResourceConstraints::minimal();
        constraints.max_memory_bytes = 2048;
        let manager = ResourceManager::new(constraints)?;

        let _guard1 = manager.allocate_memory(1024)?;
        let _guard2 = manager.allocate_memory(512)?;

        // This should fail
        let result = manager.allocate_memory(1024);
        assert!(result.is_err());

        Ok(())
    }

    #[test]
    fn test_concurrent_operations_limit() -> Result<()> {
        let mut constraints = ResourceConstraints::minimal();
        constraints.max_concurrent_ops = 2;
        let manager = ResourceManager::new(constraints)?;

        let _guard1 = manager.start_operation()?;
        let _guard2 = manager.start_operation()?;

        // This should fail
        let result = manager.start_operation();
        assert!(result.is_err());

        Ok(())
    }

    #[test]
    fn test_cpu_tracking() -> Result<()> {
        let constraints = ResourceConstraints::minimal();
        let manager = ResourceManager::new(constraints)?;

        manager.record_cpu_sample(10.0);
        manager.record_cpu_sample(20.0);
        manager.record_cpu_sample(30.0);

        let cpu = manager.current_cpu();
        assert!((cpu - 20.0).abs() < 1.0);

        Ok(())
    }

    #[test]
    fn test_metrics() -> Result<()> {
        let constraints = ResourceConstraints::minimal();
        let manager = ResourceManager::new(constraints)?;

        let _guard = manager.start_operation()?;
        manager.record_failure();

        let metrics = manager.metrics();
        assert_eq!(metrics.active_operations, 1);
        assert_eq!(metrics.total_operations, 1);
        assert_eq!(metrics.failed_operations, 1);

        Ok(())
    }

    #[test]
    fn test_health_check() -> Result<()> {
        let constraints = ResourceConstraints::minimal();
        let max_memory = constraints.max_memory_bytes;
        let manager = ResourceManager::new(constraints)?;

        assert_eq!(manager.health_check(), HealthStatus::Healthy);

        // Fill up memory
        let _guard = manager.allocate_memory(max_memory - 100)?;
        assert_eq!(manager.health_check(), HealthStatus::Critical);

        Ok(())
    }

    #[test]
    fn test_peak_memory() -> Result<()> {
        let constraints = ResourceConstraints::minimal();
        let manager = ResourceManager::new(constraints)?;

        {
            let _guard = manager.allocate_memory(1000)?;
            let metrics = manager.metrics();
            assert_eq!(metrics.peak_memory_bytes, 1000);
        }

        let metrics = manager.metrics();
        assert_eq!(metrics.memory_bytes, 0);
        assert_eq!(metrics.peak_memory_bytes, 1000);

        Ok(())
    }
}