scirs2-metrics 0.3.0

Machine Learning evaluation metrics module for SciRS2 (scirs2-metrics)
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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
//! Interactive visualization dashboard for metrics
//!
//! This module provides a web-based interactive dashboard for visualizing
//! machine learning metrics in real-time, with export capabilities and
//! customizable visualizations.
//!
//! # HTTP Server Support
//!
//! When the `dashboard_server` feature is enabled, this module provides
//! a real HTTP server implementation using tokio. To use it:
//!
//! ```no_run
//! # #[cfg(feature = "dashboard_server")]
//! # {
//! use scirs2_metrics::dashboard::{InteractiveDashboard, DashboardConfig};
//! use scirs2_metrics::dashboard::server::start_http_server;
//!
//! let dashboard = InteractiveDashboard::default();
//! dashboard.add_metric("accuracy", 0.95).expect("Operation failed");
//!
//! // Start the HTTP server
//! let server = start_http_server(dashboard).expect("Operation failed");
//! # }
//! ```

use crate::error::{MetricsError, Result};
use scirs2_core::ndarray::{Array1, Array2};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

// Include dashboard server implementation if tokio is available
#[cfg(feature = "dashboard_server")]
pub mod server;

/// Configuration for the interactive dashboard
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DashboardConfig {
    /// Server listening address
    pub address: SocketAddr,
    /// Auto-refresh interval in seconds
    pub refresh_interval: u64,
    /// Maximum number of data points to keep in memory
    pub max_data_points: usize,
    /// Enable real-time updates
    pub enable_realtime: bool,
    /// Dashboard title
    pub title: String,
    /// Theme configuration
    pub theme: DashboardTheme,
}

impl Default for DashboardConfig {
    fn default() -> Self {
        Self {
            address: "127.0.0.1:8080".parse().expect("Operation failed"),
            refresh_interval: 5,
            max_data_points: 1000,
            enable_realtime: true,
            title: "ML Metrics Dashboard".to_string(),
            theme: DashboardTheme::default(),
        }
    }
}

/// Theme configuration for the dashboard
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DashboardTheme {
    /// Primary color (hex)
    pub primary_color: String,
    /// Background color (hex)
    pub background_color: String,
    /// Text color (hex)
    pub text_color: String,
    /// Chart colors
    pub chart_colors: Vec<String>,
}

impl Default for DashboardTheme {
    fn default() -> Self {
        Self {
            primary_color: "#2563eb".to_string(),
            background_color: "#ffffff".to_string(),
            text_color: "#1f2937".to_string(),
            chart_colors: vec![
                "#2563eb".to_string(),
                "#dc2626".to_string(),
                "#059669".to_string(),
                "#d97706".to_string(),
                "#7c3aed".to_string(),
                "#db2777".to_string(),
            ],
        }
    }
}

/// Metric data point for dashboard visualization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricDataPoint {
    /// Timestamp of the measurement
    pub timestamp: u64,
    /// Metric name
    pub name: String,
    /// Metric value
    pub value: f64,
    /// Optional metadata
    pub metadata: HashMap<String, String>,
}

impl MetricDataPoint {
    /// Create a new metric data point
    pub fn new(name: String, value: f64) -> Self {
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or(Duration::from_secs(0))
            .as_secs();

        Self {
            timestamp,
            name,
            value,
            metadata: HashMap::new(),
        }
    }

    /// Create a new metric data point with metadata
    pub fn with_metadata(name: String, value: f64, metadata: HashMap<String, String>) -> Self {
        let mut point = Self::new(name, value);
        point.metadata = metadata;
        point
    }
}

/// Dashboard data storage and management
#[derive(Debug, Clone)]
pub struct DashboardData {
    /// Stored metric data points
    data_points: Arc<Mutex<Vec<MetricDataPoint>>>,
    /// Configuration
    config: DashboardConfig,
}

