#![allow(clippy::too_many_arguments)]
#![allow(dead_code)]
use crate::error::{MetricsError, Result};
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::numeric::Float;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::time::{Duration, Instant, SystemTime};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StreamingConfig {
pub base_window_size: usize,
pub max_window_size: usize,
pub min_window_size: usize,
pub drift_sensitivity: f64,
pub warning_threshold: f64,
pub drift_threshold: f64,
pub adaptive_windowing: bool,
pub adaptation_strategy: WindowAdaptationStrategy,
pub enable_drift_detection: bool,
pub drift_detection_methods: Vec<DriftDetectionMethod>,
pub enable_anomaly_detection: bool,
pub anomaly_algorithm: AnomalyDetectionAlgorithm,
pub monitoring_interval: Duration,
pub enable_alerts: bool,
pub alert_config: AlertConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum WindowAdaptationStrategy {
Fixed,
ExponentialDecay { decay_rate: f64 },
PerformanceBased { target_accuracy: f64 },
DriftBased,
Hybrid {
strategies: Vec<WindowAdaptationStrategy>,
weights: Vec<f64>,
},
MLBased { model_type: String },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DriftDetectionMethod {
Adwin { confidence: f64 },
Ddm {
warning_level: f64,
drift_level: f64,
},
Eddm { alpha: f64, beta: f64 },
PageHinkley { threshold: f64, alpha: f64 },
Cusum {
threshold: f64,
drift_threshold: f64,
},
KolmogorovSmirnov { p_value_threshold: f64 },
Ensemble { methods: Vec<DriftDetectionMethod> },
Custom {
name: String,
parameters: HashMap<String, f64>,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AnomalyDetectionAlgorithm {
ZScore { threshold: f64 },
IsolationForest { contamination: f64 },
OneClassSvm { nu: f64 },
LocalOutlierFactor { n_neighbors: usize },
Dbscan { eps: f64, min_samples: usize },
Autoencoder { threshold: f64 },
Ensemble {
algorithms: Vec<AnomalyDetectionAlgorithm>,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlertConfig {
pub email_enabled: bool,
pub email_addresses: Vec<String>,
pub webhook_enabled: bool,
pub webhook_urls: Vec<String>,
pub log_enabled: bool,
pub log_file: Option<String>,
pub severity_levels: HashMap<String, AlertSeverity>,
pub rate_limit: Duration,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AlertSeverity {
Critical,
High,
Medium,
Low,
Info,
}
pub trait ConceptDriftDetector<F: Float + std::fmt::Debug + Send + Sync + std::iter::Sum>:
std::fmt::Debug
{
fn update(&mut self, prediction_correct: bool, error: F) -> Result<DriftDetectionResult>;
fn get_status(&self) -> DriftStatus;
fn reset(&mut self);
fn get_config(&self) -> HashMap<String, f64>;
fn get_statistics(&self) -> DriftStatistics<F>;
}
#[derive(Debug, Clone)]
pub struct DriftDetectionResult {
pub status: DriftStatus,
pub confidence: f64,
pub change_point: Option<usize>,
pub statistics: HashMap<String, f64>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum DriftStatus {
Stable,
Warning,
Drift,
Unknown,
}
#[derive(Debug, Clone)]
pub struct DriftStatistics<F: Float + std::fmt::Debug> {
pub samples_since_reset: usize,
pub warnings_count: usize,
pub drifts_count: usize,
pub current_error_rate: F,
pub baseline_error_rate: F,
pub drift_score: F,
pub last_detection_time: Option<SystemTime>,
}
pub trait StreamingMetric<F: Float> {
fn update(&mut self, true_value: F, predicted_value: F) -> Result<()>;
fn get_value(&self) -> F;
fn reset(&mut self);
fn get_name(&self) -> &str;
fn get_confidence(&self) -> F;
}
#[derive(Debug, Clone)]
pub enum EnsembleAggregation {
WeightedAverage,
Majority,
Maximum,
Minimum,
Median,
Stacking { meta_learner: String },
}
#[derive(Debug, Clone)]
pub struct DataPoint<F: Float + std::fmt::Debug> {
pub true_value: F,
pub predicted_value: F,
pub error: F,
pub confidence: F,
pub features: Option<Vec<F>>,
}
#[derive(Debug, Clone)]
pub struct Alert {
pub id: String,
pub timestamp: Instant,
pub severity: AlertSeverity,
pub title: String,
pub message: String,
pub data: HashMap<String, String>,
pub tags: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct SentAlert {
pub alert: Alert,
pub sent_at: Instant,
pub channels: Vec<String>,
pub success: bool,
pub error_message: Option<String>,
}
impl Default for StreamingConfig {
fn default() -> Self {
Self {
base_window_size: 1000,
max_window_size: 10000,
min_window_size: 100,
drift_sensitivity: 0.05,
warning_threshold: 0.5,
drift_threshold: 0.8,
adaptive_windowing: true,
adaptation_strategy: WindowAdaptationStrategy::DriftBased,
enable_drift_detection: true,
drift_detection_methods: vec![
DriftDetectionMethod::Adwin { confidence: 0.95 },
DriftDetectionMethod::Ddm {
warning_level: 2.0,
drift_level: 3.0,
},
],
enable_anomaly_detection: true,
anomaly_algorithm: AnomalyDetectionAlgorithm::ZScore { threshold: 3.0 },
monitoring_interval: Duration::from_secs(60),
enable_alerts: false,
alert_config: AlertConfig::default(),
}
}
}
impl Default for AlertConfig {
fn default() -> Self {
Self {
email_enabled: false,
email_addresses: Vec::new(),
webhook_enabled: false,
webhook_urls: Vec::new(),
log_enabled: true,
log_file: None,
severity_levels: HashMap::new(),
rate_limit: Duration::from_secs(300),
}
}
}