torsh-profiler 0.1.2

Performance profiling and monitoring for ToRSh
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
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
//! Dashboard HTML generation and web interface functionality
//!
//! This module provides comprehensive HTML generation capabilities for the ToRSh
//! performance dashboard, including responsive layouts, interactive features,
//! and customizable themes.

use crate::{MemoryProfiler, Profiler, TorshResult};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use torsh_core::TorshError;

use super::types::{
    DashboardAlert, DashboardAlertSeverity, DashboardConfig, DashboardData, DashboardTheme,
    MemoryMetrics, OperationSummary, PerformanceMetrics, SystemMetrics,
};
use super::{create_dashboard, create_dashboard_with_config, Dashboard};

// =============================================================================
// Core HTML Generation
// =============================================================================

/// Generate complete dashboard HTML interface
pub fn generate_dashboard_html(dashboard: &Dashboard) -> TorshResult<String> {
    dashboard.generate_dashboard_html()
}

/// Create default dashboard data for empty state
fn create_default_data() -> DashboardData {
    DashboardData {
        timestamp: 0,
        performance_metrics: PerformanceMetrics {
            total_operations: 0,
            average_duration_ms: 0.0,
            operations_per_second: 0.0,
            total_flops: 0,
            gflops_per_second: 0.0,
            cpu_utilization: 0.0,
            thread_count: 0,
        },
        memory_metrics: MemoryMetrics {
            current_usage_mb: 0.0,
            peak_usage_mb: 0.0,
            total_allocations: 0,
            total_deallocations: 0,
            active_allocations: 0,
            fragmentation_ratio: 0.0,
            allocation_rate: 0.0,
        },
        system_metrics: SystemMetrics {
            uptime_seconds: 0,
            load_average: 0.0,
            available_memory_mb: 0.0,
            disk_usage_percent: 0.0,
            network_io_mbps: 0.0,
        },
        alerts: Vec::new(),
        top_operations: Vec::new(),
    }
}

/// Build complete HTML document structure
pub fn build_html_document(
    data: &DashboardData,
    alerts: &[DashboardAlert],
    config: &DashboardConfig,
    theme: &DashboardTheme,
) -> TorshResult<String> {
    let head_section = generate_head_section(theme, config);
    let header_section = generate_header_section();
    let dashboard_section = generate_dashboard_section(data, alerts);
    let scripts_section = generate_scripts_section(config);
    let footer_section = generate_footer_section(data);

    let html = format!(
        r#"<!DOCTYPE html>
<html lang="en">
{head_section}
<body>
    {header_section}
    {dashboard_section}
    {footer_section}
    {scripts_section}
</body>
</html>"#
    );

    Ok(html)
}

// =============================================================================
// HTML Section Generators
// =============================================================================

/// Generate HTML head section with styles and metadata
fn generate_head_section(theme: &DashboardTheme, config: &DashboardConfig) -> String {
    let base_styles = generate_base_css(theme);
    let custom_css = config.custom_css.as_deref().unwrap_or("");

    format!(
        r#"<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="ToRSh Performance Dashboard - Real-time monitoring">
    <title>ToRSh Performance Dashboard</title>
    <style>
        {base_styles}
        {custom_css}
    </style>
    <link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>🚀</text></svg>">
</head>"#
    )
}

/// Generate dashboard header section
fn generate_header_section() -> String {
    r#"<div class="header">
        <h1>🚀 ToRSh Performance Dashboard</h1>
        <p>Real-time performance monitoring and analytics</p>
        <div class="header-controls">
            <button class="refresh-button" onclick="refreshDashboard()">🔄 Refresh</button>
            <button class="settings-button" onclick="toggleSettings()">⚙️ Settings</button>
            <button class="fullscreen-button" onclick="toggleFullscreen()">⛶ Fullscreen</button>
        </div>
    </div>"#
        .to_string()
}

/// Generate main dashboard content section
fn generate_dashboard_section(data: &DashboardData, alerts: &[DashboardAlert]) -> String {
    let performance_card = generate_performance_card(&data.performance_metrics);
    let memory_card = generate_memory_card(&data.memory_metrics);
    let system_card = generate_system_card(&data.system_metrics);
    let alerts_card = generate_alerts_card(alerts);
    let operations_card = generate_operations_card(&data.top_operations);
    let charts_card = generate_charts_card(data);

    format!(
        r#"<div class="dashboard">
        {performance_card}
        {memory_card}
        {system_card}
        {alerts_card}
        {operations_card}
        {charts_card}
    </div>"#
    )
}