impl DashboardData {
    /// Create new dashboard data storage
    pub fn new(config: DashboardConfig) -> Self {
        Self {
            data_points: Arc::new(Mutex::new(Vec::new())),
            config,
        }
    }

    /// Add a metric data point
    pub fn add_metric(&self, point: MetricDataPoint) -> Result<()> {
        let mut data = self
            .data_points
            .lock()
            .map_err(|_| MetricsError::InvalidInput("Failed to acquire data lock".to_string()))?;

        data.push(point);

        // Keep only the most recent data points
        if data.len() > self.config.max_data_points {
            let excess = data.len() - self.config.max_data_points;
            data.drain(0..excess);
        }

        Ok(())
    }

    /// Add multiple metric data points
    pub fn add_metrics(&self, points: Vec<MetricDataPoint>) -> Result<()> {
        for point in points {
            self.add_metric(point)?;
        }
        Ok(())
    }

    /// Get all metric data points
    pub fn get_all_metrics(&self) -> Result<Vec<MetricDataPoint>> {
        let data = self
            .data_points
            .lock()
            .map_err(|_| MetricsError::InvalidInput("Failed to acquire data lock".to_string()))?;

        Ok(data.clone())
    }

    /// Get metric data points by name
    pub fn get_metrics_by_name(&self, name: &str) -> Result<Vec<MetricDataPoint>> {
        let data = self
            .data_points
            .lock()
            .map_err(|_| MetricsError::InvalidInput("Failed to acquire data lock".to_string()))?;

        let filtered: Vec<MetricDataPoint> = data
            .iter()
            .filter(|point| point.name == name)
            .cloned()
            .collect();

        Ok(filtered)
    }

    /// Get metric names
    pub fn get_metric_names(&self) -> Result<Vec<String>> {
        let data = self
            .data_points
            .lock()
            .map_err(|_| MetricsError::InvalidInput("Failed to acquire data lock".to_string()))?;

        let mut names: Vec<String> = data.iter().map(|point| point.name.clone()).collect();
        names.sort();
        names.dedup();

        Ok(names)
    }

    /// Clear all data
    pub fn clear(&self) -> Result<()> {
        let mut data = self
            .data_points
            .lock()
            .map_err(|_| MetricsError::InvalidInput("Failed to acquire data lock".to_string()))?;

        data.clear();
        Ok(())
    }

    /// Get data points within time range
    pub fn get_metrics_in_range(
        &self,
        start_time: u64,
        end_time: u64,
    ) -> Result<Vec<MetricDataPoint>> {
        let data = self
            .data_points
            .lock()
            .map_err(|_| MetricsError::InvalidInput("Failed to acquire data lock".to_string()))?;

        let filtered: Vec<MetricDataPoint> = data
            .iter()
            .filter(|point| point.timestamp >= start_time && point.timestamp <= end_time)
            .cloned()
            .collect();

        Ok(filtered)
    }
}

/// Interactive dashboard server
#[derive(Debug, Clone)]
pub struct InteractiveDashboard {
    /// Dashboard data
    data: DashboardData,
    /// Server configuration
    config: DashboardConfig,
}

impl InteractiveDashboard {
    /// Create new interactive dashboard
    pub fn new(config: DashboardConfig) -> Self {
        let data = DashboardData::new(config.clone());

        Self { data, config }
    }
}

impl Default for InteractiveDashboard {
    fn default() -> Self {
        Self::new(DashboardConfig::default())
    }
}

impl InteractiveDashboard {
    /// Add metric measurement to dashboard
    pub fn add_metric(&self, name: &str, value: f64) -> Result<()> {
        let point = MetricDataPoint::new(name.to_string(), value);
        self.data.add_metric(point)
    }

    /// Add metric measurement with metadata
    pub fn add_metric_with_metadata(
        &self,
        name: &str,
        value: f64,
        metadata: HashMap<String, String>,
    ) -> Result<()> {
        let point = MetricDataPoint::with_metadata(name.to_string(), value, metadata);
        self.data.add_metric(point)
    }

