mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-05 07:27:15 +08:00
refactor: migrate agent task query ownership
This commit is contained in:
+30
-13
@@ -4,7 +4,32 @@ from sqlalchemy import Boolean, Index, Integer, String, Text, select, update
|
||||
from sqlalchemy.orm import Mapped, Session, mapped_column
|
||||
|
||||
from app.db.base import Base, execute_dml, get_id_column
|
||||
from app.db.decorators import db_query
|
||||
|
||||
|
||||
def _get_for_user_statement(
|
||||
model: type["AgentTask"],
|
||||
task_id: int,
|
||||
user_id: Optional[str] = None,
|
||||
):
|
||||
"""构造按任务 ID 与可选用户归属收窄的查询语句。"""
|
||||
statement = select(model).where(model.id == task_id)
|
||||
if user_id is not None:
|
||||
statement = statement.where(model.user_id == user_id)
|
||||
return statement
|
||||
|
||||
|
||||
def _list_for_user_statement(
|
||||
model: type["AgentTask"],
|
||||
user_id: Optional[str] = None,
|
||||
enabled: Optional[bool] = None,
|
||||
):
|
||||
"""构造按用户、启用状态和创建时间排序的任务列表语句。"""
|
||||
statement = select(model)
|
||||
if user_id is not None:
|
||||
statement = statement.where(model.user_id == user_id)
|
||||
if enabled is not None:
|
||||
statement = statement.where(model.enabled.is_(enabled))
|
||||
return statement.order_by(model.created_at.desc(), model.id.desc())
|
||||
|
||||
|
||||
class AgentTask(Base):
|
||||
@@ -59,7 +84,6 @@ class AgentTask(Base):
|
||||
return task.id
|
||||
|
||||
@classmethod
|
||||
@db_query
|
||||
def get_for_user(
|
||||
cls,
|
||||
db: Session,
|
||||
@@ -69,13 +93,11 @@ class AgentTask(Base):
|
||||
"""
|
||||
按任务 ID 和可选用户 ID 查询 Agent 定时任务。
|
||||
"""
|
||||
statement = select(cls).where(cls.id == task_id)
|
||||
if user_id is not None:
|
||||
statement = statement.where(cls.user_id == user_id)
|
||||
return db.execute(statement).scalars().first()
|
||||
return db.execute(
|
||||
_get_for_user_statement(cls, task_id=task_id, user_id=user_id)
|
||||
).scalars().first()
|
||||
|
||||
@classmethod
|
||||
@db_query
|
||||
def list_for_user(
|
||||
cls,
|
||||
db: Session,
|
||||
@@ -85,13 +107,8 @@ class AgentTask(Base):
|
||||
"""
|
||||
按用户和启用状态查询 Agent 定时任务。
|
||||
"""
|
||||
statement = select(cls)
|
||||
if user_id is not None:
|
||||
statement = statement.where(cls.user_id == user_id)
|
||||
if enabled is not None:
|
||||
statement = statement.where(cls.enabled.is_(enabled))
|
||||
return list(db.execute(
|
||||
statement.order_by(cls.created_at.desc(), cls.id.desc())
|
||||
_list_for_user_statement(cls, user_id=user_id, enabled=enabled)
|
||||
).scalars().all())
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -4,9 +4,16 @@ from datetime import datetime
|
||||
from typing import Optional
|
||||
from uuid import uuid4
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db.base import DbOper
|
||||
from app.db.models.agenttask import AgentTask
|
||||
from app.db.models.agenttask import (
|
||||
AgentTask,
|
||||
_get_for_user_statement,
|
||||
_list_for_user_statement,
|
||||
)
|
||||
from app.db.models.agenttaskrun import AgentTaskRun
|
||||
from app.db.uow import run_sync_transaction
|
||||
|
||||
|
||||
class AgentTaskOper(DbOper):
|
||||
@@ -45,7 +52,19 @@ class AgentTaskOper(DbOper):
|
||||
"""
|
||||
查询单个 Agent 定时任务。
|
||||
"""
|
||||
return AgentTask.get_for_user(self._db, task_id=task_id, user_id=user_id)
|
||||
def query(session: Session) -> Optional[AgentTask]:
|
||||
"""在调用方会话中读取单个任务。"""
|
||||
return session.execute(
|
||||
_get_for_user_statement(
|
||||
AgentTask,
|
||||
task_id=task_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
).scalars().first()
|
||||
|
||||
if isinstance(self._db, Session):
|
||||
return query(self._db)
|
||||
return run_sync_transaction(query)
|
||||
|
||||
def list(
|
||||
self,
|
||||
@@ -55,7 +74,19 @@ class AgentTaskOper(DbOper):
|
||||
"""
|
||||
查询 Agent 定时任务列表。
|
||||
"""
|
||||
return AgentTask.list_for_user(self._db, user_id=user_id, enabled=enabled)
|
||||
def query(session: Session) -> list[AgentTask]:
|
||||
"""在调用方会话中读取任务列表。"""
|
||||
return list(session.execute(
|
||||
_list_for_user_statement(
|
||||
AgentTask,
|
||||
user_id=user_id,
|
||||
enabled=enabled,
|
||||
)
|
||||
).scalars().all())
|
||||
|
||||
if isinstance(self._db, Session):
|
||||
return query(self._db)
|
||||
return run_sync_transaction(query)
|
||||
|
||||
def update(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user