ipfrs-network 0.2.0

Peer-to-peer networking layer with libp2p and QUIC for IPFRS
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
//! Network Traffic Analysis and Pattern Detection
//!
//! This module provides tools for analyzing network traffic patterns, detecting anomalies,
//! and gaining insights into network behavior.
//!
//! # Features
//!
//! - **Traffic Pattern Analysis**: Identify patterns in connection and query behavior
//! - **Anomaly Detection**: Detect unusual network activity
//! - **Bandwidth Analysis**: Analyze bandwidth usage patterns over time
//! - **Peer Behavior Profiling**: Profile peer connection and query patterns
//! - **Protocol Distribution**: Analyze protocol usage distribution
//! - **Time-Series Analysis**: Track metrics over time windows
//! - **Statistical Analysis**: Compute statistics and trends
//!
//! # Example
//!
//! ```rust
//! use ipfrs_network::traffic_analyzer::{TrafficAnalyzer, TrafficAnalyzerConfig, TrafficEvent};
//! use std::time::Duration;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let config = TrafficAnalyzerConfig::default();
//! let mut analyzer = TrafficAnalyzer::new(config);
//!
//! // Record traffic events
//! analyzer.record_connection("peer1".to_string(), 1024);
//! analyzer.record_query("peer1".to_string(), Duration::from_millis(50), true);
//!
//! // Analyze patterns
//! let analysis = analyzer.analyze()?;
//! println!("Total bandwidth: {} bytes", analysis.total_bandwidth);
//! println!("Anomalies detected: {}", analysis.anomalies.len());
//! # Ok(())
//! # }
//! ```

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, Instant};

// Helper function for serde default
fn instant_now() -> Instant {
    Instant::now()
}

/// Configuration for traffic analyzer
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrafficAnalyzerConfig {
    /// Window size for pattern analysis
    pub window_size: Duration,
    /// Number of historical windows to keep
    pub history_size: usize,
    /// Threshold for anomaly detection (standard deviations)
    pub anomaly_threshold: f64,
    /// Minimum samples needed for statistics
    pub min_samples: usize,
    /// Enable detailed peer profiling
    pub enable_peer_profiling: bool,
    /// Enable protocol distribution tracking
    pub enable_protocol_tracking: bool,
}

impl Default for TrafficAnalyzerConfig {
    fn default() -> Self {
        Self {
            window_size: Duration::from_secs(60),
            history_size: 100,
            anomaly_threshold: 3.0,
            min_samples: 10,
            enable_peer_profiling: true,
            enable_protocol_tracking: true,
        }
    }
}

impl TrafficAnalyzerConfig {
    /// Create configuration for short-term analysis (1 minute windows)
    pub fn short_term() -> Self {
        Self {
            window_size: Duration::from_secs(60),
            history_size: 60,
            ..Default::default()
        }
    }

    /// Create configuration for long-term analysis (1 hour windows)
    pub fn long_term() -> Self {
        Self {
            window_size: Duration::from_secs(3600),
            history_size: 24,
            ..Default::default()
        }
    }

    /// Create configuration for real-time monitoring (5 second windows)
    pub fn realtime() -> Self {
        Self {
            window_size: Duration::from_secs(5),
            history_size: 720, // 1 hour of 5-second windows
            anomaly_threshold: 2.5,
            min_samples: 5,
            ..Default::default()
        }
    }
}

/// Type of traffic event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TrafficEvent {
    /// Connection established
    ConnectionEstablished {
        peer_id: String,
        #[serde(skip, default = "instant_now")]
        timestamp: Instant,
    },
    /// Connection closed
    ConnectionClosed {
        peer_id: String,
        duration: Duration,
        bytes_transferred: u64,
    },
    /// DHT query
    Query {
        peer_id: String,
        latency: Duration,
        success: bool,
    },
    /// Bandwidth sample
    BandwidthSample {
        bytes_sent: u64,
        bytes_received: u64,
        duration: Duration,
    },
    /// Protocol usage
    ProtocolUsage {
        protocol: String,
        message_count: u64,
    },
}