    /// Add batch of metrics from arrays
    pub fn add_metrics_from_arrays(
        &self,
        metric_names: &[String],
        values: &Array1<f64>,
    ) -> Result<()> {
        if metric_names.len() != values.len() {
            return Err(MetricsError::InvalidInput(
                "Metric _names and values must have same length".to_string(),
            ));
        }

        let points: Vec<MetricDataPoint> = metric_names
            .iter()
            .zip(values.iter())
            .map(|(name, &value)| MetricDataPoint::new(name.clone(), value))
            .collect();

        self.data.add_metrics(points)
    }

    /// Start the dashboard server
    pub fn start_server(&self) -> Result<DashboardServer> {
        #[cfg(feature = "dashboard_server")]
        {
            // Use actual HTTP server when feature is enabled
            let _http_server = server::start_http_server(self.clone())?;

            Ok(DashboardServer {
                address: self.config.address,
                is_running: true,
            })
        }

        #[cfg(not(feature = "dashboard_server"))]
        {
            println!(
                "Dashboard server feature not enabled. Starting mock server at http://{}",
                self.config.address
            );
            println!("Dashboard title: {}", self.config.title);
            println!("Refresh interval: {} seconds", self.config.refresh_interval);
            println!("Float-time updates: {}", self.config.enable_realtime);
            println!("To use the real HTTP server, enable the 'dashboard_server' feature");

            // Return mock server when feature is not enabled
            Ok(DashboardServer {
                address: self.config.address,
                is_running: true,
            })
        }
    }

    /// Export data to JSON
    pub fn export_to_json(&self) -> Result<String> {
        let data = self.data.get_all_metrics()?;
        serde_json::to_string_pretty(&data)
            .map_err(|e| MetricsError::InvalidInput(format!("Failed to serialize data: {e}")))
    }

    /// Export data to CSV
    pub fn export_to_csv(&self) -> Result<String> {
        let data = self.data.get_all_metrics()?;

        let mut csv = "timestamp,name,value,metadata\n".to_string();

        for point in data {
            let metadata_str = if point.metadata.is_empty() {
                String::new()
            } else {
                serde_json::to_string(&point.metadata).unwrap_or_default()
            };

            csv.push_str(&format!(
                "{},{},{},{}\n",
                point.timestamp, point.name, point.value, metadata_str
            ));
        }

        Ok(csv)
    }

    /// Get all metric data points
    pub fn get_all_metrics(&self) -> Result<Vec<MetricDataPoint>> {
        self.data.get_all_metrics()
    }

    /// Get metric data points by name
    pub fn get_metrics_by_name(&self, name: &str) -> Result<Vec<MetricDataPoint>> {
        self.data.get_metrics_by_name(name)
    }

    /// Get metric names
    pub fn get_metric_names(&self) -> Result<Vec<String>> {
        self.data.get_metric_names()
    }

    /// Get data points within time range
    pub fn get_metrics_in_range(
        &self,
        start_time: u64,
        end_time: u64,
    ) -> Result<Vec<MetricDataPoint>> {
        self.data.get_metrics_in_range(start_time, end_time)
    }

    /// Clear all data
    pub fn clear_data(&self) -> Result<()> {
        self.data.clear()
    }

    /// Get dashboard statistics
    pub fn get_statistics(&self) -> Result<DashboardStatistics> {
        let data = self.data.get_all_metrics()?;
        let metric_names = self.data.get_metric_names()?;

        let mut metric_counts = HashMap::new();
        let mut latest_values = HashMap::new();

        for point in &data {
            *metric_counts.entry(point.name.clone()).or_insert(0) += 1;
            latest_values.insert(point.name.clone(), point.value);
        }

        let total_points = data.len();
        let unique_metrics = metric_names.len();
        let time_range = if data.is_empty() {
            (0, 0)
        } else {
            let timestamps: Vec<u64> = data.iter().map(|p| p.timestamp).collect();
            (
                *timestamps.iter().min().expect("Operation failed"),
                *timestamps.iter().max().expect("Operation failed"),
            )
        };

        Ok(DashboardStatistics {
            total_data_points: total_points,
            unique_metrics,
            metric_counts,
            latest_values,
            time_range,
        })
    }

