spawn-access-control 0.1.12

A Rust library for access control management with WebAssembly support, including role-based access control (RBAC), permissions, and audit logging.
Documentation
use crate::alert_ml::AlertMLAnalyzer;
use crate::alert_storage::StoredAlert;
use crate::alert_analyzer::AlertPattern;
use chrono::{Utc, Duration};

#[test]
fn test_alert_clustering() {
    let mut analyzer = AlertMLAnalyzer::new();
    let alerts = create_test_alerts();
    let patterns = create_test_patterns();

    analyzer.train(&alerts, &patterns);

    let clusters = analyzer.analyze_patterns(&alerts);
    
    assert!(!clusters.is_empty());
    
    let first_cluster = &clusters[0];
    assert!(first_cluster.alerts.len() >= 3);
    assert!(first_cluster.radius > 0.0);
    assert!(!first_cluster.characteristics.severity_distribution.is_empty());
}

fn create_test_alerts() -> Vec<StoredAlert> {
    let base_time = Utc::now();
    let mut alerts = Vec::new();

    for i in 0..5 {
        alerts.push(StoredAlert {
            id: uuid::Uuid::new_v4(),
            incident_id: format!("INC_{}", i),
            severity: "Warning".to_string(),
            message: "Test alert".to_string(),
            metric_name: "cpu_usage".to_string(),
            current_value: 85.0 + (i as f64 * 2.0),
            threshold: 80.0,
            created_at: base_time + Duration::minutes(i * 5),
            resolved_at: None,
            metadata: serde_json::Value::Null,
        });
    }

    for i in 0..4 {
        alerts.push(StoredAlert {
            id: uuid::Uuid::new_v4(),
            incident_id: format!("INC_B_{}", i),
            severity: "Critical".to_string(),
            message: "Memory alert".to_string(),
            metric_name: "memory_usage".to_string(),
            current_value: 95.0 - (i as f64 * 3.0),
            threshold: 90.0,
            created_at: base_time + Duration::minutes(i * 10),
            resolved_at: None,
            metadata: serde_json::Value::Null,
        });
    }

    alerts
}

fn create_test_patterns() -> Vec<AlertPattern> {
    vec![
        AlertPattern {
            pattern_type: crate::alert_analyzer::PatternType::Periodic,
            frequency: 5,
            confidence: 0.8,
            affected_metrics: vec!["cpu_usage".to_string()],
        },
        AlertPattern {
            pattern_type: crate::alert_analyzer::PatternType::Cascading,
            frequency: 4,
            confidence: 0.7,
            affected_metrics: vec!["memory_usage".to_string()],
        },
    ]
}