From 9732a3e65fad64f29ae0fff15e2e07c8ed706059 Mon Sep 17 00:00:00 2001 From: DLmaster361 Date: Wed, 13 Aug 2025 21:41:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=B0=83=E5=BA=A6=E9=98=9F=E5=88=97?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=B7=BB=E5=8A=A0=E5=AD=97=E6=AE=B5=E8=AF=B4?= =?UTF-8?q?=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/queue.py | 25 +++++++--- app/core/config.py | 15 +++--- app/core/task_manager.py | 6 +-- app/models/ConfigBase.py | 5 +- app/models/schema.py | 101 ++++++++++++++++----------------------- 5 files changed, 71 insertions(+), 81 deletions(-) diff --git a/app/api/queue.py b/app/api/queue.py index d3f6dd4..75e5a22 100644 --- a/app/api/queue.py +++ b/app/api/queue.py @@ -34,7 +34,8 @@ router = APIRouter(prefix="/api/queue", tags=["调度队列管理"]) async def add_queue() -> QueueCreateOut: uid, config = await Config.add_queue() - return QueueCreateOut(queueId=str(uid), data=await config.toDict()) + data = QueueConfig(**(await config.toDict())) + return QueueCreateOut(queueId=str(uid), data=data) @router.post( @@ -43,7 +44,9 @@ async def add_queue() -> QueueCreateOut: async def get_queues(queue: QueueGetIn = Body(...)) -> QueueGetOut: try: - index, data = await Config.get_queue(queue.queueId) + index, config = await Config.get_queue(queue.queueId) + index = [QueueIndexItem(**_) for _ in index] + data = {uid: QueueConfig(**cfg) for uid, cfg in config.items()} except Exception as e: return QueueGetOut(code=500, status="error", message=str(e), index=[], data={}) return QueueGetOut(index=index, data=data) @@ -55,7 +58,9 @@ async def get_queues(queue: QueueGetIn = Body(...)) -> QueueGetOut: async def update_queue(queue: QueueUpdateIn = Body(...)) -> OutBase: try: - await Config.update_queue(queue.queueId, queue.data) + await Config.update_queue( + queue.queueId, queue.data.model_dump(exclude_unset=True) + ) except Exception as e: return OutBase(code=500, status="error", message=str(e)) return OutBase() @@ -87,7 +92,8 @@ async def reorder_queue(script: QueueReorderIn = Body(...)) -> OutBase: async def add_time_set(time: QueueSetInBase = Body(...)) -> TimeSetCreateOut: uid, config = await Config.add_time_set(time.queueId) - return TimeSetCreateOut(timeSetId=str(uid), data=await config.toDict()) + data = TimeSet(**(await config.toDict())) + return TimeSetCreateOut(timeSetId=str(uid), data=data) @router.post( @@ -96,7 +102,9 @@ async def add_time_set(time: QueueSetInBase = Body(...)) -> TimeSetCreateOut: async def update_time_set(time: TimeSetUpdateIn = Body(...)) -> OutBase: try: - await Config.update_time_set(time.queueId, time.timeSetId, time.data) + await Config.update_time_set( + time.queueId, time.timeSetId, time.data.model_dump(exclude_unset=True) + ) except Exception as e: return OutBase(code=500, status="error", message=str(e)) return OutBase() @@ -135,7 +143,8 @@ async def reorder_time_set(time: TimeSetReorderIn = Body(...)) -> OutBase: async def add_item(item: QueueSetInBase = Body(...)) -> QueueItemCreateOut: uid, config = await Config.add_queue_item(item.queueId) - return QueueItemCreateOut(queueItemId=str(uid), data=await config.toDict()) + data = QueueItem(**(await config.toDict())) + return QueueItemCreateOut(queueItemId=str(uid), data=data) @router.post( @@ -144,7 +153,9 @@ async def add_item(item: QueueSetInBase = Body(...)) -> QueueItemCreateOut: async def update_item(item: QueueItemUpdateIn = Body(...)) -> OutBase: try: - await Config.update_queue_item(item.queueId, item.queueItemId, item.data) + await Config.update_queue_item( + item.queueId, item.queueItemId, item.data.model_dump(exclude_unset=True) + ) except Exception as e: return OutBase(code=500, status="error", message=str(e)) return OutBase() diff --git a/app/core/config.py b/app/core/config.py index f3efef2..3004420 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -243,13 +243,6 @@ class QueueConfig(ConfigBase): ), ) - self.Data_LastProxyTime = ConfigItem( - "Data", "LastProxyTime", "2000-01-01 00:00:00" - ) - self.Data_LastProxyHistory = ConfigItem( - "Data", "LastProxyHistory", "暂无历史运行记录" - ) - self.TimeSet = MultipleConfig([TimeSet]) self.QueueItem = MultipleConfig([QueueItem]) @@ -820,6 +813,8 @@ class AppConfig(GlobalConfig): else: data = await self.QueueConfig.get(uuid.UUID(queue_id)) + print(data) + index = data.pop("instances", []) return list(index), data @@ -971,6 +966,7 @@ class AppConfig(GlobalConfig): if isinstance(queue_config, QueueConfig): uid, config = await queue_config.QueueItem.add(QueueItem) else: + logger.warning(f"Unsupported script config type: {type(queue_config)}") raise TypeError(f"Unsupported script config type: {type(queue_config)}") await self.QueueConfig.save() @@ -989,6 +985,7 @@ class AppConfig(GlobalConfig): for group, items in data.items(): for name, value in items.items(): if uuid.UUID(value) not in self.ScriptConfig: + logger.warning(f"Script with uid {value} does not exist.") raise ValueError(f"Script with uid {value} does not exist.") logger.debug(f"更新队列项配置:{queue_id} - {group}.{name} = {value}") if isinstance(queue_config, QueueConfig): @@ -1269,7 +1266,7 @@ class AppConfig(GlobalConfig): for uid, script in self.ScriptConfig.items(): data.append( { - "label": f"{TYPE_BOOK[script.__class__.__name__]} - {script.get('Info', 'Name')}", + "label": f"{TYPE_BOOK[type(script).__name__]} - {script.get('Info', 'Name')}", "value": str(uid), } ) @@ -1292,7 +1289,7 @@ class AppConfig(GlobalConfig): for uid, script in self.ScriptConfig.items(): data.append( { - "label": f"脚本 - {TYPE_BOOK[script.__class__.__name__]} - {script.get('Info', 'Name')}", + "label": f"脚本 - {TYPE_BOOK[type(script).__name__]} - {script.get('Info', 'Name')}", "value": str(uid), } ) diff --git a/app/core/task_manager.py b/app/core/task_manager.py index 2e5e67e..01ba921 100644 --- a/app/core/task_manager.py +++ b/app/core/task_manager.py @@ -122,7 +122,7 @@ class _TaskManager: task_item = GeneralManager(mode, task_id, actual_id, websocket) else: logger.error( - f"不支持的脚本类型:{Config.ScriptConfig[task_id].__class__.__name__}" + f"不支持的脚本类型:{type(Config.ScriptConfig[task_id]).__name__}" ) await websocket.send_json( TaskMessage( @@ -146,7 +146,7 @@ class _TaskManager: queue = Config.QueueConfig[task_id] if not isinstance(queue, QueueConfig): logger.error( - f"不支持的队列类型:{Config.QueueConfig[task_id].__class__.__name__}" + f"不支持的队列类型:{type(Config.QueueConfig[task_id]).__name__}" ) await websocket.send_json( TaskMessage( @@ -203,7 +203,7 @@ class _TaskManager: task_item = GeneralManager(mode, task_id, actual_id, websocket) else: logger.error( - f"不支持的脚本类型:{Config.ScriptConfig[script_id].__class__.__name__}" + f"不支持的脚本类型:{type(Config.ScriptConfig[script_id]).__name__}" ) await websocket.send_json( TaskMessage( diff --git a/app/models/ConfigBase.py b/app/models/ConfigBase.py index 90d780d..785f503 100644 --- a/app/models/ConfigBase.py +++ b/app/models/ConfigBase.py @@ -591,8 +591,7 @@ class MultipleConfig: data: Dict[str, Union[list, dict]] = { "instances": [ - {"uid": str(_), "type": self.data[_].__class__.__name__} - for _ in self.order + {"uid": str(_), "type": type(self.data[_]).__name__} for _ in self.order ] } for uid, config in self.items(): @@ -618,7 +617,7 @@ class MultipleConfig: data: Dict[str, Union[list, dict]] = { "instances": [ - {"uid": str(_), "type": self.data[_].__class__.__name__} + {"uid": str(_), "type": type(self.data[_]).__name__} for _ in self.order if _ == uid ] diff --git a/app/models/schema.py b/app/models/schema.py index 05d0eca..b95f535 100644 --- a/app/models/schema.py +++ b/app/models/schema.py @@ -133,63 +133,44 @@ class GlobalConfig(BaseModel): Update: Optional[GlobalConfig_Update] = Field(None, description="更新相关配置") -# class QueueItem(ConfigBase): -# """队列项配置""" - -# def __init__(self) -> None: -# super().__init__() - -# self.Info_ScriptId = ConfigItem("Info", "ScriptId", None, UidValidator()) +class QueueItem_Info(BaseModel): + ScriptId: Optional[str] = Field( + None, description="任务所对应的脚本ID, 为None时表示未选择" + ) -# class TimeSet(ConfigBase): -# """时间设置配置""" - -# def __init__(self) -> None: -# super().__init__() - -# self.Info_Enabled = ConfigItem("Info", "Enabled", False, BoolValidator()) -# self.Info_Time = ConfigItem("Info", "Time", "00:00") +class QueueItem(BaseModel): + Info: Optional[QueueItem_Info] = Field(None, description="队列项") -# class QueueConfig(ConfigBase): -# """队列配置""" +class TimeSet_Info(BaseModel): + Enabled: Optional[bool] = Field(None, description="是否启用") + Time: Optional[str] = Field(None, description="时间设置, 格式为HH:MM") -# def __init__(self) -> None: -# super().__init__() -# self.Info_Name = ConfigItem("Info", "Name", "") -# self.Info_TimeEnabled = ConfigItem( -# "Info", "TimeEnabled", False, BoolValidator() -# ) -# self.Info_StartUpEnabled = ConfigItem( -# "Info", "StartUpEnabled", False, BoolValidator() -# ) -# self.Info_AfterAccomplish = ConfigItem( -# "Info", -# "AfterAccomplish", -# "NoAction", -# OptionsValidator( -# [ -# "NoAction", -# "KillSelf", -# "Sleep", -# "Hibernate", -# "Shutdown", -# "ShutdownForce", -# ] -# ), -# ) +class TimeSet(BaseModel): + Info: Optional[TimeSet_Info] = Field(None, description="时间项") -# self.Data_LastProxyTime = ConfigItem( -# "Data", "LastProxyTime", "2000-01-01 00:00:00" -# ) -# self.Data_LastProxyHistory = ConfigItem( -# "Data", "LastProxyHistory", "暂无历史运行记录" -# ) -# self.TimeSet = MultipleConfig([TimeSet]) -# self.QueueItem = MultipleConfig([QueueItem]) +class QueueIndexItem(BaseModel): + uid: str = Field(..., description="唯一标识符") + type: Literal["QueueConfig"] = Field(..., description="配置类型") + + +class QueueConfig_Info(BaseModel): + Name: Optional[str] = Field(None, description="队列名称") + TimeEnabled: Optional[bool] = Field(None, description="是否启用定时") + StartUpEnabled: Optional[bool] = Field(None, description="是否启动时运行") + AfterAccomplish: Optional[ + Literal[ + "NoAction", "KillSelf", "Sleep", "Hibernate", "Shutdown", "ShutdownForce" + ] + ] = Field(None, description="完成后操作") + + +class QueueConfig(BaseModel): + + Info: Optional[QueueConfig_Info] = Field(None, description="队列信息") # class MaaUserConfig(ConfigBase): @@ -652,21 +633,23 @@ class PlanReorderIn(BaseModel): class QueueCreateOut(OutBase): queueId: str = Field(..., description="新创建的队列ID") - data: Dict[str, Any] = Field(..., description="队列配置数据") + data: QueueConfig = Field(..., description="队列配置数据") class QueueGetIn(BaseModel): - queueId: Optional[str] = Field(None, description="队列ID,仅在模式为Single时需要") + queueId: Optional[str] = Field( + None, description="队列ID, 未携带时表示获取所有队列数据" + ) class QueueGetOut(OutBase): - index: List[Dict[str, str]] = Field(..., description="队列索引列表") - data: Dict[str, Any] = Field(..., description="队列列表或单个队列数据") + index: List[QueueIndexItem] = Field(..., description="队列索引列表") + data: Dict[str, QueueConfig] = Field(..., description="队列列表或单个队列数据") class QueueUpdateIn(BaseModel): queueId: str = Field(..., description="队列ID") - data: Dict[str, Dict[str, Any]] = Field(..., description="队列更新数据") + data: QueueConfig = Field(..., description="队列更新数据") class QueueDeleteIn(BaseModel): @@ -674,7 +657,7 @@ class QueueDeleteIn(BaseModel): class QueueReorderIn(BaseModel): - indexList: List[str] = Field(..., description="调度队列ID列表,按新顺序排列") + indexList: List[str] = Field(..., description="按新顺序排列的调度队列UID列表") class QueueSetInBase(BaseModel): @@ -683,12 +666,12 @@ class QueueSetInBase(BaseModel): class TimeSetCreateOut(OutBase): timeSetId: str = Field(..., description="新创建的时间设置ID") - data: Dict[str, Any] = Field(..., description="时间设置配置数据") + data: TimeSet = Field(..., description="时间设置配置数据") class TimeSetUpdateIn(QueueSetInBase): timeSetId: str = Field(..., description="时间设置ID") - data: Dict[str, Dict[str, Any]] = Field(..., description="时间设置更新数据") + data: TimeSet = Field(..., description="时间设置更新数据") class TimeSetDeleteIn(QueueSetInBase): @@ -701,12 +684,12 @@ class TimeSetReorderIn(QueueSetInBase): class QueueItemCreateOut(OutBase): queueItemId: str = Field(..., description="新创建的队列项ID") - data: Dict[str, Any] = Field(..., description="队列项配置数据") + data: QueueItem = Field(..., description="队列项配置数据") class QueueItemUpdateIn(QueueSetInBase): queueItemId: str = Field(..., description="队列项ID") - data: Dict[str, Dict[str, Any]] = Field(..., description="队列项更新数据") + data: QueueItem = Field(..., description="队列项更新数据") class QueueItemDeleteIn(QueueSetInBase):