    /// Generate HTML dashboard (basic implementation)
    pub fn generate_html(&self) -> Result<String> {
        let stats = self.get_statistics()?;
        let data = self.data.get_all_metrics()?;

        let html = format!(
            r#"
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{}</title>
    <style>
        body {{
            font-family: Arial, sans-serif;
            background-color: {};
            color: {};
            margin: 0;
            padding: 20px;
        }}
        .header {{
            background-color: {};
            color: white;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 20px;
        }}
        .stats {{
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 20px;
        }}
        .stat-card {{
            background: white;
            padding: 15px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }}
        .data-table {{
            background: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }}
        table {{
            width: 100%;
            border-collapse: collapse;
        }}
        th, td {{
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }}
        th {{
            background-color: {};
            color: white;
        }}
    </style>
</head>
<body>
    <div class="header">
        <h1>{}</h1>
        <p>Interactive Machine Learning Metrics Dashboard</p>
    </div>
    
    <div class="stats">
        <div class="stat-card">
            <h3>Total Data Points</h3>
            <p style="font-size: 24px; margin: 0;">{}</p>
        </div>
        <div class="stat-card">
            <h3>Unique Metrics</h3>
            <p style="font-size: 24px; margin: 0;">{}</p>
        </div>
        <div class="stat-card">
            <h3>Time Range</h3>
            <p style="font-size: 14px; margin: 0;">{} - {}</p>
        </div>
    </div>
    
    <div class="data-table">
        <table>
            <thead>
                <tr>
                    <th>Timestamp</th>
                    <th>Metric Name</th>
                    <th>Value</th>
                    <th>Metadata</th>
                </tr>
            </thead>
            <tbody>
"#,
            self.config.title,
            self.config.theme.background_color,
            self.config.theme.text_color,
            self.config.theme.primary_color,
            self.config.theme.primary_color,
            self.config.title,
            stats.total_data_points,
            stats.unique_metrics,
            stats.time_range.0,
            stats.time_range.1
        );

        let mut rows = String::new();
        for point in data.iter().take(100) {
            // Show only first 100 points
            let metadata_display = if point.metadata.is_empty() {
                "-".to_string()
            } else {
                format!("{} keys", point.metadata.len())
            };

            rows.push_str(&format!(
                "<tr><td>{}</td><td>{}</td><td>{:.6}</td><td>{}</td></tr>\n",
                point.timestamp, point.name, point.value, metadata_display
            ));
        }

        let footer = r#"
            </tbody>
        </table>
    </div>
    
    <script>
        // Auto-refresh functionality (placeholder)
        setInterval(() => {
            console.log('Refreshing dashboard data...');
        }, 5000);
    </script>
</body>
</html>
"#;

        Ok(format!("{html}{rows}{footer}"))
    }
}

/// Dashboard server handle (placeholder for actual server)
#[derive(Debug)]
pub struct DashboardServer {
    /// Server address
    pub address: SocketAddr,
    /// Server running status
    pub is_running: bool,
}

impl DashboardServer {
    /// Stop the server
    pub fn stop(&mut self) {
        self.is_running = false;
        println!("Dashboard server stopped");
    }

    /// Check if server is running
    pub fn is_running(&self) -> bool {
        self.is_running
    }
}

/// Dashboard statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DashboardStatistics {
    /// Total number of data points
    pub total_data_points: usize,
    /// Number of unique metrics
    pub unique_metrics: usize,
    /// Count of data points per metric
    pub metric_counts: HashMap<String, usize>,
    /// Latest value for each metric
    pub latest_values: HashMap<String, f64>,
    /// Time range (start, end) timestamps
    pub time_range: (u64, u64),
}

