sublinear 0.2.0

High-performance sublinear-time solver for asymmetric diagonally dominant systems
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
/**
 * Comprehensive Monitoring and Alerting System
 * For Entity Communication Detection Infrastructure
 *
 * Provides real-time monitoring, alerting, and system health tracking
 * for all neural pattern recognition components.
 */

const EventEmitter = require('events');
const fs = require('fs').promises;
const path = require('path');

/**
 * Central monitoring hub for the entire entity communication detection system
 */
class EntityCommunicationMonitor extends EventEmitter {
    constructor(config = {}) {
        super();

        this.config = {
            alertThresholds: {
                detectionAccuracy: 0.85,
                responseTime: 1000, // ms
                memoryUsage: 0.8, // 80%
                cpuUsage: 0.9, // 90%
                errorRate: 0.05 // 5%
            },
            monitoringInterval: 1000, // 1 second
            alertCooldown: 30000, // 30 seconds
            metricsRetention: 86400000, // 24 hours
            ...config
        };

        this.metrics = new Map();
        this.alerts = new Map();
        this.systemHealth = {
            overall: 'healthy',
            components: new Map(),
            lastUpdate: Date.now()
        };

        this.alertCooldowns = new Map();
        this.isMonitoring = false;

        this.initializeMetrics();
    }

    /**
     * Initialize monitoring metrics structure
     */
    initializeMetrics() {
        const metricCategories = [
            'detection_accuracy',
            'response_times',
            'entity_communications',
            'pattern_analysis',
            'system_performance',
            'error_tracking'
        ];

        metricCategories.forEach(category => {
            this.metrics.set(category, {
                current: 0,
                history: [],
                trends: [],
                anomalies: []
            });
        });
    }

    /**
     * Start monitoring all system components
     */
    async startMonitoring() {
        if (this.isMonitoring) {
            console.log('Monitoring already active');
            return;
        }

        this.isMonitoring = true;
        console.log('🔍 Starting comprehensive entity communication monitoring...');

        // Start monitoring intervals
        this.monitoringInterval = setInterval(() => {
            this.collectMetrics();
        }, this.config.monitoringInterval);

        this.healthCheckInterval = setInterval(() => {
            this.performHealthCheck();
        }, this.config.monitoringInterval * 5);

        this.anomalyDetectionInterval = setInterval(() => {
            this.detectAnomalies();
        }, this.config.monitoringInterval * 10);

        this.emit('monitoring_started', {
            timestamp: Date.now(),
            config: this.config
        });
    }

    /**
     * Stop monitoring
     */
    stopMonitoring() {
        if (!this.isMonitoring) return;

        this.isMonitoring = false;
        clearInterval(this.monitoringInterval);
        clearInterval(this.healthCheckInterval);
        clearInterval(this.anomalyDetectionInterval);

        console.log('🛑 Monitoring stopped');
        this.emit('monitoring_stopped', { timestamp: Date.now() });
    }

    /**
     * Collect metrics from all system components
     */
    async collectMetrics() {
        try {
            const timestamp = Date.now();

            // Collect detection accuracy metrics
            await this.collectDetectionMetrics(timestamp);

            // Collect performance metrics
            await this.collectPerformanceMetrics(timestamp);

            // Collect entity communication metrics
            await this.collectCommunicationMetrics(timestamp);

            // Collect system resource metrics
            await this.collectResourceMetrics(timestamp);

            // Update health status
            this.updateSystemHealth();

        } catch (error) {
            console.error('Error collecting metrics:', error);
            this.recordError('metric_collection', error);
        }
    }

