import json import hashlib from typing import Optional, List, Dict, Any, Tuple from fastapi import UploadFile from sqlalchemy import select, func, delete from sqlalchemy.ext.asyncio import AsyncSession from app.core.biz_exception import BizLogicError from app.core.redis import get_redis_client from app.core.logger import logger from app.models.plugin import Plugin, PluginVersion from app.schemas.plugin import PluginUpdate from app.services.seaweedfs_service import SeaweedFSService class PluginService: @classmethod async def publish_plugin( cls, db: AsyncSession, name: str, version: str, display_name: Optional[str] = "", filename: Optional[str] = "", class_name: Optional[str] = "", protocol: Optional[str] = "IPlugin", description: Optional[str] = "", file: Optional[UploadFile] = None, download_url: Optional[str] = None, sha256: Optional[str] = "", changelog: Optional[str] = None, set_as_latest: bool = True, developer_id: Optional[str] = None, ) -> Dict[str, Any]: """ 发布插件新版本: 1. 支持直接上传 .py 文件(通过 SeaweedFSService 存储并计算 sha256)或直接传入 download_url 2. 自动更新/创建 plugins 基础信息(记录开发管理者 developer_id、脚本文件名 filename、入口类名 class_name、协议 protocol) 3. 记录 plugin_versions 发布历史 """ if not name or not version: raise BizLogicError("Plugin name and version are required") final_download_url = download_url final_sha256 = (sha256 or "").strip() # 计算并推导文件名 resolved_filename = (filename or "").strip() if file is not None and file.filename: if not resolved_filename: resolved_filename = file.filename file_bytes = await file.read() if not file_bytes: raise BizLogicError("Uploaded file is empty") final_sha256 = hashlib.sha256(file_bytes).hexdigest() await file.seek(0) # 上传至 SeaweedFS upload_result = await SeaweedFSService.upload(file) final_download_url = upload_result.get("url") if not final_download_url: raise BizLogicError("Failed to retrieve file download URL from storage") if not final_download_url: raise BizLogicError("Either file upload or download_url must be provided") if not resolved_filename: resolved_filename = f"{name}.py" resolved_class_name = (class_name or "").strip() resolved_protocol = (protocol or "").strip() or "IPlugin" # 1. 查找或创建 Plugin 基础记录 stmt = select(Plugin).where(Plugin.name == name) res = await db.execute(stmt) plugin = res.scalar_one_or_none() dev_id = developer_id.strip() if (developer_id and developer_id.strip()) else None if plugin is None: # 若初次创建且类名未指定,智能推导驼峰类名,如 vfs_plugin -> VFSPlugin if not resolved_class_name: parts = [p.capitalize() for p in name.split("_") if p] resolved_class_name = "".join(parts) or name plugin = Plugin( name=name, display_name=display_name or name, filename=resolved_filename, class_name=resolved_class_name, protocol=resolved_protocol, latest_version=version, description=description or "", developer_id=dev_id, status=1, ) db.add(plugin) await db.flush() else: if display_name: plugin.display_name = display_name if description is not None and description != "": plugin.description = description if developer_id is not None: plugin.developer_id = dev_id if resolved_filename: plugin.filename = resolved_filename if resolved_class_name: plugin.class_name = resolved_class_name if resolved_protocol: plugin.protocol = resolved_protocol if set_as_latest: plugin.latest_version = version # 2. 查找或创建 PluginVersion 历史版本记录 v_stmt = select(PluginVersion).where( PluginVersion.plugin_id == plugin.id, PluginVersion.version == version, ) v_res = await db.execute(v_stmt) pv = v_res.scalar_one_or_none() if pv is None: pv = PluginVersion( plugin_id=plugin.id, plugin_name=name, version=version, download_url=final_download_url, sha256=final_sha256, changelog=changelog, ) db.add(pv) else: pv.download_url = final_download_url pv.sha256 = final_sha256 if changelog is not None: pv.changelog = changelog if set_as_latest: plugin.latest_version = version await db.commit() await db.refresh(plugin) await db.refresh(pv) return { "plugin_id": plugin.id, "name": plugin.name, "version": pv.version, "download_url": pv.download_url, "sha256": pv.sha256, "class_name": plugin.class_name, "protocol": plugin.protocol, } @classmethod async def get_plugin_list( cls, db: AsyncSession, page: int = 1, page_size: int = 20, status: Optional[int] = None, ) -> Dict[str, Any]: """分页获取插件列表""" query = select(Plugin) count_query = select(func.count(Plugin.id)) if status is not None: query = query.where(Plugin.status == status) count_query = count_query.where(Plugin.status == status) total_res = await db.execute(count_query) total = total_res.scalar() or 0 query = query.order_by(Plugin.id.desc()).offset((page - 1) * page_size).limit(page_size) res = await db.execute(query) plugins = res.scalars().all() return { "list": plugins, "total": total, } @classmethod async def get_plugin_detail(cls, db: AsyncSession, plugin_id: int) -> Dict[str, Any]: """获取单个插件详情(包含所有历史发布版本)""" stmt = select(Plugin).where(Plugin.id == plugin_id) res = await db.execute(stmt) plugin = res.scalar_one_or_none() if not plugin: raise BizLogicError(f"Plugin with id {plugin_id} not found") v_stmt = select(PluginVersion).where( PluginVersion.plugin_id == plugin_id ).order_by(PluginVersion.id.desc()) v_res = await db.execute(v_stmt) versions = v_res.scalars().all() return { "id": plugin.id, "name": plugin.name, "display_name": plugin.display_name, "filename": plugin.filename, "latest_version": plugin.latest_version, "description": plugin.description, "status": plugin.status, "developer_id": plugin.developer_id, "versions": versions, } @classmethod async def update_plugin( cls, db: AsyncSession, plugin_id: int, plugin_in: PluginUpdate, ) -> Plugin: """更新插件基础信息""" stmt = select(Plugin).where(Plugin.id == plugin_id) res = await db.execute(stmt) plugin = res.scalar_one_or_none() if not plugin: raise BizLogicError(f"Plugin with id {plugin_id} not found") if plugin_in.display_name is not None: plugin.display_name = plugin_in.display_name if plugin_in.filename is not None: plugin.filename = plugin_in.filename.strip() if plugin_in.class_name is not None: plugin.class_name = plugin_in.class_name.strip() if plugin_in.protocol is not None: plugin.protocol = plugin_in.protocol.strip() if plugin_in.description is not None: plugin.description = plugin_in.description if plugin_in.status is not None: plugin.status = plugin_in.status if plugin_in.developer_id is not None: dev_id = plugin_in.developer_id.strip() plugin.developer_id = dev_id if dev_id else None await db.commit() await db.refresh(plugin) return plugin @classmethod async def switch_version( cls, db: AsyncSession, plugin_id: int, version: str, ) -> Plugin: """切换插件的生效版本(一键回滚/切换推荐版本)""" stmt = select(Plugin).where(Plugin.id == plugin_id) res = await db.execute(stmt) plugin = res.scalar_one_or_none() if not plugin: raise BizLogicError(f"Plugin with id {plugin_id} not found") # 校验该版本是否存在 v_stmt = select(PluginVersion).where( PluginVersion.plugin_id == plugin_id, PluginVersion.version == version, ) v_res = await db.execute(v_stmt) pv = v_res.scalar_one_or_none() if not pv: raise BizLogicError(f"Version '{version}' does not exist for plugin '{plugin.name}'") plugin.latest_version = version await db.commit() await db.refresh(plugin) return plugin @classmethod async def delete_plugin(cls, db: AsyncSession, plugin_id: int) -> bool: """删除整套插件及其所有版本记录""" stmt = select(Plugin).where(Plugin.id == plugin_id) res = await db.execute(stmt) plugin = res.scalar_one_or_none() if not plugin: raise BizLogicError(f"Plugin with id {plugin_id} not found") # 删除所有版本 await db.execute( delete(PluginVersion).where(PluginVersion.plugin_id == plugin_id) ) # 删除插件主体 await db.delete(plugin) await db.commit() return True @classmethod async def delete_version(cls, db: AsyncSession, plugin_id: int, version: str) -> bool: """删除单个历史版本记录(禁止直接删除当前生效的 latest_version)""" stmt = select(Plugin).where(Plugin.id == plugin_id) res = await db.execute(stmt) plugin = res.scalar_one_or_none() if not plugin: raise BizLogicError(f"Plugin with id {plugin_id} not found") if plugin.latest_version == version: raise BizLogicError( f"Cannot delete active latest version '{version}'. Please switch to another version first." ) v_stmt = select(PluginVersion).where( PluginVersion.plugin_id == plugin_id, PluginVersion.version == version, ) v_res = await db.execute(v_stmt) pv = v_res.scalar_one_or_none() if not pv: raise BizLogicError(f"Version '{version}' not found for plugin '{plugin.name}'") await db.delete(pv) await db.commit() return True @classmethod async def dispatch_upgrade( cls, db: AsyncSession, target_node: str = "all", plugins: Optional[List[str]] = None, ) -> List[Dict[str, Any]]: """ 下发插件升级命令: 1. 组装指定插件(或所有已启用插件)的最新版本配置 2. 通过 Redis 发布到指定频道广播通知节点 Upgrade-Listener """ query = select(Plugin).where(Plugin.status == 1) if plugins and len(plugins) > 0: query = query.where(Plugin.name.in_(plugins)) res = await db.execute(query) target_plugins = res.scalars().all() payload = [] for p in target_plugins: # 获取对应的 latest_version 记录 v_stmt = select(PluginVersion).where( PluginVersion.plugin_id == p.id, PluginVersion.version == p.latest_version, ) v_res = await db.execute(v_stmt) pv = v_res.scalar_one_or_none() if pv: payload.append({ "name": p.name, "filename": p.filename or f"{p.name}.py", "version": p.latest_version, "download_url": pv.download_url, "sha256": pv.sha256, }) # 确定广播频道 if not target_node or target_node.strip() == "" or target_node == "all": channel = "vs:channel:upgrade" else: channel = f"vs:channel:upgrade:{target_node.strip()}" try: redis_client = await get_redis_client() await redis_client.publish(channel, json.dumps(payload)) logger.info(f"🚀 [Plugin Upgrade] Dispatched {len(payload)} plugins to Redis channel '{channel}'") except Exception as e: logger.error(f"❌ [Plugin Upgrade] Failed to publish upgrade to Redis: {e}", exc_info=True) raise BizLogicError(f"Failed to publish upgrade notification to Redis: {str(e)}") return payload @classmethod async def get_latest_plugins(cls, db: AsyncSession) -> List[Dict[str, Any]]: """获取当前所有已启用的最新可用插件清单(供节点主动拉取)""" stmt = select(Plugin).where(Plugin.status == 1).order_by(Plugin.id.asc()) res = await db.execute(stmt) plugins = res.scalars().all() result = [] for p in plugins: v_stmt = select(PluginVersion).where( PluginVersion.plugin_id == p.id, PluginVersion.version == p.latest_version, ) v_res = await db.execute(v_stmt) pv = v_res.scalar_one_or_none() result.append({ "name": p.name, "display_name": p.display_name, "filename": p.filename or f"{p.name}.py", "class_name": p.class_name or "", "protocol": p.protocol or "IPlugin", "version": p.latest_version, "description": p.description, "download_url": pv.download_url if pv else "", "sha256": pv.sha256 if pv else "", "changelog": pv.changelog if pv else "", }) return result