File size: 30,865 Bytes
3f5fadf
e9fdc7c
9b137fe
3f5fadf
 
 
3e4331a
 
859f566
 
5aa5b79
 
9b137fe
3e4331a
9b137fe
7f3d172
859f566
3e4331a
 
 
 
859f566
3e4331a
 
 
fef95f5
 
5aa5b79
859f566
 
9b137fe
3e4331a
9b137fe
 
 
 
 
 
 
5aa5b79
 
9b137fe
5aa5b79
 
9b137fe
 
 
 
 
 
5aa5b79
 
 
 
 
 
 
859f566
9b137fe
5aa5b79
 
9b137fe
 
 
 
 
5aa5b79
 
9b137fe
5aa5b79
9b137fe
5aa5b79
 
 
9b137fe
5aa5b79
 
9b137fe
5aa5b79
 
 
 
 
 
 
 
 
 
 
9b137fe
 
 
 
 
 
5aa5b79
9b137fe
5aa5b79
9b137fe
5aa5b79
 
9b137fe
5aa5b79
9b137fe
 
 
 
 
 
 
 
5aa5b79
9b137fe
5aa5b79
9b137fe
 
5aa5b79
9b137fe
 
 
5aa5b79
 
 
 
9b137fe
 
5aa5b79
 
 
 
 
 
 
9b137fe
 
 
 
 
 
859f566
5aa5b79
 
9b137fe
5aa5b79
9b137fe
5aa5b79
9b137fe
5aa5b79
 
 
 
 
 
 
 
 
 
 
 
 
9b137fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5aa5b79
9b137fe
 
 
5aa5b79
9b137fe
5aa5b79
 
 
 
9b137fe
5aa5b79
 
 
 
 
9b137fe
5aa5b79
 
9b137fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5aa5b79
 
 
 
9b137fe
5aa5b79
9b137fe
 
5aa5b79
9b137fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
859f566
9b137fe
 
859f566
9b137fe
 
 
 
 
 
859f566
9b137fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
859f566
9b137fe
859f566
9b137fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5aa5b79
9b137fe
5aa5b79
9b137fe
5aa5b79
 
9b137fe
5aa5b79
9b137fe
 
 
 
 
 
859f566
9b137fe
5aa5b79
 
9b137fe
5aa5b79
9b137fe
 
5aa5b79
859f566
9b137fe
5aa5b79
9b137fe
 
5aa5b79
 
 
9b137fe
5aa5b79
 
9b137fe
5aa5b79
 
 
 
9b137fe
5aa5b79
 
9b137fe
 
 
 
 
 
 
5aa5b79
9b137fe
5aa5b79
9b137fe
 
 
 
 
 
5aa5b79
9b137fe
 
 
 
 
5aa5b79
 
9b137fe
 
5aa5b79
 
9b137fe
859f566
9b137fe
 
5aa5b79
9b137fe
5aa5b79
 
9b137fe
5aa5b79
 
9b137fe
5aa5b79
9b137fe
5aa5b79
 
 
9b137fe
5aa5b79
9b137fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
859f566
9b137fe
5aa5b79
 
 
 
9b137fe
5aa5b79
 
 
 
9b137fe
5aa5b79
9b137fe
5aa5b79
 
9b137fe
5aa5b79
9b137fe
5aa5b79
 
9b137fe
5aa5b79
9b137fe
5aa5b79
9b137fe
 
 
 
 
 
 
 
5aa5b79
 
 
 
9b137fe
 
 
859f566
5aa5b79
 
 
 
9b137fe
 
5aa5b79
 
9b137fe
5aa5b79
9b137fe
859f566
5aa5b79
9b137fe
 
 
 
859f566
5aa5b79
9b137fe
859f566
9b137fe
859f566
5aa5b79
 
 
9b137fe
5aa5b79
 
9b137fe
 
 
859f566
9b137fe
 
859f566
9b137fe
 
 
 
 
 
 
 
 
 
859f566
 
5aa5b79
859f566
9b137fe
 
859f566
 
9b137fe
859f566
9b137fe
 
 
 
 
 
859f566
 
 
9b137fe
 
 
 
 
 
