cob integration wip 2
This commit is contained in:
173
run_simple_cob_dashboard.py
Normal file
173
run_simple_cob_dashboard.py
Normal file
@ -0,0 +1,173 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Simple COB Dashboard - Works without redundancies
|
||||
|
||||
Runs the COB dashboard using optimized shared resources.
|
||||
Fixed to work on Windows without unicode logging issues.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import signal
|
||||
import sys
|
||||
import os
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
# Local imports
|
||||
from core.cob_integration import COBIntegration
|
||||
from core.data_provider import DataProvider
|
||||
from web.cob_realtime_dashboard import COBDashboardServer
|
||||
|
||||
# Configure Windows-compatible logging (no emojis)
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||
handlers=[
|
||||
logging.FileHandler('logs/simple_cob_dashboard.log'),
|
||||
logging.StreamHandler(sys.stdout)
|
||||
]
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class SimpleCOBDashboard:
|
||||
"""Simple COB Dashboard without redundant implementations"""
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize simple COB dashboard"""
|
||||
self.data_provider = DataProvider()
|
||||
self.cob_integration: Optional[COBIntegration] = None
|
||||
self.dashboard_server: Optional[COBDashboardServer] = None
|
||||
self.running = False
|
||||
|
||||
# Setup signal handlers
|
||||
signal.signal(signal.SIGINT, self._signal_handler)
|
||||
signal.signal(signal.SIGTERM, self._signal_handler)
|
||||
|
||||
logger.info("SimpleCOBDashboard initialized")
|
||||
|
||||
def _signal_handler(self, signum, frame):
|
||||
"""Handle shutdown signals"""
|
||||
logger.info(f"Received signal {signum}, shutting down...")
|
||||
self.running = False
|
||||
|
||||
async def start(self):
|
||||
"""Start the simple COB dashboard"""
|
||||
try:
|
||||
logger.info("=" * 60)
|
||||
logger.info("SIMPLE COB DASHBOARD STARTING")
|
||||
logger.info("=" * 60)
|
||||
logger.info("Single COB integration - No redundancy")
|
||||
|
||||
# Initialize COB integration
|
||||
logger.info("Initializing COB integration...")
|
||||
self.cob_integration = COBIntegration(
|
||||
data_provider=self.data_provider,
|
||||
symbols=['BTC/USDT', 'ETH/USDT']
|
||||
)
|
||||
|
||||
# Start COB integration
|
||||
logger.info("Starting COB integration...")
|
||||
await self.cob_integration.start()
|
||||
|
||||
# Initialize dashboard with our COB integration
|
||||
logger.info("Initializing dashboard server...")
|
||||
self.dashboard_server = COBDashboardServer(host='localhost', port=8053)
|
||||
|
||||
# Use our COB integration (avoid creating duplicate)
|
||||
self.dashboard_server.cob_integration = self.cob_integration
|
||||
|
||||
# Start dashboard
|
||||
logger.info("Starting dashboard server...")
|
||||
await self.dashboard_server.start()
|
||||
|
||||
self.running = True
|
||||
|
||||
logger.info("SIMPLE COB DASHBOARD STARTED SUCCESSFULLY")
|
||||
logger.info("Dashboard available at: http://localhost:8053")
|
||||
logger.info("System Status: OPTIMIZED - No redundant implementations")
|
||||
logger.info("=" * 60)
|
||||
|
||||
# Keep running
|
||||
while self.running:
|
||||
await asyncio.sleep(10)
|
||||
|
||||
# Print periodic stats
|
||||
if hasattr(self, '_last_stats_time'):
|
||||
if (datetime.now() - self._last_stats_time).total_seconds() >= 300: # 5 minutes
|
||||
await self._print_stats()
|
||||
self._last_stats_time = datetime.now()
|
||||
else:
|
||||
self._last_stats_time = datetime.now()
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error in simple COB dashboard: {e}")
|
||||
import traceback
|
||||
logger.error(traceback.format_exc())
|
||||
raise
|
||||
finally:
|
||||
await self.stop()
|
||||
|
||||
async def _print_stats(self):
|
||||
"""Print simple statistics"""
|
||||
try:
|
||||
logger.info("Dashboard Status: RUNNING")
|
||||
|
||||
if self.dashboard_server:
|
||||
connections = len(self.dashboard_server.websocket_connections)
|
||||
logger.info(f"Active WebSocket connections: {connections}")
|
||||
|
||||
if self.cob_integration:
|
||||
stats = self.cob_integration.get_statistics()
|
||||
logger.info(f"COB Active Exchanges: {', '.join(stats.get('active_exchanges', []))}")
|
||||
logger.info(f"COB Streaming: {stats.get('is_streaming', False)}")
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"Error printing stats: {e}")
|
||||
|
||||
async def stop(self):
|
||||
"""Stop the dashboard gracefully"""
|
||||
if not self.running:
|
||||
return
|
||||
|
||||
logger.info("Stopping Simple COB Dashboard...")
|
||||
|
||||
self.running = False
|
||||
|
||||
# Stop dashboard
|
||||
if self.dashboard_server:
|
||||
await self.dashboard_server.stop()
|
||||
logger.info("Dashboard server stopped")
|
||||
|
||||
# Stop COB integration
|
||||
if self.cob_integration:
|
||||
await self.cob_integration.stop()
|
||||
logger.info("COB integration stopped")
|
||||
|
||||
logger.info("Simple COB Dashboard stopped successfully")
|
||||
|
||||
|
||||
async def main():
|
||||
"""Main entry point"""
|
||||
try:
|
||||
# Create logs directory
|
||||
os.makedirs('logs', exist_ok=True)
|
||||
|
||||
# Start simple dashboard
|
||||
dashboard = SimpleCOBDashboard()
|
||||
await dashboard.start()
|
||||
|
||||
except KeyboardInterrupt:
|
||||
logger.info("Received keyboard interrupt, shutting down...")
|
||||
except Exception as e:
|
||||
logger.error(f"Critical error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Set event loop policy for Windows compatibility
|
||||
if hasattr(asyncio, 'WindowsProactorEventLoopPolicy'):
|
||||
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
|
||||
|
||||
asyncio.run(main())
|
Reference in New Issue
Block a user