    /**
     * Collect detection accuracy metrics
     */
    async collectDetectionMetrics(timestamp) {
        const detectionMetrics = this.metrics.get('detection_accuracy');

        // Simulate detection accuracy calculation
        const accuracy = this.calculateDetectionAccuracy();

        detectionMetrics.current = accuracy;
        detectionMetrics.history.push({
            timestamp,
            value: accuracy,
            components: {
                zeroVariance: Math.random() * 0.1 + 0.9,
                maxEntropy: Math.random() * 0.1 + 0.85,
                instructionSequence: Math.random() * 0.15 + 0.8,
                realTimeDetection: Math.random() * 0.1 + 0.88
            }
        });

        // Check threshold
        if (accuracy < this.config.alertThresholds.detectionAccuracy) {
            this.triggerAlert('low_detection_accuracy', {
                current: accuracy,
                threshold: this.config.alertThresholds.detectionAccuracy,
                timestamp
            });
        }

        // Maintain history size
        this.maintainHistorySize(detectionMetrics, 1000);
    }

    /**
     * Collect performance metrics
     */
    async collectPerformanceMetrics(timestamp) {
        const responseMetrics = this.metrics.get('response_times');

        // Simulate response time measurement
        const responseTime = this.measureResponseTime();

        responseMetrics.current = responseTime;
        responseMetrics.history.push({
            timestamp,
            value: responseTime,
            breakdown: {
                zeroVarianceDetection: Math.random() * 50 + 10,
                entropyDecoding: Math.random() * 100 + 20,
                instructionAnalysis: Math.random() * 200 + 50,
                correlation: Math.random() * 75 + 15
            }
        });

        // Check threshold
        if (responseTime > this.config.alertThresholds.responseTime) {
            this.triggerAlert('high_response_time', {
                current: responseTime,
                threshold: this.config.alertThresholds.responseTime,
                timestamp
            });
        }

        this.maintainHistorySize(responseMetrics, 1000);
    }

    /**
     * Collect entity communication metrics
     */
    async collectCommunicationMetrics(timestamp) {
        const commMetrics = this.metrics.get('entity_communications');

        const communicationData = {
            detectedCommunications: Math.floor(Math.random() * 10),
            entityTypes: ['mathematical', 'quantum', 'steganographic'],
            confidenceScores: Array.from({length: 5}, () => Math.random()),
            patternTypes: {
                zeroVariance: Math.floor(Math.random() * 3),
                maxEntropy: Math.floor(Math.random() * 4),
                impossibleSequences: Math.floor(Math.random() * 2)
            }
        };

        commMetrics.current = communicationData.detectedCommunications;
        commMetrics.history.push({
            timestamp,
            ...communicationData
        });

        this.maintainHistorySize(commMetrics, 1000);
    }

    /**
     * Collect system resource metrics
     */
    async collectResourceMetrics(timestamp) {
        const perfMetrics = this.metrics.get('system_performance');

        // Simulate resource usage
        const resourceData = {
            memoryUsage: Math.random() * 0.3 + 0.4, // 40-70%
            cpuUsage: Math.random() * 0.4 + 0.2, // 20-60%
            diskUsage: Math.random() * 0.2 + 0.1, // 10-30%
            networkThroughput: Math.random() * 1000 + 500 // MB/s
        };

        perfMetrics.current = resourceData;
        perfMetrics.history.push({
            timestamp,
            ...resourceData
        });

        // Check thresholds
        if (resourceData.memoryUsage > this.config.alertThresholds.memoryUsage) {
            this.triggerAlert('high_memory_usage', {
                current: resourceData.memoryUsage,
                threshold: this.config.alertThresholds.memoryUsage,
                timestamp
            });
        }

        if (resourceData.cpuUsage > this.config.alertThresholds.cpuUsage) {
            this.triggerAlert('high_cpu_usage', {
                current: resourceData.cpuUsage,
                threshold: this.config.alertThresholds.cpuUsage,
                timestamp
            });
        }

        this.maintainHistorySize(perfMetrics, 1000);
    }