859f566
 
 
5aa5b79
9b137fe
859f566
9b137fe
859f566
5aa5b79
9b137fe
5aa5b79
9b137fe
5aa5b79
859f566
9b137fe
5aa5b79
 
9b137fe
 
5aa5b79
9b137fe
 
859f566
5aa5b79
9b137fe
 
859f566
9b137fe
 
 
3e4331a
9b137fe
5aa5b79
9b137fe
 
 
 
 
d265a89
5aa5b79
 
 
9b137fe
5aa5b79
9b137fe
5aa5b79
 
 
859f566
5aa5b79
 
 
 
9b137fe
5aa5b79
9b137fe
5aa5b79
 
 
 
 
 
9b137fe
5aa5b79
9b137fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5aa5b79
 
9b137fe
 
 
 
 
 
5aa5b79
859f566
9b137fe
 
859f566
9b137fe
d265a89
5aa5b79
 
 
 
 
 
9b137fe
5aa5b79
9b137fe
d265a89
5aa5b79
 
9b137fe
5aa5b79
 
9b137fe
5aa5b79
9b137fe
 
 
5aa5b79
 
 
9b137fe
5aa5b79
d265a89
9b137fe
5aa5b79
9b137fe
 
 
5aa5b79
d265a89
 
 
5aa5b79
9b137fe
5aa5b79
3e4331a
9b137fe
859f566
 
 
9b137fe
 
 
 
859f566
5aa5b79
859f566
 
9b137fe
 
 
 
 
d265a89
 
3e4331a
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
"""
πŸš€ ARF Ultimate Investor Demo v3.8.0 - ENTERPRISE EDITION
COMPLETE FIXED VERSION - All components integrated
"""

import logging
import sys
import traceback
import json
import datetime
import asyncio
import time
import numpy as np
from pathlib import Path
from typing import Dict, List, Any, Optional, Tuple

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(sys.stdout),
        logging.FileHandler('arf_demo.log')
    ]
)
logger = logging.getLogger(__name__)

# Add parent directory to path
sys.path.insert(0, str(Path(__file__).parent))

# Import Plotly early to ensure availability
try:
    import plotly.graph_objects as go
    import plotly.express as px
    from plotly.subplots import make_subplots
    PLOTLY_AVAILABLE = True
except ImportError:
    logger.warning("Plotly not available - visualizations will be simplified")
    PLOTLY_AVAILABLE = False

# ===========================================
# IMPORT MODULAR COMPONENTS
# ===========================================
try:
    # Import scenarios from your modular file
    from demo.scenarios import INCIDENT_SCENARIOS as SCENARIOS_DATA
    
    # Import orchestrator
    from demo.orchestrator import DemoOrchestrator
    
    # Import UI components
    from ui.components import (
        create_header, create_status_bar, create_tab1_incident_demo,
        create_tab2_business_roi, create_tab3_audit_trail,
        create_tab4_enterprise_features, create_tab5_learning_engine,
        create_footer
    )
    
    logger.info("βœ… Successfully imported all modular components")
    
except ImportError as e:
    logger.error(f"Failed to import components: {e}")
    logger.error(traceback.format_exc())
    # Fallback to inline definitions
    SCENARIOS_DATA = {}
    DemoOrchestrator = None

