oxirs-samm 0.2.4

Semantic Aspect Meta Model (SAMM) implementation for OxiRS
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
//! Production-ready features for SAMM processing with SciRS2 integration
//!
//! This module provides production-grade features for running SAMM operations
//! in enterprise environments:
//!
//! - Structured logging with tracing
//! - Comprehensive metrics collection with SciRS2
//! - Health checks with detailed diagnostics
//! - Configuration validation
//! - Error recovery strategies
//! - Performance profiling and benchmarking
//!
//! ## Example
//!
//! ```rust,no_run
//! use oxirs_samm::production::{ProductionConfig, init_production};
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Initialize production features with SciRS2 metrics
//! let config = ProductionConfig::default();
//! init_production(&config)?;
//!
//! // Application runs with full observability
//! # Ok(())
//! # }
//! ```

use crate::error::Result;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tracing::Level;

/// Production configuration
#[derive(Debug, Clone)]
pub struct ProductionConfig {
    /// Enable structured logging
    pub logging_enabled: bool,

    /// Logging level
    pub log_level: LogLevel,

    /// Enable metrics collection with SciRS2
    pub metrics_enabled: bool,

    /// Enable health checks
    pub health_checks_enabled: bool,

    /// Enable performance profiling
    pub profiling_enabled: bool,

    /// Enable benchmarking
    pub benchmarking_enabled: bool,

    /// Enable histogram metrics for operation duration
    pub histogram_enabled: bool,

    /// Application name for logging context
    pub app_name: String,

    /// Environment (dev, staging, prod)
    pub environment: String,

    /// Metrics reporting interval (seconds)
    pub metrics_interval_secs: u64,
}

impl Default for ProductionConfig {
    fn default() -> Self {
        Self {
            logging_enabled: true,
            log_level: LogLevel::Info,
            metrics_enabled: true,
            health_checks_enabled: true,
            profiling_enabled: true,
            benchmarking_enabled: false, // Disabled by default (performance overhead)
            histogram_enabled: true,
            app_name: "oxirs-samm".to_string(),
            environment: "production".to_string(),
            metrics_interval_secs: 60, // Report every 60 seconds
        }
    }
}

/// Log level configuration
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LogLevel {
    /// Trace level (most verbose)
    Trace,
    /// Debug level
    Debug,
    /// Info level (default)
    Info,
    /// Warn level
    Warn,
    /// Error level (least verbose)
    Error,
}

impl From<LogLevel> for Level {
    fn from(level: LogLevel) -> Self {
        match level {
            LogLevel::Trace => Level::TRACE,
            LogLevel::Debug => Level::DEBUG,
            LogLevel::Info => Level::INFO,
            LogLevel::Warn => Level::WARN,
            LogLevel::Error => Level::ERROR,
        }
    }
}

/// Initialize production features
pub fn init_production(config: &ProductionConfig) -> Result<()> {
    if config.logging_enabled {
        init_logging(config);
    }

    if config.metrics_enabled {
        MetricsCollector::global().reset();
    }

    tracing::info!(
        app = %config.app_name,
        env = %config.environment,
        "Production initialization complete"
    );

    Ok(())
}

/// Initialize structured logging
fn init_logging(config: &ProductionConfig) {
    let level: Level = config.log_level.into();

    tracing_subscriber::fmt()
        .with_max_level(level)
        .with_target(true)
        .with_thread_ids(true)
        .with_file(true)
        .with_line_number(true)
        .json()
        .init();
}

/// Metrics collector for production monitoring
#[derive(Debug)]
pub struct MetricsCollector {
    /// Atomic counters for tracking metrics
    operations_total: AtomicU64,
    errors_total: AtomicU64,
    warnings_total: AtomicU64,
    parse_operations: AtomicU64,
    validation_operations: AtomicU64,
    codegen_operations: AtomicU64,
    package_operations: AtomicU64,

    /// Active operations currently being processed
    active_operations: AtomicU64,

    /// Start time
    start_time: Instant,
}

