refactor: 清理后端冗余代码和未用依赖

This commit is contained in:
2026-05-19 15:46:31 +08:00
parent d30dd0da25
commit 3240027d48
11 changed files with 32 additions and 111 deletions
+2 -25
View File
@@ -56,11 +56,8 @@ def _dataframe_from_table(db: DatabaseManager, table_name: str) -> pd.DataFrame:
@router.post("/api/database/test")
async def test_database():
db = DatabaseManager(state.config)
try:
success, message = db.test_connection()
return {"success": success, "message": message}
finally:
db.dispose()
success, message = db.test_connection()
return {"success": success, "message": message}
@router.get("/api/database/info")
@@ -70,8 +67,6 @@ async def get_database_info():
return {"success": True, **db.get_server_info()}
except Exception as exc:
return {"success": False, "error": str(exc)}
finally:
db.dispose()
@router.get("/api/database/tables")
@@ -82,8 +77,6 @@ async def get_tables():
return {"tables": db.get_tables()}
except Exception as exc:
raise HTTPException(status_code=500, detail=str(exc)) from exc
finally:
db.dispose()
@router.post("/api/database/table/info")
@@ -93,8 +86,6 @@ async def get_table_info(table_name: str = Body(..., embed=True)):
return db.get_table_info(table_name)
except Exception as exc:
raise HTTPException(status_code=500, detail=str(exc)) from exc
finally:
db.dispose()
@router.post("/api/database/table/data")
@@ -110,8 +101,6 @@ async def query_table_data(
return db.query_table(table_name, page, page_size, order_by=order_by, order_dir=order_dir)
except Exception as exc:
raise HTTPException(status_code=500, detail=str(exc)) from exc
finally:
db.dispose()
@router.post("/api/database/table/query")
@@ -135,8 +124,6 @@ async def query_table_with_filter(
)
except Exception as exc:
raise HTTPException(status_code=500, detail=str(exc)) from exc
finally:
db.dispose()
@router.post("/api/database/table/truncate")
@@ -147,8 +134,6 @@ async def truncate_table(table_name: str = Body(..., embed=True)):
return {"success": True, "message": f"表 {table_name} 已清空"}
except Exception as exc:
raise HTTPException(status_code=500, detail=str(exc)) from exc
finally:
db.dispose()
@router.post("/api/database/table/drop")
@@ -159,8 +144,6 @@ async def drop_table(table_name: str = Body(..., embed=True)):
return {"success": True, "message": f"表 {table_name} 已删除"}
except Exception as exc:
raise HTTPException(status_code=500, detail=str(exc)) from exc
finally:
db.dispose()
@router.post("/api/database/table/drop-all")
@@ -176,8 +159,6 @@ async def drop_all_tables():
}
except Exception as exc:
raise HTTPException(status_code=500, detail=str(exc)) from exc
finally:
db.dispose()
@router.post("/api/database/execute")
@@ -192,8 +173,6 @@ async def execute_sql(sql: str = Body(..., embed=True)):
raise
except Exception as exc:
raise HTTPException(status_code=500, detail=str(exc)) from exc
finally:
db.dispose()
@router.post("/api/download")
@@ -226,8 +205,6 @@ async def download_table(
raise
except Exception as exc:
raise HTTPException(status_code=500, detail=str(exc)) from exc
finally:
db.dispose()
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename_prefix = requested_tables[0] if len(requested_tables) == 1 else "tables"
+6 -10
View File
@@ -19,15 +19,12 @@ async def health_check():
try:
db_manager = DatabaseManager(state.config)
try:
server_info = db_manager.get_server_info()
checks["database"] = {
"status": "ok",
"version": server_info.get("version", "unknown"),
"load_data_infile": server_info.get("load_data_infile", False),
}
finally:
db_manager.dispose()
server_info = db_manager.get_server_info()
checks["database"] = {
"status": "ok",
"version": server_info.get("version", "unknown"),
"load_data_infile": server_info.get("load_data_infile", False),
}
except Exception as exc:
checks["database"] = {"status": "error", "message": str(exc)}
@@ -39,4 +36,3 @@ async def health_check():
"uptime_pid": os.getpid(),
"checks": checks,
}
-2
View File
@@ -4,7 +4,6 @@ import hashlib
import hmac
import json
import time
from pathlib import Path
from typing import Optional
from app.config import BASE_DIR
@@ -81,4 +80,3 @@ def _sign(header: str, payload: str) -> str:
hashlib.sha256,
).digest()
return base64.urlsafe_b64encode(raw).decode().rstrip("=")
+1 -48
View File
@@ -2,14 +2,9 @@
数据库连接与操作模块 - 性能优化版
"""
import pymysql
from urllib.parse import quote
from contextlib import contextmanager
from typing import Any, Dict, List, Optional, Tuple
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.pool import QueuePool
from app.config import AppConfig
@@ -18,33 +13,6 @@ class DatabaseManager:
def __init__(self, config: AppConfig):
self.config = config
self._engine: Optional[sqlalchemy.Engine] = None
@property
def engine(self) -> sqlalchemy.Engine:
"""获取 SQLAlchemy 引擎(带连接池)- 优化配置"""
if self._engine is None:
mysql = self.config.mysql
self._engine = create_engine(
f'mysql+pymysql://'
f'{quote(mysql.user)}:'
f'{quote(mysql.passwd)}@'
f'{quote(mysql.host)}:'
f'{mysql.port}/'
f'{quote(mysql.dbname)}?charset=utf8mb4',
poolclass=QueuePool,
pool_size=10, # 增大连接池
max_overflow=20, # 增大溢出连接
pool_pre_ping=True,
pool_recycle=3600,
echo=False,
# 性能优化参数
connect_args={
'local_infile': True, # 允许 LOAD DATA LOCAL
'autocommit': False,
}
)
return self._engine
@contextmanager
def get_connection(self):
@@ -230,15 +198,6 @@ class DatabaseManager:
"total_pages": (total + page_size - 1) // page_size
}
def delete_rows(self, table_name: str, condition: str, params: List[Any]) -> int:
"""删除符合条件的行"""
with self.get_connection() as conn:
with conn.cursor() as cursor:
sql = f"DELETE FROM `{table_name}` WHERE {condition}"
cursor.execute(sql, params)
conn.commit()
return cursor.rowcount
def truncate_table(self, table_name: str) -> bool:
"""清空表"""
with self.get_connection() as conn:
@@ -272,7 +231,7 @@ class DatabaseManager:
try:
cursor.execute(f"DROP TABLE IF EXISTS `{table}`")
dropped_tables.append(table)
except Exception as e:
except Exception:
# 记录错误但继续删除其他表
pass
@@ -434,9 +393,3 @@ class DatabaseManager:
with conn.cursor() as cursor:
cursor.execute(sql)
conn.commit()
def dispose(self):
"""释放连接池"""
if self._engine:
self._engine.dispose()
self._engine = None
+1 -1
View File
@@ -49,7 +49,7 @@ class HistoryManager:
"""加载历史记录"""
try:
return json.loads(HISTORY_FILE.read_text(encoding='utf-8'))
except:
except (OSError, json.JSONDecodeError):
return []
def _save(self, records: List[Dict[str, Any]]):
-2
View File
@@ -220,8 +220,6 @@ class DataProcessor:
finally:
# 清理临时目录
self._cleanup_temp_dir()
# 释放数据库连接
self.db.dispose()
return self.results