Skip to main content

torsh_profiler/
advanced_visualization.rs

1//! Advanced Visualization Export
2//!
3//! This module provides export capabilities for advanced visualization libraries:
4//! - Plotly.js for interactive charts
5//! - D3.js for custom visualizations
6//! - Vega/Vega-Lite for declarative graphics
7//! - Chart.js for simple charts
8//! - Custom HTML dashboards
9
10use crate::integrated_profiler::{IntegratedReport, PerformanceSnapshot};
11use crate::ProfileEvent;
12use serde::{Deserialize, Serialize};
13use std::collections::HashMap;
14use torsh_core::{Result as TorshResult, TorshError};
15
16/// Visualization library type
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
18pub enum VisualizationLibrary {
19    Plotly,
20    D3,
21    VegaLite,
22    ChartJs,
23    Custom,
24}
25
26impl std::fmt::Display for VisualizationLibrary {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        match self {
29            Self::Plotly => write!(f, "Plotly.js"),
30            Self::D3 => write!(f, "D3.js"),
31            Self::VegaLite => write!(f, "Vega-Lite"),
32            Self::ChartJs => write!(f, "Chart.js"),
33            Self::Custom => write!(f, "Custom"),
34        }
35    }
36}
37
38/// Plotly chart specification
39#[derive(Debug, Clone, Serialize, Deserialize)]
40pub struct PlotlyChart {
41    /// Data traces
42    pub data: Vec<PlotlyTrace>,
43    /// Layout configuration
44    pub layout: PlotlyLayout,
45    /// Configuration options
46    pub config: PlotlyConfig,
47}
48
49/// Plotly trace
50#[derive(Debug, Clone, Serialize, Deserialize)]
51pub struct PlotlyTrace {
52    /// X axis data
53    pub x: Vec<serde_json::Value>,
54    /// Y axis data
55    pub y: Vec<serde_json::Value>,
56    /// Trace type (scatter, bar, line, etc.)
57    #[serde(rename = "type")]
58    pub trace_type: String,
59    /// Trace mode (lines, markers, lines+markers)
60    pub mode: Option<String>,
61    /// Trace name
62    pub name: String,
63    /// Marker configuration
64    pub marker: Option<PlotlyMarker>,
65    /// Line configuration
66    pub line: Option<PlotlyLine>,
67}
68
69/// Plotly marker configuration
70#[derive(Debug, Clone, Serialize, Deserialize)]
71pub struct PlotlyMarker {
72    /// Marker color
73    pub color: Option<String>,
74    /// Marker size
75    pub size: Option<f64>,
76}
77
78/// Plotly line configuration
79#[derive(Debug, Clone, Serialize, Deserialize)]
80pub struct PlotlyLine {
81    /// Line color
82    pub color: Option<String>,
83    /// Line width
84    pub width: Option<f64>,
85}
86
87/// Plotly layout
88#[derive(Debug, Clone, Serialize, Deserialize)]
89pub struct PlotlyLayout {
90    /// Chart title
91    pub title: String,
92    /// X axis configuration
93    pub xaxis: PlotlyAxis,
94    /// Y axis configuration
95    pub yaxis: PlotlyAxis,
96    /// Show legend
97    pub showlegend: bool,
98    /// Height
99    pub height: Option<usize>,
100    /// Width
101    pub width: Option<usize>,
102}
103
104/// Plotly axis configuration
105#[derive(Debug, Clone, Serialize, Deserialize)]
106pub struct PlotlyAxis {
107    /// Axis title
108    pub title: String,
109}
110
111/// Plotly configuration
112#[derive(Debug, Clone, Serialize, Deserialize)]
113pub struct PlotlyConfig {
114    /// Responsive sizing
115    pub responsive: bool,
116    /// Display mode bar
117    #[serde(rename = "displayModeBar")]
118    pub display_mode_bar: bool,
119}
120
121impl Default for PlotlyConfig {
122    fn default() -> Self {
123        Self {
124            responsive: true,
125            display_mode_bar: true,
126        }
127    }
128}
129
130/// D3.js visualization specification
131#[derive(Debug, Clone, Serialize, Deserialize)]
132pub struct D3Visualization {
133    /// Data array
134    pub data: Vec<HashMap<String, serde_json::Value>>,
135    /// Visualization type
136    pub vis_type: String,
137    /// Configuration
138    pub config: D3Config,
139}
140
141/// D3 configuration
142#[derive(Debug, Clone, Serialize, Deserialize)]
143pub struct D3Config {
144    /// Width
145    pub width: usize,
146    /// Height
147    pub height: usize,
148    /// Margins
149    pub margin: D3Margin,
150    /// Colors
151    pub colors: Vec<String>,
152}
153
154/// D3 margins
155#[derive(Debug, Clone, Serialize, Deserialize)]
156pub struct D3Margin {
157    pub top: usize,
158    pub right: usize,
159    pub bottom: usize,
160    pub left: usize,
161}
162
163impl Default for D3Config {
164    fn default() -> Self {
165        Self {
166            width: 960,
167            height: 500,
168            margin: D3Margin {
169                top: 20,
170                right: 20,
171                bottom: 30,
172                left: 50,
173            },
174            colors: vec![
175                "#1f77b4".to_string(),
176                "#ff7f0e".to_string(),
177                "#2ca02c".to_string(),
178                "#d62728".to_string(),
179                "#9467bd".to_string(),
180            ],
181        }
182    }
183}
184
185/// Vega-Lite specification
186#[derive(Debug, Clone, Serialize, Deserialize)]
187pub struct VegaLiteSpec {
188    #[serde(rename = "$schema")]
189    pub schema: String,
190    pub description: String,
191    pub data: VegaData,
192    pub mark: String,
193    pub encoding: VegaEncoding,
194    pub width: Option<usize>,
195    pub height: Option<usize>,
196}
197
198/// Vega data
199#[derive(Debug, Clone, Serialize, Deserialize)]
200pub struct VegaData {
201    pub values: Vec<HashMap<String, serde_json::Value>>,
202}
203
204/// Vega encoding
205#[derive(Debug, Clone, Serialize, Deserialize)]
206pub struct VegaEncoding {
207    pub x: Option<VegaChannel>,
208    pub y: Option<VegaChannel>,
209    pub color: Option<VegaChannel>,
210}
211
212/// Vega channel
213#[derive(Debug, Clone, Serialize, Deserialize)]
214pub struct VegaChannel {
215    pub field: String,
216    #[serde(rename = "type")]
217    pub channel_type: String,
218    pub title: Option<String>,
219}
220
221/// Advanced visualization exporter
222pub struct AdvancedVisualizationExporter;
223
224impl AdvancedVisualizationExporter {
225    /// Generate Plotly performance timeline
226    pub fn plotly_performance_timeline(
227        history: &[PerformanceSnapshot],
228    ) -> TorshResult<PlotlyChart> {
229        let timestamps: Vec<_> = history
230            .iter()
231            .map(|s| serde_json::Value::String(s.timestamp.to_rfc3339()))
232            .collect();
233
234        let durations: Vec<_> = history
235            .iter()
236            .map(|s| {
237                serde_json::Value::Number(
238                    serde_json::Number::from_f64(s.avg_duration_us)
239                        .expect("avg_duration_us should be a valid f64 for JSON serialization"),
240                )
241            })
242            .collect();
243
244        let memory_values: Vec<_> = history
245            .iter()
246            .map(|s| {
247                serde_json::Value::Number(
248                    serde_json::Number::from_f64(s.avg_memory_bytes / 1_048_576.0)
249                        .expect("avg_memory_bytes should be a valid f64 for JSON serialization"),
250                )
251            })
252            .collect();
253
254        Ok(PlotlyChart {
255            data: vec![
256                PlotlyTrace {
257                    x: timestamps.clone(),
258                    y: durations,
259                    trace_type: "scatter".to_string(),
260                    mode: Some("lines+markers".to_string()),
261                    name: "Duration (μs)".to_string(),
262                    marker: Some(PlotlyMarker {
263                        color: Some("#1f77b4".to_string()),
264                        size: Some(6.0),
265                    }),
266                    line: Some(PlotlyLine {
267                        color: Some("#1f77b4".to_string()),
268                        width: Some(2.0),
269                    }),
270                },
271                PlotlyTrace {
272                    x: timestamps,
273                    y: memory_values,
274                    trace_type: "scatter".to_string(),
275                    mode: Some("lines+markers".to_string()),
276                    name: "Memory (MB)".to_string(),
277                    marker: Some(PlotlyMarker {
278                        color: Some("#ff7f0e".to_string()),
279                        size: Some(6.0),
280                    }),
281                    line: Some(PlotlyLine {
282                        color: Some("#ff7f0e".to_string()),
283                        width: Some(2.0),
284                    }),
285                },
286            ],
287            layout: PlotlyLayout {
288                title: "Performance Timeline".to_string(),
289                xaxis: PlotlyAxis {
290                    title: "Time".to_string(),
291                },
292                yaxis: PlotlyAxis {
293                    title: "Value".to_string(),
294                },
295                showlegend: true,
296                height: Some(500),
297                width: Some(900),
298            },
299            config: PlotlyConfig::default(),
300        })
301    }
302
303    /// Generate D3 force-directed graph of operation dependencies
304    pub fn d3_operation_graph(events: &[ProfileEvent]) -> TorshResult<D3Visualization> {
305        let mut nodes = HashMap::new();
306        let mut node_id = 0;
307
308        // Create nodes from unique operations
309        for event in events {
310            if !nodes.contains_key(&event.name) {
311                nodes.insert(event.name.clone(), node_id);
312                node_id += 1;
313            }
314        }
315
316        // Create data array
317        let mut data = Vec::new();
318        for (name, id) in &nodes {
319            let mut node = HashMap::new();
320            node.insert("id".to_string(), serde_json::Value::Number((*id).into()));
321            node.insert("name".to_string(), serde_json::Value::String(name.clone()));
322            data.push(node);
323        }
324
325        Ok(D3Visualization {
326            data,
327            vis_type: "force-directed".to_string(),
328            config: D3Config::default(),
329        })
330    }
331
332    /// Generate Vega-Lite anomaly scatter plot
333    pub fn vegalite_anomaly_plot(report: &IntegratedReport) -> TorshResult<VegaLiteSpec> {
334        let mut values = Vec::new();
335
336        // Create data points (simplified - would use actual anomaly data)
337        for i in 0..10 {
338            let mut point = HashMap::new();
339            point.insert(
340                "duration".to_string(),
341                serde_json::Value::Number((100 + i * 10).into()),
342            );
343            point.insert(
344                "memory".to_string(),
345                serde_json::Value::Number((1000 + i * 100).into()),
346            );
347            point.insert("anomaly".to_string(), serde_json::Value::Bool(i > 7));
348            values.push(point);
349        }
350
351        Ok(VegaLiteSpec {
352            schema: "https://vega.github.io/schema/vega-lite/v5.json".to_string(),
353            description: "Anomaly Detection Scatter Plot".to_string(),
354            data: VegaData { values },
355            mark: "point".to_string(),
356            encoding: VegaEncoding {
357                x: Some(VegaChannel {
358                    field: "duration".to_string(),
359                    channel_type: "quantitative".to_string(),
360                    title: Some("Duration (μs)".to_string()),
361                }),
362                y: Some(VegaChannel {
363                    field: "memory".to_string(),
364                    channel_type: "quantitative".to_string(),
365                    title: Some("Memory (bytes)".to_string()),
366                }),
367                color: Some(VegaChannel {
368                    field: "anomaly".to_string(),
369                    channel_type: "nominal".to_string(),
370                    title: Some("Anomaly".to_string()),
371                }),
372            },
373            width: Some(600),
374            height: Some(400),
375        })
376    }
377
378    /// Generate interactive HTML dashboard
379    pub fn generate_html_dashboard(report: &IntegratedReport) -> TorshResult<String> {
380        let html = format!(
381            r#"<!DOCTYPE html>
382<html>
383<head>
384    <title>ToRSh Profiler Dashboard</title>
385    <script src="https://cdn.plot.ly/plotly-2.26.0.min.js"></script>
386    <script src="https://cdn.jsdelivr.net/npm/vega@5"></script>
387    <script src="https://cdn.jsdelivr.net/npm/vega-lite@5"></script>
388    <script src="https://cdn.jsdelivr.net/npm/vega-embed@6"></script>
389    <style>
390        body {{
391            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
392            margin: 0;
393            padding: 20px;
394            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
395        }}
396        .container {{
397            max-width: 1200px;
398            margin: 0 auto;
399            background: white;
400            border-radius: 10px;
401            padding: 30px;
402            box-shadow: 0 10px 30px rgba(0,0,0,0.3);
403        }}
404        h1 {{
405            color: #333;
406            border-bottom: 3px solid #667eea;
407            padding-bottom: 10px;
408        }}
409        .stats-grid {{
410            display: grid;
411            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
412            gap: 20px;
413            margin: 20px 0;
414        }}
415        .stat-card {{
416            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
417            color: white;
418            padding: 20px;
419            border-radius: 8px;
420            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
421        }}
422        .stat-value {{
423            font-size: 2em;
424            font-weight: bold;
425            margin: 10px 0;
426        }}
427        .stat-label {{
428            font-size: 0.9em;
429            opacity: 0.9;
430        }}
431        .chart {{
432            margin: 30px 0;
433            padding: 20px;
434            background: #f8f9fa;
435            border-radius: 8px;
436        }}
437        .recommendation {{
438            background: #fff3cd;
439            border-left: 4px solid #ffc107;
440            padding: 15px;
441            margin: 10px 0;
442            border-radius: 4px;
443        }}
444    </style>
445</head>
446<body>
447    <div class="container">
448        <h1>🚀 ToRSh Profiler Dashboard</h1>
449
450        <div class="stats-grid">
451            <div class="stat-card">
452                <div class="stat-label">Total Events</div>
453                <div class="stat-value">{}</div>
454            </div>
455            <div class="stat-card">
456                <div class="stat-label">Anomalies Detected</div>
457                <div class="stat-value">{}</div>
458            </div>
459            <div class="stat-card">
460                <div class="stat-label">Avg Prediction Error</div>
461                <div class="stat-value">{:.2}%</div>
462            </div>
463            <div class="stat-card">
464                <div class="stat-label">Platform</div>
465                <div class="stat-value">{}</div>
466            </div>
467        </div>
468
469        <h2>📊 Performance Metrics</h2>
470        <div class="chart" id="performanceChart"></div>
471
472        <h2>💡 Top Recommendations</h2>
473        {}
474
475        <h2>📈 Performance Trends</h2>
476        <div class="chart">
477            <p><strong>Average Duration:</strong> {:.2}μs</p>
478            <p><strong>Duration Trend:</strong> {:.2}%</p>
479            <p><strong>Stability Score:</strong> {:.2}/1.0</p>
480        </div>
481    </div>
482
483    <script>
484        // Placeholder for Plotly chart
485        var trace = {{
486            x: [1, 2, 3, 4, 5],
487            y: [100, 105, 103, 110, 108],
488            type: 'scatter',
489            mode: 'lines+markers',
490            name: 'Duration'
491        }};
492        var layout = {{
493            title: 'Performance Over Time',
494            xaxis: {{ title: 'Sample' }},
495            yaxis: {{ title: 'Duration (μs)' }}
496        }};
497        Plotly.newPlot('performanceChart', [trace], layout);
498    </script>
499</body>
500</html>"#,
501            report.stats.total_events,
502            report.stats.total_anomalies,
503            report.stats.avg_prediction_error_percent,
504            report.stats.platform_arch,
505            report
506                .top_recommendations
507                .iter()
508                .take(5)
509                .map(|r| format!(
510                    r#"<div class="recommendation">
511                <strong>{}</strong>: {} (Expected improvement: {:.1}%)
512            </div>"#,
513                    r.rec_type, r.description, r.expected_improvement_percent
514                ))
515                .collect::<Vec<_>>()
516                .join("\n        "),
517            report.performance_trends.avg_duration_us,
518            report.performance_trends.duration_trend_percent,
519            report.performance_trends.stability_score,
520        );
521
522        Ok(html)
523    }
524
525    /// Export to JSON for any library
526    pub fn export_json<T: Serialize>(data: &T) -> TorshResult<String> {
527        serde_json::to_string_pretty(data)
528            .map_err(|e| TorshError::operation_error(&format!("JSON export failed: {}", e)))
529    }
530}
531
532#[cfg(test)]
533mod tests {
534    use super::*;
535    use crate::integrated_profiler::IntegratedProfiler;
536
537    #[test]
538    fn test_plotly_chart_generation() {
539        let snapshots = vec![PerformanceSnapshot {
540            timestamp: chrono::Utc::now(),
541            avg_duration_us: 100.0,
542            avg_memory_bytes: 1_048_576.0,
543            flops_rate: 1_000_000.0,
544            throughput_ops_per_sec: 100.0,
545            anomaly_count: 0,
546            active_cluster: Some(0),
547        }];
548
549        let chart = AdvancedVisualizationExporter::plotly_performance_timeline(&snapshots);
550        assert!(chart.is_ok());
551
552        let chart = chart.unwrap();
553        assert_eq!(chart.data.len(), 2); // Duration and memory traces
554        assert_eq!(chart.layout.title, "Performance Timeline");
555    }
556
557    #[test]
558    fn test_d3_graph_generation() {
559        let events = vec![
560            ProfileEvent {
561                name: "op1".to_string(),
562                category: "test".to_string(),
563                thread_id: 1,
564                start_us: 0,
565                duration_us: 100,
566                operation_count: None,
567                flops: None,
568                bytes_transferred: None,
569                stack_trace: None,
570            },
571            ProfileEvent {
572                name: "op2".to_string(),
573                category: "test".to_string(),
574                thread_id: 1,
575                start_us: 100,
576                duration_us: 150,
577                operation_count: None,
578                flops: None,
579                bytes_transferred: None,
580                stack_trace: None,
581            },
582        ];
583
584        let graph = AdvancedVisualizationExporter::d3_operation_graph(&events);
585        assert!(graph.is_ok());
586
587        let graph = graph.unwrap();
588        assert_eq!(graph.data.len(), 2);
589        assert_eq!(graph.vis_type, "force-directed");
590    }
591
592    #[test]
593    fn test_vegalite_spec_generation() {
594        let mut profiler = IntegratedProfiler::new().unwrap();
595        profiler.start().unwrap();
596        let report = profiler.export_report().unwrap();
597
598        let spec = AdvancedVisualizationExporter::vegalite_anomaly_plot(&report);
599        assert!(spec.is_ok());
600
601        let spec = spec.unwrap();
602        assert!(spec.schema.contains("vega-lite"));
603        assert_eq!(spec.mark, "point");
604    }
605
606    #[test]
607    fn test_html_dashboard_generation() {
608        let mut profiler = IntegratedProfiler::new().unwrap();
609        profiler.start().unwrap();
610        let report = profiler.export_report().unwrap();
611
612        let html = AdvancedVisualizationExporter::generate_html_dashboard(&report);
613        assert!(html.is_ok());
614
615        let html = html.unwrap();
616        assert!(html.contains("<!DOCTYPE html>"));
617        assert!(html.contains("ToRSh Profiler Dashboard"));
618        assert!(html.contains("Plotly"));
619    }
620
621    #[test]
622    fn test_json_export() {
623        let chart = PlotlyChart {
624            data: vec![],
625            layout: PlotlyLayout {
626                title: "Test".to_string(),
627                xaxis: PlotlyAxis {
628                    title: "X".to_string(),
629                },
630                yaxis: PlotlyAxis {
631                    title: "Y".to_string(),
632                },
633                showlegend: true,
634                height: None,
635                width: None,
636            },
637            config: PlotlyConfig::default(),
638        };
639
640        let json = AdvancedVisualizationExporter::export_json(&chart);
641        assert!(json.is_ok());
642
643        let json = json.unwrap();
644        assert!(json.contains("\"title\""));
645        assert!(json.contains("\"Test\""));
646    }
647}