/// Dashboard widget for embedding metrics
#[derive(Debug, Clone)]
pub struct DashboardWidget {
    /// Widget identifier
    pub id: String,
    /// Widget title
    pub title: String,
    /// Metric names to display
    pub metrics: Vec<String>,
    /// Widget type
    pub widget_type: WidgetType,
    /// Configuration options
    pub config: HashMap<String, String>,
}

/// Types of dashboard widgets
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum WidgetType {
    /// Line chart for time series data
    LineChart,
    /// Bar chart for categorical data
    BarChart,
    /// Gauge for single value metrics
    Gauge,
    /// Table for tabular data
    Table,
    /// Heatmap for correlation matrices
    Heatmap,
    /// Confusion matrix visualization
    ConfusionMatrix,
    /// ROC curve
    RocCurve,
    /// Custom widget type
    Custom(String),
}

impl DashboardWidget {
    /// Create new line chart widget
    pub fn line_chart(id: String, title: String, metrics: Vec<String>) -> Self {
        Self {
            id,
            title,
            metrics,
            widget_type: WidgetType::LineChart,
            config: HashMap::new(),
        }
    }

    /// Create new gauge widget
    pub fn gauge(id: String, title: String, metric: String) -> Self {
        Self {
            id,
            title,
            metrics: vec![metric],
            widget_type: WidgetType::Gauge,
            config: HashMap::new(),
        }
    }

    /// Create new table widget
    pub fn table(id: String, title: String, metrics: Vec<String>) -> Self {
        Self {
            id,
            title,
            metrics,
            widget_type: WidgetType::Table,
            config: HashMap::new(),
        }
    }

    /// Add configuration option
    pub fn with_config(mut self, key: String, value: String) -> Self {
        self.config.insert(key, value);
        self
    }
}

/// Utility functions for dashboard creation
pub mod utils {
    use super::*;

    /// Create a dashboard from classification metrics
    pub fn create_classification_dashboard(
        accuracy: f64,
        precision: f64,
        recall: f64,
        f1_score: f64,
    ) -> Result<InteractiveDashboard> {
        let dashboard = InteractiveDashboard::default();

        dashboard.add_metric("accuracy", accuracy)?;
        dashboard.add_metric("precision", precision)?;
        dashboard.add_metric("recall", recall)?;
        dashboard.add_metric("f1_score", f1_score)?;

        Ok(dashboard)
    }

    /// Create a dashboard from regression metrics
    pub fn create_regression_dashboard(
        mse: f64,
        rmse: f64,
        mae: f64,
        r2: f64,
    ) -> Result<InteractiveDashboard> {
        let dashboard = InteractiveDashboard::default();

        dashboard.add_metric("mse", mse)?;
        dashboard.add_metric("rmse", rmse)?;
        dashboard.add_metric("mae", mae)?;
        dashboard.add_metric("r2", r2)?;

        Ok(dashboard)
    }

    /// Create a dashboard from clustering metrics
    pub fn create_clustering_dashboard(
        silhouette_score: f64,
        davies_bouldin: f64,
        calinski_harabasz: f64,
    ) -> Result<InteractiveDashboard> {
        let dashboard = InteractiveDashboard::default();

        dashboard.add_metric("silhouette_score", silhouette_score)?;
        dashboard.add_metric("davies_bouldin", davies_bouldin)?;
        dashboard.add_metric("calinski_harabasz", calinski_harabasz)?;

        Ok(dashboard)
    }

    /// Export dashboard data to file
    pub fn export_dashboard_to_file(
        dashboard: &InteractiveDashboard,
        file_path: &str,
        format: ExportFormat,
    ) -> Result<()> {
        let content = match format {
            ExportFormat::Json => dashboard.export_to_json()?,
            ExportFormat::Csv => dashboard.export_to_csv()?,
            ExportFormat::Html => dashboard.generate_html()?,
        };

        std::fs::write(file_path, content)
            .map_err(|e| MetricsError::InvalidInput(format!("Failed to write file: {e}")))?;

        Ok(())
    }