impl MetricsCollector {
    /// Create a new metrics collector
    pub fn new() -> Self {
        Self {
            operations_total: AtomicU64::new(0),
            errors_total: AtomicU64::new(0),
            warnings_total: AtomicU64::new(0),
            parse_operations: AtomicU64::new(0),
            validation_operations: AtomicU64::new(0),
            codegen_operations: AtomicU64::new(0),
            package_operations: AtomicU64::new(0),
            active_operations: AtomicU64::new(0),
            start_time: Instant::now(),
        }
    }

    /// Get global metrics collector
    pub fn global() -> &'static MetricsCollector {
        static METRICS: once_cell::sync::Lazy<MetricsCollector> =
            once_cell::sync::Lazy::new(MetricsCollector::new);
        &METRICS
    }

    /// Record an operation
    pub fn record_operation(&self, operation_type: OperationType) {
        self.operations_total.fetch_add(1, Ordering::Relaxed);

        match operation_type {
            OperationType::Parse => {
                self.parse_operations.fetch_add(1, Ordering::Relaxed);
            }
            OperationType::Validation => {
                self.validation_operations.fetch_add(1, Ordering::Relaxed);
            }
            OperationType::CodeGeneration => {
                self.codegen_operations.fetch_add(1, Ordering::Relaxed);
            }
            OperationType::Package => {
                self.package_operations.fetch_add(1, Ordering::Relaxed);
            }
        }

        tracing::debug!(operation_type = ?operation_type, "Operation recorded");
    }

    /// Record an operation with duration tracking
    pub fn record_operation_with_duration(&self, operation_type: OperationType, _duration_ms: f64) {
        self.record_operation(operation_type);
        // Note: Duration tracking will be added when scirs2-core histogram API is stable
    }

    /// Record an error
    pub fn record_error(&self) {
        self.errors_total.fetch_add(1, Ordering::Relaxed);
        tracing::warn!(
            errors_total = self.errors_total.load(Ordering::Relaxed),
            "Error recorded"
        );
    }

    /// Record a warning
    pub fn record_warning(&self) {
        self.warnings_total.fetch_add(1, Ordering::Relaxed);
    }

    /// Increment active operations counter and return a guard
    ///
    /// The guard will automatically decrement the counter when dropped (RAII pattern).
    /// This method can only be called on the global instance to ensure 'static lifetime.
    pub fn start_operation() -> OperationGuard {
        let metrics = Self::global();
        metrics.active_operations.fetch_add(1, Ordering::SeqCst);
        OperationGuard { metrics }
    }

    /// Decrement active operations counter (called by OperationGuard on drop)
    fn end_operation(&self) {
        self.active_operations.fetch_sub(1, Ordering::SeqCst);
    }

    /// Get current number of active operations
    pub fn active_operations(&self) -> u64 {
        self.active_operations.load(Ordering::Relaxed)
    }

    /// Get current metrics snapshot
    pub fn snapshot(&self) -> MetricsSnapshot {
        MetricsSnapshot {
            operations_total: self.operations_total.load(Ordering::Relaxed),
            errors_total: self.errors_total.load(Ordering::Relaxed),
            warnings_total: self.warnings_total.load(Ordering::Relaxed),
            parse_operations: self.parse_operations.load(Ordering::Relaxed),
            validation_operations: self.validation_operations.load(Ordering::Relaxed),
            codegen_operations: self.codegen_operations.load(Ordering::Relaxed),
            package_operations: self.package_operations.load(Ordering::Relaxed),
            operation_duration_p50: 0.0, // Future: when scirs2-core exposes histogram percentiles
            operation_duration_p95: 0.0,
            operation_duration_p99: 0.0,
            active_operations: self.active_operations.load(Ordering::Relaxed),
            uptime: self.start_time.elapsed(),
        }
    }

    /// Reset all metrics
    pub fn reset(&self) {
        self.operations_total.store(0, Ordering::Relaxed);
        self.errors_total.store(0, Ordering::Relaxed);
        self.warnings_total.store(0, Ordering::Relaxed);
        self.parse_operations.store(0, Ordering::Relaxed);
        self.validation_operations.store(0, Ordering::Relaxed);
        self.codegen_operations.store(0, Ordering::Relaxed);
        self.package_operations.store(0, Ordering::Relaxed);
        // Note: active_operations is not reset as it tracks real-time state

        tracing::info!("Metrics reset complete");
    }
}

