TCPDashboard/dashboard/callbacks/system_health.py
2025-06-04 17:03:35 +08:00

96 lines
4.0 KiB
Python

"""
System health callbacks for the dashboard.
"""
from dash import Output, Input, html
from datetime import datetime
from utils.logger import get_logger
from database.connection import DatabaseManager
from components.charts import create_data_status_indicator, check_data_availability
logger = get_logger("default_logger")
def register_system_health_callbacks(app):
"""Register system health callbacks."""
@app.callback(
Output('database-status', 'children'),
Input('interval-component', 'n_intervals')
)
def update_database_status(n_intervals):
"""Update database connection status."""
try:
db_manager = DatabaseManager()
# Test database connection
with db_manager.get_session() as session:
# Simple query to test connection
result = session.execute("SELECT 1").fetchone()
if result:
return html.Div([
html.Span("🟢 Connected", style={'color': '#27ae60', 'font-weight': 'bold'}),
html.P(f"Last checked: {datetime.now().strftime('%H:%M:%S')}",
style={'margin': '5px 0', 'color': '#7f8c8d'})
])
else:
return html.Div([
html.Span("🔴 Connection Error", style={'color': '#e74c3c', 'font-weight': 'bold'})
])
except Exception as e:
logger.error(f"System health callback: Database status check failed: {e}")
return html.Div([
html.Span("🔴 Connection Failed", style={'color': '#e74c3c', 'font-weight': 'bold'}),
html.P(f"Error: {str(e)}", style={'color': '#7f8c8d', 'font-size': '12px'})
])
@app.callback(
Output('collection-status', 'children'),
[Input('symbol-dropdown', 'value'),
Input('timeframe-dropdown', 'value'),
Input('interval-component', 'n_intervals')]
)
def update_data_status(symbol, timeframe, n_intervals):
"""Update data collection status."""
try:
# Check real data availability
status = check_data_availability(symbol, timeframe)
return html.Div([
html.Div(
create_data_status_indicator(symbol, timeframe),
style={'margin': '10px 0'}
),
html.P(f"Checking data for {symbol} {timeframe}",
style={'color': '#7f8c8d', 'margin': '5px 0', 'font-style': 'italic'})
], style={'background-color': '#f8f9fa', 'padding': '15px', 'border-radius': '5px'})
except Exception as e:
logger.error(f"System health callback: Error updating data status: {e}")
return html.Div([
html.Span("🔴 Status Check Failed", style={'color': '#e74c3c', 'font-weight': 'bold'}),
html.P(f"Error: {str(e)}", style={'color': '#7f8c8d', 'margin': '5px 0'})
])
@app.callback(
Output('redis-status', 'children'),
Input('interval-component', 'n_intervals')
)
def update_redis_status(n_intervals):
"""Update Redis connection status."""
try:
# TODO: Implement Redis status check when Redis is integrated
return html.Div([
html.Span("🟡 Not Configured", style={'color': '#f39c12', 'font-weight': 'bold'}),
html.P("Redis integration pending", style={'color': '#7f8c8d', 'margin': '5px 0'})
])
except Exception as e:
logger.error(f"System health callback: Redis status check failed: {e}")
return html.Div([
html.Span("🔴 Check Failed", style={'color': '#e74c3c', 'font-weight': 'bold'}),
html.P(f"Error: {str(e)}", style={'color': '#7f8c8d', 'font-size': '12px'})
])
logger.info("System health callback: System health callbacks registered successfully")