# ===========================================
# ENHANCED SCENARIOS WITH OSS vs ENTERPRISE SEPARATION
# ===========================================
ENHANCED_SCENARIOS = {
    "Cache Miss Storm": {
        "description": "Redis cluster experiencing 80% cache miss rate causing database overload",
        "severity": "CRITICAL",
        "component": "redis_cache",
        "metrics": {
            "Cache Hit Rate": "18.5% (Critical)",
            "Database Load": "92% (Overloaded)",
            "Response Time": "1850ms (Slow)",
            "Affected Users": "45,000",
            "Eviction Rate": "125/sec"
        },
        "impact": {
            "Revenue Loss": "$8,500/hour",
            "Page Load Time": "+300%",
            "Users Impacted": "45,000",
            "SLA Violation": "Yes",
            "Customer Sat": "-40%"
        },
        # OSS RESULTS - ADVISORY ONLY
        "oss_results": {
            "status": "βœ… OSS Analysis Complete",
            "confidence": 0.87,
            "similar_incidents": 3,
            "rag_similarity_score": 0.72,
            "recommendations": [
                "Scale Redis cache memory from 4GB β†’ 8GB",
                "Implement cache warming strategy",
                "Optimize key patterns with TTL adjustments",
                "Add circuit breaker for database fallback"
            ],
            "estimated_time": "60+ minutes manually",
            "engineers_needed": "2-3 SREs + 1 DBA",
            "advisory_only": True,
            "healing_intent": {
                "action": "scale_out",
                "component": "redis_cache",
                "parameters": {"scale_factor": 2.0},
                "confidence": 0.87,
                "requires_enterprise": True
            }
        },
        # ENTERPRISE RESULTS - ACTUAL EXECUTION
        "enterprise_results": {
            "execution_mode": "Autonomous",
            "actions_executed": [
                "βœ… Auto-scaled Redis cluster: 4GB β†’ 8GB",
                "βœ… Deployed intelligent cache warming service",
                "βœ… Optimized 12 key patterns with ML recommendations",
                "βœ… Implemented circuit breaker with 95% success rate"
            ],
            "metrics_improvement": {
                "Cache Hit Rate": "18.5% β†’ 72%",
                "Response Time": "1850ms β†’ 450ms",
                "Database Load": "92% β†’ 45%",
                "Throughput": "1250 β†’ 2450 req/sec"
            },
            "business_impact": {
                "Recovery Time": "60 min β†’ 12 min",
                "Cost Saved": "$7,200",
                "Users Impacted": "45,000 β†’ 0",
                "Revenue Protected": "$1,700",
                "MTTR Improvement": "80% reduction"
            },
            "audit_info": {
                "execution_id": "exec_001",
                "timestamp": datetime.datetime.now().isoformat(),
                "approval_required": False,
                "success": True
            }
        }
    },
    
    "Database Connection Pool Exhaustion": {
        "description": "PostgreSQL connection pool exhausted causing API timeouts",
        "severity": "HIGH",
        "component": "postgresql_database",
        "metrics": {
            "Active Connections": "98/100 (Critical)",
            "API Latency": "2450ms",
            "Error Rate": "15.2%",
            "Queue Depth": "1250",
            "Connection Wait": "45s"
        },
        "impact": {
            "Revenue Loss": "$4,200/hour",
            "Affected Services": "API Gateway, User Service, Payment",
            "SLA Violation": "Yes",
            "Partner Impact": "3 external APIs"
        },
        "oss_results": {
            "status": "βœ… OSS Analysis Complete",
            "confidence": 0.82,
            "similar_incidents": 2,
            "rag_similarity_score": 0.65,
            "recommendations": [
                "Increase connection pool size from 100 β†’ 200",
                "Implement connection pooling monitoring",
                "Add query timeout enforcement",
                "Deploy read replica for read-heavy queries"
            ],
            "estimated_time": "45+ minutes manually",
            "engineers_needed": "1 DBA + 1 Backend Engineer",
            "advisory_only": True,
            "healing_intent": {
                "action": "scale_connection_pool",
                "component": "postgresql_database",
                "parameters": {"max_connections": 200},
                "confidence": 0.82,
                "requires_enterprise": True
            }
        },
        "enterprise_results": {
            "execution_mode": "Approval Required",
            "actions_executed": [
                "βœ… Increased connection pool: 100 β†’ 200 connections",
                "βœ… Deployed real-time connection monitoring",
                "βœ… Implemented query timeout: 30s β†’ 10s",
                "βœ… Automated read replica traffic routing"
            ],
            "metrics_improvement": {
                "API Latency": "2450ms β†’ 320ms",
                "Error Rate": "15.2% β†’ 0.8%",
                "Connection Wait": "45s β†’ 120ms",
                "Throughput": "850 β†’ 2100 req/sec"
            },
            "business_impact": {
                "Recovery Time": "45 min β†’ 8 min",
                "Cost Saved": "$3,150",
                "Failed Transactions": "12,500 β†’ 0",
                "SLA Compliance": "Restored to 99.9%"
            },
            "audit_info": {
                "execution_id": "exec_002",
                "timestamp": datetime.datetime.now().isoformat(),
                "approval_required": True,
                "success": True
            }
        }
    },
    
    "Kubernetes Memory Leak": {
        "description": "Java microservice memory leak causing pod restarts",
        "severity": "HIGH",
        "component": "java_payment_service",
        "metrics": {
            "Memory Usage": "96% (Critical)",
            "GC Pause Time": "4500ms",
            "Error Rate": "28.5%",
            "Pod Restarts": "12/hour",
            "Heap Fragmentation": "42%"
        },
        "impact": {
            "Revenue Loss": "$5,500/hour",
            "Session Loss": "8,500 users",
            "Payment Failures": "3.2% of transactions",
            "Support Tickets": "+300%"
        },
        "oss_results": {
            "status": "βœ… OSS Analysis Complete",
            "confidence": 0.79,
            "similar_incidents": 4,
            "rag_similarity_score": 0.68,
            "recommendations": [
                "Increase pod memory limits from 2GB β†’ 4GB",
                "Implement memory leak detection",
                "Deploy canary with fixed version",
                "Add circuit breaker for graceful degradation"
            ],
            "estimated_time": "90+ minutes manually",
            "engineers_needed": "2 Java Devs + 1 SRE",
            "advisory_only": True,
            "healing_intent": {
                "action": "scale_memory",
                "component": "java_payment_service",
                "parameters": {"memory_limit_gb": 4},
                "confidence": 0.79,
                "requires_enterprise": True
            }
        },
        "enterprise_results": {
            "execution_mode": "Autonomous with Rollback",
            "actions_executed": [
                "βœ… Scaled pod memory: 2GB β†’ 4GB with monitoring",
                "βœ… Deployed memory leak detection service",
                "βœ… Rolled out canary with memory fixes",
                "βœ… Implemented auto-rollback on failure"
            ],
            "metrics_improvement": {
                "Memory Usage": "96% β†’ 68%",
                "GC Pause Time": "4500ms β†’ 320ms",
                "Error Rate": "28.5% β†’ 1.2%",
                "Pod Stability": "12/hour β†’ 0 restarts"
            },
            "business_impact": {
                "Recovery Time": "90 min β†’ 15 min",
                "Cost Saved": "$4,950",
                "Transaction Success": "96.8% β†’ 99.9%",
                "User Impact": "8,500 β†’ 0 affected"
            },
            "audit_info": {
                "execution_id": "exec_003",
                "timestamp": datetime.datetime.now().isoformat(),
                "approval_required": False,
                "success": True
            }
        }
    },
    
    "API Rate Limit Storm": {
        "description": "Third-party API rate limiting causing cascading failures",
        "severity": "MEDIUM",
        "component": "external_api_gateway",
        "metrics": {
            "Rate Limit Hits": "95% of requests",
            "Error Rate": "42.8%",
            "Retry Storm": "Active",
            "Cascade Effect": "3 dependent services",
            "Queue Backlog": "8,500 requests"
        },
        "impact": {
            "Revenue Loss": "$3,800/hour",
            "Partner SLA Breach": "Yes",
            "Data Sync Delay": "4+ hours",
            "Customer Reports": "Delayed by 6 hours"
        },
        "oss_results": {
            "status": "βœ… OSS Analysis Complete",
            "confidence": 0.85,
            "similar_incidents": 3,
            "rag_similarity_score": 0.71,
            "recommendations": [
                "Implement exponential backoff with jitter",
                "Deploy circuit breaker pattern",
                "Add request queuing with prioritization",
                "Implement adaptive rate limiting"
            ],
            "estimated_time": "75+ minutes manually",
            "engineers_needed": "2 Backend Engineers + 1 DevOps",
            "advisory_only": True,
            "healing_intent": {
                "action": "implement_rate_limiting",
                "component": "external_api_gateway",
                "parameters": {"backoff_strategy": "exponential"},
                "confidence": 0.85,
                "requires_enterprise": True
            }
        },
        "enterprise_results": {
            "execution_mode": "Autonomous",
            "actions_executed": [
                "βœ… Implemented exponential backoff: 1s β†’ 32s with jitter",
                "βœ… Deployed circuit breaker with 80% success threshold",
                "βœ… Added intelligent request queuing",
                "βœ… Enabled adaptive rate limiting based on API health"
            ],
            "metrics_improvement": {
                "Rate Limit Hits": "95% β†’ 12%",
                "Error Rate": "42.8% β†’ 3.5%",
                "Successful Retries": "18% β†’ 89%",
                "Queue Processing": "8,500 β†’ 0 backlog"
            },
            "business_impact": {
                "Recovery Time": "75 min β†’ 10 min",
                "Cost Saved": "$3,420",
                "SLA Compliance": "Restored within 5 minutes",
                "Data Freshness": "4+ hours β†’ <5 minute delay"
            },
            "audit_info": {
                "execution_id": "exec_004",
                "timestamp": datetime.datetime.now().isoformat(),
                "approval_required": False,
                "success": True
            }
        }
    }
}