    /// Export formats
    #[derive(Debug, Clone)]
    pub enum ExportFormat {
        Json,
        Csv,
        Html,
    }
}

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

    #[test]
    fn test_dashboard_creation() {
        let config = DashboardConfig::default();
        let dashboard = InteractiveDashboard::new(config);

        assert!(dashboard.add_metric("accuracy", 0.95).is_ok());
        assert!(dashboard.add_metric("precision", 0.92).is_ok());
        assert!(dashboard.add_metric("recall", 0.88).is_ok());
    }

    #[test]
    fn test_metric_data_point() {
        let point = MetricDataPoint::new("accuracy".to_string(), 0.95);
        assert_eq!(point.name, "accuracy");
        assert_eq!(point.value, 0.95);
        assert!(point.timestamp > 0);
    }

    #[test]
    fn test_dashboard_data() {
        let config = DashboardConfig::default();
        let data = DashboardData::new(config);

        let point1 = MetricDataPoint::new("accuracy".to_string(), 0.95);
        let point2 = MetricDataPoint::new("precision".to_string(), 0.92);

        assert!(data.add_metric(point1).is_ok());
        assert!(data.add_metric(point2).is_ok());

        let all_metrics = data.get_all_metrics().expect("Operation failed");
        assert_eq!(all_metrics.len(), 2);

        let accuracy_metrics = data
            .get_metrics_by_name("accuracy")
            .expect("Operation failed");
        assert_eq!(accuracy_metrics.len(), 1);
        assert_eq!(accuracy_metrics[0].value, 0.95);
    }

    #[test]
    fn test_dashboard_statistics() {
        let dashboard = InteractiveDashboard::default();

        assert!(dashboard.add_metric("accuracy", 0.95).is_ok());
        assert!(dashboard.add_metric("precision", 0.92).is_ok());
        assert!(dashboard.add_metric("accuracy", 0.97).is_ok());

        let stats = dashboard.get_statistics().expect("Operation failed");
        assert_eq!(stats.total_data_points, 3);
        assert_eq!(stats.unique_metrics, 2);
        assert_eq!(stats.metric_counts["accuracy"], 2);
        assert_eq!(stats.metric_counts["precision"], 1);
    }

    #[test]
    fn test_export_functions() {
        let dashboard = InteractiveDashboard::default();

        assert!(dashboard.add_metric("accuracy", 0.95).is_ok());
        assert!(dashboard.add_metric("precision", 0.92).is_ok());

        let json_export = dashboard.export_to_json();
        assert!(json_export.is_ok());
        assert!(json_export.expect("Operation failed").contains("accuracy"));

        let csv_export = dashboard.export_to_csv();
        assert!(csv_export.is_ok());
        assert!(csv_export
            .expect("Operation failed")
            .contains("timestamp,name,value"));

        let html_export = dashboard.generate_html();
        assert!(html_export.is_ok());
        assert!(html_export
            .expect("Operation failed")
            .contains("<!DOCTYPE html>"));
    }

    #[test]
    fn test_dashboard_widgets() {
        let widget = DashboardWidget::line_chart(
            "accuracy_chart".to_string(),
            "Model Accuracy".to_string(),
            vec!["accuracy".to_string()],
        );

        assert_eq!(widget.id, "accuracy_chart");
        assert_eq!(widget.title, "Model Accuracy");
        assert!(matches!(widget.widget_type, WidgetType::LineChart));
    }

    #[test]
    fn test_utility_functions() {
        let dashboard = utils::create_classification_dashboard(0.95, 0.92, 0.88, 0.90)
            .expect("Operation failed");
        let stats = dashboard.get_statistics().expect("Operation failed");

        assert_eq!(stats.total_data_points, 4);
        assert_eq!(stats.unique_metrics, 4);
        assert!(stats.latest_values.contains_key("accuracy"));
    }
}