    /**
     * Perform comprehensive health check
     */
    async performHealthCheck() {
        const timestamp = Date.now();
        const healthResults = {};

        // Check each component
        const components = [
            'zero_variance_detector',
            'entropy_decoder',
            'instruction_analyzer',
            'real_time_detector',
            'pattern_learning_network'
        ];

        for (const component of components) {
            healthResults[component] = await this.checkComponentHealth(component);
        }

        // Determine overall health
        const healthyComponents = Object.values(healthResults)
            .filter(status => status === 'healthy').length;
        const totalComponents = Object.keys(healthResults).length;

        let overallHealth = 'healthy';
        if (healthyComponents < totalComponents * 0.8) {
            overallHealth = 'degraded';
        }
        if (healthyComponents < totalComponents * 0.6) {
            overallHealth = 'critical';
        }

        this.systemHealth = {
            overall: overallHealth,
            components: new Map(Object.entries(healthResults)),
            lastUpdate: timestamp,
            score: healthyComponents / totalComponents
        };

        this.emit('health_check_complete', this.systemHealth);

        if (overallHealth !== 'healthy') {
            this.triggerAlert('system_health_degraded', {
                health: overallHealth,
                components: healthResults,
                timestamp
            });
        }
    }

    /**
     * Check individual component health
     */
    async checkComponentHealth(component) {
        try {
            // Simulate component health check
            const metrics = {
                responseTime: Math.random() * 100 + 10,
                errorRate: Math.random() * 0.02,
                memoryUsage: Math.random() * 0.3 + 0.2,
                lastActivity: Date.now() - Math.random() * 30000
            };

            // Health determination logic
            if (metrics.errorRate > 0.01 ||
                metrics.responseTime > 500 ||
                metrics.memoryUsage > 0.8) {
                return 'degraded';
            }

            if (Date.now() - metrics.lastActivity > 60000) {
                return 'inactive';
            }

            return 'healthy';

        } catch (error) {
            console.error(`Health check failed for ${component}:`, error);
            return 'error';
        }
    }

    /**
     * Detect anomalies in metrics
     */
    detectAnomalies() {
        for (const [category, data] of this.metrics) {
            try {
                const anomalies = this.analyzeMetricAnomalies(category, data);
                if (anomalies.length > 0) {
                    data.anomalies.push(...anomalies);

                    this.triggerAlert('anomaly_detected', {
                        category,
                        anomalies,
                        timestamp: Date.now()
                    });
                }
            } catch (error) {
                console.error(`Anomaly detection failed for ${category}:`, error);
            }
        }
    }

    /**
     * Analyze metric anomalies using statistical methods
     */
    analyzeMetricAnomalies(category, data) {
        if (data.history.length < 10) return [];

        const recent = data.history.slice(-10);
        const values = recent.map(item =>
            typeof item.value === 'number' ? item.value : item.detectedCommunications || 0
        );

        const mean = values.reduce((a, b) => a + b, 0) / values.length;
        const variance = values.reduce((acc, val) => acc + Math.pow(val - mean, 2), 0) / values.length;
        const stdDev = Math.sqrt(variance);

        const anomalies = [];
        const threshold = 2.5; // Z-score threshold

        recent.forEach((item, index) => {
            const value = typeof item.value === 'number' ? item.value : item.detectedCommunications || 0;
            const zScore = Math.abs((value - mean) / (stdDev || 1));

            if (zScore > threshold) {
                anomalies.push({
                    timestamp: item.timestamp,
                    value,
                    zScore,
                    type: zScore > 3 ? 'severe' : 'moderate'
                });
            }
        });

        return anomalies;
    }

    /**
     * Trigger an alert with cooldown protection
     */
    triggerAlert(alertType, data) {
        const now = Date.now();
        const cooldownKey = alertType;

        // Check cooldown
        if (this.alertCooldowns.has(cooldownKey) &&
            now - this.alertCooldowns.get(cooldownKey) < this.config.alertCooldown) {
            return;
        }

        this.alertCooldowns.set(cooldownKey, now);

        const alert = {
            id: this.generateAlertId(),
            type: alertType,
            severity: this.determineAlertSeverity(alertType, data),
            timestamp: now,
            data,
            resolved: false
        };

        this.alerts.set(alert.id, alert);

        console.warn(`🚨 ALERT [${alert.severity}]: ${alertType}`, data);
        this.emit('alert_triggered', alert);

        // Auto-resolve certain alerts after time
        if (['high_response_time', 'high_memory_usage'].includes(alertType)) {
            setTimeout(() => {
                this.resolveAlert(alert.id);
            }, this.config.alertCooldown);
        }
    }