/// Generate JavaScript section for interactivity
fn generate_scripts_section(config: &DashboardConfig) -> String {
    let websocket_config = if config.websocket_config.enabled {
        format!(
            "const WEBSOCKET_URL = 'ws://localhost:{}/ws';",
            config.websocket_config.port
        )
    } else {
        "const WEBSOCKET_URL = null;".to_string()
    };

    format!(
        r#"<script>
        {websocket_config}
        const REFRESH_INTERVAL = {};

        {}
    </script>"#,
        config.refresh_interval * 1000,
        include_str!("../../assets/dashboard.js") // Would contain dashboard JavaScript
    )
}

/// Generate footer section with timestamp
fn generate_footer_section(data: &DashboardData) -> String {
    let timestamp = chrono::DateTime::from_timestamp(data.timestamp as i64, 0)
        .map(|dt| dt.format("%Y-%m-%d %H:%M:%S UTC").to_string())
        .unwrap_or_else(|| "Unknown".to_string());

    format!(
        r#"<div class="footer">
        <div class="status-indicator">
            <span class="status-dot active"></span>
            <span>Live</span>
        </div>
        <div class="timestamp">Last updated: {timestamp}</div>
        <div class="attribution">Powered by ToRSh Dashboard</div>
    </div>"#
    )
}

// =============================================================================
// Card Generators
// =============================================================================

/// Generate performance metrics card
fn generate_performance_card(metrics: &PerformanceMetrics) -> String {
    format!(
        r#"<div class="card performance-card">
            <h3><span class="card-icon">⚡</span>Performance Metrics</h3>
            <div class="metrics-grid">
                <div class="metric">
                    <span class="metric-label">Total Operations</span>
                    <span class="metric-value">{}</span>
                </div>
                <div class="metric">
                    <span class="metric-label">Average Duration</span>
                    <span class="metric-value">{:.2} ms</span>
                </div>
                <div class="metric">
                    <span class="metric-label">Operations/Second</span>
                    <span class="metric-value">{:.1}</span>
                </div>
                <div class="metric">
                    <span class="metric-label">GFLOPS/Second</span>
                    <span class="metric-value">{:.2}</span>
                </div>
                <div class="metric">
                    <span class="metric-label">CPU Utilization</span>
                    <span class="metric-value">{:.1}%</span>
                    <div class="progress-bar">
                        <div class="progress-fill" style="width: {:.1}%"></div>
                    </div>
                </div>
                <div class="metric">
                    <span class="metric-label">Active Threads</span>
                    <span class="metric-value">{}</span>
                </div>
            </div>
        </div>"#,
        metrics.total_operations,
        metrics.average_duration_ms,
        metrics.operations_per_second,
        metrics.gflops_per_second,
        metrics.cpu_utilization,
        metrics.cpu_utilization.min(100.0),
        metrics.thread_count
    )
}

/// Generate memory metrics card
fn generate_memory_card(metrics: &MemoryMetrics) -> String {
    let usage_percentage = if metrics.peak_usage_mb > 0.0 {
        (metrics.current_usage_mb / metrics.peak_usage_mb * 100.0).min(100.0)
    } else {
        0.0
    };

    format!(
        r#"<div class="card memory-card">
            <h3><span class="card-icon">💾</span>Memory Metrics</h3>
            <div class="metrics-grid">
                <div class="metric">
                    <span class="metric-label">Current Usage</span>
                    <span class="metric-value">{:.1} MB</span>
                    <div class="progress-bar">
                        <div class="progress-fill" style="width: {:.1}%"></div>
                    </div>
                </div>
                <div class="metric">
                    <span class="metric-label">Peak Usage</span>
                    <span class="metric-value">{:.1} MB</span>
                </div>
                <div class="metric">
                    <span class="metric-label">Total Allocations</span>
                    <span class="metric-value">{}</span>
                </div>
                <div class="metric">
                    <span class="metric-label">Active Allocations</span>
                    <span class="metric-value">{}</span>
                </div>
                <div class="metric">
                    <span class="metric-label">Fragmentation</span>
                    <span class="metric-value">{:.1}%</span>
                </div>
                <div class="metric">
                    <span class="metric-label">Allocation Rate</span>
                    <span class="metric-value">{:.2}/s</span>
                </div>
            </div>
        </div>"#,
        metrics.current_usage_mb,
        usage_percentage,
        metrics.peak_usage_mb,
        metrics.total_allocations,
        metrics.active_allocations,
        metrics.fragmentation_ratio * 100.0,
        metrics.allocation_rate
    )
}

