-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapp.py.backup
More file actions
1271 lines (1099 loc) · 43.5 KB
/
app.py.backup
File metadata and controls
1271 lines (1099 loc) · 43.5 KB
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
#!/usr/bin/env python3
"""
XMRT-Ecosystem Enhanced Autonomous Learning System
==================================================
Advanced Flask Application with comprehensive AI agent coordination,
real-time monitoring, blockchain integration, and autonomous learning capabilities.
Features:
- Multi-agent coordination system
- Real-time WebSocket monitoring dashboard
- Blockchain and Web3 integration
- Advanced memory and learning systems
- GitHub automation and repository management
- AI-powered code analysis and optimization
- Deployment automation and health monitoring
- Security and error handling
- Performance optimization and caching
Version: 2.0 Enhanced
Author: XMRT Ecosystem Team
License: MIT
"""
import os
import sys
import json
import logging
import asyncio
import threading
import time
from datetime import datetime, timedelta
from typing import Dict, List, Optional, Any
from dataclasses import dataclass, asdict
from contextlib import asynccontextmanager
import traceback
# Core Flask and Web Framework
from flask import Flask, render_template_string, jsonify, request, abort, make_response
from flask_socketio import SocketIO, emit, join_room, leave_room
from flask_cors import CORS
import eventlet
# Enhanced Async Support
import aiohttp
import asyncio
from concurrent.futures import ThreadPoolExecutor
# Database and Caching
import redis
from cachetools import TTLCache, LRUCache
# Monitoring and Performance
import psutil
from prometheus_client import Counter, Histogram, Gauge, generate_latest
import structlog
# Security and Authentication
from cryptography.fernet import Fernet
import jwt
from functools import wraps
# Import our enhanced autonomous system components
try:
from autonomous_controller import AutonomousController
from multi_agent_system import MultiAgentSystem
from github_manager import GitHubManager
from memory_system import MemorySystem
from learning_optimizer import LearningOptimizer
from performance_analyzer import PerformanceAnalyzer
from web3_dapp_factory import Web3DAppFactory
from analytics_system import AnalyticsSystem
except ImportError as e:
print(f"Warning: Some advanced modules not available: {e}")
# Enhanced Configuration Management
@dataclass
class SystemConfig:
"""Enhanced system configuration with validation"""
secret_key: str
github_token: str
openai_api_key: str = ""
anthropic_api_key: str = ""
redis_url: str = "redis://localhost:6379"
debug_mode: bool = False
max_agents: int = 10
learning_interval: int = 300 # 5 minutes
blockchain_enabled: bool = True
analytics_enabled: bool = True
security_enabled: bool = True
def __post_init__(self):
"""Validate configuration"""
if not self.secret_key:
raise ValueError("SECRET_KEY is required")
if not self.github_token:
raise ValueError("GITHUB_TOKEN is required")
# Enhanced Logging Configuration
def setup_enhanced_logging():
"""Setup structured logging with multiple outputs"""
structlog.configure(
processors=[
structlog.stdlib.filter_by_level,
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.UnicodeDecoder(),
structlog.processors.JSONRenderer()
],
context_class=dict,
logger_factory=structlog.stdlib.LoggerFactory(),
cache_logger_on_first_use=True,
)
logging.basicConfig(
format="%(message)s",
stream=sys.stdout,
level=logging.INFO,
)
# Initialize enhanced logging
setup_enhanced_logging()
logger = structlog.get_logger(__name__)
# Prometheus Metrics
REQUEST_COUNT = Counter('xmrt_requests_total', 'Total requests', ['method', 'endpoint'])
REQUEST_LATENCY = Histogram('xmrt_request_duration_seconds', 'Request latency')
AGENT_COUNT = Gauge('xmrt_active_agents', 'Number of active agents')
SYSTEM_HEALTH = Gauge('xmrt_system_health', 'System health score (0-1)')
# Enhanced JSON Encoder with datetime and complex object support
class EnhancedJSONEncoder(json.JSONEncoder):
"""Advanced JSON encoder handling various Python objects"""
def default(self, obj):
if isinstance(obj, datetime):
return obj.isoformat()
elif isinstance(obj, set):
return list(obj)
elif hasattr(obj, '__dict__'):
return obj.__dict__
elif hasattr(obj, '_asdict'): # namedtuple
return obj._asdict()
return super().default(obj)
# Load configuration from environment
def load_config() -> SystemConfig:
"""Load and validate system configuration"""
try:
config = SystemConfig(
secret_key=os.environ.get('SECRET_KEY', 'xmrt-ecosystem-enhanced-key-2024'),
github_token=os.environ.get('GITHUB_TOKEN', ''),
openai_api_key=os.environ.get('OPENAI_API_KEY', ''),
anthropic_api_key=os.environ.get('ANTHROPIC_API_KEY', ''),
redis_url=os.environ.get('REDIS_URL', 'redis://localhost:6379'),
debug_mode=os.environ.get('DEBUG', 'False').lower() == 'true',
max_agents=int(os.environ.get('MAX_AGENTS', '10')),
learning_interval=int(os.environ.get('LEARNING_INTERVAL', '300')),
blockchain_enabled=os.environ.get('BLOCKCHAIN_ENABLED', 'True').lower() == 'true',
analytics_enabled=os.environ.get('ANALYTICS_ENABLED', 'True').lower() == 'true',
security_enabled=os.environ.get('SECURITY_ENABLED', 'True').lower() == 'true'
)
logger.info("Configuration loaded successfully", config=asdict(config))
return config
except Exception as e:
logger.error("Failed to load configuration", error=str(e))
raise
# Initialize configuration
config = load_config()
# Initialize Flask app with enhanced configuration
app = Flask(__name__)
app.config.update({
'SECRET_KEY': config.secret_key,
'JSON_ENCODER': EnhancedJSONEncoder,
'MAX_CONTENT_LENGTH': 100 * 1024 * 1024, # 100MB max file size
'JSONIFY_PRETTYPRINT_REGULAR': config.debug_mode,
})
# Enable CORS for cross-origin requests
CORS(app, origins=["*"], methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"])
# Initialize Redis cache
redis_client = None
try:
redis_client = redis.from_url(config.redis_url, decode_responses=True)
redis_client.ping()
logger.info("Redis connection established")
except Exception as e:
logger.warning("Redis not available, using memory cache", error=str(e))
redis_client = None
# Memory caches
status_cache = TTLCache(maxsize=1000, ttl=60) # 1-minute TTL
metrics_cache = LRUCache(maxsize=500)
# Enhanced SocketIO initialization with better error handling
def initialize_socketio():
"""Initialize SocketIO with fallback modes"""
socket_configs = [
{'async_mode': 'gevent', 'logger': False, 'engineio_logger': False},
{'async_mode': 'eventlet', 'logger': False, 'engineio_logger': False},
{'async_mode': 'threading', 'logger': False, 'engineio_logger': False}
]
for i, socket_config in enumerate(socket_configs):
try:
socketio = SocketIO(
app,
cors_allowed_origins="*",
ping_timeout=60,
ping_interval=25,
**socket_config
)
logger.info("SocketIO initialized", mode=socket_config['async_mode'])
return socketio
except Exception as e:
logger.warning("SocketIO mode failed", mode=socket_config['async_mode'], error=str(e))
if i == len(socket_configs) - 1:
raise RuntimeError("All SocketIO modes failed")
socketio = initialize_socketio()
# Enhanced System Status with comprehensive metrics
@dataclass
class SystemStatus:
"""Comprehensive system status tracking"""
status: str = 'initializing'
last_cycle: Optional[datetime] = None
total_cycles: int = 0
repositories_managed: int = 0
last_commit: Optional[str] = None
agents_active: bool = False
agents_count: int = 0
memory_usage: float = 0.0
cpu_usage: float = 0.0
uptime: float = 0.0
errors_count: int = 0
warnings_count: int = 0
learning_progress: Dict[str, Any] = None
blockchain_status: str = 'disabled'
ai_models_active: List[str] = None
performance_score: float = 0.0
security_status: str = 'unknown'
last_update: datetime = None
def __post_init__(self):
if self.learning_progress is None:
self.learning_progress = {}
if self.ai_models_active is None:
self.ai_models_active = []
if self.last_update is None:
self.last_update = datetime.now()
# Global system state
system_status = SystemStatus()
autonomous_system = None
start_time = datetime.now()
thread_pool = ThreadPoolExecutor(max_workers=4)
# Security utilities
def generate_api_key():
"""Generate secure API key"""
return Fernet.generate_key().decode()
def verify_api_key(f):
"""Decorator to verify API key for protected endpoints"""
@wraps(f)
def decorated_function(*args, **kwargs):
if not config.security_enabled:
return f(*args, **kwargs)
api_key = request.headers.get('X-API-Key')
if not api_key or api_key != os.environ.get('XMRT_API_KEY'):
abort(401)
return f(*args, **kwargs)
return decorated_function
# Performance monitoring
def update_system_metrics():
"""Update comprehensive system metrics"""
try:
# System metrics
system_status.memory_usage = psutil.virtual_memory().percent
system_status.cpu_usage = psutil.cpu_percent(interval=1)
system_status.uptime = (datetime.now() - start_time).total_seconds()
# Update Prometheus metrics
AGENT_COUNT.set(system_status.agents_count)
# Calculate health score
health_factors = {
'cpu': max(0, 1 - system_status.cpu_usage / 100),
'memory': max(0, 1 - system_status.memory_usage / 100),
'agents': 1 if system_status.agents_active else 0,
'errors': max(0, 1 - system_status.errors_count / 100)
}
system_status.performance_score = sum(health_factors.values()) / len(health_factors)
SYSTEM_HEALTH.set(system_status.performance_score)
system_status.last_update = datetime.now()
except Exception as e:
logger.error("Failed to update system metrics", error=str(e))
system_status.errors_count += 1
# Enhanced autonomous system initialization
async def initialize_autonomous_system():
"""Initialize the enhanced autonomous learning system"""
global autonomous_system, system_status
try:
logger.info("🤖 Initializing XMRT Enhanced Autonomous Learning System...")
if not config.github_token:
raise ValueError("GITHUB_TOKEN environment variable not set!")
# Initialize core components with enhanced features
components = {}
# GitHub Manager with advanced features
components['github'] = GitHubManager(config.github_token)
# Multi-agent system with coordination
components['agents'] = MultiAgentSystem(
max_agents=config.max_agents,
learning_interval=config.learning_interval
)
# Memory system with persistent storage
components['memory'] = MemorySystem(redis_client=redis_client)
# Learning optimizer
if 'LearningOptimizer' in globals():
components['optimizer'] = LearningOptimizer()
# Performance analyzer
if 'PerformanceAnalyzer' in globals():
components['analyzer'] = PerformanceAnalyzer()
# Web3 integration if enabled
if config.blockchain_enabled and 'Web3DAppFactory' in globals():
components['web3'] = Web3DAppFactory()
system_status.blockchain_status = 'active'
# Analytics system
if config.analytics_enabled and 'AnalyticsSystem' in globals():
components['analytics'] = AnalyticsSystem()
# Create enhanced autonomous controller
autonomous_system = AutonomousController(
github_token=config.github_token,
components=components
)
# Start autonomous learning cycles
await autonomous_system.start_autonomous_learning()
# Update system status
system_status.status = 'running'
system_status.agents_active = True
system_status.agents_count = len(components.get('agents', {}).get('active_agents', []))
system_status.ai_models_active = ['GPT-4', 'Claude-3', 'Local-LLM']
system_status.security_status = 'active' if config.security_enabled else 'disabled'
logger.info("✅ Enhanced autonomous learning system initialized successfully!")
# Start background tasks
asyncio.create_task(background_health_monitor())
except Exception as e:
logger.error("❌ Error initializing autonomous system", error=str(e), traceback=traceback.format_exc())
system_status.status = 'error'
system_status.errors_count += 1
raise
async def background_health_monitor():
"""Background task for continuous health monitoring"""
while True:
try:
update_system_metrics()
# Emit real-time updates via WebSocket
if socketio:
socketio.emit('system_update', clean_system_status_for_json(asdict(system_status)))
await asyncio.sleep(30) # Update every 30 seconds
except Exception as e:
logger.error("Health monitor error", error=str(e))
system_status.errors_count += 1
await asyncio.sleep(60) # Longer delay on error
# Enhanced Web Interface with modern dashboard
ENHANCED_WEB_INTERFACE = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>XMRT-Ecosystem Enhanced Dashboard</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.7.2/socket.io.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.9.1/chart.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
color: #ffffff;
overflow-x: hidden;
}
.header {
background: rgba(0, 0, 0, 0.3);
padding: 1rem 2rem;
display: flex;
justify-content: space-between;
align-items: center;
backdrop-filter: blur(10px);
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.logo {
font-size: 1.8rem;
font-weight: bold;
color: #00d4ff;
text-shadow: 0 0 20px rgba(0, 212, 255, 0.5);
}
.status-indicator {
display: flex;
align-items: center;
gap: 0.5rem;
padding: 0.5rem 1rem;
border-radius: 25px;
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
}
.status-dot {
width: 12px;
height: 12px;
border-radius: 50%;
animation: pulse 2s infinite;
}
.status-running { background-color: #00ff88; }
.status-error { background-color: #ff4757; }
.status-initializing { background-color: #ffa502; }
@keyframes pulse {
0% { transform: scale(1); opacity: 1; }
50% { transform: scale(1.2); opacity: 0.7; }
100% { transform: scale(1); opacity: 1; }
}
.dashboard {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 2rem;
padding: 2rem;
max-width: 1400px;
margin: 0 auto;
}
.card {
background: rgba(255, 255, 255, 0.1);
border-radius: 15px;
padding: 1.5rem;
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.2);
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
}
.card-header {
display: flex;
align-items: center;
gap: 0.5rem;
margin-bottom: 1rem;
font-size: 1.2rem;
font-weight: 600;
}
.card-icon {
font-size: 1.5rem;
color: #00d4ff;
}
.metric {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0.75rem 0;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.metric:last-child {
border-bottom: none;
}
.metric-value {
font-weight: bold;
color: #00ff88;
}
.chart-container {
height: 200px;
margin-top: 1rem;
}
.log-container {
max-height: 300px;
overflow-y: auto;
background: rgba(0, 0, 0, 0.3);
border-radius: 8px;
padding: 1rem;
font-family: 'Courier New', monospace;
font-size: 0.9rem;
}
.log-entry {
margin-bottom: 0.5rem;
padding: 0.25rem;
border-radius: 4px;
}
.log-info { background: rgba(0, 123, 255, 0.2); }
.log-warning { background: rgba(255, 193, 7, 0.2); }
.log-error { background: rgba(220, 53, 69, 0.2); }
.progress-bar {
width: 100%;
height: 8px;
background: rgba(255, 255, 255, 0.2);
border-radius: 4px;
overflow: hidden;
margin-top: 0.5rem;
}
.progress-fill {
height: 100%;
background: linear-gradient(90deg, #00d4ff, #00ff88);
transition: width 0.3s ease;
}
.agent-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
gap: 1rem;
margin-top: 1rem;
}
.agent-card {
background: rgba(255, 255, 255, 0.1);
border-radius: 8px;
padding: 1rem;
text-align: center;
transition: all 0.3s ease;
}
.agent-card:hover {
background: rgba(255, 255, 255, 0.2);
}
.agent-status {
font-size: 2rem;
margin-bottom: 0.5rem;
}
.controls {
display: flex;
gap: 1rem;
margin-top: 1rem;
}
.btn {
padding: 0.75rem 1.5rem;
border: none;
border-radius: 25px;
background: linear-gradient(45deg, #00d4ff, #00ff88);
color: white;
font-weight: bold;
cursor: pointer;
transition: all 0.3s ease;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 10px 20px rgba(0, 212, 255, 0.3);
}
.blockchain-status {
display: flex;
align-items: center;
gap: 1rem;
padding: 1rem;
background: rgba(255, 255, 255, 0.1);
border-radius: 8px;
margin-top: 1rem;
}
.network-indicator {
width: 40px;
height: 40px;
border-radius: 50%;
background: conic-gradient(from 0deg, #00d4ff, #00ff88, #ffa502, #00d4ff);
animation: rotate 3s linear infinite;
display: flex;
align-items: center;
justify-content: center;
}
@keyframes rotate {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.network-core {
width: 30px;
height: 30px;
background: rgba(0, 0, 0, 0.8);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
color: #00d4ff;
font-weight: bold;
}
@media (max-width: 768px) {
.dashboard {
grid-template-columns: 1fr;
padding: 1rem;
}
.header {
padding: 1rem;
flex-direction: column;
gap: 1rem;
}
}
</style>
</head>
<body>
<div class="header">
<div class="logo">
<i class="fas fa-robot"></i> XMRT-Ecosystem Enhanced
</div>
<div class="status-indicator">
<div class="status-dot status-initializing" id="statusDot"></div>
<span id="statusText">Initializing...</span>
</div>
</div>
<div class="dashboard">
<!-- System Overview Card -->
<div class="card">
<div class="card-header">
<i class="fas fa-tachometer-alt card-icon"></i>
System Overview
</div>
<div class="metric">
<span>Status:</span>
<span class="metric-value" id="systemStatus">Initializing</span>
</div>
<div class="metric">
<span>Uptime:</span>
<span class="metric-value" id="uptime">00:00:00</span>
</div>
<div class="metric">
<span>Total Cycles:</span>
<span class="metric-value" id="totalCycles">0</span>
</div>
<div class="metric">
<span>Repositories:</span>
<span class="metric-value" id="repositories">0</span>
</div>
<div class="metric">
<span>Performance Score:</span>
<span class="metric-value" id="performanceScore">0%</span>
</div>
<div class="progress-bar">
<div class="progress-fill" id="performanceBar" style="width: 0%"></div>
</div>
</div>
<!-- Agents Status Card -->
<div class="card">
<div class="card-header">
<i class="fas fa-users card-icon"></i>
AI Agents Status
</div>
<div class="metric">
<span>Active Agents:</span>
<span class="metric-value" id="activeAgents">0</span>
</div>
<div class="agent-grid" id="agentGrid">
<!-- Agent cards will be populated dynamically -->
</div>
</div>
<!-- Performance Metrics Card -->
<div class="card">
<div class="card-header">
<i class="fas fa-chart-line card-icon"></i>
Performance Metrics
</div>
<div class="chart-container">
<canvas id="performanceChart"></canvas>
</div>
</div>
<!-- System Resources Card -->
<div class="card">
<div class="card-header">
<i class="fas fa-server card-icon"></i>
System Resources
</div>
<div class="metric">
<span>CPU Usage:</span>
<span class="metric-value" id="cpuUsage">0%</span>
</div>
<div class="progress-bar">
<div class="progress-fill" id="cpuBar" style="width: 0%"></div>
</div>
<div class="metric">
<span>Memory Usage:</span>
<span class="metric-value" id="memoryUsage">0%</span>
</div>
<div class="progress-bar">
<div class="progress-fill" id="memoryBar" style="width: 0%"></div>
</div>
</div>
<!-- Blockchain Integration Card -->
<div class="card">
<div class="card-header">
<i class="fas fa-link card-icon"></i>
Blockchain Integration
</div>
<div class="blockchain-status">
<div class="network-indicator">
<div class="network-core">⚡</div>
</div>
<div>
<div>Status: <span class="metric-value" id="blockchainStatus">Disabled</span></div>
<div>Network: <span id="networkName">None</span></div>
</div>
</div>
</div>
<!-- Learning Progress Card -->
<div class="card">
<div class="card-header">
<i class="fas fa-brain card-icon"></i>
Learning Progress
</div>
<div class="metric">
<span>Learning Cycles:</span>
<span class="metric-value" id="learningCycles">0</span>
</div>
<div class="metric">
<span>Improvements Made:</span>
<span class="metric-value" id="improvements">0</span>
</div>
<div class="metric">
<span>Success Rate:</span>
<span class="metric-value" id="successRate">0%</span>
</div>
</div>
<!-- System Logs Card -->
<div class="card">
<div class="card-header">
<i class="fas fa-file-alt card-icon"></i>
System Logs
</div>
<div class="log-container" id="logContainer">
<div class="log-entry log-info">[INFO] System initializing...</div>
</div>
<div class="controls">
<button class="btn" onclick="clearLogs()"><i class="fas fa-trash"></i> Clear</button>
<button class="btn" onclick="downloadLogs()"><i class="fas fa-download"></i> Export</button>
</div>
</div>
<!-- AI Models Card -->
<div class="card">
<div class="card-header">
<i class="fas fa-robot card-icon"></i>
AI Models
</div>
<div id="aiModels">
<div class="metric">
<span>Available Models:</span>
<span class="metric-value" id="modelCount">0</span>
</div>
<div id="modelList"></div>
</div>
</div>
</div>
<script>
// Initialize Socket.IO connection
const socket = io();
// Chart.js setup for performance monitoring
const ctx = document.getElementById('performanceChart').getContext('2d');
const performanceChart = new Chart(ctx, {
type: 'line',
data: {
labels: [],
datasets: [{
label: 'Performance Score',
data: [],
borderColor: '#00d4ff',
backgroundColor: 'rgba(0, 212, 255, 0.1)',
tension: 0.4
}, {
label: 'CPU Usage',
data: [],
borderColor: '#ffa502',
backgroundColor: 'rgba(255, 165, 2, 0.1)',
tension: 0.4
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
labels: { color: '#ffffff' }
}
},
scales: {
x: {
ticks: { color: '#ffffff' },
grid: { color: 'rgba(255, 255, 255, 0.1)' }
},
y: {
ticks: { color: '#ffffff' },
grid: { color: 'rgba(255, 255, 255, 0.1)' },
min: 0,
max: 100
}
}
}
});
// Update dashboard with system data
function updateDashboard(data) {
// Update status indicator
const statusDot = document.getElementById('statusDot');
const statusText = document.getElementById('statusText');
statusDot.className = `status-dot status-${data.status}`;
statusText.textContent = data.status.charAt(0).toUpperCase() + data.status.slice(1);
// Update system overview
document.getElementById('systemStatus').textContent = data.status;
document.getElementById('uptime').textContent = formatUptime(data.uptime || 0);
document.getElementById('totalCycles').textContent = data.total_cycles || 0;
document.getElementById('repositories').textContent = data.repositories_managed || 0;
const perfScore = Math.round((data.performance_score || 0) * 100);
document.getElementById('performanceScore').textContent = `${perfScore}%`;
document.getElementById('performanceBar').style.width = `${perfScore}%`;
// Update agents
document.getElementById('activeAgents').textContent = data.agents_count || 0;
updateAgentGrid(data.ai_models_active || []);
// Update resources
const cpuUsage = Math.round(data.cpu_usage || 0);
const memoryUsage = Math.round(data.memory_usage || 0);
document.getElementById('cpuUsage').textContent = `${cpuUsage}%`;
document.getElementById('cpuBar').style.width = `${cpuUsage}%`;
document.getElementById('memoryUsage').textContent = `${memoryUsage}%`;
document.getElementById('memoryBar').style.width = `${memoryUsage}%`;
// Update blockchain status
document.getElementById('blockchainStatus').textContent = data.blockchain_status || 'Disabled';
// Update learning progress
const learningProgress = data.learning_progress || {};
document.getElementById('learningCycles').textContent = learningProgress.cycles || 0;
document.getElementById('improvements').textContent = learningProgress.improvements || 0;
document.getElementById('successRate').textContent = `${learningProgress.success_rate || 0}%`;
// Update AI models
updateAIModels(data.ai_models_active || []);
// Update performance chart
updatePerformanceChart(perfScore, cpuUsage);
// Add log entry
addLogEntry('info', `System update received - Status: ${data.status}`);
}
function updateAgentGrid(agents) {
const agentGrid = document.getElementById('agentGrid');
agentGrid.innerHTML = '';
agents.forEach((agent, index) => {
const agentCard = document.createElement('div');
agentCard.className = 'agent-card';
agentCard.innerHTML = `
<div class="agent-status">🤖</div>
<div>${agent}</div>
`;
agentGrid.appendChild(agentCard);
});
}
function updateAIModels(models) {
const modelCount = document.getElementById('modelCount');
const modelList = document.getElementById('modelList');
modelCount.textContent = models.length;
modelList.innerHTML = '';
models.forEach(model => {
const modelDiv = document.createElement('div');
modelDiv.className = 'metric';
modelDiv.innerHTML = `
<span>${model}:</span>
<span class="metric-value">Active</span>
`;
modelList.appendChild(modelDiv);
});
}
function updatePerformanceChart(performance, cpu) {
const now = new Date().toLocaleTimeString();
const chart = performanceChart;
// Keep only last 20 data points
if (chart.data.labels.length >= 20) {
chart.data.labels.shift();
chart.data.datasets[0].data.shift();
chart.data.datasets[1].data.shift();
}
chart.data.labels.push(now);
chart.data.datasets[0].data.push(performance);
chart.data.datasets[1].data.push(cpu);
chart.update('none');
}
function addLogEntry(level, message) {
const logContainer = document.getElementById('logContainer');
const timestamp = new Date().toLocaleTimeString();
const logEntry = document.createElement('div');
logEntry.className = `log-entry log-${level}`;
logEntry.textContent = `[${timestamp}] [${level.toUpperCase()}] ${message}`;
logContainer.appendChild(logEntry);
logContainer.scrollTop = logContainer.scrollHeight;
// Keep only last 50 log entries
while (logContainer.children.length > 50) {
logContainer.removeChild(logContainer.firstChild);
}
}
function formatUptime(seconds) {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const secs = Math.floor(seconds % 60);
return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
}
function clearLogs() {
document.getElementById('logContainer').innerHTML = '';
addLogEntry('info', 'Logs cleared');
}
function downloadLogs() {
const logs = document.getElementById('logContainer').innerText;
const blob = new Blob([logs], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `xmrt-logs-${new Date().toISOString().split('T')[0]}.txt`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);