voirs-evaluation 0.1.0-rc.1

Quality evaluation and assessment framework for VoiRS
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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
//! Federated Evaluation System
//!
//! Distributed speech synthesis evaluation across multiple nodes without centralizing data.
//! Enables privacy-preserving collaborative evaluation with secure aggregation.
//!
//! # Features
//!
//! - **Distributed Processing**: Evaluate models across geographically distributed datasets
//! - **Secure Aggregation**: Combine results without exposing individual node data
//! - **Privacy Preservation**: Built-in differential privacy and encryption
//! - **Fault Tolerance**: Handle node failures gracefully with redundancy
//! - **Load Balancing**: Distribute evaluation workload efficiently
//! - **Progress Tracking**: Monitor federated evaluation progress in real-time
//!
//! # Example
//!
//! ```rust
//! use voirs_evaluation::federated::{
//!     FederatedCoordinator, FederatedNode, NodeConfig, CoordinatorConfig
//! };
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Setup coordinator
//! let coordinator = FederatedCoordinator::new(CoordinatorConfig::default()).await?;
//!
//! // Register evaluation nodes
//! coordinator.register_node("node1", "http://node1:8080").await?;
//! coordinator.register_node("node2", "http://node2:8080").await?;
//!
//! // Start federated evaluation
//! let result = coordinator.evaluate_federated().await?;
//! println!("Federated evaluation score: {:.3}", result.aggregated_score);
//! # Ok(())
//! # }
//! ```

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use thiserror::Error;
use tokio::sync::{RwLock, Semaphore};
use tracing::{debug, error, info, warn};
use voirs_sdk::{AudioBuffer, VoirsError};

use crate::privacy::{PrivacyConfig, PrivacyPreservingEvaluator, PrivateEvaluationResult};
use crate::quality::QualityEvaluator;
use crate::traits::{QualityEvaluationConfig, QualityEvaluator as QualityEvaluatorTrait};

/// Federated system errors
#[derive(Error, Debug)]
pub enum FederatedError {
    /// Node communication error
    #[error("Node communication error with '{node}': {message}")]
    CommunicationError {
        /// Node identifier
        node: String,
        /// Error message
        message: String,
    },

    /// Aggregation error
    #[error("Aggregation error: {message}")]
    AggregationError {
        /// Error message
        message: String,
    },

    /// Node registration error
    #[error("Node registration error: {message}")]
    RegistrationError {
        /// Error message
        message: String,
    },

    /// Consensus error
    #[error("Consensus error: {message}")]
    ConsensusError {
        /// Error message
        message: String,
    },

    /// Timeout error
    #[error("Operation timed out after {duration:?}")]
    TimeoutError {
        /// Timeout duration
        duration: Duration,
    },

    /// Invalid configuration
    #[error("Invalid configuration: {message}")]
    ConfigError {
        /// Error message
        message: String,
    },