/// Generate system metrics card
fn generate_system_card(metrics: &SystemMetrics) -> String {
    let uptime_formatted = format_duration(metrics.uptime_seconds);

    format!(
        r#"<div class="card system-card">
            <h3><span class="card-icon">🖥️</span>System Metrics</h3>
            <div class="metrics-grid">
                <div class="metric">
                    <span class="metric-label">Uptime</span>
                    <span class="metric-value">{}</span>
                </div>
                <div class="metric">
                    <span class="metric-label">Load Average</span>
                    <span class="metric-value">{:.2}</span>
                </div>
                <div class="metric">
                    <span class="metric-label">Available Memory</span>
                    <span class="metric-value">{:.1} MB</span>
                </div>
                <div class="metric">
                    <span class="metric-label">Disk Usage</span>
                    <span class="metric-value">{:.1}%</span>
                    <div class="progress-bar">
                        <div class="progress-fill" style="width: {:.1}%"></div>
                    </div>
                </div>
                <div class="metric">
                    <span class="metric-label">Network I/O</span>
                    <span class="metric-value">{:.2} MB/s</span>
                </div>
            </div>
        </div>"#,
        uptime_formatted,
        metrics.load_average,
        metrics.available_memory_mb,
        metrics.disk_usage_percent,
        metrics.disk_usage_percent.min(100.0),
        metrics.network_io_mbps
    )
}

/// Generate alerts card
fn generate_alerts_card(alerts: &[DashboardAlert]) -> String {
    let alerts_content = if alerts.is_empty() {
        "<div class=\"no-alerts\">✅ No active alerts</div>".to_string()
    } else {
        alerts
            .iter()
            .map(|alert| {
                let (class, icon) = match alert.severity {
                    DashboardAlertSeverity::Emergency => ("alert-emergency", "🚨"),
                    DashboardAlertSeverity::Critical => ("alert-critical", "🔴"),
                    DashboardAlertSeverity::Warning => ("alert-warning", "⚠️"),
                    DashboardAlertSeverity::Info => ("alert-info", "ℹ️"),
                };

                format!(
                    r#"<div class="alert {class}">
                        <div class="alert-header">
                            <span class="alert-icon">{icon}</span>
                            <span class="alert-title">{}</span>
                            <span class="alert-time">{}</span>
                        </div>
                        <div class="alert-message">{}</div>
                    </div>"#,
                    alert.title,
                    format_timestamp(alert.timestamp),
                    alert.message
                )
            })
            .collect::<Vec<_>>()
            .join("")
    };

    format!(
        r#"<div class="card alerts-card">
            <h3><span class="card-icon">🚨</span>Active Alerts <span class="alert-count">{}</span></h3>
            <div class="alerts-container">
                {alerts_content}
            </div>
        </div>"#,
        alerts.len()
    )
}

/// Generate top operations card
fn generate_operations_card(operations: &[OperationSummary]) -> String {
    let operations_content = if operations.is_empty() {
        "<div class=\"no-operations\">No operations recorded</div>".to_string()
    } else {
        operations
            .iter()
            .take(10)
            .enumerate()
            .map(|(index, op)| {
                format!(
                    r#"<div class="operation" data-category="{}">
                        <div class="operation-rank">#{}</div>
                        <div class="operation-details">
                            <div class="operation-name">{}</div>
                            <div class="operation-category">{}</div>
                        </div>
                        <div class="operation-metrics">
                            <div class="operation-duration">{:.2} ms</div>
                            <div class="operation-percentage">{:.1}%</div>
                            <div class="operation-count">{} ops</div>
                        </div>
                        <div class="operation-bar">
                            <div class="operation-fill" style="width: {:.1}%"></div>
                        </div>
                    </div>"#,
                    op.category,
                    index + 1,
                    op.name,
                    op.category,
                    op.average_duration_ms,
                    op.percentage_of_total,
                    op.count,
                    op.percentage_of_total.min(100.0)
                )
            })
            .collect::<Vec<_>>()
            .join("")
    };

    format!(
        r#"<div class="card operations-card">
            <h3><span class="card-icon">📊</span>Top Operations</h3>
            <div class="operations-container">
                {operations_content}
            </div>
        </div>"#
    )
}

