mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-11-01 21:07:01 +08:00
refactor(types): specify workflow model query and transition results
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
from builtins import list as builtin_list
|
||||
from datetime import datetime
|
||||
from typing import Any, Optional
|
||||
from typing import Any, Optional, Self
|
||||
|
||||
from sqlalchemy import JSON, Index, Integer, String, and_, or_, select, update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
from sqlalchemy.orm import Mapped, Session, mapped_column
|
||||
|
||||
from app.db.base import Base, get_id_column
|
||||
|
||||
@@ -55,16 +55,18 @@ class Workflow(Base):
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_enabled_workflows(cls, db):
|
||||
def get_enabled_workflows(cls, db: Session) -> list[Self]:
|
||||
"""读取全部未暂停的工作流。"""
|
||||
return list(db.execute(select(cls).where(cls.state != 'P')).scalars().all())
|
||||
|
||||
@classmethod
|
||||
async def async_get_enabled_workflows(cls, db: AsyncSession):
|
||||
async def async_get_enabled_workflows(cls, db: AsyncSession) -> list[Self]:
|
||||
"""异步读取全部未暂停的工作流。"""
|
||||
result = await db.execute(select(cls).where(cls.state != 'P'))
|
||||
return list(result.scalars().all())
|
||||
|
||||
@classmethod
|
||||
def get_timer_triggered_workflows(cls, db):
|
||||
def get_timer_triggered_workflows(cls, db: Session) -> list[Self]:
|
||||
"""获取定时触发的工作流"""
|
||||
return list(db.execute(select(cls).where(
|
||||
and_(
|
||||
@@ -77,7 +79,7 @@ class Workflow(Base):
|
||||
)).scalars().all())
|
||||
|
||||
@classmethod
|
||||
async def async_get_timer_triggered_workflows(cls, db: AsyncSession):
|
||||
async def async_get_timer_triggered_workflows(cls, db: AsyncSession) -> list[Self]:
|
||||
"""异步获取定时触发的工作流"""
|
||||
result = await db.execute(select(cls).where(
|
||||
and_(
|
||||
@@ -91,7 +93,7 @@ class Workflow(Base):
|
||||
return list(result.scalars().all())
|
||||
|
||||
@classmethod
|
||||
def get_event_triggered_workflows(cls, db):
|
||||
def get_event_triggered_workflows(cls, db: Session) -> list[Self]:
|
||||
"""获取事件触发的工作流"""
|
||||
return list(db.execute(select(cls).where(
|
||||
and_(
|
||||
@@ -101,7 +103,7 @@ class Workflow(Base):
|
||||
)).scalars().all())
|
||||
|
||||
@classmethod
|
||||
async def async_get_event_triggered_workflows(cls, db: AsyncSession):
|
||||
async def async_get_event_triggered_workflows(cls, db: AsyncSession) -> list[Self]:
|
||||
"""异步获取事件触发的工作流"""
|
||||
result = await db.execute(select(cls).where(
|
||||
and_(
|
||||
@@ -112,38 +114,45 @@ class Workflow(Base):
|
||||
return list(result.scalars().all())
|
||||
|
||||
@classmethod
|
||||
def get_by_name(cls, db, name: str):
|
||||
return db.execute(select(cls).where(cls.name == name)).scalars().first()
|
||||
def get_by_name(cls, db: Session, name: str) -> Optional[Self]:
|
||||
"""在调用方会话中按名称查询工作流。"""
|
||||
workflow: Optional[Self] = db.execute(select(cls).where(cls.name == name)).scalars().first()
|
||||
return workflow
|
||||
|
||||
@classmethod
|
||||
async def async_get_by_name(cls, db: AsyncSession, name: str):
|
||||
async def async_get_by_name(cls, db: AsyncSession, name: str) -> Optional[Self]:
|
||||
"""在调用方异步会话中按名称查询工作流。"""
|
||||
result = await db.execute(select(cls).where(cls.name == name))
|
||||
return result.scalars().first()
|
||||
workflow: Optional[Self] = result.scalars().first()
|
||||
return workflow
|
||||
|
||||
@classmethod
|
||||
def update_state(cls, db, wid: int, state: str):
|
||||
def update_state(cls, db: Session, wid: int, state: str) -> bool:
|
||||
"""在调用方事务中暂存指定工作流状态。"""
|
||||
db.execute(update(cls).where(cls.id == wid).values(state=state))
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
async def async_update_state(cls, db: AsyncSession, wid: int, state: str):
|
||||
async def async_update_state(cls, db: AsyncSession, wid: int, state: str) -> bool:
|
||||
"""在调用方持有的异步事务中暂存工作流状态。"""
|
||||
await db.execute(update(cls).where(cls.id == wid).values(state=state))
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def start(cls, db, wid: int):
|
||||
def start(cls, db: Session, wid: int) -> bool:
|
||||
"""在调用方事务中暂存运行中状态。"""
|
||||
db.execute(update(cls).where(cls.id == wid).values(state='R'))
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
async def async_start(cls, db: AsyncSession, wid: int):
|
||||
async def async_start(cls, db: AsyncSession, wid: int) -> bool:
|
||||
"""在调用方持有的异步事务中暂存运行中状态。"""
|
||||
await db.execute(update(cls).where(cls.id == wid).values(state='R'))
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def fail(cls, db, wid: int, result: str):
|
||||
def fail(cls, db: Session, wid: int, result: str) -> bool:
|
||||
"""暂存未暂停工作流的失败结果和执行时间。"""
|
||||
db.execute(update(cls).where(
|
||||
and_(cls.id == wid, cls.state != "P")
|
||||
).values(
|
||||
@@ -154,7 +163,7 @@ class Workflow(Base):
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
async def async_fail(cls, db: AsyncSession, wid: int, result: str):
|
||||
async def async_fail(cls, db: AsyncSession, wid: int, result: str) -> bool:
|
||||
"""在调用方持有的异步事务中暂存失败结果。"""
|
||||
await db.execute(update(cls).where(
|
||||
and_(cls.id == wid, cls.state != "P")
|
||||
@@ -166,7 +175,8 @@ class Workflow(Base):
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def success(cls, db, wid: int, result: Optional[str] = None):
|
||||
def success(cls, db: Session, wid: int, result: Optional[str] = None) -> bool:
|
||||
"""暂存未暂停工作流的成功结果并递增运行次数。"""
|
||||
db.execute(update(cls).where(
|
||||
and_(cls.id == wid, cls.state != "P")
|
||||
).values(
|
||||
@@ -178,7 +188,7 @@ class Workflow(Base):
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
async def async_success(cls, db: AsyncSession, wid: int, result: Optional[str] = None):
|
||||
async def async_success(cls, db: AsyncSession, wid: int, result: Optional[str] = None) -> bool:
|
||||
"""在调用方持有的异步事务中暂存成功结果。"""
|
||||
await db.execute(update(cls).where(
|
||||
and_(cls.id == wid, cls.state != "P")
|
||||
@@ -191,7 +201,8 @@ class Workflow(Base):
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def reset(cls, db, wid: int, reset_count: Optional[bool] = False):
|
||||
def reset(cls, db: Session, wid: int, reset_count: Optional[bool] = False) -> bool:
|
||||
"""清空执行上下文,可选择同时重置运行次数。"""
|
||||
db.execute(update(cls).where(cls.id == wid).values(
|
||||
state='W',
|
||||
result=None,
|
||||
@@ -203,7 +214,7 @@ class Workflow(Base):
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
async def async_reset(cls, db: AsyncSession, wid: int, reset_count: Optional[bool] = False):
|
||||
async def async_reset(cls, db: AsyncSession, wid: int, reset_count: Optional[bool] = False) -> bool:
|
||||
"""在调用方持有的异步事务中暂存执行状态重置。"""
|
||||
await db.execute(update(cls).where(cls.id == wid).values(
|
||||
state='W',
|
||||
@@ -216,8 +227,9 @@ class Workflow(Base):
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def update_current_action(cls, db, wid: int, action_id: str, context: dict,
|
||||
execution_state: Optional[dict] = None):
|
||||
def update_current_action(cls, db: Session, wid: int, action_id: str, context: dict[str, object],
|
||||
execution_state: Optional[dict[str, object]] = None) -> bool:
|
||||
"""去重记录已执行动作并暂存上下文和执行状态。"""
|
||||
workflow = db.execute(select(cls).where(cls.id == wid)).scalars().first()
|
||||
current_actions = []
|
||||
if workflow and workflow.current_action:
|
||||
@@ -234,8 +246,8 @@ class Workflow(Base):
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
async def async_update_current_action(cls, db: AsyncSession, wid: int, action_id: str, context: dict,
|
||||
execution_state: Optional[dict] = None):
|
||||
async def async_update_current_action(cls, db: AsyncSession, wid: int, action_id: str, context: dict[str, object],
|
||||
execution_state: Optional[dict[str, object]] = None) -> bool:
|
||||
"""在调用方持有的异步事务中暂存动作进度。"""
|
||||
# 先获取当前current_action
|
||||
result = await db.execute(select(cls.current_action).where(cls.id == wid))
|
||||
|
||||
@@ -75,7 +75,7 @@ uv run --locked --no-sync python scripts/architecture/complexity.py
|
||||
回调和返回值类型;全仓错误计数为零并不保证单独纳入 strict frontier 后也为零。
|
||||
|
||||
2026-10-16 开始逐文件清理类型债务,`app/domain/title.py` 通过局部可空类型声明保留
|
||||
既有搜索解析行为,并纳入严格清单;当前清单共 127 个文件(126 个生产文件和 1 个脚本)。
|
||||
既有搜索解析行为,并纳入严格清单;当前清单共 128 个文件(127 个生产文件和 1 个脚本)。
|
||||
每个治理提交聚焦一个生产文件及其必要测试、门禁配置、真实下降基线和指标文档。
|
||||
验收同时检查完整严格清单与全宿主 Linux 分析,避免 `follow_imports = skip` 隐藏依赖
|
||||
签名冲突;不通过忽略诊断或放宽类型合同接受回退。最新文件清单以 `mypy.ini` 为准,
|
||||
|
||||
@@ -106,9 +106,9 @@ ARCH-201 至 ARCH-204 均达到实现、验证、提交、推送和远端门禁
|
||||
| Model/Oper 事务债务 | 0 | 自建 Session、自动事务装饰器、直接 commit/rollback 等基线均为零 |
|
||||
| Module Contract | 215 specs / 211 methods / 263 calls | 动态方法名为 0;分类事实富化通过显式合同进入插件调度,旧 YAML 分类方法已移除;download 新增 custom_words 可选参数,旧 provider 签名通过显式合同兼容 |
|
||||
| Event Contract | 53 | 均已有 payload model,但当前全部是 diagnostic enforcement |
|
||||
| Python 源码量 | 372,609 行 | 本次重新扫描物理行数,排除 `app/plugins/**`;74 个文件超过 1,000 行,12 个超过 2,000 行;源码尺寸仅作观察 |
|
||||
| Python 源码量 | 372,621 行 | 本次重新扫描物理行数,排除 `app/plugins/**`;74 个文件超过 1,000 行,12 个超过 2,000 行;源码尺寸仅作观察 |
|
||||
| 长方法 | 309 个超过 80 行 | 本次 AST 扫描排除 `app/plugins/**`;61 个超过 150 行,17 个超过 250 行;新增函数仍受 C901/嵌套深度门禁约束 |
|
||||
| 全量 mypy 历史债务 | 8,702 / 428 文件 | 通知排版补齐 Slack 消息块类型,历史债务减少 2 项;工作流资源过滤改用领域对象调用过滤链,恢复的节点输出在消费前归一化,并收紧可空资源列表的类型低水位;消息媒体交互类型债务已清零,canonical Facade 与 endpoint 类型边界已补齐,低水位只允许继续下降;微信 ClawBot 入站 item_list 媒体解析改为按子项键名分派后,类型错误减少 4 项;站点解析器分享率改用浮点初值,类型错误减少 20 项;本次下载与文件安全修复再减少 3 项;Transmission 进度回退值归一化再减少 2 项,逐文件逐错误码无增长 |
|
||||
| 全量 mypy 历史债务 | 8,662 / 427 文件 | 通知排版补齐 Slack 消息块类型,历史债务减少 2 项;工作流资源过滤改用领域对象调用过滤链,恢复的节点输出在消费前归一化,并收紧可空资源列表的类型低水位;消息媒体交互类型债务已清零,canonical Facade 与 endpoint 类型边界已补齐,低水位只允许继续下降;微信 ClawBot 入站 item_list 媒体解析改为按子项键名分派后,类型错误减少 4 项;站点解析器分享率改用浮点初值,类型错误减少 20 项;本次下载与文件安全修复再减少 3 项;Transmission 进度回退值归一化再减少 2 项,逐文件逐错误码无增长 |
|
||||
| Ruff 历史诊断 | 98 | 低水位门禁通过,但规则集只覆盖 `E4/E7/E9/F/I` |
|
||||
| 覆盖率固定基线 | Application 80.00%,Domain 80.00% | Chain、Runtime、Agent、Adapter、Startup 未进入包级覆盖率门禁 |
|
||||
|
||||
|
||||
1
mypy.ini
1
mypy.ini
@@ -119,6 +119,7 @@ files =
|
||||
app/db/models/systemconfig.py,
|
||||
app/db/models/user.py,
|
||||
app/db/models/userconfig.py,
|
||||
app/db/models/workflow.py,
|
||||
app/db/uow.py,
|
||||
app/startup/initializers/routers.py,
|
||||
app/startup/initializers/workflow.py,
|
||||
|
||||
@@ -993,10 +993,6 @@
|
||||
"no-untyped-def": 20,
|
||||
"type-arg": 1
|
||||
},
|
||||
"app/db/models/workflow.py": {
|
||||
"no-untyped-def": 27,
|
||||
"type-arg": 4
|
||||
},
|
||||
"app/db/oper/agentchat.py": {
|
||||
"type-arg": 6
|
||||
},
|
||||
@@ -1039,8 +1035,6 @@
|
||||
},
|
||||
"app/db/oper/workflow.py": {
|
||||
"arg-type": 1,
|
||||
"no-any-return": 6,
|
||||
"no-untyped-call": 3,
|
||||
"no-untyped-def": 1,
|
||||
"union-attr": 3
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user