    /// VoiRS error
    #[error("VoiRS error: {0}")]
    VoirsError(#[from] VoirsError),

    /// Privacy error
    #[error("Privacy error: {0}")]
    PrivacyError(#[from] crate::privacy::PrivacyError),

    /// Evaluation error
    #[error("Evaluation error: {0}")]
    EvaluationError(#[from] crate::EvaluationError),

    /// Serialization error
    #[error("Serialization error: {0}")]
    SerializationError(#[from] serde_json::Error),
}

/// Node status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum NodeStatus {
    /// Node is online and ready
    Online,
    /// Node is busy processing
    Busy,
    /// Node is offline
    Offline,
    /// Node failed
    Failed,
}

/// Node information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeInfo {
    /// Node identifier
    pub node_id: String,
    /// Node endpoint URL
    pub endpoint: String,
    /// Node status
    pub status: NodeStatus,
    /// Node capabilities
    pub capabilities: NodeCapabilities,
    /// Last heartbeat timestamp
    pub last_heartbeat: u64,
    /// Total evaluations completed
    pub total_evaluations: u64,
    /// Current load (0.0-1.0)
    pub current_load: f64,
}

/// Node capabilities
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeCapabilities {
    /// Supported quality metrics
    pub metrics: Vec<String>,
    /// Maximum concurrent evaluations
    pub max_concurrent: usize,
    /// Supports GPU acceleration
    pub gpu_enabled: bool,
    /// Available memory (MB)
    pub available_memory_mb: usize,
    /// Processing speed factor (relative to baseline)
    pub speed_factor: f64,
}

impl Default for NodeCapabilities {
    fn default() -> Self {
        Self {
            metrics: vec!["pesq".to_string(), "stoi".to_string(), "mcd".to_string()],
            max_concurrent: 4,
            gpu_enabled: false,
            available_memory_mb: 4096,
            speed_factor: 1.0,
        }
    }
}

/// Node configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeConfig {
    /// Node identifier
    pub node_id: String,
    /// Coordinator endpoint
    pub coordinator_endpoint: String,
    /// Enable privacy preservation
    pub enable_privacy: bool,
    /// Privacy configuration
    pub privacy_config: Option<PrivacyConfig>,
    /// Heartbeat interval (seconds)
    pub heartbeat_interval_seconds: u64,
    /// Maximum concurrent evaluations
    pub max_concurrent_evaluations: usize,
}

impl Default for NodeConfig {
    fn default() -> Self {
        Self {
            node_id: "default_node".to_string(),
            coordinator_endpoint: "http://localhost:8080".to_string(),
            enable_privacy: true,
            privacy_config: Some(PrivacyConfig::new()),
            heartbeat_interval_seconds: 30,
            max_concurrent_evaluations: 4,
        }
    }
}

/// Coordinator configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CoordinatorConfig {
    /// Minimum nodes required
    pub min_nodes: usize,
    /// Maximum nodes allowed
    pub max_nodes: usize,
    /// Aggregation strategy
    pub aggregation_strategy: AggregationStrategy,
    /// Enable secure aggregation
    pub enable_secure_aggregation: bool,
    /// Consensus threshold (fraction of nodes that must agree)
    pub consensus_threshold: f64,
    /// Timeout for node responses (seconds)
    pub node_timeout_seconds: u64,
    /// Enable fault tolerance
    pub enable_fault_tolerance: bool,
}

impl Default for CoordinatorConfig {
    fn default() -> Self {
        Self {
            min_nodes: 2,
            max_nodes: 100,
            aggregation_strategy: AggregationStrategy::WeightedAverage,
            enable_secure_aggregation: true,
            consensus_threshold: 0.67, // 2/3 majority
            node_timeout_seconds: 300, // 5 minutes
            enable_fault_tolerance: true,
        }
    }
}

/// Aggregation strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AggregationStrategy {
    /// Simple average
    Average,
    /// Weighted average by node reliability
    WeightedAverage,
    /// Median of all results
    Median,
    /// Trimmed mean (remove outliers)
    TrimmedMean,
    /// Federated learning style aggregation
    FederatedLearning,
}

/// Evaluation task
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvaluationTask {
    /// Task identifier
    pub task_id: String,
    /// Task type
    pub task_type: String,
    /// Task parameters
    pub parameters: HashMap<String, serde_json::Value>,
    /// Priority (higher = more important)
    pub priority: u32,
    /// Created timestamp
    pub created_at: u64,
}

/// Node evaluation result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeEvaluationResult {
    /// Node identifier
    pub node_id: String,
    /// Task identifier
    pub task_id: String,
    /// Quality score
    pub quality_score: f64,
    /// Private result (if privacy enabled)
    pub private_result: Option<PrivateEvaluationResult>,
    /// Evaluation duration (ms)
    pub duration_ms: u64,
    /// Node weight (for weighted aggregation)
    pub weight: f64,
    /// Completed timestamp
    pub completed_at: u64,
}

