2025-06-22 23:07:57 +08:00
|
|
|
|
import os
|
2025-12-21 21:25:13 +08:00
|
|
|
|
import aiofiles
|
2025-09-01 22:37:03 +08:00
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
import yaml
|
2025-09-02 01:08:42 +08:00
|
|
|
|
from fastapi import APIRouter, Body, Depends, HTTPException
|
2024-10-02 20:11:28 +08:00
|
|
|
|
|
2026-03-17 10:16:44 +08:00
|
|
|
|
from yuxi.storage.postgres.models_business import User
|
2025-11-12 01:21:38 +08:00
|
|
|
|
from server.utils.auth_middleware import get_admin_user
|
2026-04-01 16:55:29 +08:00
|
|
|
|
from yuxi import config, get_version
|
2026-03-17 10:16:44 +08:00
|
|
|
|
from yuxi.utils.logging_config import logger
|
2024-10-02 20:11:28 +08:00
|
|
|
|
|
2025-07-22 17:29:38 +08:00
|
|
|
|
system = APIRouter(prefix="/system", tags=["system"])
|
|
|
|
|
|
|
|
|
|
|
|
# =============================================================================
|
|
|
|
|
|
# === 健康检查分组 ===
|
|
|
|
|
|
# =============================================================================
|
|
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-07-22 17:29:38 +08:00
|
|
|
|
@system.get("/health")
|
|
|
|
|
|
async def health_check():
|
|
|
|
|
|
"""系统健康检查接口(公开接口)"""
|
2026-04-01 16:55:29 +08:00
|
|
|
|
return {"status": "ok", "message": "服务正常运行", "version": get_version()}
|
2025-07-22 17:29:38 +08:00
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-07-22 17:29:38 +08:00
|
|
|
|
# =============================================================================
|
|
|
|
|
|
# === 配置管理分组 ===
|
|
|
|
|
|
# =============================================================================
|
|
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-07-22 17:29:38 +08:00
|
|
|
|
@system.get("/config")
|
2025-12-21 21:25:13 +08:00
|
|
|
|
async def get_config(current_user: User = Depends(get_admin_user)):
|
2025-07-22 17:29:38 +08:00
|
|
|
|
"""获取系统配置"""
|
|
|
|
|
|
return config.dump_config()
|
|
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-07-22 17:29:38 +08:00
|
|
|
|
@system.post("/config")
|
2025-09-01 22:37:03 +08:00
|
|
|
|
async def update_config_single(key=Body(...), value=Body(...), current_user: User = Depends(get_admin_user)) -> dict:
|
2025-07-22 17:29:38 +08:00
|
|
|
|
"""更新单个配置项"""
|
2026-05-17 13:06:25 +08:00
|
|
|
|
if not hasattr(config, key):
|
|
|
|
|
|
raise HTTPException(status_code=400, detail=f"未知配置项: {key}")
|
|
|
|
|
|
setattr(config, key, value)
|
2025-07-22 17:29:38 +08:00
|
|
|
|
config.save()
|
|
|
|
|
|
return config.dump_config()
|
|
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-07-22 17:29:38 +08:00
|
|
|
|
@system.post("/config/update")
|
2025-09-01 22:37:03 +08:00
|
|
|
|
async def update_config_batch(items: dict = Body(...), current_user: User = Depends(get_admin_user)) -> dict:
|
2025-07-22 17:29:38 +08:00
|
|
|
|
"""批量更新配置项"""
|
|
|
|
|
|
config.update(items)
|
|
|
|
|
|
config.save()
|
|
|
|
|
|
return config.dump_config()
|
|
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-07-22 17:29:38 +08:00
|
|
|
|
@system.get("/logs")
|
2025-12-29 10:32:07 +08:00
|
|
|
|
async def get_system_logs(levels: str | None = None, current_user: User = Depends(get_admin_user)):
|
2025-12-23 11:21:34 +08:00
|
|
|
|
"""获取系统日志
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
levels: 可选的日志级别过滤,多个级别用逗号分隔,如 "INFO,ERROR,DEBUG,WARNING"
|
|
|
|
|
|
"""
|
2025-07-22 17:29:38 +08:00
|
|
|
|
try:
|
2026-03-17 10:16:44 +08:00
|
|
|
|
from yuxi.utils.logging_config import LOG_FILE
|
2025-07-22 17:29:38 +08:00
|
|
|
|
|
2025-12-23 11:21:34 +08:00
|
|
|
|
# 解析日志级别过滤条件
|
|
|
|
|
|
level_filter = None
|
|
|
|
|
|
if levels:
|
2025-12-29 10:32:07 +08:00
|
|
|
|
level_filter = set(level.strip().upper() for level in levels.split(",") if level.strip())
|
2025-12-23 11:21:34 +08:00
|
|
|
|
|
2026-05-14 13:04:17 +08:00
|
|
|
|
# 修复 GBK 编码报错:强制 utf-8 读取,忽略错误
|
2026-05-17 13:06:25 +08:00
|
|
|
|
async with aiofiles.open(LOG_FILE, encoding="utf-8", errors="ignore") as f:
|
2025-12-21 21:25:13 +08:00
|
|
|
|
# 读取最后1000行
|
|
|
|
|
|
lines = []
|
|
|
|
|
|
async for line in f:
|
2025-12-29 10:32:07 +08:00
|
|
|
|
filtered_line = line.rstrip("\n\r")
|
2025-12-23 11:21:34 +08:00
|
|
|
|
# 如果指定了日志级别过滤,则按级别过滤
|
|
|
|
|
|
if level_filter:
|
|
|
|
|
|
# 日志格式: 2025-03-10 08:26:37,269 - INFO - module - message
|
|
|
|
|
|
# 提取日志级别
|
2025-12-29 10:32:07 +08:00
|
|
|
|
parts = filtered_line.split(" - ")
|
2025-12-23 11:21:34 +08:00
|
|
|
|
if len(parts) >= 2 and parts[1].strip() in level_filter:
|
2025-12-29 10:32:07 +08:00
|
|
|
|
lines.append(filtered_line + "\n")
|
2025-12-23 11:21:34 +08:00
|
|
|
|
# 继续读取以保持行数统计准确
|
|
|
|
|
|
if len(lines) > 1000:
|
|
|
|
|
|
lines.pop(0)
|
|
|
|
|
|
else:
|
2025-12-29 10:32:07 +08:00
|
|
|
|
lines.append(filtered_line + "\n")
|
2025-12-23 11:21:34 +08:00
|
|
|
|
if len(lines) > 1000:
|
|
|
|
|
|
lines.pop(0)
|
2025-07-22 17:29:38 +08:00
|
|
|
|
|
2025-12-21 21:25:13 +08:00
|
|
|
|
log = "".join(lines)
|
2025-07-22 17:29:38 +08:00
|
|
|
|
return {"log": log, "message": "success", "log_file": LOG_FILE}
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"获取系统日志失败: {e}")
|
|
|
|
|
|
raise HTTPException(status_code=500, detail=f"获取系统日志失败: {str(e)}")
|
|
|
|
|
|
|
2026-05-17 13:06:25 +08:00
|
|
|
|
|
2025-07-22 17:29:38 +08:00
|
|
|
|
# =============================================================================
|
|
|
|
|
|
# === 信息管理分组 ===
|
|
|
|
|
|
# =============================================================================
|
2025-05-24 11:29:45 +08:00
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-12-21 21:25:13 +08:00
|
|
|
|
async def load_info_config():
|
2025-06-22 23:07:57 +08:00
|
|
|
|
"""加载信息配置文件"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
# 配置文件路径
|
2026-03-17 10:16:44 +08:00
|
|
|
|
brand_file_path = os.environ.get("YUXI_BRAND_FILE_PATH", "package/yuxi/config/static/info.local.yaml")
|
2025-08-24 21:07:55 +08:00
|
|
|
|
config_path = Path(brand_file_path)
|
2025-06-22 23:07:57 +08:00
|
|
|
|
|
|
|
|
|
|
# 检查文件是否存在
|
|
|
|
|
|
if not config_path.exists():
|
2025-06-27 01:47:52 +08:00
|
|
|
|
logger.debug(f"The config file {config_path} does not exist, using default config")
|
2026-03-17 10:16:44 +08:00
|
|
|
|
config_path = Path("package/yuxi/config/static/info.template.yaml")
|
2025-06-22 23:07:57 +08:00
|
|
|
|
|
2025-12-21 21:25:13 +08:00
|
|
|
|
# 异步读取配置文件
|
|
|
|
|
|
async with aiofiles.open(config_path, encoding="utf-8") as file:
|
|
|
|
|
|
content = await file.read()
|
2026-04-24 19:32:16 +08:00
|
|
|
|
|
|
|
|
|
|
# 注入版本号占位符
|
|
|
|
|
|
content = content.replace("{{YUXI_VERSION}}", get_version())
|
|
|
|
|
|
|
|
|
|
|
|
config = yaml.safe_load(content)
|
2025-06-22 23:07:57 +08:00
|
|
|
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
2025-06-27 01:47:52 +08:00
|
|
|
|
logger.error(f"Failed to load info config: {e}")
|
2025-11-08 20:59:24 +08:00
|
|
|
|
return {}
|
2025-06-22 23:07:57 +08:00
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-07-22 17:29:38 +08:00
|
|
|
|
@system.get("/info")
|
2025-06-22 23:07:57 +08:00
|
|
|
|
async def get_info_config():
|
|
|
|
|
|
"""获取系统信息配置(公开接口,无需认证)"""
|
|
|
|
|
|
try:
|
2025-12-21 21:25:13 +08:00
|
|
|
|
config = await load_info_config()
|
2025-09-01 22:37:03 +08:00
|
|
|
|
return {"success": True, "data": config}
|
2025-06-22 23:07:57 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"获取信息配置失败: {e}")
|
|
|
|
|
|
raise HTTPException(status_code=500, detail="获取信息配置失败")
|
|
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-07-22 17:29:38 +08:00
|
|
|
|
@system.post("/info/reload")
|
|
|
|
|
|
async def reload_info_config(current_user: User = Depends(get_admin_user)):
|
|
|
|
|
|
"""重新加载信息配置"""
|
2025-06-22 23:07:57 +08:00
|
|
|
|
try:
|
2025-12-21 21:25:13 +08:00
|
|
|
|
config = await load_info_config()
|
2025-09-01 22:37:03 +08:00
|
|
|
|
return {"success": True, "message": "配置重新加载成功", "data": config}
|
2025-06-22 23:07:57 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"重新加载信息配置失败: {e}")
|
|
|
|
|
|
raise HTTPException(status_code=500, detail="重新加载信息配置失败")
|
|
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-07-22 17:29:38 +08:00
|
|
|
|
# =============================================================================
|
|
|
|
|
|
# === OCR服务分组 ===
|
|
|
|
|
|
# =============================================================================
|
|
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-07-22 17:29:38 +08:00
|
|
|
|
@system.get("/ocr/health")
|
2025-07-21 19:25:07 +08:00
|
|
|
|
async def check_ocr_services_health(current_user: User = Depends(get_admin_user)):
|
|
|
|
|
|
"""
|
|
|
|
|
|
检查所有OCR服务的健康状态
|
|
|
|
|
|
返回各个OCR服务的可用性信息
|
|
|
|
|
|
"""
|
2026-05-29 22:19:58 +08:00
|
|
|
|
from yuxi.knowledge.parser.factory import DocumentProcessorFactory
|
2025-07-21 19:25:07 +08:00
|
|
|
|
|
|
|
|
|
|
try:
|
2025-10-25 14:26:47 +08:00
|
|
|
|
# 使用统一的健康检查接口
|
2026-03-18 00:02:24 +08:00
|
|
|
|
health_status = await DocumentProcessorFactory.check_all_health_async()
|
2025-10-25 14:26:47 +08:00
|
|
|
|
|
2026-05-17 13:06:25 +08:00
|
|
|
|
# 格式化健康检查响应
|
2025-10-25 14:26:47 +08:00
|
|
|
|
formatted_status = {}
|
|
|
|
|
|
for service_name, health_info in health_status.items():
|
|
|
|
|
|
formatted_status[service_name] = {
|
|
|
|
|
|
"status": health_info.get("status", "unknown"),
|
|
|
|
|
|
"message": health_info.get("message", ""),
|
|
|
|
|
|
"details": health_info.get("details", {}),
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# 计算整体健康状态
|
|
|
|
|
|
overall_status = (
|
|
|
|
|
|
"healthy" if any(svc["status"] == "healthy" for svc in formatted_status.values()) else "unhealthy"
|
2025-09-01 22:37:03 +08:00
|
|
|
|
)
|
2025-07-21 19:25:07 +08:00
|
|
|
|
|
2025-10-25 14:26:47 +08:00
|
|
|
|
return {
|
|
|
|
|
|
"overall_status": overall_status,
|
|
|
|
|
|
"services": formatted_status,
|
|
|
|
|
|
"message": "OCR服务健康检查完成",
|
|
|
|
|
|
}
|
2025-07-21 19:25:07 +08:00
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
2025-10-25 14:26:47 +08:00
|
|
|
|
logger.error(f"OCR健康检查失败: {str(e)}")
|
|
|
|
|
|
return {
|
|
|
|
|
|
"overall_status": "error",
|
|
|
|
|
|
"services": {},
|
|
|
|
|
|
"message": f"OCR健康检查失败: {str(e)}",
|
|
|
|
|
|
}
|