# ===========================================
# SIMPLE VISUALIZATION ENGINE (No external dependencies)
# ===========================================
class SimpleVizEngine:
    """Simple visualization engine that works without complex imports"""
    
    @staticmethod
    def create_timeline_plot(scenario_name="Incident"):
        """Create a simple timeline plot"""
        if not PLOTLY_AVAILABLE:
            # Return a placeholder if plotly not available
            import matplotlib.pyplot as plt
            import io
            import base64
            
            fig, ax = plt.subplots(figsize=(10, 4))
            events = ['Detection', 'Analysis', 'Action', 'Recovery']
            times = [0, 1, 2, 3]
            
            ax.plot(times, [1, 1, 1, 1], 'bo-', markersize=10)
            for i, (event, t) in enumerate(zip(events, times)):
                ax.text(t, 1.1, event, ha='center', fontsize=10)
            
            ax.set_ylim(0.5, 1.5)
            ax.set_xlim(-0.5, 3.5)
            ax.set_title(f'Timeline: {scenario_name}')
            ax.axis('off')
            
            buf = io.BytesIO()
            plt.savefig(buf, format='png', bbox_inches='tight')
            plt.close(fig)
            buf.seek(0)
            
            return f"data:image/png;base64,{base64.b64encode(buf.read()).decode()}"
        
        # Use Plotly if available
        fig = go.Figure()
        
        events = [
            {"time": "T-5m", "event": "Detection", "type": "detection"},
            {"time": "T-3m", "event": "OSS Analysis", "type": "analysis"},
            {"time": "T-2m", "event": "Enterprise Action", "type": "action"},
            {"time": "T-0m", "event": "Recovery", "type": "recovery"}
        ]
        
        for event in events:
            fig.add_trace(go.Scatter(
                x=[event["time"]],
                y=[1],
                mode='markers+text',
                marker=dict(size=20, color='#4ECDC4'),
                text=[event["event"]],
                textposition="top center"
            ))
        
        fig.update_layout(
            title=f"Timeline: {scenario_name}",
            height=300,
            showlegend=False,
            yaxis=dict(showticklabels=False, range=[0.5, 1.5]),
            margin=dict(l=20, r=20, t=40, b=20)
        )
        
        return fig
    
    @staticmethod
    def create_dashboard_plot():
        """Create simple dashboard plot"""
        if not PLOTLY_AVAILABLE:
            return None
        
        fig = make_subplots(rows=1, cols=2, subplot_titles=('Cost Savings', 'MTTR Improvement'))
        
        # Cost savings
        fig.add_trace(
            go.Bar(x=['Without ARF', 'With ARF'], y=[100, 25], name='Cost'),
            row=1, col=1
        )
        
        # MTTR improvement
        fig.add_trace(
            go.Bar(x=['Manual', 'ARF OSS', 'ARF Enterprise'], y=[120, 25, 8], name='MTTR'),
            row=1, col=2
        )
        
        fig.update_layout(height=400, showlegend=False)
        return fig