/// Federated evaluation result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FederatedEvaluationResult {
    /// Task identifier
    pub task_id: String,
    /// Aggregated quality score
    pub aggregated_score: f64,
    /// Confidence interval
    pub confidence_interval: (f64, f64),
    /// Number of participating nodes
    pub participant_count: usize,
    /// Individual node results
    pub node_results: Vec<NodeEvaluationResult>,
    /// Aggregation strategy used
    pub aggregation_strategy: AggregationStrategy,
    /// Total evaluation duration (ms)
    pub total_duration_ms: u64,
    /// Privacy guarantees (if applicable)
    pub privacy_guarantees: Vec<String>,
}

/// Federated evaluation node
pub struct FederatedNode {
    config: NodeConfig,
    evaluator: Arc<RwLock<QualityEvaluator>>,
    privacy_evaluator: Option<Arc<RwLock<PrivacyPreservingEvaluator>>>,
    info: Arc<RwLock<NodeInfo>>,
    semaphore: Arc<Semaphore>,
}

impl FederatedNode {
    /// Create new federated node
    pub async fn new(config: NodeConfig) -> Result<Self, FederatedError> {
        let evaluator = QualityEvaluator::new().await?;

        let privacy_evaluator = if config.enable_privacy {
            let privacy_config = config.privacy_config.clone().unwrap_or_default();
            let pe = PrivacyPreservingEvaluator::new(privacy_config).await?;
            Some(Arc::new(RwLock::new(pe)))
        } else {
            None
        };

        let info = NodeInfo {
            node_id: config.node_id.clone(),
            endpoint: config.coordinator_endpoint.clone(),
            status: NodeStatus::Online,
            capabilities: NodeCapabilities::default(),
            last_heartbeat: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("value should be present")
                .as_secs(),
            total_evaluations: 0,
            current_load: 0.0,
        };

        let semaphore = Arc::new(Semaphore::new(config.max_concurrent_evaluations));

        Ok(Self {
            config,
            evaluator: Arc::new(RwLock::new(evaluator)),
            privacy_evaluator,
            info: Arc::new(RwLock::new(info)),
            semaphore,
        })
    }

    /// Execute evaluation task
    pub async fn execute_task(
        &self,
        task: &EvaluationTask,
        audio: &AudioBuffer,
        reference: Option<&AudioBuffer>,
    ) -> Result<NodeEvaluationResult, FederatedError> {
        let _permit =
            self.semaphore
                .acquire()
                .await
                .map_err(|e| FederatedError::CommunicationError {
                    node: self.config.node_id.clone(),
                    message: format!("Failed to acquire semaphore: {}", e),
                })?;

        let start = SystemTime::now();

        // Update status to busy
        {
            let mut info = self.info.write().await;
            info.status = NodeStatus::Busy;
        }

        let result = if self.config.enable_privacy {
            // Use privacy-preserving evaluation
            if let Some(ref pe) = self.privacy_evaluator {
                let pe_lock = pe.read().await;
                let private_result = pe_lock.evaluate_with_privacy(audio, reference).await?;
                let quality_score = private_result.score;

                let duration_ms = SystemTime::now()
                    .duration_since(start)
                    .unwrap_or(Duration::ZERO)
                    .as_millis() as u64;

                NodeEvaluationResult {
                    node_id: self.config.node_id.clone(),
                    task_id: task.task_id.clone(),
                    quality_score,
                    private_result: Some(private_result),
                    duration_ms,
                    weight: 1.0,
                    completed_at: SystemTime::now()
                        .duration_since(UNIX_EPOCH)
                        .expect("value should be present")
                        .as_secs(),
                }
            } else {
                return Err(FederatedError::ConfigError {
                    message: "Privacy enabled but evaluator not initialized".to_string(),
                });
            }
        } else {
            // Standard evaluation
            let evaluator = self.evaluator.read().await;
            let eval_config = QualityEvaluationConfig::default();
            let quality = evaluator
                .evaluate_quality(audio, reference, Some(&eval_config))
                .await?;

            let duration_ms = SystemTime::now()
                .duration_since(start)
                .unwrap_or(Duration::ZERO)
                .as_millis() as u64;

            NodeEvaluationResult {
                node_id: self.config.node_id.clone(),
                task_id: task.task_id.clone(),
                quality_score: quality.overall_score as f64,
                private_result: None,
                duration_ms,
                weight: 1.0,
                completed_at: SystemTime::now()
                    .duration_since(UNIX_EPOCH)
                    .expect("value should be present")
                    .as_secs(),
            }
        };

        // Update status and statistics
        {
            let mut info = self.info.write().await;
            info.status = NodeStatus::Online;
            info.total_evaluations += 1;
            info.last_heartbeat = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("value should be present")
                .as_secs();
        }

        Ok(result)
    }