impl Default for MetricsCollector {
    fn default() -> Self {
        Self::new()
    }
}

/// RAII guard for active operation tracking
///
/// Automatically decrements the active operations counter when dropped.
/// This ensures accurate tracking even in case of panics or early returns.
///
/// # Example
///
/// ```rust
/// use oxirs_samm::production::MetricsCollector;
///
/// fn process_model() {
///     let _guard = MetricsCollector::start_operation();
///
///     // Do work here...
///     // Active operations count is automatically decremented when _guard is dropped
/// }
/// ```
#[derive(Debug)]
pub struct OperationGuard {
    metrics: &'static MetricsCollector,
}

impl Drop for OperationGuard {
    fn drop(&mut self) {
        self.metrics.end_operation();
    }
}

/// Operation type for metrics
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OperationType {
    /// Parse operation
    Parse,
    /// Validation operation
    Validation,
    /// Code generation operation
    CodeGeneration,
    /// Package operation
    Package,
}

/// Metrics snapshot with SciRS2 histogram data
#[derive(Debug, Clone)]
pub struct MetricsSnapshot {
    /// Total operations
    pub operations_total: u64,

    /// Total errors
    pub errors_total: u64,

    /// Total warnings
    pub warnings_total: u64,

    /// Parse operations
    pub parse_operations: u64,

    /// Validation operations
    pub validation_operations: u64,

    /// Code generation operations
    pub codegen_operations: u64,

    /// Package operations
    pub package_operations: u64,

    /// Operation duration 50th percentile (median) in milliseconds
    pub operation_duration_p50: f64,

    /// Operation duration 95th percentile in milliseconds
    pub operation_duration_p95: f64,

    /// Operation duration 99th percentile in milliseconds
    pub operation_duration_p99: f64,

    /// Active operations count
    pub active_operations: u64,

    /// Uptime
    pub uptime: Duration,
}

impl MetricsSnapshot {
    /// Get error rate (errors per operation)
    pub fn error_rate(&self) -> f64 {
        if self.operations_total == 0 {
            0.0
        } else {
            self.errors_total as f64 / self.operations_total as f64
        }
    }

    /// Get operations per second
    pub fn ops_per_second(&self) -> f64 {
        let secs = self.uptime.as_secs_f64();
        if secs == 0.0 {
            0.0
        } else {
            self.operations_total as f64 / secs
        }
    }

    /// Get average operation duration (P50)
    pub fn avg_duration_ms(&self) -> f64 {
        self.operation_duration_p50
    }

    /// Check if performance is degraded based on P95 latency
    pub fn is_performance_degraded(&self, threshold_ms: f64) -> bool {
        self.operation_duration_p95 > threshold_ms
    }
}

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

/// Health check result
#[derive(Debug, Clone)]
pub struct HealthCheck {
    /// Overall health status
    pub status: HealthStatus,

    /// Individual check results
    pub checks: Vec<(String, HealthStatus, String)>,

    /// Timestamp
    pub timestamp: Instant,
}