# ===========================================
# AUDIT TRAIL MANAGER
# ===========================================
class AuditTrailManager:
    def __init__(self):
        self.executions = []
        self.incidents = []
        
    def add_execution(self, scenario_name, mode, success=True, savings=0):
        entry = {
            "id": f"exec_{len(self.executions):03d}",
            "time": datetime.datetime.now().strftime("%H:%M"),
            "scenario": scenario_name,
            "mode": mode,
            "status": "βœ… Success" if success else "❌ Failed",
            "savings": f"${savings:,}",
            "details": f"{mode} execution"
        }
        self.executions.insert(0, entry)
        return entry
    
    def add_incident(self, scenario_name, severity="HIGH"):
        entry = {
            "id": f"inc_{len(self.incidents):03d}",
            "time": datetime.datetime.now().strftime("%H:%M"),
            "scenario": scenario_name,
            "severity": severity,
            "component": ENHANCED_SCENARIOS.get(scenario_name, {}).get("component", "unknown"),
            "status": "Analyzed"
        }
        self.incidents.insert(0, entry)
        return entry
    
    def get_execution_table(self):
        return [
            [e["time"], e["scenario"], e["mode"], e["status"], e["savings"], e["details"]]
            for e in self.executions[:10]
        ]
    
    def get_incident_table(self):
        return [
            [e["time"], e["component"], e["scenario"], e["severity"], e["status"]]
            for e in self.incidents[:15]
        ]