/// Traffic pattern detected
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrafficPattern {
    /// Pattern type
    pub pattern_type: PatternType,
    /// Pattern description
    pub description: String,
    /// Confidence score (0.0-1.0)
    pub confidence: f64,
    /// Pattern start time
    #[serde(skip, default = "instant_now")]
    pub start_time: Instant,
    /// Pattern duration
    pub duration: Duration,
}

/// Type of traffic pattern
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PatternType {
    /// Steady traffic
    Steady,
    /// Increasing traffic
    Increasing,
    /// Decreasing traffic
    Decreasing,
    /// Bursty traffic
    Bursty,
    /// Periodic traffic
    Periodic,
    /// Anomalous traffic
    Anomalous,
}

/// Detected anomaly
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrafficAnomaly {
    /// Anomaly type
    pub anomaly_type: AnomalyType,
    /// Description
    pub description: String,
    /// Severity (0.0-1.0, higher is more severe)
    pub severity: f64,
    /// Detection timestamp
    #[serde(skip, default = "instant_now")]
    pub timestamp: Instant,
    /// Affected peer (if applicable)
    pub peer_id: Option<String>,
}

/// Type of anomaly
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AnomalyType {
    /// Bandwidth spike
    BandwidthSpike,
    /// Unusual connection pattern
    ConnectionAnomaly,
    /// Query failure spike
    QueryFailureSpike,
    /// Latency spike
    LatencySpike,
    /// Suspicious peer behavior
    SuspiciousPeer,
}

/// Peer behavior profile
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PeerProfile {
    /// Peer ID
    pub peer_id: String,
    /// Total connections
    pub total_connections: usize,
    /// Total queries
    pub total_queries: u64,
    /// Successful queries
    pub successful_queries: u64,
    /// Average latency
    pub average_latency: Duration,
    /// Total bytes transferred
    pub total_bytes: u64,
    /// First seen timestamp
    #[serde(skip, default = "instant_now")]
    pub first_seen: Instant,
    /// Last seen timestamp
    #[serde(skip, default = "instant_now")]
    pub last_seen: Instant,
    /// Behavior score (0.0-1.0, higher is better)
    pub behavior_score: f64,
}

/// Traffic analysis results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrafficAnalysis {
    /// Analysis timestamp
    #[serde(skip, default = "instant_now")]
    pub timestamp: Instant,
    /// Total bandwidth (bytes)
    pub total_bandwidth: u64,
    /// Total connections
    pub total_connections: usize,
    /// Total queries
    pub total_queries: u64,
    /// Query success rate
    pub query_success_rate: f64,
    /// Average latency
    pub average_latency: Duration,
    /// Detected patterns
    pub patterns: Vec<TrafficPattern>,
    /// Detected anomalies
    pub anomalies: Vec<TrafficAnomaly>,
    /// Peer profiles
    pub peer_profiles: HashMap<String, PeerProfile>,
    /// Protocol distribution
    pub protocol_distribution: HashMap<String, u64>,
    /// Bandwidth trend (increasing/decreasing/steady)
    pub bandwidth_trend: TrendDirection,
    /// Connection trend
    pub connection_trend: TrendDirection,
}

/// Trend direction
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TrendDirection {
    /// Increasing trend
    Increasing,
    /// Decreasing trend
    Decreasing,
    /// Steady trend
    Steady,
    /// Insufficient data
    Unknown,
}

/// Traffic analyzer
pub struct TrafficAnalyzer {
    config: TrafficAnalyzerConfig,
    events: Vec<TrafficEvent>,
    peer_data: HashMap<String, PeerData>,
    bandwidth_history: Vec<BandwidthSample>,
    connection_history: Vec<usize>,
    query_history: Vec<QuerySample>,
    start_time: Instant,
}

