diff --git a/scripts/resource/sql/init.sql b/scripts/resource/sql/init.sql index 71beed6..24cdd28 100644 --- a/scripts/resource/sql/init.sql +++ b/scripts/resource/sql/init.sql @@ -263,15 +263,15 @@ CREATE TABLE IF NOT EXISTS `mcp_call_log` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Stores API call logs'; CREATE TABLE IF NOT EXISTS `sys_config_large` ( - `id` char(36) NOT NULL COMMENT '主键,UUID', - `key` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '配置键名', - `value` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '配置值', - `description` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '配置项描述', - `created_at` timestamp NULL DEFAULT NULL COMMENT '记录创建时间', - `updated_at` timestamp NULL DEFAULT NULL COMMENT '记录最后更新时间', + `id` char(36) NOT NULL COMMENT 'Primary key, UUID', + `key` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'Configuration key name', + `value` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'Configuration value', + `description` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'Configuration item description', + `created_at` timestamp NULL DEFAULT NULL COMMENT 'Record creation time', + `updated_at` timestamp NULL DEFAULT NULL COMMENT 'Record last update time', PRIMARY KEY (`id`) USING BTREE, -UNIQUE KEY `uk_key` (`key`) USING BTREE COMMENT '配置键名唯一索引' -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC COMMENT='系统参数配置表,用于存储系统级配置信息'; +UNIQUE KEY `uk_key` (`key`) USING BTREE COMMENT 'Unique index for configuration key name' +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC COMMENT='System configuration table for storing system-level configuration information'; INSERT INTO `user` (`id`, `name`, `email`, `password`, `is_active`, `is_deleted`, `register_type`, `role_id`, `created_at`, `updated_at`) VALUES ('admin', 'admin', 'admin@xpack.com', '25f9e794323b453885f5181f1b624d0b', 1, 0, 'inner', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) diff --git a/scripts/version.json b/scripts/version.json index 6c1bc83..bd2f069 100644 --- a/scripts/version.json +++ b/scripts/version.json @@ -1,11 +1,11 @@ { - "version": "v1.0", - "releaseDate": "2025-07-30", + "version": "v1.1", + "releaseDate": "2025-10-17", "description": "This is the latest release of the application.", "downloads": { "program": { "xpack_mcp_market": { - "version": "v1.0.1", + "version": "v1.1.0", "docker": "xpackai/xpack-mcp-market", "tar": "xpack-mcp-market.tar.gz", "url": "https://public.eolinker.com/xpack/images/xpack-mcp-market.${VERSION}.${ARCH}.tar.gz" diff --git a/services/admin_service/controllers/admin_stats.py b/services/admin_service/controllers/admin_stats.py index 3e1cf33..ff028bf 100644 --- a/services/admin_service/controllers/admin_stats.py +++ b/services/admin_service/controllers/admin_stats.py @@ -30,34 +30,3 @@ async def stats( ) except Exception as e: return ResponseUtils.error(f"Failed to get stats analytics: {str(e)}", 500) - -# @router.get("/platform") -# async def get_platform_overview(db: Session = Depends(get_db)): -# """Get platform overview statistics and metrics.""" -# try: -# # Get total users (non-admin and not deleted) -# total_user = db.query(User).filter(User.role_id != 1, User.is_deleted == 0).count() # Non-admin (role_id 1 is admin), not deleted - -# # Get total balance -# total_balance_result = db.query(func.sum(UserWallet.balance)).scalar() -# total_balance = int(total_balance_result) if total_balance_result else 0 - -# # Get total number of services -# total_service = db.query(McpService).count() - -# # Get today's invocation count -# today = date.today() -# today_invoke_count = db.query(McpCallLog).filter(func.date(McpCallLog.created_at) == today).count() - -# # Build response data -# response_data = { -# "total_user": total_user, -# "total_balance": total_balance, -# "total_service": total_service, -# "invoke_count": {"today": today_invoke_count}, -# } - -# return ResponseUtils.success(response_data) - -# except Exception as e: -# return ResponseUtils.error(f"Failed to get platform overview: {str(e)}", 500) diff --git a/services/admin_service/controllers/init_config.py b/services/admin_service/controllers/init_config.py index 48ff5f2..e138335 100644 --- a/services/admin_service/controllers/init_config.py +++ b/services/admin_service/controllers/init_config.py @@ -33,15 +33,15 @@ def get_config( try: faq = json.loads(faq) except json.JSONDecodeError: - return ResponseUtils.error("FAQ配置格式错误") + return ResponseUtils.error("Invalid FAQ configuration format") try: embeded_html = json.loads(embeded_html) except json.JSONDecodeError: - return ResponseUtils.error("Embeded HTML配置格式错误") + return ResponseUtils.error("Invalid embedded HTML configuration format") try: top_navigation = json.loads(top_navigation) except json.JSONDecodeError: - return ResponseUtils.error("Top Navigation配置格式错误") + return ResponseUtils.error("Invalid top navigation configuration format") # Build response data config_data = { "login": { @@ -93,15 +93,15 @@ def get_homepage_config(db: Session = Depends(get_db)): try: faq = json.loads(faq) except json.JSONDecodeError: - return ResponseUtils.error("FAQ配置格式错误") + return ResponseUtils.error("Invalid FAQ configuration format") try: embeded_html = json.loads(embeded_html) except json.JSONDecodeError: - return ResponseUtils.error("Embeded HTML配置格式错误") + return ResponseUtils.error("Invalid embedded HTML configuration format") try: top_navigation = json.loads(top_navigation) except json.JSONDecodeError: - return ResponseUtils.error("Top Navigation配置格式错误") + return ResponseUtils.error("Invalid top navigation configuration format") config_data = { "faq": faq, "embeded_html": embeded_html, diff --git a/services/admin_service/controllers/mcp_manager.py b/services/admin_service/controllers/mcp_manager.py index a7e3b29..fad0207 100644 --- a/services/admin_service/controllers/mcp_manager.py +++ b/services/admin_service/controllers/mcp_manager.py @@ -1,3 +1,8 @@ +"""Admin MCP service management controller. + +Includes endpoints to enable/disable services, update/delete services, +import OpenAPI definitions, and query service info and lists. +""" import uuid import logging from fastapi import APIRouter, Depends, Request, Body, UploadFile, File, HTTPException, Form @@ -50,7 +55,7 @@ def update_mcp_service_info(request: Request, body: dict = Body(...), mcp_manage if not UserUtils.is_admin(request): return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION) - # 验证必须传递 ID + # Validate ID is required if not body.get("id"): return ResponseUtils.error(error_msg=error_msg.MISSING_PARAMETER) @@ -94,7 +99,7 @@ async def openapi_parse( else: return ResponseUtils.error(error_msg=error_msg.MISSING_URL_OR_FILE) - # 创建MCP服务 + # Create MCP service service_id = mcp_manager_service.create_service_from_openapi(openapi_for_ai) result = {"service_id": service_id} @@ -106,10 +111,10 @@ async def openapi_parse( return ResponseUtils.error(error_msg=error_msg.INTERNAL_ERROR) -@router.post("/openapi_parse_update", summary="openapi解析(更新)", response_model=dict) +@router.post("/openapi_parse_update", summary="OpenAPI parse (update)", response_model=dict) async def openapi_parse_update( request: Request, - id: str = Form(..., description="服务ID"), + id: str = Form(..., description="Service ID"), url: Optional[HttpUrl] = Form(None, description="OpenAPI document URL (optional)"), file: Optional[UploadFile] = File(None, description="OpenAPI document file (JSON/YAML, optional)"), mcp_manager_service: McpManagerService = Depends(get_mcp_manager), @@ -119,11 +124,11 @@ async def openapi_parse_update( return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION) try: - # 验证必须提供URL或文件 + # Validate URL or file must be provided if not url and not file: return ResponseUtils.error(error_msg=error_msg.MISSING_URL_OR_FILE) - # 解析OpenAPI文档 + # Parse OpenAPI document if url: url_str = str(url) is_valid = await openapi_manager.validate_openapi_url(url_str) @@ -135,7 +140,7 @@ async def openapi_parse_update( else: return ResponseUtils.error(error_msg=error_msg.MISSING_URL_OR_FILE) - # 更新服务并保存到临时表 + # Update service and save into temporary table result = mcp_manager_service.update_service_from_openapi(id, openapi_for_ai) return ResponseUtils.success(data=result) @@ -148,7 +153,7 @@ async def openapi_parse_update( return ResponseUtils.error(error_msg=error_msg.INTERNAL_ERROR) -@router.get("/service/info", summary="获取MCP服务信息") +@router.get("/service/info", summary="Get MCP service information") def get_mcp_service_info(request: Request, id: str, mcp_manager_service: McpManagerService = Depends(get_mcp_manager)): """Get detailed information of a specific MCP service including API list.""" if not UserUtils.is_admin(request): @@ -168,7 +173,7 @@ def get_mcp_service_info(request: Request, id: str, mcp_manager_service: McpMana return ResponseUtils.error(error_msg=error_msg.INTERNAL_ERROR) -@router.get("/service/list", summary="获取mcp服务列表") +@router.get("/service/list", summary="Get MCP service list") def get_mcp_service_list( request: Request, page: int = 1, page_size: int = 10, mcp_manager_service: McpManagerService = Depends(get_mcp_manager) ): @@ -177,11 +182,11 @@ def get_mcp_service_list( return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION) try: - # 获取分页数据 + # Fetch paginated data try: services, total = mcp_manager_service.get_all_paginated(page=page, page_size=page_size) except AttributeError: - # 如果方法不存在,使用非分页方式 + # Fallback to non-paginated method when missing all_services = mcp_manager_service.get_all() total = len(all_services) start = (page - 1) * page_size diff --git a/services/admin_service/controllers/sys_config.py b/services/admin_service/controllers/sys_config.py index b16d19d..137bac6 100644 --- a/services/admin_service/controllers/sys_config.py +++ b/services/admin_service/controllers/sys_config.py @@ -1,3 +1,8 @@ +"""Admin system configuration controller. + +Provides endpoints to read and update platform configurations, homepage content, +and to test email settings. All responses and comments are standardized in English. +""" from fastapi import APIRouter, Depends, Body import json import socket @@ -31,32 +36,33 @@ def get_user_service(db: Session = Depends(get_db)) -> UserService: def get_homepage( sysconfig_service: SysConfigService = Depends(get_sysconfig_service), ): + """Get homepage configuration including FAQ, top navigation, and embedded HTML.""" faq = sysconfig_service.get_value_by_key(sys_config_key.KEY_FAQ) or "[]" - # 判断是否为空值,若非空,则json反序列化 + # Check empty value; if not empty, deserialize JSON try: - # 数组类型 + # Array type if faq.startswith("[") and faq.endswith("]"): faq = json.loads(faq) else: - return ResponseUtils.error("FAQ配置格式错误") + return ResponseUtils.error("Invalid FAQ configuration format") except json.JSONDecodeError: - return ResponseUtils.error("FAQ配置格式错误") + return ResponseUtils.error("Invalid FAQ configuration format") top_navigation = sysconfig_service.get_value_by_key(sys_config_key.KEY_TOP_NAVIGATION) or "[]" try: - # 数组类型 + # Array type if top_navigation.startswith("[") and top_navigation.endswith("]"): top_navigation = json.loads(top_navigation) else: - return ResponseUtils.error("Top Navigation配置格式错误") + return ResponseUtils.error("Invalid top navigation configuration format") except json.JSONDecodeError: - return ResponseUtils.error("Top Navigation配置格式错误") + return ResponseUtils.error("Invalid top navigation configuration format") embeded_html = sysconfig_service.get_value_by_key(sys_config_key.KEY_EMBEDED_HTML,True) or "{}" try: embeded_html = json.loads(embeded_html) except json.JSONDecodeError: - return ResponseUtils.error("Embeded HTML配置格式错误") + return ResponseUtils.error("Invalid embedded HTML configuration format") return ResponseUtils.success( data={ @@ -90,7 +96,7 @@ def get_sysconfig( ): """Get all system configuration settings.""" admin_user = user_service.get_admin_user() - # 批量读取配置(小表) + # Batch read configurations (small table) large_keys = [ sys_config_key.KEY_ABOUT_PAGE, @@ -103,12 +109,12 @@ def get_sysconfig( cname_a_ip = small_values.get(sys_config_key.KEY_CNAME_A_IP, "") if not cname_a_ip: - # 获取外网IP,若无法连接外网,则获取局域网IP + # Get public IP; if unavailable, fall back to local IP public_ip = get_public_ip() cname_a_ip = public_ip if public_ip else get_local_ip() sysconfig_service.set_value_by_key(sys_config_key.KEY_CNAME_A_IP, cname_a_ip,"CNAME A IP") - # 管理员用户名 + # Admin username admin_username = "" if admin_user and admin_user.name: admin_username = admin_user.name @@ -190,7 +196,7 @@ def set_sysconfig( email_smtp_password = "" email_smtp_sender = "" - # 使用 get 方法安全地获取嵌套值 + # Safely get nested values using `.get` platform = body.get("platform", {}) platform_name = platform.get("name") platform_logo = platform.get("logo") @@ -222,12 +228,12 @@ def set_sysconfig( google_config = login.get("google", {}) login_google_client = google_config.get("client_id") login_google_secret = google_config.get("client_secret") - login_google_enable = google_config.get("is_enabled") # 设置默认值为 False + login_google_enable = google_config.get("is_enabled") # default is False login_email_config = login.get("email", {}) login_email_mode = login_email_config.get("mode") - login_email_is_enable = login_email_config.get("is_enabled") # 设置默认值为 False + login_email_is_enable = login_email_config.get("is_enabled") # default is False - # 获取邮件配置 - 现在从外层获取 + # Get email configuration from outer payload email_config = body.get("email", {}) email_smtp_host = email_config.get("smtp_host") email_smtp_port = email_config.get("smtp_port") @@ -237,7 +243,7 @@ def set_sysconfig( - # 批量更新配置 + # Batch update configuration configs = [ (sys_config_key.KEY_PLATFORM_NAME, platform_name, "Platform name"), (sys_config_key.KEY_PLATFORM_LOGO, platform_logo, "Platform logo",), @@ -262,21 +268,21 @@ def set_sysconfig( (sys_config_key.KEY_SOCIAL_ACCOUNT_X_URL, social_account_x_url, "X URL"), (sys_config_key.KEY_LOGIN_GOOGLE_CLIENT, login_google_client, "Google login client ID"), (sys_config_key.KEY_LOGIN_GOOGLE_SECRET, login_google_secret, "Google login client secret"), - (sys_config_key.KEY_LOGIN_GOOGLE_ENABLE, login_google_enable, "谷歌登录是否启用"), - (sys_config_key.KEY_EMAIL_SMTP_HOST, email_smtp_host, "邮件SMTP主机"), - (sys_config_key.KEY_EMAIL_SMTP_PORT, email_smtp_port, "邮件SMTP端口"), - (sys_config_key.KEY_EMAIL_SMTP_USER, email_smtp_user, "邮件SMTP用户名"), - (sys_config_key.KEY_EMAIL_SMTP_PASSWORD, email_smtp_password, "邮件SMTP密码"), - (sys_config_key.KEY_EMAIL_SMTP_SENDER, email_smtp_sender, "邮件发送者地址"), - (sys_config_key.KEY_LOGIN_EMAIL_ENABLE, login_email_is_enable, "邮件是否启用"), - (sys_config_key.KEY_LOGIN_EMAIL_MODE, login_email_mode, "邮件模式"), + (sys_config_key.KEY_LOGIN_GOOGLE_ENABLE, login_google_enable, "Google login enabled"), + (sys_config_key.KEY_EMAIL_SMTP_HOST, email_smtp_host, "Email SMTP host"), + (sys_config_key.KEY_EMAIL_SMTP_PORT, email_smtp_port, "Email SMTP port"), + (sys_config_key.KEY_EMAIL_SMTP_USER, email_smtp_user, "Email SMTP user"), + (sys_config_key.KEY_EMAIL_SMTP_PASSWORD, email_smtp_password, "Email SMTP password"), + (sys_config_key.KEY_EMAIL_SMTP_SENDER, email_smtp_sender, "Email sender address"), + (sys_config_key.KEY_LOGIN_EMAIL_ENABLE, login_email_is_enable, "Email login enabled"), + (sys_config_key.KEY_LOGIN_EMAIL_MODE, login_email_mode, "Email login mode"), ] - # 更新管理员用户名和密码 + # Update admin username and password user_service.update_admin(name=admin_username, password=admin_password) for key, value, desc in configs: - if value is not None: # 只更新有值的配置 + if value is not None: # Only update when value is not None is_large = False if key == sys_config_key.KEY_ABOUT_PAGE: is_large = True @@ -321,7 +327,7 @@ def set_sysconfig( return get_sysconfig(sysconfig_service, user_service) except Exception as e: - return ResponseUtils.error(f"更新系统配置失败:{str(e)}") + return ResponseUtils.error(f"Failed to update system configuration: {str(e)}") @router.post("/test_email") @@ -333,25 +339,25 @@ def test_email_config( try: test_email = body.get("email") if not test_email: - return ResponseUtils.error("请提供测试邮箱地址") + return ResponseUtils.error("Please provide a test email address") - # 测试连接配置 + # Test connection configuration success, message = EmailUtils.test_email_config(db) if not success: return ResponseUtils.error(message) - # 发送测试邮件 + # Send test email test_success = EmailUtils.send_email( - db, "XPack 邮件配置测试", "这是一封测试邮件,用于验证邮件配置是否正确。如果您收到这封邮件,说明配置成功!", test_email, False + db, "XPack Email Configuration Test", "This is a test email to verify the email configuration. If you receive this email, the configuration is working!", test_email, False ) if test_success: - return ResponseUtils.success(data={"message": "测试邮件发送成功,请检查收件箱"}) + return ResponseUtils.success(data={"message": "Test email sent successfully, please check your inbox"}) else: - return ResponseUtils.error("测试邮件发送失败") + return ResponseUtils.error("Failed to send test email") except Exception as e: - return ResponseUtils.error(f"测试邮件配置失败:{str(e)}") + return ResponseUtils.error(f"Failed to test email configuration: {str(e)}") diff --git a/services/admin_service/controllers/upload.py b/services/admin_service/controllers/upload.py index 935a24f..cf3c37d 100644 --- a/services/admin_service/controllers/upload.py +++ b/services/admin_service/controllers/upload.py @@ -7,39 +7,51 @@ from services.common.utils.response_utils import ResponseUtils router = APIRouter() +"""Admin image upload controller. + +Provides an endpoint to upload images, verify optional SHA256 checksum, +and store files under date-based directories. +""" @router.post("/image") async def upload( img: UploadFile = File(None), sha256: str = Form(None), ): - # 1. 获取上传文件的名称 + """Upload an image file. + + - Validates presence of file and filename + - Optionally verifies uploaded content against provided SHA256 + - Saves the file under `uploads/images//` + - Returns the relative `file_path` + """ + # 1. Get uploaded file name if not img: - return ResponseUtils.error(message="未提供文件", code=400) + return ResponseUtils.error(message="No file provided", code=400) filename = img.filename if not filename: - return ResponseUtils.error(message="未提供文件名称", code=400) + return ResponseUtils.error(message="No filename provided", code=400) - # 2. 计算上传文件的sha256值 + # 2. Calculate SHA256 of uploaded content contents = await img.read() file_hash = hashlib.sha256(contents).hexdigest() - # 重置文件指针,以便后续保存文件 + # Reset file pointer for subsequent save await img.seek(0) - # 检查sha256是否一致 + # Verify provided SHA256 matches computed hash if sha256 and file_hash != sha256: - return ResponseUtils.error(message="文件SHA256校验失败", code=400) + return ResponseUtils.error(message="File SHA256 checksum mismatch", code=400) - # 3. 根据当前日期创建目录 + # 3. Create directory based on current date today = datetime.now() date_dir = today.strftime("%Y%m%d") - # 创建上传目录 + # Create upload directory upload_dir = os.path.join("uploads","images") upload_dir = os.path.join(upload_dir, date_dir) os.makedirs(upload_dir, exist_ok=True) - # 保存文件 + # Save file file_path = os.path.join(upload_dir, filename) with open(file_path, "wb") as f: diff --git a/services/admin_service/main.py b/services/admin_service/main.py index 97d8c53..9efeae4 100644 --- a/services/admin_service/main.py +++ b/services/admin_service/main.py @@ -40,7 +40,7 @@ from services.admin_service.task import * setup_logging("admin_service") logger = get_logger(__name__) -# 全局消费者实例 +# Global consumer instance consumer_instance = None consumer_thread = None @@ -172,12 +172,12 @@ app.include_router(user_task.router, prefix="/api/onboarding") # Logging is already configured by setup_logging("admin_service") -# 创建uploads目录(如果不存在) +# Create uploads directory if it does not exist uploads_dir = "uploads" if not os.path.exists(uploads_dir): os.makedirs(uploads_dir) -# 挂载静态文件目录 +# Mount static file directory app.mount("/uploads", StaticFiles(directory=uploads_dir), name="uploads") @app.get("/") diff --git a/services/admin_service/repositories/mcp_service_repository.py b/services/admin_service/repositories/mcp_service_repository.py index 159ec23..b4e6815 100644 --- a/services/admin_service/repositories/mcp_service_repository.py +++ b/services/admin_service/repositories/mcp_service_repository.py @@ -4,11 +4,14 @@ from services.common.models.mcp_service import McpService from typing import Optional, Tuple, List +"""Repository for MCP services: CRUD, pagination, and public listing.""" class McpServiceRepository: + """Data access layer for McpService model.""" def __init__(self, db: Session): self.db = db def update_enabled(self, id: str, enabled: int) -> McpService: + """Enable or disable a service by ID; raises if not found.""" service = self.db.query(McpService).filter(McpService.id == id).first() if not service: raise ValueError("Service not found") @@ -18,6 +21,7 @@ class McpServiceRepository: return service def delete(self, id: str) -> Optional[McpService]: + """Delete a service by ID; returns deleted entity or None.""" service = self.db.query(McpService).filter(McpService.id == id).first() if not service: return None @@ -26,6 +30,7 @@ class McpServiceRepository: return service def update(self, mcp_service: McpService) -> McpService: + """Update mutable fields for an existing service; returns refreshed entity.""" existing_service = self.db.query(McpService).filter(McpService.id == mcp_service.id).first() if not existing_service: raise ValueError("Service not found") @@ -47,12 +52,15 @@ class McpServiceRepository: return existing_service def get_by_id(self, id: str) -> Optional[McpService]: + """Get a service by primary ID.""" return self.db.query(McpService).filter(McpService.id == id).first() def get_by_slug_name(self, slug_name: str) -> Optional[McpService]: + """Get a service by unique slug name.""" return self.db.query(McpService).filter(McpService.slug_name == slug_name).first() def get_all(self) -> List[McpService]: + """List all services ordered by creation time descending.""" return self.db.query(McpService).order_by(McpService.created_at.desc()).all() def get_all_paginated(self, page: int = 1, page_size: int = 10) -> Tuple[List[McpService], int]: @@ -66,6 +74,7 @@ class McpServiceRepository: return services, total def create(self, mcp_service: McpService) -> McpService: + """Create a service; set timestamps if missing and persist.""" # Set timestamps explicitly if not already set current_time = datetime.now(timezone.utc) if not hasattr(mcp_service, "created_at") or mcp_service.created_at is None: diff --git a/services/admin_service/repositories/payment_channel_repository.py b/services/admin_service/repositories/payment_channel_repository.py index 3943c72..9e09b60 100644 --- a/services/admin_service/repositories/payment_channel_repository.py +++ b/services/admin_service/repositories/payment_channel_repository.py @@ -1,22 +1,28 @@ +"""Repository for payment channels: list, get, update status and config.""" from datetime import datetime, timezone from sqlalchemy.orm import Session from services.common.models.payment_channel import PaymentChannel from typing import Optional, Tuple, List class PaymentChannelRepository: + """Data access layer for PaymentChannel model.""" def __init__(self, db: Session): self.db = db def payment_channel_list(self) -> Tuple[int, List[PaymentChannel]]: + """Return total count and all payment channels.""" return self.db.query(PaymentChannel).count(),self.db.query(PaymentChannel).all() def payment_channel_get(self, id: str) -> Optional[PaymentChannel]: + """Fetch a payment channel by primary ID.""" return self.db.query(PaymentChannel).filter(PaymentChannel.id == id).first() def payment_channel_available_list(self) -> List[PaymentChannel]: + """List enabled payment channels.""" return self.db.query(PaymentChannel).filter(PaymentChannel.status == 1).all() def update_status(self, id: str, status: int) -> Optional[PaymentChannel]: + """Update channel status and refresh updated_at; returns updated entity or None.""" payment_channel = self.payment_channel_get(id) if payment_channel: payment_channel.status = status @@ -26,6 +32,7 @@ class PaymentChannelRepository: return None def update_config(self, id: str, config: str) -> Optional[PaymentChannel]: + """Update channel config and refresh updated_at; returns updated entity or None.""" payment_channel = self.payment_channel_get(id) if payment_channel: payment_channel.config = config diff --git a/services/admin_service/repositories/sys_config_large_repository.py b/services/admin_service/repositories/sys_config_large_repository.py index 709be70..82d0e1e 100644 --- a/services/admin_service/repositories/sys_config_large_repository.py +++ b/services/admin_service/repositories/sys_config_large_repository.py @@ -5,23 +5,29 @@ from sqlalchemy.orm import Session from services.common.models.sys_config_large import SysConfigLarge from typing import Dict, Optional,List +"""Repository for large system configuration: KV CRUD helpers.""" class SysConfigLargeRepository: + """Data access layer for SysConfigLarge model.""" def __init__(self, db: Session): self.db = db def get_all(self, keys: List[str] = []) -> Dict[str, str]: + """Return mapping of key->value, optionally filtered by provided keys.""" if keys: return {item.key: item.value for item in self.db.query(SysConfigLarge).filter(SysConfigLarge.key.in_(keys)).all()} return {item.key: item.value for item in self.db.query(SysConfigLarge).all()} def get_by_key(self, key: str) -> Optional[SysConfigLarge]: + """Get SysConfigLarge entity by unique key.""" return self.db.query(SysConfigLarge).filter(SysConfigLarge.key == key).first() def get_value_by_key(self, key: str) -> Optional[str]: + """Get value string for given key, or None if missing.""" sys_config = self.get_by_key(key) if sys_config: return sys_config.value return None def set_value_by_key(self, key: str, value: str,description:str) -> Optional[SysConfigLarge]: + """Set or create value and description for a key; returns entity.""" sys_config = self.get_by_key(key) if sys_config: sys_config.value = value @@ -41,6 +47,7 @@ class SysConfigLargeRepository: self.db.commit() return sys_config def delete_by_key(self, key: str) -> bool: + """Delete config by key; returns True if deleted, else False.""" sys_config = self.get_by_key(key) if sys_config: self.db.delete(sys_config) diff --git a/services/admin_service/repositories/sys_config_repository.py b/services/admin_service/repositories/sys_config_repository.py index bf5f531..b834e17 100644 --- a/services/admin_service/repositories/sys_config_repository.py +++ b/services/admin_service/repositories/sys_config_repository.py @@ -5,21 +5,27 @@ from sqlalchemy.orm import Session from services.common.models.sys_config import SysConfig from typing import Optional,Dict,List +"""Repository for small system configuration: KV CRUD helpers.""" class SysConfigRepository: + """Data access layer for SysConfig model.""" def __init__(self, db: Session): self.db = db def get_all(self, keys: List[str] = []) -> Dict[str, str]: + """Return mapping of key->value, optionally filtered by provided keys.""" if keys: return {item.key: item.value for item in self.db.query(SysConfig).filter(SysConfig.key.in_(keys)).all()} return {item.key: item.value for item in self.db.query(SysConfig).all()} def get_by_key(self, key: str) -> Optional[SysConfig]: + """Get SysConfig entity by unique key.""" return self.db.query(SysConfig).filter(SysConfig.key == key).first() def get_value_by_key(self, key: str) -> Optional[str]: + """Get value string for given key, or None if missing.""" sys_config = self.get_by_key(key) if sys_config: return sys_config.value return None def set_value_by_key(self, key: str, value: str,description:str) -> Optional[SysConfig]: + """Set or create value and description for a key; returns entity.""" sys_config = self.get_by_key(key) if sys_config: sys_config.value = value @@ -39,6 +45,7 @@ class SysConfigRepository: self.db.commit() return sys_config def delete_by_key(self, key: str) -> bool: + """Delete config by key; returns True if deleted, else False.""" sys_config = self.get_by_key(key) if sys_config: self.db.delete(sys_config) diff --git a/services/admin_service/repositories/user_access_token_repository.py b/services/admin_service/repositories/user_access_token_repository.py index ac048f7..160931d 100644 --- a/services/admin_service/repositories/user_access_token_repository.py +++ b/services/admin_service/repositories/user_access_token_repository.py @@ -4,11 +4,14 @@ from sqlalchemy.orm import Session from services.common.models.user_access_token import UserAccessToken +"""Repository for user access tokens: create and revoke by token.""" class UserAccessTokenRepository: + """Data access layer for UserAccessToken model.""" def __init__(self, db: Session): self.db = db def create(self, user_id: str) -> UserAccessToken: + """Create a new access token for the user, expiring in 30 days.""" user_access_token = UserAccessToken( id=str(uuid.uuid4()), user_id=user_id, token=str(uuid.uuid4()), expire_at=datetime.now(timezone.utc) + timedelta(days=30) ) @@ -19,6 +22,7 @@ class UserAccessTokenRepository: return user_access_token def delete_by_token(self, token: str) -> None: + """Revoke an access token by its token string.""" user_access_token = self.db.query(UserAccessToken).filter(UserAccessToken.token == token).first() if user_access_token: self.db.delete(user_access_token) diff --git a/services/admin_service/repositories/user_apikey_repository.py b/services/admin_service/repositories/user_apikey_repository.py index 1db573b..97aab3f 100644 --- a/services/admin_service/repositories/user_apikey_repository.py +++ b/services/admin_service/repositories/user_apikey_repository.py @@ -1,3 +1,4 @@ +"""Repository for managing user API keys: create, update, delete, list.""" import uuid import secrets from datetime import datetime, timezone @@ -7,10 +8,12 @@ from typing import Optional, List class UserApiKeyRepository: + """Repository for generating and managing user API keys.""" def __init__(self, db: Session): self.db = db def create(self, user_id: str, name: str, description: str) -> UserApiKey: + """Create a new API key for the user with generated token.""" now = datetime.now(timezone.utc) apikey = secrets.token_urlsafe(32) user_apikey = UserApiKey( @@ -29,6 +32,7 @@ class UserApiKeyRepository: return user_apikey def update(self, id: str, name: Optional[str] = None, description: Optional[str] = None, expire_at: Optional[datetime] = None) -> UserApiKey: + """Update metadata for an existing API key (name, description, expiration).""" user_apikey = self.db.query(UserApiKey).filter(UserApiKey.id == id).first() if not user_apikey: raise ValueError("API key not found or does not belong to the user") @@ -46,6 +50,7 @@ class UserApiKeyRepository: return user_apikey def delete(self, id: str) -> None: + """Delete API key by primary ID.""" user_apikey = self.db.query(UserApiKey).filter(UserApiKey.id == id).first() if not user_apikey: raise ValueError("API key not found or does not belong to the user") @@ -54,7 +59,9 @@ class UserApiKeyRepository: self.db.commit() def get_by_user_id(self, user_id: str) -> List[UserApiKey]: + """List API keys belonging to the specified user.""" return self.db.query(UserApiKey).filter(UserApiKey.user_id == user_id).all() def get_by_id(self, id: str) -> Optional[UserApiKey]: + """Get API key by primary ID.""" return self.db.query(UserApiKey).filter(UserApiKey.id == id).first() diff --git a/services/admin_service/repositories/user_task_repository.py b/services/admin_service/repositories/user_task_repository.py index 4df7ef5..720c6f0 100644 --- a/services/admin_service/repositories/user_task_repository.py +++ b/services/admin_service/repositories/user_task_repository.py @@ -1,16 +1,20 @@ +"""User task repository: list and update user task status.""" from sqlalchemy.orm import Session from datetime import datetime, timezone from typing import List from services.common.models.user_task import UserTask class UserTaskRepository: + """Repository for managing user tasks (list, skip, update).""" def __init__(self, db: Session): self.db = db def user_tasks_by_id(self, user_id: str) -> List[UserTask]: + """Return all tasks belonging to the given user.""" return self.db.query(UserTask).filter(UserTask.user_id == user_id).all() def skip_task(self, user_id: str, task_id: str): + """Mark task as skipped (status=1); create record if missing.""" user_task = self.db.query(UserTask).filter(UserTask.user_id == user_id, UserTask.task_id == task_id).first() if user_task: user_task.task_status = 1 @@ -20,6 +24,7 @@ class UserTaskRepository: self.db.add(user_task) self.db.commit() def update_task_status(self, user_id: str, task_id: str, task_status: int): + """Update task status for user and task; create record if missing.""" user_task = self.db.query(UserTask).filter(UserTask.user_id == user_id, UserTask.task_id == task_id).first() status_int = 1 if int(task_status) == 1 else 0 if user_task: diff --git a/services/admin_service/services/billing_message_handler.py b/services/admin_service/services/billing_message_handler.py index b7f0029..0c0cc0e 100644 --- a/services/admin_service/services/billing_message_handler.py +++ b/services/admin_service/services/billing_message_handler.py @@ -56,7 +56,7 @@ class BillingMessageHandler: return False # Update stats_mcp_service_date record - # 统计按小时分桶(UTC 时区),对齐到整点 + # Bucket stats by hour (UTC), aligned to full hours start_dt = billing_message.call_start_time start_utc = start_dt.replace(tzinfo=timezone.utc) if start_dt.tzinfo is None else start_dt.astimezone(timezone.utc) # Store as naive UTC datetime to match MySQL DATETIME behavior diff --git a/services/admin_service/services/openapi_manager.py b/services/admin_service/services/openapi_manager.py index 910a189..a409622 100644 --- a/services/admin_service/services/openapi_manager.py +++ b/services/admin_service/services/openapi_manager.py @@ -128,5 +128,5 @@ class OpenApiManager: return await HttpUtils.validate_url_accessibility(url, timeout=10) -# 创建全局实例 +# Create global instance openapi_manager = OpenApiManager() diff --git a/services/admin_service/services/payment_service.py b/services/admin_service/services/payment_service.py index cc50ead..60b2aee 100644 --- a/services/admin_service/services/payment_service.py +++ b/services/admin_service/services/payment_service.py @@ -166,7 +166,7 @@ class PaymentService: if not user: logger.error(f"User with ID {user_id} not found.") raise ValueError("User not found.") - # 生成32位uuid + # Generate 32-character UUID transaction_id = str(uuid4()).replace("-", "") # create a new user wallet history entry for the deposit user_wallet_history = self.user_wallet_history_repository.add_deposit(user_id=user_id, transaction_id=transaction_id, amount=amount, payment_method="wechat") @@ -174,7 +174,7 @@ class PaymentService: logger.error("Failed to create user wallet history for deposit.") raise RuntimeError("Failed to create payment.") - # 获取配置参数 + # Get configuration parameters app_id = config.get(WxPayConfig.APP_ID) apiv3_key = config.get(WxPayConfig.APIV3_KEY) mch_id = config.get(WxPayConfig.MCH_ID) @@ -182,7 +182,7 @@ class PaymentService: cert_serial_no = config.get(WxPayConfig.CERT_SERIAL_NO) notify_url = config.get(WxPayConfig.NOTIFY_URL) - # 详细的配置验证 + # Detailed configuration validation missing_configs = [] if not app_id: missing_configs.append("app_id") if not apiv3_key: missing_configs.append("apiv3_key") @@ -196,7 +196,7 @@ class PaymentService: logger.error(f"Current config keys: {list(config.keys()) if config else 'None'}") return None - # 验证配置值的格式 + # Validate configuration value formats if not str(mch_id).isdigit(): logger.error(f"Invalid mch_id format: {mch_id} (should be numeric)") return None diff --git a/services/admin_service/services/stats_service.py b/services/admin_service/services/stats_service.py index 575f62d..236a78c 100644 --- a/services/admin_service/services/stats_service.py +++ b/services/admin_service/services/stats_service.py @@ -24,7 +24,7 @@ class StatsService: Returns: dict: Registered stats """ - # 当天0点 + # Start of day (00:00) today_start = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) pass30 = today_start - timedelta(days=30) @@ -40,7 +40,7 @@ class StatsService: Returns: dict: Deposit stats """ - # 当天0点 + # Start of day (00:00) today_start = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) pass30 = today_start - timedelta(days=30) @@ -56,7 +56,7 @@ class StatsService: Returns: dict: Call stats """ - # 当天0点 + # Start of day (00:00) today_start = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) pass30 = today_start - timedelta(days=30) @@ -75,11 +75,11 @@ class StatsService: """ now = datetime.now() pass30 = now - timedelta(days=30) - # 获取统计数据(仅包含有调用的服务) + # Get stats (include only services with calls) stats = self.stats_mcp_service_date_repository.stats_call_count_group_by_service(pass30) stats_map = {item["service_id"]: int(item.get("count", 0)) for item in stats} - # 遍历所有服务,填充缺失调用数为 0,并按调用数降序 + # Iterate all services, fill missing call count as 0, sort by calls desc services = self.mcp_service_repository.get_all() result = [ { @@ -90,6 +90,6 @@ class StatsService: } for service in services ] - # 先按调用数降序,再按名称升序 + # Sort by calls desc, then by name asc result.sort(key=lambda x: (-x["call_count"], x["name"])) return result \ No newline at end of file diff --git a/services/admin_service/services/user_stats_service.py b/services/admin_service/services/user_stats_service.py index 6f92f1b..5406172 100644 --- a/services/admin_service/services/user_stats_service.py +++ b/services/admin_service/services/user_stats_service.py @@ -80,7 +80,7 @@ class UserStatsService: if len(columns) == 0: result.append({"stats_day": date_str, "call_tool_count": 0, "amount": "0.0"}) else: - #columns[1]不用科学记数法,保留六位小数 + # columns[1] use fixed-point with six decimals, not scientific notation result.append({"stats_day": date_str, "call_tool_count": columns[0], "amount": columns[1]}) # result.append({"stats_day": date_str, "call_tool_count": call_count, "amount": amount}) diff --git a/services/admin_service/tasks/order_monitor_task.py b/services/admin_service/tasks/order_monitor_task.py index 693a344..aa24910 100644 --- a/services/admin_service/tasks/order_monitor_task.py +++ b/services/admin_service/tasks/order_monitor_task.py @@ -19,7 +19,7 @@ logger = get_logger(__name__) @dataclass class OrderInfo: - """订单信息数据类""" + """Order information data class""" created_time: datetime user_id: Optional[str] = None amount: Optional[float] = None @@ -28,7 +28,7 @@ class OrderInfo: @dataclass class PaymentChannelConfig: - """支付渠道配置""" + """Payment channel configuration""" channel_id: str enabled: bool config: Dict[str, Any] @@ -36,7 +36,7 @@ class PaymentChannelConfig: @dataclass class ClientStatus: - """客户端状态""" + """Client status""" enabled: bool last_check_time: datetime error_count: int = 0 @@ -44,57 +44,57 @@ class ClientStatus: class PaymentClient(ABC): - """支付客户端抽象基类""" + """Payment client abstract base class""" @abstractmethod def query_order_status(self, order_id: str) -> Optional[Dict[str, Any]]: - """查询订单状态""" + """Query order status""" pass @abstractmethod def parse_order_status(self, response: Dict[str, Any]) -> tuple[str, bool]: """ - 解析订单状态响应 + Parse order status response Returns: - tuple[status, should_remove]: (状态描述, 是否应该从监控队列移除) + tuple[status, should_remove]: (status description, whether to remove from monitor queue) """ pass class AlipayPaymentClient(PaymentClient): - """支付宝支付客户端""" + """Alipay payment client""" def __init__(self, app_id: str, app_private_key: str, alipay_public_key: str): self.client = AlipayClient(app_id, app_private_key, alipay_public_key) def query_order_status(self, order_id: str) -> Optional[Dict[str, Any]]: - """查询支付宝订单状态""" + """Query Alipay order status""" try: response = self.client.query_order_status(order_id) return response except Exception as e: - logger.error(f"查询支付宝订单 {order_id} 状态失败: {e}") + logger.error(f"Failed to query Alipay order {order_id} status: {e}") return None def parse_order_status(self, response: Dict[str, Any]) -> tuple[str, bool]: - """解析支付宝订单状态""" + """Parse Alipay order status""" try: - # 解析响应JSON + # Parse response JSON if isinstance(response, str): response_data = json.loads(response) else: response_data = response - # 获取响应内容 + # Get response content alipay_response = response_data.get('alipay_trade_query_response', {}) if alipay_response.get('code') != '10000': - logger.warning(f"支付宝订单查询失败: {alipay_response.get('msg', '未知错误')}") + logger.warning(f"Alipay order query failed: {alipay_response.get('msg', 'Unknown error')}") return "QUERY_FAILED", False trade_status = alipay_response.get('trade_status', '') - # 根据交易状态处理 + # Handle according to trade status if trade_status == 'WAIT_BUYER_PAY': return "WAITING", False elif trade_status in ['TRADE_SUCCESS', 'TRADE_FINISHED']: @@ -102,24 +102,24 @@ class AlipayPaymentClient(PaymentClient): elif trade_status in ['TRADE_CLOSED', 'TRADE_CANCELED']: return "FAILED", True else: - logger.warning(f"支付宝订单未知状态: {trade_status}") + logger.warning(f"Unknown Alipay trade status: {trade_status}") return f"UNKNOWN_{trade_status}", False except Exception as e: - logger.error(f"解析支付宝订单响应失败: {e}") + logger.error(f"Failed to parse Alipay order response: {e}") return "PARSE_ERROR", False class WxPayPaymentClient(PaymentClient): - """微信支付客户端""" + """WeChat Pay client""" def __init__(self, app_id: str, apiv3_key: str, mch_id: str, private_key: str, cert_serial_no: str,notify_url: str): self.client = WxPayClient(app_id, apiv3_key, mch_id, private_key, cert_serial_no,notify_url) def query_order_status(self, order_id: str) -> Optional[Dict[str, Any]]: - """查询微信支付订单状态""" + """Query WeChat Pay order status""" try: - # 微信支付客户端是异步的,需要在事件循环中运行 + # WeChat Pay client is async; run in event loop loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) try: @@ -128,15 +128,15 @@ class WxPayPaymentClient(PaymentClient): finally: loop.close() except Exception as e: - logger.error(f"查询微信支付订单 {order_id} 状态失败: {e}") + logger.error(f"Failed to query WeChat Pay order {order_id} status: {e}") return None def parse_order_status(self, response: Dict[str, Any]) -> tuple[str, bool]: - """解析微信支付订单状态""" + """Parse WeChat Pay order status""" try: trade_state = response.get('trade_state', '') - # 根据交易状态处理 + # Handle according to trade state if trade_state == 'NOTPAY': return "WAITING", False elif trade_state == 'SUCCESS': @@ -146,16 +146,16 @@ class WxPayPaymentClient(PaymentClient): elif trade_state == 'USERPAYING': return "PAYING", False else: - logger.warning(f"微信支付订单未知状态: {trade_state}") + logger.warning(f"Unknown WeChat trade state: {trade_state}") return f"UNKNOWN_{trade_state}", False except Exception as e: - logger.error(f"解析微信支付订单响应失败: {e}") + logger.error(f"Failed to parse WeChat order response: {e}") return "PARSE_ERROR", False class OrderMonitorTask: - """通用支付订单状态监控服务 - 单例模式""" + """Generic payment order status monitoring service - singleton pattern""" _instance = None _lock = threading.Lock() @@ -169,38 +169,37 @@ class OrderMonitorTask: return cls._instance def __init__(self): - # 确保只初始化一次 + # Ensure initialization only happens once if hasattr(self, '_initialized') and self._initialized: return self.payment_clients: Dict[str, PaymentClient] = {} - self.client_status: Dict[str, ClientStatus] = {} # 客户端状态管理 + self.client_status: Dict[str, ClientStatus] = {} # Client status management self.order_queue: Queue[OrderInfo] = Queue(maxsize=200) self.is_running = False self.monitor_thread: Optional[threading.Thread] = None - self.config_check_thread: Optional[threading.Thread] = None # 配置检查线程 - self.check_interval = 10 # 检查间隔10秒 - self.config_check_interval = 60 # 配置检查间隔60秒 - self.timeout_minutes = 6 # 超时时间6分钟 + self.config_check_thread: Optional[threading.Thread] = None # Configuration check thread + self.check_interval = 10 # Check interval: 10s + self.config_check_interval = 60 # Config check interval: 60s + self.timeout_minutes = 6 # Timeout: 6 minutes self._initialized = True - - logger.info("OrderMonitorTask 通用订单监控服务单例实例已创建") + logger.info("OrderMonitorTask singleton instance created") @classmethod def get_instance(cls) -> 'OrderMonitorTask': - """获取单例实例""" + """Get singleton instance""" if cls._instance is None: cls._instance = cls() return cls._instance def _load_payment_channels(self) -> List[PaymentChannelConfig]: - """加载支付渠道配置""" + """Load payment channel configuration""" db = SessionLocal() try: payment_channel_service = PaymentChannelService(db) channels = [] - # 加载支付宝配置 + # Load Alipay config alipay_config = payment_channel_service.get_config("alipay") if alipay_config: channels.append(PaymentChannelConfig( @@ -209,7 +208,7 @@ class OrderMonitorTask: config=alipay_config )) - # 加载微信支付配置 + # Load WeChat Pay config wechat_config = payment_channel_service.get_config("wechat") if wechat_config: channels.append(PaymentChannelConfig( @@ -223,24 +222,24 @@ class OrderMonitorTask: db.close() def _initialize_payment_clients(self, channels: List[PaymentChannelConfig]): - """初始化支付客户端""" - # 保存当前启用的客户端状态 + """Initialize payment clients""" + # Save currently enabled client status current_enabled = {k: v.enabled for k, v in self.client_status.items()} for channel in channels: try: - # 检查客户端是否应该启用 + # Check whether client should be enabled should_enable = channel.enabled and current_enabled.get(channel.channel_id, True) if not should_enable: - # 如果客户端被禁用,从payment_clients中移除但保留状态 + # If the client is disabled, remove from payment_clients but keep status if channel.channel_id in self.payment_clients: del self.payment_clients[channel.channel_id] self.client_status[channel.channel_id] = ClientStatus( enabled=False, last_check_time=datetime.now() ) - logger.info(f"{channel.channel_id} 支付客户端已禁用") + logger.info(f"Payment client {channel.channel_id} is disabled") continue if channel.channel_id == "alipay": @@ -254,7 +253,7 @@ class OrderMonitorTask: enabled=True, last_check_time=datetime.now() ) - logger.info("支付宝支付客户端初始化成功") + logger.info("Alipay payment client initialized successfully") elif channel.channel_id == "wechat": client = WxPayPaymentClient( @@ -270,11 +269,11 @@ class OrderMonitorTask: enabled=True, last_check_time=datetime.now() ) - logger.info("微信支付客户端初始化成功") + logger.info("WeChat Pay client initialized successfully") except Exception as e: - logger.error(f"初始化 {channel.channel_id} 支付客户端失败: {e}") - # 记录错误状态 + logger.error(f"Failed to initialize {channel.channel_id} payment client: {e}") + # Record error status if channel.channel_id in self.client_status: self.client_status[channel.channel_id].error_count += 1 self.client_status[channel.channel_id].last_error = str(e) @@ -288,21 +287,21 @@ class OrderMonitorTask: def add_order_to_monitor(self, order_info: Optional[OrderInfo] = None, payment_id: Optional[str] = None) -> bool: """ - 添加订单到监控队列 + Add order to monitor queue Args: - order_info: 订单信息(可选) - payment_id: 支付订单ID(可选) + order_info: Order information (optional) + payment_id: Payment order ID (optional) Returns: - bool: 是否添加成功 + bool: Whether added successfully """ if payment_id is not None: - # 创建新的数据库会话来查询订单 + # Create new DB session to query order db = SessionLocal() try: user_wallet_history_service = UserWalletHistoryService(db) order = user_wallet_history_service.get_order_by_id(payment_id) if order is None: - logger.error(f"订单 {payment_id} 不存在,无法添加到监控队列") + logger.error(f"Order {payment_id} does not exist; cannot add to monitor queue") return False order_info = OrderInfo( created_time=order.created_at, @@ -315,78 +314,78 @@ class OrderMonitorTask: db.close() if order_info is None: - logger.error("订单信息为空,无法添加到监控队列") + logger.error("Order info is empty; cannot add to monitor queue") return False - # 检查支付渠道是否支持且启用 + # Check whether payment channel is supported and enabled if order_info.payment_channel_id is None: - logger.warning("支付渠道ID为空,无法添加到监控队列") + logger.warning("Payment channel ID is empty; cannot add to monitor queue") return False if (order_info.payment_channel_id not in self.client_status or not self.client_status[order_info.payment_channel_id].enabled): - logger.warning(f"支付渠道 {order_info.payment_channel_id} 未启用,无法添加到监控队列") + logger.warning(f"Payment channel {order_info.payment_channel_id} not enabled; cannot add to monitor queue") return False try: self.order_queue.put_nowait(order_info) - logger.info(f"订单 {order_info.payment_id} ({order_info.payment_channel_id}) 已添加到监控队列") + logger.info(f"Order {order_info.payment_id} ({order_info.payment_channel_id}) added to monitor queue") return True except Exception as e: - logger.error(f"添加订单到监控队列失败: {e}") + logger.error(f"Failed to add order to monitor queue: {e}") return False def start_monitor(self) -> bool: """ - 启动订单状态监控 + Start order status monitoring Returns: - bool: 是否启动成功 + bool: Whether started successfully """ if self.is_running: - logger.warning("订单监控服务已在运行中") + logger.warning("Order monitoring service is already running") return False try: - # 加载支付渠道配置 + # Load payment channel configuration channels = self._load_payment_channels() if not channels: - logger.error("未找到可用的支付渠道配置") + logger.error("No available payment channel configuration found") return False - # 初始化支付客户端 + # Initialize payment clients self._initialize_payment_clients(channels) - # 加载待监控的订单 + # Load pending orders self._load_pending_orders() - # 启动监控线程 + # Start monitoring thread self.is_running = True self.monitor_thread = threading.Thread(target=self._monitor_loop, daemon=True) self.monitor_thread.start() - # 启动配置检查线程 + # Start configuration check thread self.config_check_thread = threading.Thread(target=self._config_check_loop, daemon=True) self.config_check_thread.start() enabled_clients = [k for k, v in self.client_status.items() if v.enabled] - logger.info(f"通用订单监控服务已启动,启用的支付渠道: {enabled_clients}") + logger.info(f"Order monitoring service started; enabled payment channels: {enabled_clients}") return True except Exception as e: - logger.error(f"启动订单监控服务失败: {e}") + logger.error(f"Failed to start order monitoring service: {e}") self.is_running = False return False def _load_pending_orders(self): - """加载待监控的订单""" + """Load pending orders to monitor""" db = SessionLocal() try: user_wallet_history_service = UserWalletHistoryService(db) - # 获取5分钟前的时间 + # Get timestamp for 5 minutes ago start_time = datetime.now() - timedelta(minutes=5) - # 加载各个支付渠道的待处理订单 + # Load pending orders for each payment channel for channel_id in self.payment_clients.keys(): order_list = user_wallet_history_service.order_list(channel_id, 0, start_time) for order in order_list: @@ -399,100 +398,100 @@ class OrderMonitorTask: ) self.add_order_to_monitor(order_info=order_info) - logger.info(f"已加载待监控订单,当前队列大小: {self.order_queue.qsize()}") + logger.info(f"Loaded pending orders; current queue size: {self.order_queue.qsize()}") finally: db.close() def stop_monitor(self) -> bool: """ - 停止订单状态监控 + Stop order status monitoring Returns: - bool: 是否停止成功 + bool: Whether stopped successfully """ if not self.is_running: - logger.warning("订单监控服务未在运行") + logger.warning("Order monitoring service is not running") return False try: self.is_running = False - # 停止监控线程 + # Stop monitoring thread if self.monitor_thread and self.monitor_thread.is_alive(): self.monitor_thread.join(timeout=10) - # 停止配置检查线程 + # Stop configuration check thread if self.config_check_thread and self.config_check_thread.is_alive(): self.config_check_thread.join(timeout=5) - logger.info("通用订单监控服务已停止") + logger.info("Order monitoring service stopped") return True except Exception as e: - logger.error(f"停止订单监控服务失败: {e}") + logger.error(f"Failed to stop order monitoring service: {e}") return False def _monitor_loop(self): - """监控循环主逻辑""" - logger.info("订单监控循环开始") + """Main monitoring loop logic""" + logger.info("Order monitoring loop started") while self.is_running: try: - # 获取当前队列中的所有订单 + # Get all orders in the current queue current_orders = self._get_all_orders_from_queue() if not current_orders: time.sleep(self.check_interval) continue - logger.info(f"开始检查 {len(current_orders)} 个订单状态") + logger.info(f"Starting to check status of {len(current_orders)} orders") - # 按支付渠道分组检查订单 + # Group orders by payment channel for checking orders_by_channel = {} for order in current_orders: channel_id = order.payment_channel_id if channel_id is None: - logger.warning(f"订单 {order.payment_id} 的支付渠道ID为空,跳过监控") + logger.warning(f"Order {order.payment_id} payment channel ID is empty; skipping monitoring") continue if channel_id not in orders_by_channel: orders_by_channel[channel_id] = [] orders_by_channel[channel_id].append(order) - # 检查每个渠道的订单 + # Check orders for each channel remaining_orders = [] for channel_id, orders in orders_by_channel.items(): - # 检查客户端是否启用 + # Check whether client is enabled if channel_id not in self.client_status or not self.client_status[channel_id].enabled: - logger.warning(f"支付渠道 {channel_id} 未启用,跳过 {len(orders)} 个订单的检查") - # 将订单重新放回队列等待客户端启用 + logger.warning(f"Payment channel {channel_id} not enabled; skipping checks for {len(orders)} orders") + # Put orders back into queue while waiting for client to be enabled remaining_orders.extend(orders) continue - logger.info(f"检查 {channel_id} 渠道的 {len(orders)} 个订单") + logger.info(f"Checking {len(orders)} orders for channel {channel_id}") for order in orders: try: should_keep = self._check_single_order(order) if should_keep: remaining_orders.append(order) except Exception as e: - logger.error(f"检查订单 {order.payment_id} 状态失败: {e}") - # 出错的订单重新放回队列 + logger.error(f"Check status for order {order.payment_id}: {e}") + # Put failed-checked orders back into the queue remaining_orders.append(order) - # 将剩余订单重新放回队列 + # Put remaining orders back into the queue for order in remaining_orders: self.order_queue.put_nowait(order) - logger.info(f"本轮检查完成,剩余 {len(remaining_orders)} 个订单继续监控") + logger.info(f"Round complete; {len(remaining_orders)} orders remain for monitoring") except Exception as e: - logger.error(f"监控循环出现异常: {e}") + logger.error(f"Monitoring loop encountered an exception: {e}") - # 等待下次检查 + # Wait for next check time.sleep(self.check_interval) - logger.info("订单监控循环结束") + logger.info("Order monitoring loop ended") def _get_all_orders_from_queue(self) -> List[OrderInfo]: - """从队列中获取所有订单""" + """Get all orders from queue""" orders = [] while True: try: @@ -504,42 +503,42 @@ class OrderMonitorTask: def _check_single_order(self, order: OrderInfo) -> bool: """ - 检查单个订单状态 + Check single order status Args: - order: 订单信息 + order: Order information Returns: - bool: 是否需要继续监控(True=继续,False=移除) + bool: Whether to continue monitoring (True=continue, False=remove) """ - # 检查是否超时 + # Check timeout if self._is_order_timeout(order): - logger.info(f"订单 {order.payment_id} 已超时,从监控队列移除") + logger.info(f"Order {order.payment_id} timed out; removing from monitor queue") self._handle_payment_failed(order) return False - # 获取对应的支付客户端 + # Get the corresponding payment client if order.payment_channel_id is None: - logger.error("支付渠道ID为空,无法查询状态") + logger.error("Payment channel ID is empty; cannot query status") return False client = self.payment_clients.get(order.payment_channel_id) if client is None: - logger.error(f"未找到 {order.payment_channel_id} 支付客户端") + logger.error(f"Payment client for {order.payment_channel_id} not found") return False if order.payment_id is None: - logger.error("订单ID为空,无法查询状态") + logger.error("Order ID is empty; cannot query status") return False try: - # 查询订单状态 + # Query order status response = client.query_order_status(order.payment_id) if response is None: - logger.warning(f"订单 {order.payment_id} 查询响应为空,继续监控") + logger.warning(f"Order {order.payment_id} query response is empty; continue monitoring") return True - # 解析订单状态 + # Parse order status status, should_remove = client.parse_order_status(response) - logger.info(f"订单 {order.payment_id} ({order.payment_channel_id}) 状态: {status}") + logger.info(f"Order {order.payment_id} ({order.payment_channel_id}) status: {status}") if should_remove: if status == "SUCCESS": @@ -548,26 +547,26 @@ class OrderMonitorTask: self._handle_payment_failed(order) return False else: - # 订单仍在等待支付,继续监控 + # Order is still awaiting payment; continue monitoring return True except Exception as e: - logger.error(f"查询订单 {order.payment_id} 状态失败: {e}") - # 查询失败,继续监控 + logger.error(f"Failed to query status for order {order.payment_id}: {e}") + # Query failed; continue monitoring return True def _is_order_timeout(self, order: OrderInfo) -> bool: - """检查订单是否超时""" + """Check if order timed out""" timeout_time = order.created_time + timedelta(minutes=self.timeout_minutes) return datetime.utcnow() > timeout_time def _handle_payment_success(self, order: OrderInfo): - """处理支付成功""" + """Handle payment success""" try: - logger.info(f"订单 {order.payment_id} ({order.payment_channel_id}) 支付成功") + logger.info(f"Order {order.payment_id} ({order.payment_channel_id}) payment succeeded") if order.payment_id is not None and order.payment_channel_id is not None: - # 创建新的数据库会话来处理支付回调 + # Create new DB session to handle payment callback db = SessionLocal() try: payment_service = PaymentService(db) @@ -576,23 +575,23 @@ class OrderMonitorTask: payment_channel_id=order.payment_channel_id, ) if result: - logger.info(f"订单 {order.payment_id} 支付成功处理完成") + logger.info(f"Order {order.payment_id} payment success handling completed") else: - logger.error(f"订单 {order.payment_id} 支付成功处理失败") + logger.error(f"Order {order.payment_id} payment success handling failed") finally: db.close() else: - logger.error("订单ID或支付渠道ID为空,无法处理支付成功") + logger.error("Order ID or payment channel ID is empty; cannot handle payment success") except Exception as e: - logger.error(f"处理订单 {order.payment_id} 支付成功时发生错误: {e}") + logger.error(f"Error while handling payment success for order {order.payment_id}: {e}") def _handle_payment_failed(self, order: OrderInfo): - """处理支付失败""" + """Handle payment failure""" try: - logger.info(f"订单 {order.payment_id} ({order.payment_channel_id}) 支付失败") + logger.info(f"Order {order.payment_id} ({order.payment_channel_id}) payment failed") if order.payment_id is not None and order.payment_channel_id is not None: - # 创建新的数据库会话来处理支付回调 + # Create new DB session to handle payment callback db = SessionLocal() try: payment_service = PaymentService(db) @@ -602,18 +601,18 @@ class OrderMonitorTask: status=2 ) if result: - logger.info(f"订单 {order.payment_id} 支付失败处理完成") + logger.info(f"Order {order.payment_id} payment failure handling completed") else: - logger.error(f"订单 {order.payment_id} 支付失败处理失败") + logger.error(f"Order {order.payment_id} payment failure handling failed") finally: db.close() else: - logger.error("订单ID或支付渠道ID为空,无法处理支付失败") + logger.error("Order ID or payment channel ID is empty; cannot handle payment failure") except Exception as e: - logger.error(f"处理订单 {order.payment_id} 支付失败时发生错误: {e}") - + logger.error(f"Error while handling payment failure for order {order.payment_id}: {e}") + def get_monitor_status(self) -> Dict[str, Any]: - """获取监控服务状态""" + """Get monitoring service status""" client_status_info = {} for channel_id, status in self.client_status.items(): client_status_info[channel_id] = { @@ -636,22 +635,22 @@ class OrderMonitorTask: def enable_client(self, channel_id: str) -> bool: """ - 启用指定的支付客户端 + Enable specified payment client Args: - channel_id: 支付渠道ID + channel_id: Payment channel ID Returns: - bool: 是否启用成功 + bool: Whether enabled successfully """ if channel_id not in self.client_status: - logger.error(f"未知的支付渠道: {channel_id}") + logger.error(f"Unknown payment channel: {channel_id}") return False if self.client_status[channel_id].enabled: - logger.warning(f"支付客户端 {channel_id} 已经启用") + logger.warning(f"Payment client {channel_id} is already enabled") return True try: - # 重新加载配置并初始化客户端 + # Reload configuration and initialize client channels = self._load_payment_channels() target_channel = None for channel in channels: @@ -660,82 +659,82 @@ class OrderMonitorTask: break if target_channel is None: - logger.error(f"未找到 {channel_id} 的配置") + logger.error(f"Configuration for {channel_id} not found") return False - # 启用客户端状态 + # Enable client status self.client_status[channel_id].enabled = True - # 重新初始化该客户端 + # Re-initialize the client self._initialize_payment_clients([target_channel]) - logger.info(f"支付客户端 {channel_id} 已启用") + logger.info(f"Payment client {channel_id} enabled") return True except Exception as e: - logger.error(f"启用支付客户端 {channel_id} 失败: {e}") + logger.error(f"Failed to enable payment client {channel_id}: {e}") self.client_status[channel_id].enabled = False return False def disable_client(self, channel_id: str) -> bool: """ - 禁用指定的支付客户端 + Disable specified payment client Args: - channel_id: 支付渠道ID + channel_id: Payment channel ID Returns: - bool: 是否禁用成功 + bool: Whether disabled successfully """ if channel_id not in self.client_status: - logger.error(f"未知的支付渠道: {channel_id}") + logger.error(f"Unknown payment channel: {channel_id}") return False if not self.client_status[channel_id].enabled: - logger.warning(f"支付客户端 {channel_id} 已经禁用") + logger.warning(f"Payment client {channel_id} is already disabled") return True try: - # 禁用客户端状态 + # Disable client status self.client_status[channel_id].enabled = False - # 从payment_clients中移除 + # Remove from payment_clients if channel_id in self.payment_clients: del self.payment_clients[channel_id] - logger.info(f"支付客户端 {channel_id} 已禁用") + logger.info(f"Payment client {channel_id} disabled") return True except Exception as e: - logger.error(f"禁用支付客户端 {channel_id} 失败: {e}") + logger.error(f"Failed to disable payment client {channel_id}: {e}") return False def _config_check_loop(self): - """配置检查循环 - 定时检查配置变化并重新加载客户端""" - logger.info("配置检查循环开始") + """Configuration check loop - periodically checks for config changes and reloads clients""" + logger.info("Configuration check loop started") while self.is_running: try: - # 等待检查间隔 + # Wait for check interval time.sleep(self.config_check_interval) if not self.is_running: break - logger.debug("开始检查支付渠道配置变化") + logger.debug("Starting to check payment channel configuration changes") - # 重新加载配置 + # Reload configuration channels = self._load_payment_channels() if not channels: - logger.warning("未找到任何支付渠道配置") + logger.warning("No payment channel configuration found") continue - # 检查配置是否有变化 + # Check whether configuration changed config_changed = False for channel in channels: if channel.channel_id not in self.client_status: config_changed = True break - # 检查启用状态是否变化 + # Check whether enabled state changed current_enabled = self.client_status[channel.channel_id].enabled if channel.enabled != current_enabled: config_changed = True @@ -743,22 +742,22 @@ class OrderMonitorTask: if config_changed: logger.info(self.client_status) - logger.info("检测到支付渠道配置变化,重新初始化客户端") + logger.info("Detected payment channel configuration changes; reinitializing clients") self._initialize_payment_clients(channels) - # 更新检查时间 + # Update check time for channel_id in self.client_status: self.client_status[channel_id].last_check_time = datetime.now() else: - logger.debug("支付渠道配置无变化") + logger.debug("No changes in payment channel configuration") except Exception as e: - logger.error(f"配置检查循环出现异常: {e}") + logger.error(f"Configuration check loop encountered an exception: {e}") - logger.info("配置检查循环结束") + logger.info("Configuration check loop ended") def get_client_list(self) -> List[Dict[str, Any]]: - """获取所有客户端列表及状态""" + """Get list of all clients and their status""" clients = [] for channel_id, status in self.client_status.items(): clients.append({ diff --git a/services/admin_service/utils/alipay_client.py b/services/admin_service/utils/alipay_client.py index 098f011..44daeef 100644 --- a/services/admin_service/utils/alipay_client.py +++ b/services/admin_service/utils/alipay_client.py @@ -2,10 +2,7 @@ import os import ssl import urllib3 -# 禁用SSL证书验证警告 -urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) - -# 禁用SSL证书验证 +# Disable SSL certificate verification warnings try: _create_unverified_https_context = ssl._create_unverified_context except AttributeError: diff --git a/services/admin_service/utils/wxpay_client.py b/services/admin_service/utils/wxpay_client.py index b6f2b53..b834621 100644 --- a/services/admin_service/utils/wxpay_client.py +++ b/services/admin_service/utils/wxpay_client.py @@ -20,7 +20,7 @@ class WxPayClient: def __init__(self, app_id: str, apiv3_key: str, mch_id: str, private_key: str, cert_serial_no: str, notify_url: str): """Initialize WeChat Pay client with required parameters.""" - # 验证输入参数 + # Validate input parameters if not all([app_id, apiv3_key, mch_id, private_key, cert_serial_no, notify_url]): missing_params = [] if not app_id: missing_params.append("app_id") @@ -34,7 +34,7 @@ class WxPayClient: logger.error(error_msg) raise ValueError(error_msg) - # 存储配置参数 + # Store configuration parameters self.app_id = app_id self.apiv3_key = apiv3_key self.mch_id = mch_id @@ -42,21 +42,21 @@ class WxPayClient: self.cert_serial_no = cert_serial_no self.notify_url = notify_url - # 修正证书目录路径计算 - 从 services/admin_service/utils/ 向上4级到项目根目录 + # Fix certificate directory path: go up 4 levels from services/admin_service/utils to project root current_dir = os.path.dirname(__file__) # utils/ admin_service_dir = os.path.dirname(current_dir) # admin_service/ services_dir = os.path.dirname(admin_service_dir) # services/ - project_root = os.path.dirname(services_dir) # 项目根目录 + project_root = os.path.dirname(services_dir) # Project root directory self.cert_dir = os.path.join(project_root, 'wx_cer') - # 检查证书目录是否存在 + # Check if certificate directory exists if not os.path.exists(self.cert_dir): logger.error(f"Certificate directory not found: {self.cert_dir}") logger.error(f"Please ensure wx_cer directory exists in project root") raise FileNotFoundError(f"Certificate directory not found: {self.cert_dir}") else: logger.info(f"Certificate directory found: {self.cert_dir}") - # 列出证书目录中的文件 + # List files in certificate directory cert_files = os.listdir(self.cert_dir) logger.info(f"Certificate files: {cert_files}") @@ -68,13 +68,13 @@ class WxPayClient: logger.info(f"Creating WeChat Pay order: {order_id}, amount: {amount}, description: {description}") logger.info(f"Using certificate directory: {self.cert_dir}") - # 验证金额格式 (微信支付要求金额为分,整数) + # Validate amount format (WeChat requires amount in cents, integer) amount_fen = int(amount * 100) if amount_fen <= 0: logger.error(f"Invalid amount: {amount} (must be positive)") return None - # 使用异步上下文管理器 + # Use asynchronous context manager async with AsyncWeChatPay( wechatpay_type=WeChatPayType.NATIVE, mchid=self.mch_id, @@ -83,17 +83,17 @@ class WxPayClient: apiv3_key=self.apiv3_key, appid=self.app_id, notify_url=self.notify_url, - cert_dir=self.cert_dir, # 使用正确的证书目录路径 + cert_dir=self.cert_dir, # Use correct certificate directory path partner_mode=False ) as wxpay: - # 调用微信支付API + # Call WeChat Pay API code, message = await wxpay.pay( out_trade_no=order_id, amount={ "total": amount_fen, "currency": "CNY" - }, # 微信支付金额单位为分 + }, # WeChat Pay amount unit is cents # amount= amount_fen, description=description, ) @@ -126,7 +126,7 @@ class WxPayClient: try: logger.info(f"Querying WeChat Pay order: {order_id}") - # 使用异步上下文管理器 + # Use asynchronous context manager async with AsyncWeChatPay( wechatpay_type=WeChatPayType.NATIVE, mchid=self.mch_id, @@ -135,7 +135,7 @@ class WxPayClient: apiv3_key=self.apiv3_key, appid=self.app_id, notify_url=self.notify_url, - cert_dir=self.cert_dir, # 使用正确的证书目录路径 + cert_dir=self.cert_dir, # Use correct certificate directory path partner_mode=False ) as wxpay: diff --git a/services/api_service/repositories/mcp_service_repository.py b/services/api_service/repositories/mcp_service_repository.py index c33437a..6dd601a 100644 --- a/services/api_service/repositories/mcp_service_repository.py +++ b/services/api_service/repositories/mcp_service_repository.py @@ -1,3 +1,4 @@ +"""Repository for MCP services in API service: cached reads by ID/slug.""" from sqlalchemy.orm import Session from services.common.models.mcp_service import McpService from services.common.utils.cache_utils import CacheUtils @@ -8,7 +9,7 @@ logger = logging.getLogger(__name__) class McpServiceRepository: - """MCP service repository layer for API service""" + """Repository for MCP services in API service: cached reads by ID/slug.""" def __init__(self, db: Session): self.db = db @@ -37,7 +38,7 @@ class McpServiceRepository: """ Get single MCP service by slug name """ - cache_key = f"mcp_service:slug:{slug_name}" + cache_key = f"xpakc:mcp_service:slug:{slug_name}" # Try to get from cache using SQLAlchemy-specific method cached_model = CacheUtils.get_sqlalchemy_cache(cache_key, McpService) diff --git a/services/api_service/repositories/mcp_tool_api_repository.py b/services/api_service/repositories/mcp_tool_api_repository.py index 3d2e180..a0f7122 100644 --- a/services/api_service/repositories/mcp_tool_api_repository.py +++ b/services/api_service/repositories/mcp_tool_api_repository.py @@ -1,3 +1,4 @@ +"""Repository for MCP tool APIs in API service: list and get operations.""" from sqlalchemy.orm import Session from services.common.models.mcp_tool_api import McpToolApi from typing import Optional, List diff --git a/services/api_service/services/billing_service.py b/services/api_service/services/billing_service.py index f31ef12..eb408eb 100644 --- a/services/api_service/services/billing_service.py +++ b/services/api_service/services/billing_service.py @@ -66,7 +66,7 @@ class BillingService: charge_type=charge_type.value ) case ChargeType.PER_TOKEN: - # 计算预估费用(按每百万Token计费) + # Calculate estimated cost (charged per million tokens) estimated_input_cost = (Decimal(100) / Decimal("1000000")) * input_token_price estimated_output_cost = (Decimal(500) / Decimal("1000000")) * output_token_price service_price = estimated_input_cost + estimated_output_cost