TCPDashboard/dashboard/callbacks/system_health_modules/system_performance_callbacks.py
Vasily.onl d5db9402e8 Add system health monitoring features with modular callbacks
- Introduced a new `system_health_constants.py` file to define thresholds and constants for system health metrics.
- Refactored existing system health callbacks into modular components, enhancing maintainability and clarity.
- Implemented dynamic loading of time range options in `charts.py`, improving flexibility in time range selection.
- Added detailed documentation for new callback functions, ensuring clarity on their purpose and usage.
- Enhanced error handling and logging practices across the new modules to ensure robust monitoring and debugging capabilities.

These changes significantly improve the architecture and maintainability of the system health monitoring features, aligning with project standards for modularity and performance.
2025-06-11 19:33:08 +08:00

75 lines
2.9 KiB
Python

from dash import Output, Input, html
import dash_bootstrap_components as dbc
from utils.logger import get_logger
import psutil
from config.constants.system_health_constants import (
CAPACITY_GOOD_THRESHOLD, CAPACITY_WARNING_THRESHOLD,
CPU_GOOD_THRESHOLD, CPU_WARNING_THRESHOLD,
MEMORY_GOOD_THRESHOLD, MEMORY_WARNING_THRESHOLD,
DISK_GOOD_THRESHOLD, DISK_WARNING_THRESHOLD,
BYTE_TO_GB
)
logger = get_logger("default_logger")
def register_system_performance_callbacks(app):
"""Register system performance metrics callbacks."""
# System Performance Metrics
@app.callback(
Output('system-performance-metrics', 'children'),
Input('interval-component', 'n_intervals')
)
def update_system_performance(n_intervals):
"""Update system performance metrics."""
try:
return _get_system_performance_metrics()
except Exception as e:
logger.error(f"Error updating system performance: {e}")
return dbc.Alert(
f"Error: {str(e)}",
color="danger",
dismissable=True
)
def _get_system_performance_metrics() -> html.Div:
"""Get system performance metrics."""
try:
cpu_percent = psutil.cpu_percent(interval=0.1)
cpu_count = psutil.cpu_count()
memory = psutil.virtual_memory()
disk = psutil.disk_usage('/')
def get_color(percent):
if percent < CAPACITY_GOOD_THRESHOLD: return "success"
if percent < CAPACITY_WARNING_THRESHOLD: return "warning"
return "danger"
return html.Div([
html.Div([
html.Strong("CPU Usage: "),
dbc.Badge(f"{cpu_percent:.1f}%", color=get_color(cpu_percent)),
html.Span(f" ({cpu_count} cores)", className="text-muted ms-1")
], className="mb-2"),
dbc.Progress(value=cpu_percent, color=get_color(cpu_percent), style={"height": "10px"}, className="mb-3"),
html.Div([
html.Strong("Memory Usage: "),
dbc.Badge(f"{memory.percent:.1f}%", color=get_color(memory.percent)),
html.Span(f" ({memory.used / BYTE_TO_GB:.1f} / {memory.total / BYTE_TO_GB:.1f} GB)", className="text-muted ms-1")
], className="mb-2"),
dbc.Progress(value=memory.percent, color=get_color(memory.percent), style={"height": "10px"}, className="mb-3"),
html.Div([
html.Strong("Disk Usage: "),
dbc.Badge(f"{disk.percent:.1f}%", color=get_color(disk.percent)),
html.Span(f" ({disk.used / BYTE_TO_GB:.1f} / {disk.total / BYTE_TO_GB:.1f} GB)", className="text-muted ms-1")
], className="mb-2"),
dbc.Progress(value=disk.percent, color=get_color(disk.percent), style={"height": "10px"})
])
except Exception as e:
return dbc.Alert(f"Error loading performance metrics: {e}", color="danger")