/// Generate charts and visualizations card
fn generate_charts_card(data: &DashboardData) -> String {
    format!(
        r#"<div class="card charts-card">
            <h3><span class="card-icon">📈</span>Performance Charts</h3>
            <div class="charts-container">
                <div class="chart-placeholder" id="performance-chart">
                    <div class="chart-title">Performance Over Time</div>
                    <div class="chart-content">Chart will be rendered here</div>
                </div>
                <div class="chart-placeholder" id="memory-chart">
                    <div class="chart-title">Memory Usage</div>
                    <div class="chart-content">Chart will be rendered here</div>
                </div>
                <div class="chart-placeholder" id="operations-chart">
                    <div class="chart-title">Operation Distribution</div>
                    <div class="chart-content">Chart will be rendered here</div>
                </div>
            </div>
        </div>"#
    )
}

// =============================================================================
// CSS Generation
// =============================================================================

/// Generate base CSS styles for the dashboard
fn generate_base_css(theme: &DashboardTheme) -> String {
    format!(
        r#"
        :root {{
            --primary-color: {};
            --secondary-color: {};
            --background-color: {};
            --text-color: {};
            --accent-color: {};
            --font-family: {};
        }}

        * {{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }}

        body {{
            font-family: var(--font-family);
            background-color: var(--background-color);
            color: var(--text-color);
            line-height: 1.6;
        }}

        .header {{
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
            color: white;
            padding: 2rem;
            text-align: center;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }}

        .header h1 {{
            font-size: 2.5rem;
            margin-bottom: 0.5rem;
            font-weight: 700;
        }}

        .header p {{
            font-size: 1.1rem;
            opacity: 0.9;
            margin-bottom: 1rem;
        }}

        .header-controls {{
            display: flex;
            justify-content: center;
            gap: 1rem;
            flex-wrap: wrap;
        }}

        .dashboard {{
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
            gap: 1.5rem;
            padding: 2rem;
            max-width: 1400px;
            margin: 0 auto;
        }}

        .card {{
            background: white;
            border-radius: 12px;
            padding: 1.5rem;
            box-shadow: 0 4px 6px rgba(0,0,0,0.05), 0 1px 3px rgba(0,0,0,0.1);
            border-left: 4px solid var(--primary-color);
            transition: transform 0.2s ease, box-shadow 0.2s ease;
        }}

        .card:hover {{
            transform: translateY(-2px);
            box-shadow: 0 8px 15px rgba(0,0,0,0.1), 0 3px 6px rgba(0,0,0,0.05);
        }}

        .card h3 {{
            font-size: 1.3rem;
            margin-bottom: 1rem;
            display: flex;
            align-items: center;
            gap: 0.5rem;
            color: var(--text-color);
        }}

        .card-icon {{
            font-size: 1.2em;
        }}

        .metrics-grid {{
            display: grid;
            gap: 0.75rem;
        }}

        .metric {{
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0.75rem;
            background: #f8f9fa;
            border-radius: 8px;
            transition: background-color 0.2s ease;
        }}

        .metric:hover {{
            background: #e9ecef;
        }}

        .metric-label {{
            font-weight: 500;
            color: #666;
        }}

        .metric-value {{
            font-weight: 700;
            font-size: 1.1em;
            color: var(--primary-color);
        }}

        .progress-bar {{
            width: 100%;
            height: 6px;
            background: #e9ecef;
            border-radius: 3px;
            margin-top: 0.5rem;
            overflow: hidden;
        }}

        .progress-fill {{
            height: 100%;
            background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
            border-radius: 3px;
            transition: width 0.3s ease;
        }}

        .alert {{
            margin: 0.5rem 0;
            padding: 1rem;
            border-radius: 8px;
            border-left: 4px solid;
        }}

        .alert-info {{
            background: #e3f2fd;
            border-color: #2196f3;
            color: #0d47a1;
        }}

        .alert-warning {{
            background: #fff3e0;
            border-color: #ff9800;
            color: #e65100;
        }}

        .alert-critical {{
            background: #ffebee;
            border-color: #f44336;
            color: #b71c1c;
        }}

        .alert-emergency {{
            background: #fce4ec;
            border-color: #e91e63;
            color: #880e4f;
            animation: pulse 2s infinite;
        }}

        @keyframes pulse {{
            0% {{ opacity: 1; }}
            50% {{ opacity: 0.7; }}
            100% {{ opacity: 1; }}
        }}

        .alert-header {{
            display: flex;
            align-items: center;
            gap: 0.5rem;
            margin-bottom: 0.5rem;
        }}

        .alert-title {{
            font-weight: 600;
            flex: 1;
        }}

        .alert-time {{
            font-size: 0.85em;
            opacity: 0.7;
        }}

        .operation {{
            display: flex;
            align-items: center;
            gap: 1rem;
            padding: 0.75rem;
            margin: 0.5rem 0;
            background: #f8f9fa;
            border-radius: 8px;
            position: relative;
            overflow: hidden;
        }}

        .operation-rank {{
            font-weight: 700;
            color: var(--primary-color);
            min-width: 2rem;
        }}

        .operation-details {{
            flex: 1;
        }}

        .operation-name {{
            font-weight: 600;
            margin-bottom: 0.25rem;
        }}

        .operation-category {{
            font-size: 0.85em;
            color: #666;
        }}

        .operation-metrics {{
            display: flex;
            gap: 1rem;
            font-size: 0.9em;
        }}

        .operation-bar {{
            position: absolute;
            bottom: 0;
            left: 0;
            height: 3px;
            width: 100%;
            background: rgba(0,0,0,0.1);
        }}

        .operation-fill {{
            height: 100%;
            background: var(--primary-color);
            transition: width 0.3s ease;
        }}

        .charts-container {{
            display: grid;
            gap: 1rem;
        }}

        .chart-placeholder {{
            background: #f8f9fa;
            border-radius: 8px;
            padding: 1rem;
            min-height: 200px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            text-align: center;
        }}

        .chart-title {{
            font-weight: 600;
            margin-bottom: 0.5rem;
            color: var(--text-color);
        }}

        .footer {{
            background: #f8f9fa;
            padding: 1rem 2rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-top: 1px solid #dee2e6;
            font-size: 0.9em;
            color: #666;
        }}

        .status-indicator {{
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }}

        .status-dot {{
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background: #28a745;
        }}

        .status-dot.active {{
            animation: blink 2s infinite;
        }}

        @keyframes blink {{
            0%, 50% {{ opacity: 1; }}
            51%, 100% {{ opacity: 0.3; }}
        }}

        .refresh-button, .settings-button, .fullscreen-button {{
            background: rgba(255,255,255,0.2);
            color: white;
            border: 1px solid rgba(255,255,255,0.3);
            padding: 0.5rem 1rem;
            border-radius: 6px;
            cursor: pointer;
            font-size: 0.9rem;
            transition: all 0.2s ease;
        }}

        .refresh-button:hover, .settings-button:hover, .fullscreen-button:hover {{
            background: rgba(255,255,255,0.3);
            transform: translateY(-1px);
        }}

        .no-alerts, .no-operations {{
            text-align: center;
            color: #666;
            font-style: italic;
            padding: 2rem;
        }}

        .alert-count {{
            background: var(--accent-color);
            color: white;
            padding: 0.25rem 0.5rem;
            border-radius: 12px;
            font-size: 0.8em;
            margin-left: 0.5rem;
        }}

        @media (max-width: 768px) {{
            .dashboard {{
                grid-template-columns: 1fr;
                padding: 1rem;
            }}

            .header {{
                padding: 1rem;
            }}

            .header h1 {{
                font-size: 2rem;
            }}

            .operation-metrics {{
                flex-direction: column;
                gap: 0.25rem;
            }}
        }}
        "#,
        theme.primary_color,
        theme.secondary_color,
        theme.background_color,
        theme.text_color,
        theme.accent_color,
        theme.font_family
    )
}

