use scirs2_core::ndarray::Array2;
use scirs2_core::Complex64;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use super::collectors::*;
use super::metrics::*;
pub struct PerformanceAnalyzer {
pub config: AnalysisConfig,
pub historical_data: HistoricalPerformanceData,
pub performance_models: PerformanceModels,
pub anomaly_detector: AnomalyDetector,
pub prediction_engine: PredictionEngine,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnalysisConfig {
pub analysis_depth: AnalysisDepth,
pub statistical_methods: HashSet<StatisticalMethod>,
pub ml_models: HashSet<MlModel>,
pub confidence_level: f64,
pub min_data_points: usize,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AnalysisDepth {
Basic,
Standard,
Advanced,
Comprehensive,
}
#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub enum StatisticalMethod {
Descriptive,
Correlation,
Regression,
TimeSeries,
HypothesisTesting,
}
#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub enum MlModel {
LinearRegression,
RandomForest,
NeuralNetwork,
SupportVectorMachine,
Clustering,
}
#[derive(Debug, Clone)]
pub struct HistoricalPerformanceData {
pub snapshots: VecDeque<PerformanceSnapshot>,
pub retention_policy: DataRetentionPolicy,
pub compression_settings: CompressionSettings,
pub integrity_checks: IntegrityChecks,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceSnapshot {
pub timestamp: SystemTime,
pub metrics: HashMap<String, f64>,
pub system_state: SystemState,
pub environment: EnvironmentInfo,
pub metadata: HashMap<String, String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemState {
pub cpu_state: CpuState,
pub memory_state: MemoryState,
pub io_state: IoState,
pub network_state: NetworkState,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CpuState {
pub utilization: f64,
pub frequency: f64,
pub temperature: Option<f64>,
pub active_processes: usize,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryState {
pub total_memory: usize,
pub used_memory: usize,
pub free_memory: usize,
pub cached_memory: usize,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IoState {
pub disk_usage: f64,
pub read_iops: f64,
pub write_iops: f64,
pub queue_depth: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkState {
pub bandwidth_utilization: f64,
pub active_connections: usize,
pub packet_rate: f64,
pub error_rate: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnvironmentInfo {
pub operating_system: String,
pub hardware_config: HardwareConfig,
pub software_versions: HashMap<String, String>,
pub environment_variables: HashMap<String, String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HardwareConfig {
pub cpu_model: String,
pub cpu_cores: u32,
pub total_memory: usize,
pub gpu_info: Option<GpuInfo>,
pub storage_info: StorageInfo,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpuInfo {
pub model: String,
pub memory: usize,
pub compute_capability: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageInfo {
pub storage_type: StorageType,
pub total_capacity: usize,
pub available_capacity: usize,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum StorageType {
HDD,
SSD,
NVMe,
Network,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataRetentionPolicy {
pub max_age: Duration,
pub max_snapshots: usize,
pub compression_threshold: Duration,
pub archival_policy: ArchivalPolicy,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ArchivalPolicy {
Delete,
Compress,
Archive { location: String },
KeepAll,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompressionSettings {
pub algorithm: CompressionAlgorithm,
pub compression_level: u8,
pub realtime_compression: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CompressionAlgorithm {
None,
LZ4,
Gzip,
Zstd,
Custom { name: String },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IntegrityChecks {
pub enable_checksums: bool,
pub checksum_algorithm: ChecksumAlgorithm,
pub verification_frequency: Duration,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ChecksumAlgorithm {
CRC32,
MD5,
SHA256,
Blake3,
}
#[derive(Debug, Clone)]
pub struct PerformanceModels {
pub statistical_models: HashMap<String, StatisticalModel>,
pub ml_models: HashMap<String, MachineLearningModel>,
pub hybrid_models: HashMap<String, HybridModel>,
pub evaluation_results: ModelEvaluationResults,
}
#[derive(Debug, Clone)]
pub struct StatisticalModel {
pub model_type: StatisticalModelType,
pub parameters: HashMap<String, f64>,
pub accuracy: f64,
pub training_data_size: usize,
}
#[derive(Debug, Clone)]
pub enum StatisticalModelType {
LinearRegression,
Autoregressive,
MovingAverage,
Arima,
ExponentialSmoothing,
}
#[derive(Debug, Clone)]
pub struct MachineLearningModel {
pub model_type: MlModelType,
pub hyperparameters: HashMap<String, f64>,
pub accuracy: f64,
pub training_data_size: usize,
pub feature_importance: HashMap<String, f64>,
}
#[derive(Debug, Clone)]
pub enum MlModelType {
RandomForest,
GradientBoosting,
NeuralNetwork,
SupportVectorRegression,
GaussianProcess,
}
#[derive(Debug, Clone)]
pub struct HybridModel {
pub component_models: Vec<ComponentModel>,
pub ensemble_weights: HashMap<String, f64>,
pub accuracy: f64,
pub combination_strategy: CombinationStrategy,
}
#[derive(Debug, Clone)]
pub struct ComponentModel {
pub name: String,
pub model_type: ComponentModelType,
pub weight: f64,
pub accuracy: f64,
}
#[derive(Debug, Clone)]
pub enum ComponentModelType {
Statistical(StatisticalModelType),
MachineLearning(MlModelType),
PhysicsBased,
Empirical,
}
#[derive(Debug, Clone)]
pub enum CombinationStrategy {
WeightedAverage,
Voting,
Stacking,
BayesianAveraging,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelEvaluationResults {
pub cv_scores: HashMap<String, f64>,
pub test_performance: HashMap<String, f64>,
pub model_comparison: ModelComparison,
pub feature_analysis: FeatureAnalysis,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelComparison {
pub best_model: String,
pub performance_rankings: Vec<ModelRanking>,
pub significance_tests: HashMap<String, f64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelRanking {
pub model_name: String,
pub performance_score: f64,
pub rank: u32,
pub confidence_interval: (f64, f64),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FeatureAnalysis {
pub feature_importance: HashMap<String, f64>,
pub feature_correlations: HashMap<String, f64>,
pub feature_selection: FeatureSelectionResults,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FeatureSelectionResults {
pub selected_features: Vec<String>,
pub selection_method: String,
pub selection_criteria: HashMap<String, f64>,
}
#[derive(Debug, Clone)]
pub struct AnomalyDetector {
pub algorithms: HashMap<String, AnomalyDetectionAlgorithm>,
pub detected_anomalies: Vec<PerformanceAnomaly>,
pub config: AnomalyDetectionConfig,
pub alert_system: AlertSystem,
}
#[derive(Debug, Clone)]
pub struct AnomalyDetectionAlgorithm {
pub algorithm_type: AnomalyAlgorithmType,
pub parameters: HashMap<String, f64>,
pub threshold: f64,
pub false_positive_rate: f64,
}
#[derive(Debug, Clone)]
pub enum AnomalyAlgorithmType {
StatisticalOutlier,
IsolationForest,
OneClassSVM,
DBSCAN,
Autoencoder,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceAnomaly {
pub id: String,
pub timestamp: SystemTime,
pub anomaly_type: AnomalyType,
pub severity: AnomySeverity,
pub affected_metrics: Vec<String>,
pub anomaly_score: f64,
pub root_cause: Option<String>,
pub recommended_actions: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AnomalyType {
PerformanceDegradation,
ResourceSpike,
ErrorRateIncrease,
LatencyIncrease,
ThroughputDecrease,
MemoryLeak,
Custom { name: String },
}
#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub enum AnomySeverity {
Low,
Medium,
High,
Critical,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnomalyDetectionConfig {
pub enable_realtime: bool,
pub sensitivity: f64,
pub min_duration: Duration,
pub alert_thresholds: HashMap<AnomySeverity, f64>,
}
#[derive(Debug, Clone)]
pub struct AlertSystem {
pub alert_channels: Vec<AlertChannel>,
pub alert_history: VecDeque<Alert>,
pub alert_rules: Vec<AlertRule>,
pub suppression_rules: Vec<SuppressionRule>,
}
#[derive(Debug, Clone)]
pub struct AlertChannel {
pub channel_type: AlertChannelType,
pub config: HashMap<String, String>,
pub enabled: bool,
}
#[derive(Debug, Clone)]
pub enum AlertChannelType {
Email,
Slack,
Webhook,
LogFile,
SystemNotification,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Alert {
pub id: String,
pub timestamp: SystemTime,
pub level: AlertLevel,
pub message: String,
pub anomaly_id: Option<String>,
pub source: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AlertLevel {
Info,
Warning,
Error,
Critical,
}
#[derive(Debug, Clone)]
pub struct AlertRule {
pub name: String,
pub condition: AlertCondition,
pub alert_level: AlertLevel,
pub message_template: String,
pub enabled: bool,
}
#[derive(Debug, Clone)]
pub enum AlertCondition {
Threshold {
metric: String,
operator: ComparisonOperator,
value: f64,
},
Rate {
metric: String,
rate_threshold: f64,
time_window: Duration,
},
Composite {
conditions: Vec<Self>,
operator: LogicalOperator,
},
}
#[derive(Debug, Clone)]
pub enum ComparisonOperator {
GreaterThan,
LessThan,
EqualTo,
NotEqualTo,
GreaterThanOrEqual,
LessThanOrEqual,
}
#[derive(Debug, Clone)]
pub enum LogicalOperator {
And,
Or,
Not,
}
#[derive(Debug, Clone)]
pub struct SuppressionRule {
pub name: String,
pub condition: SuppressionCondition,
pub duration: Duration,
pub enabled: bool,
}
#[derive(Debug, Clone)]
pub enum SuppressionCondition {
TimeBased {
start_time: SystemTime,
end_time: SystemTime,
},
MetricBased { metric: String, threshold: f64 },
EventBased { event_type: String },
}
#[derive(Debug, Clone)]
pub struct PredictionEngine {
pub models: HashMap<String, PredictionModel>,
pub predictions: HashMap<String, PredictionResult>,
pub config: PredictionConfig,
pub accuracy_tracking: AccuracyTracking,
}
#[derive(Debug, Clone)]
pub struct PredictionModel {
pub name: String,
pub model_type: PredictionModelType,
pub parameters: HashMap<String, f64>,
pub training_status: TrainingStatus,
pub accuracy: f64,
}
#[derive(Debug, Clone)]
pub enum PredictionModelType {
TimeSeries,
Regression,
Classification,
Ensemble,
DeepLearning,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PredictionResult {
pub timestamp: SystemTime,
pub predicted_value: f64,
pub confidence_interval: (f64, f64),
pub accuracy: f64,
pub time_horizon: Duration,
pub metadata: HashMap<String, String>,
}
#[derive(Debug, Clone)]
pub enum TrainingStatus {
NotTrained,
Training,
Trained,
Failed { error: String },
NeedsRetraining,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PredictionConfig {
pub prediction_horizon: Duration,
pub update_frequency: Duration,
pub min_data_points: usize,
pub confidence_level: f64,
pub enable_ensemble: bool,
}
#[derive(Debug, Clone)]
pub struct AccuracyTracking {
pub accuracy_history: VecDeque<AccuracyMeasurement>,
pub model_comparison: HashMap<String, f64>,
pub accuracy_trends: HashMap<String, TrendDirection>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccuracyMeasurement {
pub timestamp: SystemTime,
pub model_name: String,
pub actual_value: f64,
pub predicted_value: f64,
pub accuracy_score: f64,
}