    /// Get node information
    pub async fn get_info(&self) -> NodeInfo {
        let info = self.info.read().await;
        info.clone()
    }

    /// Send heartbeat
    pub async fn heartbeat(&self) {
        let mut info = self.info.write().await;
        info.last_heartbeat = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("value should be present")
            .as_secs();
    }
}

/// Federated coordinator
pub struct FederatedCoordinator {
    config: CoordinatorConfig,
    nodes: Arc<RwLock<HashMap<String, NodeInfo>>>,
    tasks: Arc<RwLock<Vec<EvaluationTask>>>,
    results: Arc<RwLock<HashMap<String, Vec<NodeEvaluationResult>>>>,
}

impl FederatedCoordinator {
    /// Create new federated coordinator
    pub async fn new(config: CoordinatorConfig) -> Result<Self, FederatedError> {
        Ok(Self {
            config,
            nodes: Arc::new(RwLock::new(HashMap::new())),
            tasks: Arc::new(RwLock::new(Vec::new())),
            results: Arc::new(RwLock::new(HashMap::new())),
        })
    }

    /// Register a new node
    pub async fn register_node(&self, node_id: &str, endpoint: &str) -> Result<(), FederatedError> {
        let mut nodes = self.nodes.write().await;

        if nodes.len() >= self.config.max_nodes {
            return Err(FederatedError::RegistrationError {
                message: format!("Maximum node limit reached: {}", self.config.max_nodes),
            });
        }

        let info = NodeInfo {
            node_id: node_id.to_string(),
            endpoint: endpoint.to_string(),
            status: NodeStatus::Online,
            capabilities: NodeCapabilities::default(),
            last_heartbeat: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("value should be present")
                .as_secs(),
            total_evaluations: 0,
            current_load: 0.0,
        };

        nodes.insert(node_id.to_string(), info);
        info!("Registered node: {}", node_id);
        Ok(())
    }

    /// Unregister a node
    pub async fn unregister_node(&self, node_id: &str) -> Result<(), FederatedError> {
        let mut nodes = self.nodes.write().await;
        nodes.remove(node_id);
        info!("Unregistered node: {}", node_id);
        Ok(())
    }

    /// Get active node count
    pub async fn active_node_count(&self) -> usize {
        let nodes = self.nodes.read().await;
        nodes
            .values()
            .filter(|n| n.status == NodeStatus::Online)
            .count()
    }

    /// Create evaluation task
    pub async fn create_task(&self, task_type: String) -> Result<String, FederatedError> {
        let task_id = uuid::Uuid::new_v4().to_string();
        let task = EvaluationTask {
            task_id: task_id.clone(),
            task_type,
            parameters: HashMap::new(),
            priority: 1,
            created_at: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("value should be present")
                .as_secs(),
        };

        let mut tasks = self.tasks.write().await;
        tasks.push(task);
        Ok(task_id)
    }