#[derive(Debug, Clone)]
struct PeerData {
    connections: usize,
    queries: u64,
    successful_queries: u64,
    latencies: Vec<Duration>,
    bytes_transferred: u64,
    first_seen: Instant,
    last_seen: Instant,
}

#[derive(Debug, Clone)]
struct BandwidthSample {
    timestamp: Instant,
    bytes: u64,
}

#[derive(Debug, Clone)]
#[allow(dead_code)]
struct QuerySample {
    timestamp: Instant,
    success_rate: f64,
    latency: Duration,
}

impl TrafficAnalyzer {
    /// Create a new traffic analyzer
    pub fn new(config: TrafficAnalyzerConfig) -> Self {
        Self {
            config,
            events: Vec::new(),
            peer_data: HashMap::new(),
            bandwidth_history: Vec::new(),
            connection_history: Vec::new(),
            query_history: Vec::new(),
            start_time: Instant::now(),
        }
    }

    /// Record a connection event
    pub fn record_connection(&mut self, peer_id: String, bytes: u64) {
        let timestamp = Instant::now();
        self.events.push(TrafficEvent::ConnectionEstablished {
            peer_id: peer_id.clone(),
            timestamp,
        });

        let data = self.peer_data.entry(peer_id).or_insert(PeerData {
            connections: 0,
            queries: 0,
            successful_queries: 0,
            latencies: Vec::new(),
            bytes_transferred: 0,
            first_seen: timestamp,
            last_seen: timestamp,
        });

        data.connections += 1;
        data.bytes_transferred += bytes;
        data.last_seen = timestamp;

        self.connection_history.push(self.peer_data.len());
    }

    /// Record a query event
    pub fn record_query(&mut self, peer_id: String, latency: Duration, success: bool) {
        self.events.push(TrafficEvent::Query {
            peer_id: peer_id.clone(),
            latency,
            success,
        });

        if let Some(data) = self.peer_data.get_mut(&peer_id) {
            data.queries += 1;
            if success {
                data.successful_queries += 1;
            }
            data.latencies.push(latency);
        }

        let success_rate = if success { 1.0 } else { 0.0 };
        self.query_history.push(QuerySample {
            timestamp: Instant::now(),
            success_rate,
            latency,
        });
    }

    /// Record bandwidth usage
    pub fn record_bandwidth(&mut self, bytes_sent: u64, bytes_received: u64) {
        let total = bytes_sent + bytes_received;
        self.bandwidth_history.push(BandwidthSample {
            timestamp: Instant::now(),
            bytes: total,
        });

        self.events.push(TrafficEvent::BandwidthSample {
            bytes_sent,
            bytes_received,
            duration: Duration::from_secs(1),
        });
    }

    /// Analyze traffic and return results
    pub fn analyze(&self) -> Result<TrafficAnalysis, TrafficAnalyzerError> {
        let timestamp = Instant::now();

        // Calculate total bandwidth
        let total_bandwidth: u64 = self.bandwidth_history.iter().map(|s| s.bytes).sum();

        // Calculate total connections and queries
        let total_connections = self.connection_history.last().copied().unwrap_or(0);
        let total_queries: u64 = self.peer_data.values().map(|p| p.queries).sum();
        let successful_queries: u64 = self.peer_data.values().map(|p| p.successful_queries).sum();

        let query_success_rate = if total_queries > 0 {
            (successful_queries as f64 / total_queries as f64) * 100.0
        } else {
            0.0
        };

        // Calculate average latency
        let all_latencies: Vec<Duration> = self
            .peer_data
            .values()
            .flat_map(|p| p.latencies.iter().copied())
            .collect();

        let average_latency = if !all_latencies.is_empty() {
            let sum: Duration = all_latencies.iter().sum();
            sum / all_latencies.len() as u32
        } else {
            Duration::ZERO
        };

        // Detect patterns
        let patterns = self.detect_patterns();

        // Detect anomalies
        let anomalies = self.detect_anomalies();

        // Build peer profiles
        let peer_profiles = self.build_peer_profiles();

        // Determine trends
        let bandwidth_trend = self.calculate_trend(
            &self
                .bandwidth_history
                .iter()
                .map(|s| s.bytes as f64)
                .collect::<Vec<_>>(),
        );
        let connection_trend = self.calculate_trend(
            &self
                .connection_history
                .iter()
                .map(|&c| c as f64)
                .collect::<Vec<_>>(),
        );

        Ok(TrafficAnalysis {
            timestamp,
            total_bandwidth,
            total_connections,
            total_queries,
            query_success_rate,
            average_latency,
            patterns,
            anomalies,
            peer_profiles,
            protocol_distribution: HashMap::new(),
            bandwidth_trend,
            connection_trend,
        })
    }

