修复首页健康摘要兼容旧版数据库连接管理器
This commit is contained in:
@@ -80,7 +80,12 @@ def _extract_mysql_runtime_snapshot(db_manager) -> dict:
|
|||||||
snapshot = {
|
snapshot = {
|
||||||
"status": "healthy",
|
"status": "healthy",
|
||||||
"summary": "连接正常",
|
"summary": "连接正常",
|
||||||
"database": db_manager.get_mysql_database_name(),
|
# 这里不要假定 db_manager 一定实现了扩展 helper。
|
||||||
|
# 当前仓库存在多种 DBConnectionManager 版本,因此先走 getattr,再在 SQL 查询里补足真实值。
|
||||||
|
"database": (
|
||||||
|
str(getattr(db_manager, "get_mysql_database_name", lambda: "")() or "").strip()
|
||||||
|
if db_manager is not None else ""
|
||||||
|
),
|
||||||
"version": "",
|
"version": "",
|
||||||
"threads_connected": 0,
|
"threads_connected": 0,
|
||||||
"threads_running": 0,
|
"threads_running": 0,
|
||||||
@@ -90,7 +95,10 @@ def _extract_mysql_runtime_snapshot(db_manager) -> dict:
|
|||||||
"uptime_seconds": 0,
|
"uptime_seconds": 0,
|
||||||
"table_count": 0,
|
"table_count": 0,
|
||||||
"schema_size_mb": 0.0,
|
"schema_size_mb": 0.0,
|
||||||
"slow_query_threshold_ms": db_manager.get_slow_query_threshold_ms(),
|
"slow_query_threshold_ms": (
|
||||||
|
int(getattr(db_manager, "get_slow_query_threshold_ms", lambda default=300: default)(300))
|
||||||
|
if db_manager is not None else 300
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
mysql_conn = db_manager.get_mysql_connection()
|
mysql_conn = db_manager.get_mysql_connection()
|
||||||
|
|||||||
@@ -39,6 +39,10 @@ class DBConnectionManager:
|
|||||||
self.LOG = logger
|
self.LOG = logger
|
||||||
self.mysql_pool = None
|
self.mysql_pool = None
|
||||||
self.redis_pool = None
|
self.redis_pool = None
|
||||||
|
# 保留原始配置快照,方便后台健康看板、安全校验等辅助能力读取基础元信息。
|
||||||
|
# 注意这里只存轻量配置引用,不持久化敏感信息,也不改变现有连接池行为。
|
||||||
|
self.mysql_config = dict(mysql_config or {})
|
||||||
|
self.redis_config = dict(redis_config or {})
|
||||||
|
|
||||||
# 初始化MySQL连接池
|
# 初始化MySQL连接池
|
||||||
if mysql_config:
|
if mysql_config:
|
||||||
@@ -58,6 +62,8 @@ class DBConnectionManager:
|
|||||||
if not config:
|
if not config:
|
||||||
self.LOG.warning("MySQL配置为空,跳过初始化")
|
self.LOG.warning("MySQL配置为空,跳过初始化")
|
||||||
return
|
return
|
||||||
|
# 记录最新配置,供运行态摘要读取当前 database / 阈值等基础信息。
|
||||||
|
self.mysql_config = dict(config or {})
|
||||||
|
|
||||||
# 准备连接池配置
|
# 准备连接池配置
|
||||||
pool_config = {
|
pool_config = {
|
||||||
@@ -90,6 +96,8 @@ class DBConnectionManager:
|
|||||||
if not config:
|
if not config:
|
||||||
self.LOG.warning("Redis配置为空,跳过初始化")
|
self.LOG.warning("Redis配置为空,跳过初始化")
|
||||||
return
|
return
|
||||||
|
# Redis db 序号等信息首页会直接展示,因此这里同步保存配置快照。
|
||||||
|
self.redis_config = dict(config or {})
|
||||||
|
|
||||||
self.redis_pool = redis.ConnectionPool(
|
self.redis_pool = redis.ConnectionPool(
|
||||||
host=config.get('host', 'localhost'),
|
host=config.get('host', 'localhost'),
|
||||||
@@ -132,6 +140,23 @@ class DBConnectionManager:
|
|||||||
|
|
||||||
return redis.Redis(connection_pool=self.redis_pool)
|
return redis.Redis(connection_pool=self.redis_pool)
|
||||||
|
|
||||||
|
def get_mysql_database_name(self) -> str:
|
||||||
|
"""返回当前 MySQL 连接配置中的数据库名。"""
|
||||||
|
return str((self.mysql_config or {}).get('database') or '').strip()
|
||||||
|
|
||||||
|
def get_slow_query_threshold_ms(self, default: int = 300) -> int:
|
||||||
|
"""返回慢 SQL 阈值(毫秒)。
|
||||||
|
|
||||||
|
兼容说明:
|
||||||
|
1. 新版部分分支会在配置里维护 `slow_query_threshold_ms`;
|
||||||
|
2. 旧版没有时直接回退默认值,避免后台健康接口因辅助字段缺失而报错。
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
value = (self.mysql_config or {}).get('slow_query_threshold_ms', default)
|
||||||
|
return int(float(value))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return int(default)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
"""关闭所有连接池"""
|
"""关闭所有连接池"""
|
||||||
# MySQL连接池会自动管理连接的关闭
|
# MySQL连接池会自动管理连接的关闭
|
||||||
|
|||||||
Reference in New Issue
Block a user