// =============================================================================
// Utility Functions
// =============================================================================

/// Format duration in human-readable format
fn format_duration(seconds: u64) -> String {
    if seconds < 60 {
        format!("{}s", seconds)
    } else if seconds < 3600 {
        format!("{}m {}s", seconds / 60, seconds % 60)
    } else if seconds < 86400 {
        format!("{}h {}m", seconds / 3600, (seconds % 3600) / 60)
    } else {
        format!("{}d {}h", seconds / 86400, (seconds % 86400) / 3600)
    }
}

/// Format timestamp for display
fn format_timestamp(timestamp: u64) -> String {
    chrono::DateTime::from_timestamp(timestamp as i64, 0)
        .map(|dt| dt.format("%H:%M:%S").to_string())
        .unwrap_or_else(|| "Unknown".to_string())
}

// =============================================================================
// Export Functions
// =============================================================================

/// Export dashboard HTML to file
pub fn export_dashboard_html(
    profiler: &Profiler,
    memory_profiler: &MemoryProfiler,
    file_path: &str,
) -> TorshResult<()> {
    let dashboard = create_dashboard();
    let html = generate_dashboard_html(&dashboard)?;
    std::fs::write(file_path, html)
        .map_err(|e| TorshError::IoError(format!("Failed to write dashboard HTML: {e}")))?;
    Ok(())
}