    /// Detect traffic patterns
    fn detect_patterns(&self) -> Vec<TrafficPattern> {
        let mut patterns = Vec::new();

        // Detect bursty pattern if bandwidth varies significantly
        if self.bandwidth_history.len() >= self.config.min_samples {
            let values: Vec<f64> = self
                .bandwidth_history
                .iter()
                .map(|s| s.bytes as f64)
                .collect();
            let (mean, stddev) = Self::calculate_statistics(&values);

            if stddev > mean * 0.5 {
                patterns.push(TrafficPattern {
                    pattern_type: PatternType::Bursty,
                    description: "Traffic shows bursty pattern with high variance".to_string(),
                    confidence: 0.8,
                    start_time: self.start_time,
                    duration: Instant::now().duration_since(self.start_time),
                });
            }
        }

        patterns
    }

    /// Detect anomalies
    fn detect_anomalies(&self) -> Vec<TrafficAnomaly> {
        let mut anomalies = Vec::new();

        // Detect bandwidth spikes
        if self.bandwidth_history.len() >= self.config.min_samples {
            let values: Vec<f64> = self
                .bandwidth_history
                .iter()
                .map(|s| s.bytes as f64)
                .collect();
            let (mean, stddev) = Self::calculate_statistics(&values);

            for sample in &self.bandwidth_history {
                let z_score = (sample.bytes as f64 - mean).abs() / stddev.max(1.0);
                if z_score > self.config.anomaly_threshold {
                    anomalies.push(TrafficAnomaly {
                        anomaly_type: AnomalyType::BandwidthSpike,
                        description: format!(
                            "Bandwidth spike detected: {} bytes ({:.1} σ)",
                            sample.bytes, z_score
                        ),
                        severity: (z_score / self.config.anomaly_threshold).min(1.0),
                        timestamp: sample.timestamp,
                        peer_id: None,
                    });
                }
            }
        }

        // Detect query failure spikes
        if self.query_history.len() >= self.config.min_samples {
            let success_rates: Vec<f64> =
                self.query_history.iter().map(|q| q.success_rate).collect();
            let recent_rate = success_rates.iter().rev().take(10).sum::<f64>() / 10.0;

            if recent_rate < 0.5 {
                anomalies.push(TrafficAnomaly {
                    anomaly_type: AnomalyType::QueryFailureSpike,
                    description: format!(
                        "High query failure rate: {:.1}%",
                        (1.0 - recent_rate) * 100.0
                    ),
                    severity: 1.0 - recent_rate,
                    timestamp: Instant::now(),
                    peer_id: None,
                });
            }
        }

        anomalies
    }