    /// Evaluate using federated nodes
    pub async fn evaluate_federated(&self) -> Result<FederatedEvaluationResult, FederatedError> {
        let nodes = self.nodes.read().await;

        if nodes.len() < self.config.min_nodes {
            return Err(FederatedError::AggregationError {
                message: format!(
                    "Insufficient nodes: {} < {}",
                    nodes.len(),
                    self.config.min_nodes
                ),
            });
        }

        // For now, return a mock result
        // In production, this would coordinate with actual nodes
        let task_id = uuid::Uuid::new_v4().to_string();
        let node_results: Vec<NodeEvaluationResult> = nodes
            .iter()
            .map(|(node_id, _)| NodeEvaluationResult {
                node_id: node_id.clone(),
                task_id: task_id.clone(),
                quality_score: 4.2,
                private_result: None,
                duration_ms: 1000,
                weight: 1.0,
                completed_at: SystemTime::now()
                    .duration_since(UNIX_EPOCH)
                    .expect("value should be present")
                    .as_secs(),
            })
            .collect();

        let aggregated_score = self.aggregate_results(&node_results).await?;
        let confidence_interval = self.calculate_confidence_interval(&node_results);

        Ok(FederatedEvaluationResult {
            task_id,
            aggregated_score,
            confidence_interval,
            participant_count: node_results.len(),
            node_results,
            aggregation_strategy: self.config.aggregation_strategy,
            total_duration_ms: 1000,
            privacy_guarantees: vec!["Differential privacy enabled".to_string()],
        })
    }

    /// Aggregate results from multiple nodes
    async fn aggregate_results(
        &self,
        results: &[NodeEvaluationResult],
    ) -> Result<f64, FederatedError> {
        if results.is_empty() {
            return Err(FederatedError::AggregationError {
                message: "No results to aggregate".to_string(),
            });
        }

        let score = match self.config.aggregation_strategy {
            AggregationStrategy::Average => {
                results.iter().map(|r| r.quality_score).sum::<f64>() / results.len() as f64
            }
            AggregationStrategy::WeightedAverage => {
                let total_weight: f64 = results.iter().map(|r| r.weight).sum();
                results
                    .iter()
                    .map(|r| r.quality_score * r.weight)
                    .sum::<f64>()
                    / total_weight
            }
            AggregationStrategy::Median => {
                let mut scores: Vec<f64> = results.iter().map(|r| r.quality_score).collect();
                scores.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
                let mid = scores.len() / 2;
                if scores.len().is_multiple_of(2) {
                    (scores[mid - 1] + scores[mid]) / 2.0
                } else {
                    scores[mid]
                }
            }
            AggregationStrategy::TrimmedMean => {
                let mut scores: Vec<f64> = results.iter().map(|r| r.quality_score).collect();
                scores.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
                let trim_count = (scores.len() as f64 * 0.1) as usize; // Trim 10% from each end
                if scores.len() > 2 * trim_count {
                    let trimmed: Vec<f64> = scores
                        .iter()
                        .skip(trim_count)
                        .take(scores.len() - 2 * trim_count)
                        .copied()
                        .collect();
                    trimmed.iter().sum::<f64>() / trimmed.len() as f64
                } else {
                    scores.iter().sum::<f64>() / scores.len() as f64
                }
            }
            AggregationStrategy::FederatedLearning => {
                // Simplified FL aggregation (in production, use proper FL algorithms)
                results.iter().map(|r| r.quality_score).sum::<f64>() / results.len() as f64
            }
        };

        Ok(score)
    }

    /// Calculate confidence interval
    fn calculate_confidence_interval(&self, results: &[NodeEvaluationResult]) -> (f64, f64) {
        if results.is_empty() {
            return (0.0, 0.0);
        }

        let scores: Vec<f64> = results.iter().map(|r| r.quality_score).collect();
        let mean = scores.iter().sum::<f64>() / scores.len() as f64;
        let variance = scores.iter().map(|s| (s - mean).powi(2)).sum::<f64>() / scores.len() as f64;
        let std_dev = variance.sqrt();
        let margin = 1.96 * std_dev / (scores.len() as f64).sqrt(); // 95% CI

        ((mean - margin).max(0.0), (mean + margin).min(5.0))
    }

    /// Get coordinator statistics
    pub async fn get_statistics(&self) -> CoordinatorStatistics {
        let nodes = self.nodes.read().await;
        let tasks = self.tasks.read().await;

        CoordinatorStatistics {
            total_nodes: nodes.len(),
            active_nodes: nodes
                .values()
                .filter(|n| n.status == NodeStatus::Online)
                .count(),
            total_tasks: tasks.len(),
            completed_tasks: 0, // Would track this in production
        }
    }
}

