gogo2/debug_dashboard_500.py
2025-05-26 16:02:40 +03:00

321 lines
12 KiB
Python

#!/usr/bin/env python3
"""
Debug Dashboard - Enhanced error logging to identify 500 errors
"""
import logging
import sys
import traceback
from pathlib import Path
from datetime import datetime
import pandas as pd
# Add project root to path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
import dash
from dash import dcc, html, Input, Output
import plotly.graph_objects as go
from core.config import setup_logging
from core.data_provider import DataProvider
# Setup logging without emojis
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(sys.stdout),
logging.FileHandler('debug_dashboard.log')
]
)
logger = logging.getLogger(__name__)
class DebugDashboard:
"""Debug dashboard with enhanced error logging"""
def __init__(self):
logger.info("Initializing debug dashboard...")
try:
self.data_provider = DataProvider()
logger.info("Data provider initialized successfully")
except Exception as e:
logger.error(f"Error initializing data provider: {e}")
logger.error(f"Traceback: {traceback.format_exc()}")
raise
# Initialize app
self.app = dash.Dash(__name__)
logger.info("Dash app created")
# Setup layout and callbacks
try:
self._setup_layout()
logger.info("Layout setup completed")
except Exception as e:
logger.error(f"Error setting up layout: {e}")
logger.error(f"Traceback: {traceback.format_exc()}")
raise
try:
self._setup_callbacks()
logger.info("Callbacks setup completed")
except Exception as e:
logger.error(f"Error setting up callbacks: {e}")
logger.error(f"Traceback: {traceback.format_exc()}")
raise
logger.info("Debug dashboard initialized successfully")
def _setup_layout(self):
"""Setup minimal layout for debugging"""
logger.info("Setting up layout...")
self.app.layout = html.Div([
html.H1("Debug Dashboard - 500 Error Investigation", className="text-center"),
# Simple metrics
html.Div([
html.Div([
html.H3(id="current-time", children="Loading..."),
html.P("Current Time")
], className="col-md-3"),
html.Div([
html.H3(id="update-counter", children="0"),
html.P("Update Count")
], className="col-md-3"),
html.Div([
html.H3(id="status", children="Starting..."),
html.P("Status")
], className="col-md-3"),
html.Div([
html.H3(id="error-count", children="0"),
html.P("Error Count")
], className="col-md-3")
], className="row mb-4"),
# Error log
html.Div([
html.H4("Error Log"),
html.Div(id="error-log", children="No errors yet...")
], className="mb-4"),
# Simple chart
html.Div([
dcc.Graph(id="debug-chart", style={"height": "300px"})
]),
# Interval component
dcc.Interval(
id='debug-interval',
interval=2000, # 2 seconds for easier debugging
n_intervals=0
)
], className="container-fluid")
logger.info("Layout setup completed")
def _setup_callbacks(self):
"""Setup callbacks with extensive error handling"""
logger.info("Setting up callbacks...")
# Store reference to self
dashboard_instance = self
error_count = 0
error_log = []
@self.app.callback(
[
Output('current-time', 'children'),
Output('update-counter', 'children'),
Output('status', 'children'),
Output('error-count', 'children'),
Output('error-log', 'children'),
Output('debug-chart', 'figure')
],
[Input('debug-interval', 'n_intervals')]
)
def update_debug_dashboard(n_intervals):
"""Debug callback with extensive error handling"""
nonlocal error_count, error_log
logger.info(f"=== CALLBACK START - Interval {n_intervals} ===")
try:
# Current time
current_time = datetime.now().strftime("%H:%M:%S")
logger.info(f"Current time: {current_time}")
# Update counter
counter = f"Updates: {n_intervals}"
logger.info(f"Counter: {counter}")
# Status
status = "Running OK" if n_intervals > 0 else "Starting"
logger.info(f"Status: {status}")
# Error count
error_count_str = f"Errors: {error_count}"
logger.info(f"Error count: {error_count_str}")
# Error log display
if error_log:
error_display = html.Div([
html.P(f"Error {i+1}: {error}", className="text-danger")
for i, error in enumerate(error_log[-5:]) # Show last 5 errors
])
else:
error_display = "No errors yet..."
# Create chart
logger.info("Creating chart...")
try:
chart = dashboard_instance._create_debug_chart(n_intervals)
logger.info("Chart created successfully")
except Exception as chart_error:
logger.error(f"Error creating chart: {chart_error}")
logger.error(f"Chart error traceback: {traceback.format_exc()}")
error_count += 1
error_log.append(f"Chart error: {str(chart_error)}")
chart = dashboard_instance._create_error_chart(str(chart_error))
logger.info("=== CALLBACK SUCCESS ===")
return current_time, counter, status, error_count_str, error_display, chart
except Exception as e:
error_count += 1
error_msg = f"Callback error: {str(e)}"
error_log.append(error_msg)
logger.error(f"=== CALLBACK ERROR ===")
logger.error(f"Error: {e}")
logger.error(f"Error type: {type(e)}")
logger.error(f"Traceback: {traceback.format_exc()}")
# Return safe fallback values
error_chart = dashboard_instance._create_error_chart(str(e))
error_display = html.Div([
html.P(f"CALLBACK ERROR: {str(e)}", className="text-danger"),
html.P(f"Error count: {error_count}", className="text-warning")
])
return "ERROR", f"Errors: {error_count}", "FAILED", f"Errors: {error_count}", error_display, error_chart
logger.info("Callbacks setup completed")
def _create_debug_chart(self, n_intervals):
"""Create a simple debug chart"""
logger.info(f"Creating debug chart for interval {n_intervals}")
try:
# Try to get real data every 5 intervals
if n_intervals % 5 == 0:
logger.info("Attempting to fetch real data...")
try:
df = self.data_provider.get_historical_data('ETH/USDT', '1m', limit=20)
if df is not None and not df.empty:
logger.info(f"Fetched {len(df)} real candles")
self.chart_data = df
else:
logger.warning("No real data returned")
except Exception as data_error:
logger.error(f"Error fetching real data: {data_error}")
logger.error(f"Data fetch traceback: {traceback.format_exc()}")
# Create chart
fig = go.Figure()
if hasattr(self, 'chart_data') and not self.chart_data.empty:
logger.info("Using real data for chart")
fig.add_trace(go.Scatter(
x=self.chart_data['timestamp'],
y=self.chart_data['close'],
mode='lines',
name='ETH/USDT Real',
line=dict(color='#00ff88')
))
title = f"ETH/USDT Real Data - Update #{n_intervals}"
else:
logger.info("Using mock data for chart")
# Simple mock data
x_data = list(range(max(0, n_intervals-10), n_intervals + 1))
y_data = [3500 + 50 * (i % 5) for i in x_data]
fig.add_trace(go.Scatter(
x=x_data,
y=y_data,
mode='lines',
name='Mock Data',
line=dict(color='#ff8800')
))
title = f"Mock Data - Update #{n_intervals}"
fig.update_layout(
title=title,
template="plotly_dark",
paper_bgcolor='#1e1e1e',
plot_bgcolor='#1e1e1e',
showlegend=False,
height=300
)
logger.info("Chart created successfully")
return fig
except Exception as e:
logger.error(f"Error in _create_debug_chart: {e}")
logger.error(f"Chart creation traceback: {traceback.format_exc()}")
raise
def _create_error_chart(self, error_msg):
"""Create error chart"""
logger.info(f"Creating error chart: {error_msg}")
fig = go.Figure()
fig.add_annotation(
text=f"Chart Error: {error_msg}",
xref="paper", yref="paper",
x=0.5, y=0.5, showarrow=False,
font=dict(size=14, color="#ff4444")
)
fig.update_layout(
template="plotly_dark",
paper_bgcolor='#1e1e1e',
plot_bgcolor='#1e1e1e',
height=300
)
return fig
def run(self, host='127.0.0.1', port=8053, debug=True):
"""Run the debug dashboard"""
logger.info(f"Starting debug dashboard at http://{host}:{port}")
logger.info("This dashboard has enhanced error logging to identify 500 errors")
try:
self.app.run(host=host, port=port, debug=debug)
except Exception as e:
logger.error(f"Error running dashboard: {e}")
logger.error(f"Run error traceback: {traceback.format_exc()}")
raise
def main():
"""Main function"""
logger.info("Starting debug dashboard main...")
try:
dashboard = DebugDashboard()
dashboard.run()
except KeyboardInterrupt:
logger.info("Dashboard stopped by user")
except Exception as e:
logger.error(f"Fatal error: {e}")
logger.error(f"Fatal traceback: {traceback.format_exc()}")
if __name__ == "__main__":
main()