/// Export dashboard HTML with custom configuration
pub fn export_dashboard_html_with_config(
    profiler: &Profiler,
    memory_profiler: &MemoryProfiler,
    config: DashboardConfig,
    file_path: &str,
) -> TorshResult<()> {
    let dashboard = create_dashboard_with_config(config);
    let html = generate_dashboard_html(&dashboard)?;
    std::fs::write(file_path, html)
        .map_err(|e| TorshError::IoError(format!("Failed to write dashboard HTML: {e}")))?;
    Ok(())
}

/// Generate dashboard HTML with custom theme
pub fn generate_dashboard_html_with_theme(
    dashboard: &Dashboard,
    theme: &DashboardTheme,
) -> TorshResult<String> {
    let current_data = dashboard
        .get_current_data()?
        .unwrap_or_else(create_default_data);
    let active_alerts = dashboard.get_active_alerts()?;

    let html = build_html_document(&current_data, &active_alerts, &dashboard.config, theme)?;
    Ok(html)
}

/// Generate static dashboard HTML (without real-time features)
pub fn generate_static_dashboard_html(
    data: &DashboardData,
    alerts: &[DashboardAlert],
) -> TorshResult<String> {
    let config = DashboardConfig {
        real_time_updates: false,
        websocket_config: super::types::WebSocketConfig {
            enabled: false,
            ..Default::default()
        },
        ..Default::default()
    };
    let theme = DashboardTheme::default();

    let html = build_html_document(data, alerts, &config, &theme)?;
    Ok(html)
}

// =============================================================================
// Template System (for future extension)
// =============================================================================

/// Dashboard template configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DashboardTemplate {
    pub name: String,
    pub layout: LayoutType,
    pub components: Vec<ComponentConfig>,
    pub theme: DashboardTheme,
}

/// Layout types for dashboard templates
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum LayoutType {
    Grid,
    Sidebar,
    Tabbed,
    Single,
}

/// Component configuration for templates
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComponentConfig {
    pub component_type: String,
    pub position: (u32, u32),
    pub size: (u32, u32),
    pub config: HashMap<String, String>,
}

// =============================================================================
// DashboardRenderer - Missing Implementation
// =============================================================================

/// Dashboard HTML renderer with template system
pub struct DashboardRenderer {
    /// Renderer configuration
    pub config: DashboardRendererConfig,
}

/// Configuration for the dashboard renderer
#[derive(Debug, Clone)]
pub struct DashboardRendererConfig {
    /// Enable template caching
    pub enable_caching: bool,
    /// Enable minification
    pub enable_minification: bool,
    /// Include debug information
    pub include_debug_info: bool,
}

impl Default for DashboardRendererConfig {
    fn default() -> Self {
        Self {
            enable_caching: true,
            enable_minification: false,
            include_debug_info: false,
        }
    }
}

impl DashboardRenderer {
    /// Create a new dashboard renderer
    pub fn new() -> Self {
        Self {
            config: DashboardRendererConfig::default(),
        }
    }

    /// Create a new dashboard renderer with custom configuration
    pub fn new_with_config(config: DashboardRendererConfig) -> Self {
        Self { config }
    }

    /// Generate complete dashboard HTML interface
    pub fn generate_dashboard_html(
        &self,
        data: &DashboardData,
        alerts: &[DashboardAlert],
        config: &DashboardConfig,
        theme: &DashboardTheme,
    ) -> TorshResult<String> {
        build_html_document(data, alerts, config, theme)
    }

    /// Generate HTML for specific component
    pub fn generate_component_html(
        &self,
        component_type: &str,
        data: &DashboardData,
    ) -> TorshResult<String> {
        match component_type {
            "performance" => Ok(generate_performance_card(&data.performance_metrics)),
            "memory" => Ok(generate_memory_card(&data.memory_metrics)),
            "system" => Ok(generate_system_card(&data.system_metrics)),
            "alerts" => Ok(generate_alerts_card(&data.alerts)),
            "operations" => Ok(generate_operations_card(&data.top_operations)),
            "charts" => Ok(generate_charts_card(data)),
            _ => Err(TorshError::InvalidArgument(format!(
                "Unknown component type: {component_type}"
            ))),
        }
    }
}

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