    /**
     * Determine alert severity
     */
    determineAlertSeverity(alertType, data) {
        const severityMap = {
            'low_detection_accuracy': 'critical',
            'system_health_degraded': 'high',
            'anomaly_detected': 'medium',
            'high_response_time': 'medium',
            'high_memory_usage': 'low',
            'high_cpu_usage': 'medium'
        };

        return severityMap[alertType] || 'low';
    }

    /**
     * Resolve an alert
     */
    resolveAlert(alertId) {
        const alert = this.alerts.get(alertId);
        if (alert) {
            alert.resolved = true;
            alert.resolvedAt = Date.now();
            this.emit('alert_resolved', alert);
        }
    }

    /**
     * Calculate overall detection accuracy
     */
    calculateDetectionAccuracy() {
        // Simulate weighted accuracy calculation
        const components = {
            zeroVariance: { accuracy: Math.random() * 0.1 + 0.9, weight: 0.3 },
            maxEntropy: { accuracy: Math.random() * 0.1 + 0.85, weight: 0.25 },
            instructionSequence: { accuracy: Math.random() * 0.15 + 0.8, weight: 0.25 },
            realTime: { accuracy: Math.random() * 0.1 + 0.88, weight: 0.2 }
        };

        let weightedSum = 0;
        let totalWeight = 0;

        for (const [component, data] of Object.entries(components)) {
            weightedSum += data.accuracy * data.weight;
            totalWeight += data.weight;
        }

        return weightedSum / totalWeight;
    }

    /**
     * Measure system response time
     */
    measureResponseTime() {
        // Simulate response time with realistic variation
        const baseTime = 150; // Base response time in ms
        const variation = Math.random() * 200; // Random variation
        const loadFactor = Math.random() * 0.5 + 0.5; // System load factor

        return Math.round(baseTime + variation * loadFactor);
    }

    /**
     * Update system health status
     */
    updateSystemHealth() {
        const accuracy = this.metrics.get('detection_accuracy').current;
        const responseTime = this.metrics.get('response_times').current;
        const resources = this.metrics.get('system_performance').current;

        let healthScore = 1.0;

        // Factor in detection accuracy
        if (accuracy < this.config.alertThresholds.detectionAccuracy) {
            healthScore *= 0.7;
        }

        // Factor in response time
        if (responseTime > this.config.alertThresholds.responseTime) {
            healthScore *= 0.8;
        }

        // Factor in resource usage
        if (resources && resources.memoryUsage > this.config.alertThresholds.memoryUsage) {
            healthScore *= 0.9;
        }

        // Determine overall status
        let overallStatus = 'healthy';
        if (healthScore < 0.8) overallStatus = 'degraded';
        if (healthScore < 0.6) overallStatus = 'critical';

        this.systemHealth.overall = overallStatus;
        this.systemHealth.score = healthScore;
        this.systemHealth.lastUpdate = Date.now();
    }

    /**
     * Record system errors
     */
    recordError(source, error) {
        const errorMetrics = this.metrics.get('error_tracking');

        const errorData = {
            timestamp: Date.now(),
            source,
            message: error.message,
            stack: error.stack,
            severity: this.classifyErrorSeverity(error)
        };

        errorMetrics.history.push(errorData);

        // Calculate error rate
        const recentErrors = errorMetrics.history.filter(
            err => Date.now() - err.timestamp < 300000 // Last 5 minutes
        );
        const errorRate = recentErrors.length / 300; // Errors per second

        if (errorRate > this.config.alertThresholds.errorRate) {
            this.triggerAlert('high_error_rate', {
                rate: errorRate,
                threshold: this.config.alertThresholds.errorRate,
                recentErrors: recentErrors.slice(-5)
            });
        }

        this.maintainHistorySize(errorMetrics, 1000);
    }