    /// Build peer profiles
    fn build_peer_profiles(&self) -> HashMap<String, PeerProfile> {
        self.peer_data
            .iter()
            .map(|(peer_id, data)| {
                let average_latency = if !data.latencies.is_empty() {
                    let sum: Duration = data.latencies.iter().sum();
                    sum / data.latencies.len() as u32
                } else {
                    Duration::ZERO
                };

                let behavior_score = if data.queries > 0 {
                    (data.successful_queries as f64 / data.queries as f64) * 100.0 / 100.0
                } else {
                    1.0
                };

                let profile = PeerProfile {
                    peer_id: peer_id.clone(),
                    total_connections: data.connections,
                    total_queries: data.queries,
                    successful_queries: data.successful_queries,
                    average_latency,
                    total_bytes: data.bytes_transferred,
                    first_seen: data.first_seen,
                    last_seen: data.last_seen,
                    behavior_score,
                };

                (peer_id.clone(), profile)
            })
            .collect()
    }

    /// Calculate trend direction
    fn calculate_trend(&self, values: &[f64]) -> TrendDirection {
        if values.len() < self.config.min_samples {
            return TrendDirection::Unknown;
        }

        // Simple linear regression to determine trend
        let n = values.len() as f64;
        let x_mean = (0..values.len()).map(|i| i as f64).sum::<f64>() / n;
        let y_mean = values.iter().sum::<f64>() / n;

        let mut numerator = 0.0;
        let mut denominator = 0.0;

        for (i, &y) in values.iter().enumerate() {
            let x = i as f64;
            numerator += (x - x_mean) * (y - y_mean);
            denominator += (x - x_mean) * (x - x_mean);
        }

        let slope = if denominator != 0.0 {
            numerator / denominator
        } else {
            0.0
        };

        if slope > y_mean * 0.01 {
            TrendDirection::Increasing
        } else if slope < -y_mean * 0.01 {
            TrendDirection::Decreasing
        } else {
            TrendDirection::Steady
        }
    }

    /// Calculate statistics (mean, standard deviation)
    fn calculate_statistics(values: &[f64]) -> (f64, f64) {
        if values.is_empty() {
            return (0.0, 0.0);
        }

        let mean = values.iter().sum::<f64>() / values.len() as f64;
        let variance = values.iter().map(|v| (v - mean).powi(2)).sum::<f64>() / values.len() as f64;
        let stddev = variance.sqrt();

        (mean, stddev)
    }

    /// Get current statistics
    pub fn get_stats(&self) -> TrafficAnalyzerStats {
        TrafficAnalyzerStats {
            total_events: self.events.len(),
            total_peers: self.peer_data.len(),
            bandwidth_samples: self.bandwidth_history.len(),
            query_samples: self.query_history.len(),
            uptime: Instant::now().duration_since(self.start_time),
        }
    }

    /// Clear all recorded data
    pub fn clear(&mut self) {
        self.events.clear();
        self.peer_data.clear();
        self.bandwidth_history.clear();
        self.connection_history.clear();
        self.query_history.clear();
        self.start_time = Instant::now();
    }
}

/// Traffic analyzer statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrafficAnalyzerStats {
    /// Total events recorded
    pub total_events: usize,
    /// Total peers tracked
    pub total_peers: usize,
    /// Bandwidth samples collected
    pub bandwidth_samples: usize,
    /// Query samples collected
    pub query_samples: usize,
    /// Analyzer uptime
    pub uptime: Duration,
}

/// Error types for traffic analyzer
#[derive(Debug, thiserror::Error)]
pub enum TrafficAnalyzerError {
    #[error("Insufficient data for analysis")]
    InsufficientData,

