82 lines
2.8 KiB
Python
82 lines
2.8 KiB
Python
|
|
from dash import Output, Input, html
|
||
|
|
import dash_bootstrap_components as dbc
|
||
|
|
from utils.logger import get_logger
|
||
|
|
from database.redis_manager import get_sync_redis_manager
|
||
|
|
|
||
|
|
logger = get_logger("default_logger")
|
||
|
|
|
||
|
|
|
||
|
|
def register_redis_callbacks(app):
|
||
|
|
"""Register Redis status and statistics callbacks."""
|
||
|
|
|
||
|
|
# Redis Status and Statistics
|
||
|
|
@app.callback(
|
||
|
|
[Output('redis-status', 'children'),
|
||
|
|
Output('redis-stats', 'children')],
|
||
|
|
Input('interval-component', 'n_intervals')
|
||
|
|
)
|
||
|
|
def update_redis_status(n_intervals):
|
||
|
|
"""Update Redis connection status and statistics."""
|
||
|
|
try:
|
||
|
|
redis_status = _get_redis_status()
|
||
|
|
redis_stats = _get_redis_statistics()
|
||
|
|
|
||
|
|
return redis_status, redis_stats
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"Error updating Redis status: {e}")
|
||
|
|
error_alert = dbc.Alert(
|
||
|
|
f"Error: {str(e)}",
|
||
|
|
color="danger",
|
||
|
|
dismissable=True
|
||
|
|
)
|
||
|
|
return error_alert, error_alert
|
||
|
|
|
||
|
|
|
||
|
|
def _get_redis_status() -> html.Div:
|
||
|
|
"""Get detailed Redis server status."""
|
||
|
|
try:
|
||
|
|
redis_manager = get_sync_redis_manager()
|
||
|
|
redis_manager.initialize()
|
||
|
|
|
||
|
|
if not redis_manager.client.ping():
|
||
|
|
raise ConnectionError("Redis server is not responding.")
|
||
|
|
|
||
|
|
info = redis_manager.client.info()
|
||
|
|
status_badge = dbc.Badge("Connected", color="success", className="me-1")
|
||
|
|
|
||
|
|
return html.Div([
|
||
|
|
html.H5("Redis Status"),
|
||
|
|
status_badge,
|
||
|
|
html.P(f"Version: {info.get('redis_version', 'N/A')}"),
|
||
|
|
html.P(f"Mode: {info.get('redis_mode', 'N/A')}")
|
||
|
|
])
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"Failed to get Redis status: {e}")
|
||
|
|
return html.Div([
|
||
|
|
html.H5("Redis Status"),
|
||
|
|
dbc.Badge("Error", color="danger", className="me-1"),
|
||
|
|
dbc.Alert(f"Error: {e}", color="danger", dismissable=True)
|
||
|
|
])
|
||
|
|
|
||
|
|
|
||
|
|
def _get_redis_statistics() -> html.Div:
|
||
|
|
"""Get detailed Redis statistics."""
|
||
|
|
try:
|
||
|
|
redis_manager = get_sync_redis_manager()
|
||
|
|
redis_manager.initialize()
|
||
|
|
|
||
|
|
if not redis_manager.client.ping():
|
||
|
|
raise ConnectionError("Redis server is not responding.")
|
||
|
|
|
||
|
|
info = redis_manager.client.info()
|
||
|
|
|
||
|
|
return html.Div([
|
||
|
|
html.H5("Redis Statistics"),
|
||
|
|
html.P(f"Connected Clients: {info.get('connected_clients', 'N/A')}"),
|
||
|
|
html.P(f"Memory Used: {info.get('used_memory_human', 'N/A')}"),
|
||
|
|
html.P(f"Total Commands Processed: {info.get('total_commands_processed', 'N/A')}")
|
||
|
|
])
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"Failed to get Redis statistics: {e}")
|
||
|
|
return dbc.Alert(f"Error: {e}", color="danger", dismissable=True)
|