# ===========================================
# CREATE DEMO INTERFACE - FIXED VERSION
# ===========================================
def create_demo_interface():
    """Create the demo interface with all fixes applied"""
    
    import gradio as gr
    
    # Initialize components
    viz_engine = SimpleVizEngine()
    audit_manager = AuditTrailManager()
    
    # Initialize orchestrator if available
    orchestrator = None
    if DemoOrchestrator:
        try:
            orchestrator = DemoOrchestrator()
        except:
            pass
    
    # Custom CSS for OSS vs Enterprise separation
    custom_css = """
    .oss-section {
        background: linear-gradient(135deg, #e3f2fd 0%, #bbdefb 100%) !important;
        border-left: 4px solid #2196f3 !important;
        padding: 15px !important;
        border-radius: 8px !important;
        margin-bottom: 15px !important;
    }
    .enterprise-section {
        background: linear-gradient(135deg, #e8f5e8 0%, #c8e6c9 100%) !important;
        border-left: 4px solid #4caf50 !important;
        padding: 15px !important;
        border-radius: 8px !important;
        margin-bottom: 15px !important;
    }
    .critical { color: #d32f2f !important; font-weight: bold; }
    .success { color: #388e3c !important; font-weight: bold; }
    """
    
    with gr.Blocks(title="πŸš€ ARF Investor Demo v3.8.0", css=custom_css) as demo:
        
        # Use your modular header
        create_header("3.3.6", False)  # OSS version, Mock mode
        
        # Status bar
        create_status_bar()
        
        # Tabs
        with gr.Tabs():
            
            # TAB 1: Live Incident Demo (Fixed)
            with gr.TabItem("πŸ”₯ Live Incident Demo"):
                # Get components from your UI module
                (scenario_dropdown, scenario_description, metrics_display, impact_display,
                 timeline_output, oss_btn, enterprise_btn, approval_toggle, demo_btn,
                 approval_display, config_display, results_display) = create_tab1_incident_demo(
                    ENHANCED_SCENARIOS, "Cache Miss Storm"
                )
                
                # Add OSS and Enterprise results displays
                with gr.Row():
                    with gr.Column():
                        gr.Markdown("### πŸ“‹ OSS Analysis Results (Advisory Only)")
                        oss_results = gr.JSON(
                            value={},
                            label=""
                        )
                    
                    with gr.Column():
                        gr.Markdown("### 🎯 Enterprise Execution Results")
                        enterprise_results = gr.JSON(
                            value={},
                            label=""
                        )
            
            # TAB 2: Business Impact & ROI
            with gr.TabItem("πŸ’° Business Impact & ROI"):
                (dashboard_output, monthly_slider, impact_slider, team_slider,
                 calculate_btn, roi_output) = create_tab2_business_roi()
            
            # TAB 3: Audit Trail
            with gr.TabItem("πŸ“œ Audit Trail & History"):
                (refresh_btn, clear_btn, export_btn, execution_table, savings_chart,
                 incident_table, memory_graph, export_text) = create_tab3_audit_trail()
            
            # Other tabs...
            with gr.TabItem("🏒 Enterprise Features"):
                create_tab4_enterprise_features()
            
            with gr.TabItem("🧠 Learning Engine"):
                create_tab5_learning_engine()
        
        # Footer
        create_footer()
        
        # ============ EVENT HANDLERS (FIXED) ============
        
        # Update scenario (FIXED: Proper parameter handling)
        def update_scenario(scenario_name):
            scenario = ENHANCED_SCENARIOS.get(scenario_name, {})
            
            # Get timeline plot
            if PLOTLY_AVAILABLE:
                timeline = viz_engine.create_timeline_plot(scenario_name)
            else:
                timeline = None
            
            return (
                f"### {scenario_name}\n{scenario.get('description', 'No description')}",
                scenario.get("metrics", {}),
                scenario.get("impact", {}),
                timeline if timeline else gr.Plot(visible=False),
                {},  # Clear OSS results
                {}   # Clear Enterprise results
            )
        
        scenario_dropdown.change(
            fn=update_scenario,
            inputs=[scenario_dropdown],
            outputs=[scenario_description, metrics_display, impact_display,
                    timeline_output, oss_results, enterprise_results]
        )
        
        # Run OSS Analysis (FIXED: Proper async handling)
        async def run_oss_analysis(scenario_name):
            scenario = ENHANCED_SCENARIOS.get(scenario_name, {})
            
            # Add to audit trail
            audit_manager.add_incident(scenario_name, scenario.get("severity", "HIGH"))
            
            # Get OSS results
            oss_result = scenario.get("oss_results", {})
            
            # Update tables
            incident_table_data = audit_manager.get_incident_table()
            
            return oss_result, incident_table_data
        
        oss_btn.click(
            fn=run_oss_analysis,
            inputs=[scenario_dropdown],
            outputs=[oss_results, incident_table]
        )
        
        # Execute Enterprise Healing (FIXED: Proper parameter matching)
        def execute_enterprise_healing(scenario_name, approval_required):
            scenario = ENHANCED_SCENARIOS.get(scenario_name, {})
            
            # Get enterprise results
            enterprise_result = scenario.get("enterprise_results", {})
            
            # Determine mode
            mode = "Approval" if approval_required else "Autonomous"
            
            # Calculate savings from impact
            impact = scenario.get("impact", {})
            revenue_loss = impact.get("Revenue Loss", "$0")
            try:
                savings = int(revenue_loss.replace("$", "").replace(",", "").split("/")[0]) * 0.85
            except:
                savings = 5000
            
            # Add to audit trail
            audit_manager.add_execution(
                scenario_name,
                mode,
                savings=int(savings)
            )
            
            # Create approval display
            if approval_required:
                approval_html = f"""
                <div class='enterprise-section'>
                    <h4>βœ… Approved & Executed</h4>
                    <p>Action for <strong>{scenario_name}</strong> was approved by system administrator and executed successfully.</p>
                    <p><strong>Mode:</strong> Manual Approval</p>
                    <p><strong>Cost Saved:</strong> ${int(savings):,}</p>
                </div>
                """
            else:
                approval_html = f"""
                <div class='enterprise-section'>
                    <h4>⚑ Auto-Executed</h4>
                    <p>Action for <strong>{scenario_name}</strong> was executed autonomously by ARF Enterprise.</p>
                    <p><strong>Mode:</strong> Fully Autonomous</p>
                    <p><strong>Cost Saved:</strong> ${int(savings):,}</p>
                </div>
                """
            
            # Update execution table
            execution_table_data = audit_manager.get_execution_table()
            
            return approval_html, enterprise_result, execution_table_data
        
        enterprise_btn.click(
            fn=execute_enterprise_healing,
            inputs=[scenario_dropdown, approval_toggle],
            outputs=[approval_display, enterprise_results, execution_table]
        )
        
        # Quick Demo (FIXED: Proper async)
        async def run_quick_demo():
            # Run OSS analysis
            scenario = ENHANCED_SCENARIOS["Cache Miss Storm"]
            oss_result = scenario.get("oss_results", {})
            
            # Execute enterprise
            enterprise_result = scenario.get("enterprise_results", {})
            
            # Update audit trail
            audit_manager.add_incident("Cache Miss Storm", "CRITICAL")
            audit_manager.add_execution("Cache Miss Storm", "Autonomous", savings=7200)
            
            # Get table data
            execution_table_data = audit_manager.get_execution_table()
            incident_table_data = audit_manager.get_incident_table()
            
            # Create approval display
            approval_html = """
            <div class='enterprise-section'>
                <h4>⚑ Quick Demo Completed</h4>
                <p>Full OSS analysis β†’ Enterprise execution completed successfully.</p>
                <p><strong>Mode:</strong> Autonomous</p>
                <p><strong>Cost Saved:</strong> $7,200</p>
            </div>
            """
            
            return (
                oss_result,
                approval_html,
                enterprise_result,
                execution_table_data,
                incident_table_data,
                gr.Checkbox.update(value=False)
            )
        
        demo_btn.click(
            fn=run_quick_demo,
            outputs=[
                oss_results,
                approval_display,
                enterprise_results,
                execution_table,
                incident_table,
                approval_toggle
            ]
        )
        
        # ROI Calculator (FIXED)
        def calculate_roi(monthly, impact, team):
            if orchestrator:
                company_data = {
                    "monthly_incidents": monthly,
                    "avg_cost_per_incident": impact,
                    "team_size": team
                }
                roi_result = orchestrator.calculate_roi(company_data)
            else:
                # Simple calculation
                annual = monthly * 12 * impact
                savings = annual * 0.82
                team_cost = team * 150000
                roi_multiplier = savings / team_cost if team_cost > 0 else 0
                
                roi_result = {
                    "annual_impact": annual,
                    "team_cost": team_cost,
                    "potential_savings": savings,
                    "roi_multiplier": roi_multiplier,
                    "payback_months": (team_cost / (savings / 12)) if savings > 0 else 0
                }
            
            # Format for display
            formatted = {
                "Annual Impact": f"${roi_result.get('annual_impact', 0):,.0f}",
                "Team Cost": f"${roi_result.get('team_cost', 0):,.0f}",
                "Potential Savings": f"${roi_result.get('potential_savings', 0):,.0f}",
                "ROI Multiplier": f"{roi_result.get('roi_multiplier', 0):.1f}Γ—",
                "Payback Period": f"{roi_result.get('payback_months', 0):.1f} months"
            }
            
            # Add dashboard
            dashboard = viz_engine.create_dashboard_plot()
            
            return formatted, dashboard
        
        calculate_btn.click(
            fn=calculate_roi,
            inputs=[monthly_slider, impact_slider, team_slider],
            outputs=[roi_output, dashboard_output]
        )
        
        # Audit Trail Refresh (FIXED)
        def refresh_audit_trail():
            return audit_manager.get_execution_table(), audit_manager.get_incident_table()
        
        refresh_btn.click(
            fn=refresh_audit_trail,
            outputs=[execution_table, incident_table]
        )
        
        # Clear History (FIXED)
        def clear_audit_trail():
            audit_manager.executions = []
            audit_manager.incidents = []
            return audit_manager.get_execution_table(), audit_manager.get_incident_table()
        
        clear_btn.click(
            fn=clear_audit_trail,
            outputs=[execution_table, incident_table]
        )
        
        # Initialize with first scenario
        demo.load(
            fn=lambda: update_scenario("Cache Miss Storm"),
            outputs=[scenario_description, metrics_display, impact_display,
                    timeline_output, oss_results, enterprise_results]
        )
    
    return demo

# ===========================================
# MAIN EXECUTION
# ===========================================
def main():
    """Main entry point"""
    print("πŸš€ Starting ARF Ultimate Investor Demo v3.8.0...")
    print("=" * 70)
    print("πŸ“Š Features:")
    print("  β€’ 4 Enhanced Incident Scenarios")
    print("  β€’ Clear OSS vs Enterprise Separation")
    print("  β€’ Fixed Visualization Engine")
    print("  β€’ Working Event Handlers")
    print("=" * 70)
    print("🌐 Opening web interface...")
    
    demo = create_demo_interface()
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False
    )

if __name__ == "__main__":
    main()