    #[error("Analysis failed: {0}")]
    AnalysisFailed(String),
}

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

    #[test]
    fn test_config_presets() {
        let short = TrafficAnalyzerConfig::short_term();
        assert_eq!(short.window_size, Duration::from_secs(60));

        let long = TrafficAnalyzerConfig::long_term();
        assert_eq!(long.window_size, Duration::from_secs(3600));

        let realtime = TrafficAnalyzerConfig::realtime();
        assert_eq!(realtime.window_size, Duration::from_secs(5));
    }

    #[test]
    fn test_analyzer_creation() {
        let config = TrafficAnalyzerConfig::default();
        let analyzer = TrafficAnalyzer::new(config);
        let stats = analyzer.get_stats();
        assert_eq!(stats.total_events, 0);
        assert_eq!(stats.total_peers, 0);
    }

    #[test]
    fn test_record_connection() {
        let config = TrafficAnalyzerConfig::default();
        let mut analyzer = TrafficAnalyzer::new(config);

        analyzer.record_connection("peer1".to_string(), 1024);
        let stats = analyzer.get_stats();
        assert_eq!(stats.total_peers, 1);
    }

    #[test]
    fn test_record_query() {
        let config = TrafficAnalyzerConfig::default();
        let mut analyzer = TrafficAnalyzer::new(config);

        analyzer.record_connection("peer1".to_string(), 0);
        analyzer.record_query("peer1".to_string(), Duration::from_millis(50), true);

        let stats = analyzer.get_stats();
        assert_eq!(stats.query_samples, 1);
    }

    #[test]
    fn test_record_bandwidth() {
        let config = TrafficAnalyzerConfig::default();
        let mut analyzer = TrafficAnalyzer::new(config);

        analyzer.record_bandwidth(1000, 2000);
        let stats = analyzer.get_stats();
        assert_eq!(stats.bandwidth_samples, 1);
    }

    #[test]
    fn test_analyze() {
        let config = TrafficAnalyzerConfig::default();
        let mut analyzer = TrafficAnalyzer::new(config);

        analyzer.record_connection("peer1".to_string(), 1024);
        analyzer.record_query("peer1".to_string(), Duration::from_millis(50), true);
        analyzer.record_bandwidth(1000, 2000);

        let analysis = analyzer.analyze().expect("test: analyze should succeed");
        assert_eq!(analysis.total_connections, 1);
        assert_eq!(analysis.total_queries, 1);
        assert_eq!(analysis.query_success_rate, 100.0);
    }

    #[test]
    fn test_peer_profile() {
        let config = TrafficAnalyzerConfig::default();
        let mut analyzer = TrafficAnalyzer::new(config);

        analyzer.record_connection("peer1".to_string(), 1024);
        analyzer.record_query("peer1".to_string(), Duration::from_millis(50), true);

        let analysis = analyzer.analyze().expect("test: analyze should succeed");
        let profile = analysis
            .peer_profiles
            .get("peer1")
            .expect("test: peer1 profile should exist");
        assert_eq!(profile.total_connections, 1);
        assert_eq!(profile.total_queries, 1);
        assert_eq!(profile.behavior_score, 1.0);
    }

    #[test]
    fn test_clear() {
        let config = TrafficAnalyzerConfig::default();
        let mut analyzer = TrafficAnalyzer::new(config);

        analyzer.record_connection("peer1".to_string(), 1024);
        analyzer.clear();

        let stats = analyzer.get_stats();
        assert_eq!(stats.total_events, 0);
        assert_eq!(stats.total_peers, 0);
    }

    #[test]
    fn test_trend_calculation() {
        let config = TrafficAnalyzerConfig::default();
        let analyzer = TrafficAnalyzer::new(config);

        // Increasing trend
        let increasing = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
        assert_eq!(
            analyzer.calculate_trend(&increasing),
            TrendDirection::Increasing
        );

        // Decreasing trend
        let decreasing = vec![10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0];
        assert_eq!(
            analyzer.calculate_trend(&decreasing),
            TrendDirection::Decreasing
        );

        // Steady trend
        let steady = vec![5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0];
        assert_eq!(analyzer.calculate_trend(&steady), TrendDirection::Steady);
    }

    #[test]
    fn test_statistics_calculation() {
        let values = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let (mean, stddev) = TrafficAnalyzer::calculate_statistics(&values);
        assert_eq!(mean, 3.0);
        assert!((stddev - 1.414).abs() < 0.01);
    }
}