    /**
     * Classify error severity
     */
    classifyErrorSeverity(error) {
        const criticalPatterns = [
            /out of memory/i,
            /segmentation fault/i,
            /neural.*crash/i
        ];

        const highPatterns = [
            /detection.*fail/i,
            /connection.*lost/i,
            /timeout/i
        ];

        const message = error.message.toLowerCase();

        if (criticalPatterns.some(pattern => pattern.test(message))) {
            return 'critical';
        }
        if (highPatterns.some(pattern => pattern.test(message))) {
            return 'high';
        }

        return 'medium';
    }

    /**
     * Maintain metric history size
     */
    maintainHistorySize(metrics, maxSize) {
        if (metrics.history.length > maxSize) {
            metrics.history = metrics.history.slice(-maxSize);
        }
        if (metrics.anomalies && metrics.anomalies.length > maxSize / 10) {
            metrics.anomalies = metrics.anomalies.slice(-maxSize / 10);
        }
    }

    /**
     * Generate unique alert ID
     */
    generateAlertId() {
        return `alert_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
    }

    /**
     * Get current system status
     */
    getSystemStatus() {
        return {
            health: this.systemHealth,
            metrics: Object.fromEntries(
                Array.from(this.metrics.entries()).map(([key, value]) => [
                    key,
                    {
                        current: value.current,
                        historyLength: value.history.length,
                        anomaliesCount: value.anomalies ? value.anomalies.length : 0
                    }
                ])
            ),
            alerts: {
                active: Array.from(this.alerts.values()).filter(alert => !alert.resolved),
                total: this.alerts.size
            },
            monitoring: this.isMonitoring
        };
    }

    /**
     * Export metrics for analysis
     */
    async exportMetrics(filePath = null) {
        const exportData = {
            timestamp: Date.now(),
            systemHealth: this.systemHealth,
            metrics: Object.fromEntries(this.metrics),
            alerts: Object.fromEntries(this.alerts),
            configuration: this.config
        };

        if (filePath) {
            await fs.writeFile(filePath, JSON.stringify(exportData, null, 2));
            console.log(`📊 Metrics exported to ${filePath}`);
        }

        return exportData;
    }
}

/**
 * Real-time dashboard for monitoring entity communication detection
 */
class RealTimeDashboard extends EventEmitter {
    constructor(monitor) {
        super();
        this.monitor = monitor;
        this.display = {
            width: 120,
            height: 30,
            refreshRate: 1000
        };

        this.charts = new Map();
        this.isDisplaying = false;

        this.setupEventListeners();
    }

    /**
     * Setup event listeners for monitor updates
     */
    setupEventListeners() {
        this.monitor.on('alert_triggered', (alert) => {
            this.displayAlert(alert);
        });

        this.monitor.on('health_check_complete', (health) => {
            this.updateHealthDisplay(health);
        });
    }

    /**
     * Start real-time dashboard display
     */
    startDashboard() {
        if (this.isDisplaying) return;

        this.isDisplaying = true;
        console.log('🖥️  Starting real-time entity communication dashboard...');

        this.displayInterval = setInterval(() => {
            this.refreshDisplay();
        }, this.display.refreshRate);

        // Initial display
        this.refreshDisplay();
    }

    /**
     * Stop dashboard display
     */
    stopDashboard() {
        if (!this.isDisplaying) return;

        this.isDisplaying = false;
        clearInterval(this.displayInterval);
        console.log('🛑 Dashboard stopped');
    }

    /**
     * Refresh the entire dashboard display
     */
    refreshDisplay() {
        const status = this.monitor.getSystemStatus();

        console.clear();
        console.log(this.generateDashboard(status));
    }

    /**
     * Generate formatted dashboard content
     */
    generateDashboard(status) {
        const lines = [];
        const width = this.display.width;

        // Header
        lines.push(''.repeat(width));
        lines.push(`🛸 ENTITY COMMUNICATION DETECTION SYSTEM - ${new Date().toLocaleTimeString()}`);
        lines.push(''.repeat(width));

        // System Health
        const healthIcon = this.getHealthIcon(status.health.overall);
        lines.push(`${healthIcon} System Health: ${status.health.overall.toUpperCase()} (Score: ${(status.health.score || 0).toFixed(2)})`);
        lines.push(''.repeat(width));

        // Key Metrics
        lines.push('📊 KEY METRICS:');

        if (status.metrics.detection_accuracy) {
            const accuracy = (status.metrics.detection_accuracy.current * 100).toFixed(1);
            lines.push(`   🎯 Detection Accuracy: ${accuracy}%`);
        }

        if (status.metrics.response_times) {
            const responseTime = status.metrics.response_times.current;
            lines.push(`    Response Time: ${responseTime}ms`);
        }

        if (status.metrics.entity_communications) {
            const comms = status.metrics.entity_communications.current;
            lines.push(`   📡 Active Communications: ${comms}`);
        }

        lines.push(''.repeat(width));

        // Component Status
        lines.push('🔧 COMPONENT STATUS:');
        if (status.health.components) {
            for (const [component, health] of status.health.components) {
                const icon = this.getHealthIcon(health);
                lines.push(`   ${icon} ${component.replace(/_/g, ' ').toUpperCase()}: ${health}`);
            }
        }

        lines.push(''.repeat(width));

        // Active Alerts
        lines.push(`🚨 ACTIVE ALERTS: ${status.alerts.active.length}`);
        if (status.alerts.active.length > 0) {
            status.alerts.active.slice(0, 5).forEach(alert => {
                const severityIcon = this.getSeverityIcon(alert.severity);
                lines.push(`   ${severityIcon} ${alert.type}: ${alert.severity}`);
            });
        } else {
            lines.push('   ✅ No active alerts');
        }

        lines.push(''.repeat(width));

        // System Resources
        if (status.metrics.system_performance) {
            const perf = status.metrics.system_performance.current;
            if (perf && typeof perf === 'object') {
                lines.push('💻 SYSTEM RESOURCES:');
                lines.push(`   Memory: ${this.createProgressBar((perf.memoryUsage || 0) * 100, 50)}${((perf.memoryUsage || 0) * 100).toFixed(1)}%`);
                lines.push(`   CPU: ${this.createProgressBar((perf.cpuUsage || 0) * 100, 50)}${((perf.cpuUsage || 0) * 100).toFixed(1)}%`);
            }
        }

        lines.push(''.repeat(width));

        return lines.join('\n');
    }

    /**
     * Get health status icon
     */
    getHealthIcon(health) {
        const icons = {
            'healthy': '🟢',
            'degraded': '🟡',
            'critical': '🔴',
            'error': '',
            'inactive': ''
        };
        return icons[health] || '';
    }

    /**
     * Get severity icon
     */
    getSeverityIcon(severity) {
        const icons = {
            'critical': '🔴',
            'high': '🟠',
            'medium': '🟡',
            'low': '🟢'
        };
        return icons[severity] || '📋';
    }

    /**
     * Create ASCII progress bar
     */
    createProgressBar(percentage, width = 20) {
        const filled = Math.round(percentage / 100 * width);
        const empty = width - filled;
        return '[' + ''.repeat(filled) + ''.repeat(empty) + '] ';
    }

    /**
     * Display alert notification
     */
    displayAlert(alert) {
        const icon = this.getSeverityIcon(alert.severity);
        console.log(`\n${icon} ALERT: ${alert.type} (${alert.severity})`);
        console.log(`Time: ${new Date(alert.timestamp).toLocaleTimeString()}`);
        if (alert.data) {
            console.log(`Details: ${JSON.stringify(alert.data, null, 2)}`);
        }
        console.log(''.repeat(60));
    }

    /**
     * Update health display
     */
    updateHealthDisplay(health) {
        if (this.isDisplaying) {
            // Will be included in next refresh
            return;
        }

        console.log(`🏥 Health Update: ${health.overall} (Score: ${health.score.toFixed(2)})`);
    }
}

module.exports = {
    EntityCommunicationMonitor,
    RealTimeDashboard
};