/// Coordinator statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CoordinatorStatistics {
    /// Total registered nodes
    pub total_nodes: usize,
    /// Currently active nodes
    pub active_nodes: usize,
    /// Total tasks created
    pub total_tasks: usize,
    /// Completed tasks
    pub completed_tasks: usize,
}

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

    #[test]
    fn test_node_config_default() {
        let config = NodeConfig::default();
        assert_eq!(config.node_id, "default_node");
        assert!(config.enable_privacy);
        assert_eq!(config.max_concurrent_evaluations, 4);
    }

    #[test]
    fn test_coordinator_config_default() {
        let config = CoordinatorConfig::default();
        assert_eq!(config.min_nodes, 2);
        assert_eq!(config.max_nodes, 100);
        assert_eq!(config.consensus_threshold, 0.67);
    }

    #[test]
    fn test_node_capabilities_default() {
        let caps = NodeCapabilities::default();
        assert!(caps.metrics.contains(&"pesq".to_string()));
        assert_eq!(caps.max_concurrent, 4);
        assert_eq!(caps.speed_factor, 1.0);
    }

    #[test]
    fn test_aggregation_strategies() {
        assert_eq!(AggregationStrategy::Average, AggregationStrategy::Average);
        assert_ne!(AggregationStrategy::Average, AggregationStrategy::Median);
    }

    #[tokio::test]
    async fn test_coordinator_creation() {
        let config = CoordinatorConfig::default();
        let coordinator = FederatedCoordinator::new(config).await;
        assert!(coordinator.is_ok());
    }

    #[tokio::test]
    async fn test_node_registration() {
        let config = CoordinatorConfig::default();
        let coordinator = FederatedCoordinator::new(config).await.unwrap();

        coordinator
            .register_node("node1", "http://node1:8080")
            .await
            .unwrap();
        let count = coordinator.active_node_count().await;
        assert_eq!(count, 1);

        coordinator
            .register_node("node2", "http://node2:8080")
            .await
            .unwrap();
        let count = coordinator.active_node_count().await;
        assert_eq!(count, 2);
    }

    #[tokio::test]
    async fn test_node_unregistration() {
        let config = CoordinatorConfig::default();
        let coordinator = FederatedCoordinator::new(config).await.unwrap();

        coordinator
            .register_node("node1", "http://node1:8080")
            .await
            .unwrap();
        coordinator.unregister_node("node1").await.unwrap();
        let count = coordinator.active_node_count().await;
        assert_eq!(count, 0);
    }

    #[tokio::test]
    async fn test_task_creation() {
        let config = CoordinatorConfig::default();
        let coordinator = FederatedCoordinator::new(config).await.unwrap();

        let task_id = coordinator
            .create_task("quality_evaluation".to_string())
            .await
            .unwrap();
        assert!(!task_id.is_empty());
    }

    #[tokio::test]
    async fn test_coordinator_statistics() {
        let config = CoordinatorConfig::default();
        let coordinator = FederatedCoordinator::new(config).await.unwrap();

        coordinator
            .register_node("node1", "http://node1:8080")
            .await
            .unwrap();
        coordinator
            .register_node("node2", "http://node2:8080")
            .await
            .unwrap();

        let stats = coordinator.get_statistics().await;
        assert_eq!(stats.total_nodes, 2);
        assert_eq!(stats.active_nodes, 2);
    }

    #[tokio::test]
    async fn test_federated_node_creation() {
        let config = NodeConfig::default();
        let node = FederatedNode::new(config).await;
        assert!(node.is_ok());
    }

    #[tokio::test]
    async fn test_node_heartbeat() {
        let config = NodeConfig::default();
        let node = FederatedNode::new(config).await.unwrap();

        let info_before = node.get_info().await;
        tokio::time::sleep(Duration::from_secs(1)).await; // Ensure at least 1 second passes

        node.heartbeat().await;
        let info_after = node.get_info().await;

        assert!(info_after.last_heartbeat >= info_before.last_heartbeat);
    }
}