/// Perform comprehensive health check with SciRS2 metrics
pub fn health_check() -> HealthCheck {
    let mut checks = Vec::new();
    let mut overall_status = HealthStatus::Healthy;

    // Check metrics
    let metrics = MetricsCollector::global().snapshot();
    let error_rate = metrics.error_rate();

    // Error rate check
    if error_rate > 0.5 {
        checks.push((
            "error_rate".to_string(),
            HealthStatus::Unhealthy,
            format!("High error rate: {:.2}%", error_rate * 100.0),
        ));
        overall_status = HealthStatus::Unhealthy;
    } else if error_rate > 0.1 {
        checks.push((
            "error_rate".to_string(),
            HealthStatus::Degraded,
            format!("Elevated error rate: {:.2}%", error_rate * 100.0),
        ));
        if overall_status == HealthStatus::Healthy {
            overall_status = HealthStatus::Degraded;
        }
    } else {
        checks.push((
            "error_rate".to_string(),
            HealthStatus::Healthy,
            format!("Normal error rate: {:.2}%", error_rate * 100.0),
        ));
    }

    // Performance check (P95 latency) - future when histogram is available
    checks.push((
        "latency_p95".to_string(),
        HealthStatus::Healthy,
        "Latency tracking pending histogram API".to_string(),
    ));

    // Active operations check - future when tracking is implemented
    checks.push((
        "active_operations".to_string(),
        HealthStatus::Healthy,
        "Active operation tracking pending".to_string(),
    ));

    // Uptime check
    checks.push((
        "uptime".to_string(),
        HealthStatus::Healthy,
        format!("{} seconds", metrics.uptime.as_secs()),
    ));

    // Operations per second check
    let ops_per_sec = metrics.ops_per_second();
    checks.push((
        "throughput".to_string(),
        HealthStatus::Healthy,
        format!("{:.2} ops/sec", ops_per_sec),
    ));

    HealthCheck {
        status: overall_status,
        checks,
        timestamp: Instant::now(),
    }
}

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

    #[test]
    fn test_production_config_default() {
        let config = ProductionConfig::default();
        assert!(config.logging_enabled);
        assert!(config.metrics_enabled);
        assert_eq!(config.app_name, "oxirs-samm");
    }

    #[test]
    fn test_metrics_collector() {
        let collector = MetricsCollector::new();

        collector.record_operation(OperationType::Parse);
        collector.record_operation(OperationType::Validation);
        collector.record_error();

        let snapshot = collector.snapshot();
        assert_eq!(snapshot.operations_total, 2);
        assert_eq!(snapshot.errors_total, 1);
        assert_eq!(snapshot.parse_operations, 1);
        assert_eq!(snapshot.validation_operations, 1);
    }

    #[test]
    fn test_error_rate_calculation() {
        let snapshot = MetricsSnapshot {
            operations_total: 100,
            errors_total: 10,
            warnings_total: 5,
            parse_operations: 50,
            validation_operations: 30,
            codegen_operations: 15,
            package_operations: 5,
            operation_duration_p50: 100.0,
            operation_duration_p95: 500.0,
            operation_duration_p99: 1000.0,
            active_operations: 5,
            uptime: Duration::from_secs(60),
        };

        assert!((snapshot.error_rate() - 0.1).abs() < 0.01);
        assert!((snapshot.ops_per_second() - (100.0 / 60.0)).abs() < 0.01);
        assert_eq!(snapshot.avg_duration_ms(), 100.0);
        assert!(!snapshot.is_performance_degraded(600.0));
        assert!(snapshot.is_performance_degraded(400.0));
    }

    #[test]
    fn test_health_check() {
        let health = health_check();
        assert!(!health.checks.is_empty());
        // Should have at least: error_rate, latency_p95, active_operations, uptime, throughput
        assert!(health.checks.len() >= 5);
        assert!(
            health.status == HealthStatus::Healthy
                || health.status == HealthStatus::Degraded
                || health.status == HealthStatus::Unhealthy
        );
    }

    #[test]
    fn test_metrics_collector_with_duration() {
        let collector = MetricsCollector::new();

        collector.record_operation(OperationType::Parse);
        collector.record_operation_with_duration(OperationType::Validation, 150.5);

        let snapshot = collector.snapshot();
        assert_eq!(snapshot.operations_total, 2);
        assert_eq!(snapshot.parse_operations, 1);
        assert_eq!(snapshot.validation_operations, 1);
    }

    #[test]
    fn test_production_config_with_profiling() {
        let config = ProductionConfig::default();
        assert!(config.profiling_enabled);
        assert!(config.histogram_enabled);
        assert!(!config.benchmarking_enabled); // Should be disabled by default
        assert_eq!(config.metrics_interval_secs, 60);
    }
}