1use scirs2_core::ndarray::Array2;
8use scirs2_core::Complex64;
9use serde::{Deserialize, Serialize};
10use std::collections::{HashMap, HashSet, VecDeque};
11use std::sync::Arc;
12use std::time::{Duration, SystemTime};
13
14use super::collectors::*;
16use super::metrics::*;
17
18pub struct PerformanceAnalyzer {
19 pub config: AnalysisConfig,
21 pub historical_data: HistoricalPerformanceData,
23 pub performance_models: PerformanceModels,
25 pub anomaly_detector: AnomalyDetector,
27 pub prediction_engine: PredictionEngine,
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct AnalysisConfig {
34 pub analysis_depth: AnalysisDepth,
36 pub statistical_methods: HashSet<StatisticalMethod>,
38 pub ml_models: HashSet<MlModel>,
40 pub confidence_level: f64,
42 pub min_data_points: usize,
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize)]
48pub enum AnalysisDepth {
49 Basic,
51 Standard,
53 Advanced,
55 Comprehensive,
57}
58
59#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
61pub enum StatisticalMethod {
62 Descriptive,
64 Correlation,
66 Regression,
68 TimeSeries,
70 HypothesisTesting,
72}
73
74#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
76pub enum MlModel {
77 LinearRegression,
79 RandomForest,
81 NeuralNetwork,
83 SupportVectorMachine,
85 Clustering,
87}
88
89#[derive(Debug, Clone)]
91pub struct HistoricalPerformanceData {
92 pub snapshots: VecDeque<PerformanceSnapshot>,
94 pub retention_policy: DataRetentionPolicy,
96 pub compression_settings: CompressionSettings,
98 pub integrity_checks: IntegrityChecks,
100}
101
102#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct PerformanceSnapshot {
105 pub timestamp: SystemTime,
107 pub metrics: HashMap<String, f64>,
109 pub system_state: SystemState,
111 pub environment: EnvironmentInfo,
113 pub metadata: HashMap<String, String>,
115}
116
117#[derive(Debug, Clone, Serialize, Deserialize)]
119pub struct SystemState {
120 pub cpu_state: CpuState,
122 pub memory_state: MemoryState,
124 pub io_state: IoState,
126 pub network_state: NetworkState,
128}
129
130#[derive(Debug, Clone, Serialize, Deserialize)]
132pub struct CpuState {
133 pub utilization: f64,
135 pub frequency: f64,
137 pub temperature: Option<f64>,
139 pub active_processes: usize,
141}
142
143#[derive(Debug, Clone, Serialize, Deserialize)]
145pub struct MemoryState {
146 pub total_memory: usize,
148 pub used_memory: usize,
150 pub free_memory: usize,
152 pub cached_memory: usize,
154}
155
156#[derive(Debug, Clone, Serialize, Deserialize)]
158pub struct IoState {
159 pub disk_usage: f64,
161 pub read_iops: f64,
163 pub write_iops: f64,
165 pub queue_depth: f64,
167}
168
169#[derive(Debug, Clone, Serialize, Deserialize)]
171pub struct NetworkState {
172 pub bandwidth_utilization: f64,
174 pub active_connections: usize,
176 pub packet_rate: f64,
178 pub error_rate: f64,
180}
181
182#[derive(Debug, Clone, Serialize, Deserialize)]
184pub struct EnvironmentInfo {
185 pub operating_system: String,
187 pub hardware_config: HardwareConfig,
189 pub software_versions: HashMap<String, String>,
191 pub environment_variables: HashMap<String, String>,
193}
194
195#[derive(Debug, Clone, Serialize, Deserialize)]
197pub struct HardwareConfig {
198 pub cpu_model: String,
200 pub cpu_cores: u32,
202 pub total_memory: usize,
204 pub gpu_info: Option<GpuInfo>,
206 pub storage_info: StorageInfo,
208}
209
210#[derive(Debug, Clone, Serialize, Deserialize)]
212pub struct GpuInfo {
213 pub model: String,
215 pub memory: usize,
217 pub compute_capability: String,
219}
220
221#[derive(Debug, Clone, Serialize, Deserialize)]
223pub struct StorageInfo {
224 pub storage_type: StorageType,
226 pub total_capacity: usize,
228 pub available_capacity: usize,
230}
231
232#[derive(Debug, Clone, Serialize, Deserialize)]
234pub enum StorageType {
235 HDD,
237 SSD,
239 NVMe,
241 Network,
243}
244
245#[derive(Debug, Clone, Serialize, Deserialize)]
247pub struct DataRetentionPolicy {
248 pub max_age: Duration,
250 pub max_snapshots: usize,
252 pub compression_threshold: Duration,
254 pub archival_policy: ArchivalPolicy,
256}
257
258#[derive(Debug, Clone, Serialize, Deserialize)]
260pub enum ArchivalPolicy {
261 Delete,
263 Compress,
265 Archive { location: String },
267 KeepAll,
269}
270
271#[derive(Debug, Clone, Serialize, Deserialize)]
273pub struct CompressionSettings {
274 pub algorithm: CompressionAlgorithm,
276 pub compression_level: u8,
278 pub realtime_compression: bool,
280}
281
282#[derive(Debug, Clone, Serialize, Deserialize)]
284pub enum CompressionAlgorithm {
285 None,
287 LZ4,
289 Gzip,
291 Zstd,
293 Custom { name: String },
295}
296
297#[derive(Debug, Clone, Serialize, Deserialize)]
299pub struct IntegrityChecks {
300 pub enable_checksums: bool,
302 pub checksum_algorithm: ChecksumAlgorithm,
304 pub verification_frequency: Duration,
306}
307
308#[derive(Debug, Clone, Serialize, Deserialize)]
310pub enum ChecksumAlgorithm {
311 CRC32,
313 MD5,
315 SHA256,
317 Blake3,
319}
320
321#[derive(Debug, Clone)]
323pub struct PerformanceModels {
324 pub statistical_models: HashMap<String, StatisticalModel>,
326 pub ml_models: HashMap<String, MachineLearningModel>,
328 pub hybrid_models: HashMap<String, HybridModel>,
330 pub evaluation_results: ModelEvaluationResults,
332}
333
334#[derive(Debug, Clone)]
336pub struct StatisticalModel {
337 pub model_type: StatisticalModelType,
339 pub parameters: HashMap<String, f64>,
341 pub accuracy: f64,
343 pub training_data_size: usize,
345}
346
347#[derive(Debug, Clone)]
349pub enum StatisticalModelType {
350 LinearRegression,
352 Autoregressive,
354 MovingAverage,
356 Arima,
358 ExponentialSmoothing,
360}
361
362#[derive(Debug, Clone)]
364pub struct MachineLearningModel {
365 pub model_type: MlModelType,
367 pub hyperparameters: HashMap<String, f64>,
369 pub accuracy: f64,
371 pub training_data_size: usize,
373 pub feature_importance: HashMap<String, f64>,
375}
376
377#[derive(Debug, Clone)]
379pub enum MlModelType {
380 RandomForest,
382 GradientBoosting,
384 NeuralNetwork,
386 SupportVectorRegression,
388 GaussianProcess,
390}
391
392#[derive(Debug, Clone)]
394pub struct HybridModel {
395 pub component_models: Vec<ComponentModel>,
397 pub ensemble_weights: HashMap<String, f64>,
399 pub accuracy: f64,
401 pub combination_strategy: CombinationStrategy,
403}
404
405#[derive(Debug, Clone)]
407pub struct ComponentModel {
408 pub name: String,
410 pub model_type: ComponentModelType,
412 pub weight: f64,
414 pub accuracy: f64,
416}
417
418#[derive(Debug, Clone)]
420pub enum ComponentModelType {
421 Statistical(StatisticalModelType),
423 MachineLearning(MlModelType),
425 PhysicsBased,
427 Empirical,
429}
430
431#[derive(Debug, Clone)]
433pub enum CombinationStrategy {
434 WeightedAverage,
436 Voting,
438 Stacking,
440 BayesianAveraging,
442}
443
444#[derive(Debug, Clone, Serialize, Deserialize)]
446pub struct ModelEvaluationResults {
447 pub cv_scores: HashMap<String, f64>,
449 pub test_performance: HashMap<String, f64>,
451 pub model_comparison: ModelComparison,
453 pub feature_analysis: FeatureAnalysis,
455}
456
457#[derive(Debug, Clone, Serialize, Deserialize)]
459pub struct ModelComparison {
460 pub best_model: String,
462 pub performance_rankings: Vec<ModelRanking>,
464 pub significance_tests: HashMap<String, f64>,
466}
467
468#[derive(Debug, Clone, Serialize, Deserialize)]
470pub struct ModelRanking {
471 pub model_name: String,
473 pub performance_score: f64,
475 pub rank: u32,
477 pub confidence_interval: (f64, f64),
479}
480
481#[derive(Debug, Clone, Serialize, Deserialize)]
483pub struct FeatureAnalysis {
484 pub feature_importance: HashMap<String, f64>,
486 pub feature_correlations: HashMap<String, f64>,
488 pub feature_selection: FeatureSelectionResults,
490}
491
492#[derive(Debug, Clone, Serialize, Deserialize)]
494pub struct FeatureSelectionResults {
495 pub selected_features: Vec<String>,
497 pub selection_method: String,
499 pub selection_criteria: HashMap<String, f64>,
501}
502
503#[derive(Debug, Clone)]
505pub struct AnomalyDetector {
506 pub algorithms: HashMap<String, AnomalyDetectionAlgorithm>,
508 pub detected_anomalies: Vec<PerformanceAnomaly>,
510 pub config: AnomalyDetectionConfig,
512 pub alert_system: AlertSystem,
514}
515
516#[derive(Debug, Clone)]
518pub struct AnomalyDetectionAlgorithm {
519 pub algorithm_type: AnomalyAlgorithmType,
521 pub parameters: HashMap<String, f64>,
523 pub threshold: f64,
525 pub false_positive_rate: f64,
527}
528
529#[derive(Debug, Clone)]
531pub enum AnomalyAlgorithmType {
532 StatisticalOutlier,
534 IsolationForest,
536 OneClassSVM,
538 DBSCAN,
540 Autoencoder,
542}
543
544#[derive(Debug, Clone, Serialize, Deserialize)]
546pub struct PerformanceAnomaly {
547 pub id: String,
549 pub timestamp: SystemTime,
551 pub anomaly_type: AnomalyType,
553 pub severity: AnomySeverity,
555 pub affected_metrics: Vec<String>,
557 pub anomaly_score: f64,
559 pub root_cause: Option<String>,
561 pub recommended_actions: Vec<String>,
563}
564
565#[derive(Debug, Clone, Serialize, Deserialize)]
567pub enum AnomalyType {
568 PerformanceDegradation,
570 ResourceSpike,
572 ErrorRateIncrease,
574 LatencyIncrease,
576 ThroughputDecrease,
578 MemoryLeak,
580 Custom { name: String },
582}
583
584#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
586pub enum AnomySeverity {
587 Low,
589 Medium,
591 High,
593 Critical,
595}
596
597#[derive(Debug, Clone, Serialize, Deserialize)]
599pub struct AnomalyDetectionConfig {
600 pub enable_realtime: bool,
602 pub sensitivity: f64,
604 pub min_duration: Duration,
606 pub alert_thresholds: HashMap<AnomySeverity, f64>,
608}
609
610#[derive(Debug, Clone)]
612pub struct AlertSystem {
613 pub alert_channels: Vec<AlertChannel>,
615 pub alert_history: VecDeque<Alert>,
617 pub alert_rules: Vec<AlertRule>,
619 pub suppression_rules: Vec<SuppressionRule>,
621}
622
623#[derive(Debug, Clone)]
625pub struct AlertChannel {
626 pub channel_type: AlertChannelType,
628 pub config: HashMap<String, String>,
630 pub enabled: bool,
632}
633
634#[derive(Debug, Clone)]
636pub enum AlertChannelType {
637 Email,
639 Slack,
641 Webhook,
643 LogFile,
645 SystemNotification,
647}
648
649#[derive(Debug, Clone, Serialize, Deserialize)]
651pub struct Alert {
652 pub id: String,
654 pub timestamp: SystemTime,
656 pub level: AlertLevel,
658 pub message: String,
660 pub anomaly_id: Option<String>,
662 pub source: String,
664}
665
666#[derive(Debug, Clone, Serialize, Deserialize)]
668pub enum AlertLevel {
669 Info,
671 Warning,
673 Error,
675 Critical,
677}
678
679#[derive(Debug, Clone)]
681pub struct AlertRule {
682 pub name: String,
684 pub condition: AlertCondition,
686 pub alert_level: AlertLevel,
688 pub message_template: String,
690 pub enabled: bool,
692}
693
694#[derive(Debug, Clone)]
696pub enum AlertCondition {
697 Threshold {
699 metric: String,
700 operator: ComparisonOperator,
701 value: f64,
702 },
703 Rate {
705 metric: String,
706 rate_threshold: f64,
707 time_window: Duration,
708 },
709 Composite {
711 conditions: Vec<Self>,
712 operator: LogicalOperator,
713 },
714}
715
716#[derive(Debug, Clone)]
718pub enum ComparisonOperator {
719 GreaterThan,
721 LessThan,
723 EqualTo,
725 NotEqualTo,
727 GreaterThanOrEqual,
729 LessThanOrEqual,
731}
732
733#[derive(Debug, Clone)]
735pub enum LogicalOperator {
736 And,
738 Or,
740 Not,
742}
743
744#[derive(Debug, Clone)]
746pub struct SuppressionRule {
747 pub name: String,
749 pub condition: SuppressionCondition,
751 pub duration: Duration,
753 pub enabled: bool,
755}
756
757#[derive(Debug, Clone)]
759pub enum SuppressionCondition {
760 TimeBased {
762 start_time: SystemTime,
763 end_time: SystemTime,
764 },
765 MetricBased { metric: String, threshold: f64 },
767 EventBased { event_type: String },
769}
770
771#[derive(Debug, Clone)]
773pub struct PredictionEngine {
774 pub models: HashMap<String, PredictionModel>,
776 pub predictions: HashMap<String, PredictionResult>,
778 pub config: PredictionConfig,
780 pub accuracy_tracking: AccuracyTracking,
782}
783
784#[derive(Debug, Clone)]
786pub struct PredictionModel {
787 pub name: String,
789 pub model_type: PredictionModelType,
791 pub parameters: HashMap<String, f64>,
793 pub training_status: TrainingStatus,
795 pub accuracy: f64,
797}
798
799#[derive(Debug, Clone)]
801pub enum PredictionModelType {
802 TimeSeries,
804 Regression,
806 Classification,
808 Ensemble,
810 DeepLearning,
812}
813
814#[derive(Debug, Clone, Serialize, Deserialize)]
816pub struct PredictionResult {
817 pub timestamp: SystemTime,
819 pub predicted_value: f64,
821 pub confidence_interval: (f64, f64),
823 pub accuracy: f64,
825 pub time_horizon: Duration,
827 pub metadata: HashMap<String, String>,
829}
830
831#[derive(Debug, Clone)]
833pub enum TrainingStatus {
834 NotTrained,
836 Training,
838 Trained,
840 Failed { error: String },
842 NeedsRetraining,
844}
845
846#[derive(Debug, Clone, Serialize, Deserialize)]
848pub struct PredictionConfig {
849 pub prediction_horizon: Duration,
851 pub update_frequency: Duration,
853 pub min_data_points: usize,
855 pub confidence_level: f64,
857 pub enable_ensemble: bool,
859}
860
861#[derive(Debug, Clone)]
863pub struct AccuracyTracking {
864 pub accuracy_history: VecDeque<AccuracyMeasurement>,
866 pub model_comparison: HashMap<String, f64>,
868 pub accuracy_trends: HashMap<String, TrendDirection>,
870}
871
872#[derive(Debug, Clone, Serialize, Deserialize)]
874pub struct AccuracyMeasurement {
875 pub timestamp: SystemTime,
877 pub model_name: String,
879 pub actual_value: f64,
881 pub predicted_value: f64,
883 pub accuracy_score: f64,
885}