Optimize agent tool async blocking paths

This commit is contained in:
jxxghp
2026-04-28 20:36:49 +08:00
parent c5b716c231
commit 5c1487a9a6
31 changed files with 949 additions and 635 deletions
+12 -1
View File
@@ -1,4 +1,4 @@
from typing import List, Optional
from typing import Dict, List, Optional
from app.db import DbOper
from app.db.models.downloadhistory import DownloadHistory, DownloadFiles
@@ -23,6 +23,17 @@ class DownloadHistoryOper(DbOper):
"""
return DownloadHistory.get_by_hash(self._db, download_hash)
def get_by_hashes(self, download_hashes: List[str]) -> Dict[str, DownloadHistory]:
"""
批量按 Hash 查询下载记录,并返回以 Hash 为键的映射。
"""
histories = DownloadHistory.get_by_hashes(self._db, download_hashes)
return {
history.download_hash: history
for history in histories
if history and history.download_hash
}
def get_by_mediaid(self, tmdbid: int, doubanid: str) -> List[DownloadHistory]:
"""
按媒体ID查询下载记录
+35 -1
View File
@@ -1,5 +1,5 @@
import time
from typing import Optional
from typing import List, Optional
from sqlalchemy import Column, Integer, String, JSON, select
from sqlalchemy.ext.asyncio import AsyncSession
@@ -69,6 +69,40 @@ class DownloadHistory(Base):
.first()
)
@classmethod
@db_query
def get_by_hashes(cls, db: Session, download_hashes: List[str]):
"""
批量查询多个下载任务的最新历史记录,避免在上层形成 N+1 查询。
"""
normalized_hashes = []
seen_hashes = set()
for download_hash in download_hashes or []:
if not download_hash or download_hash in seen_hashes:
continue
seen_hashes.add(download_hash)
normalized_hashes.append(download_hash)
if not normalized_hashes:
return []
histories = (
db.query(DownloadHistory)
.filter(DownloadHistory.download_hash.in_(normalized_hashes))
.order_by(DownloadHistory.download_hash, DownloadHistory.date.desc())
.all()
)
latest_histories = {}
for history in histories:
if history.download_hash and history.download_hash not in latest_histories:
latest_histories[history.download_hash] = history
return [
latest_histories[download_hash]
for download_hash in normalized_hashes
if download_hash in latest_histories
]
@classmethod
@db_query
def get_by_mediaid(cls, db: Session, tmdbid: int, doubanid: str):
+6
View File
@@ -148,6 +148,12 @@ class SubscribeOper(DbOper):
"""
Subscribe.delete(self._db, rid=sid)
async def async_delete(self, sid: int):
"""
异步删除订阅。
"""
await Subscribe.async_delete(self._db, rid=sid)
def update(self, sid: int, payload: dict) -> Subscribe:
"""
更新订阅
+12
View File
@@ -20,6 +20,12 @@ class TransferHistoryOper(DbOper):
"""
return TransferHistory.get(self._db, historyid)
async def async_get(self, historyid: int) -> TransferHistory:
"""
异步获取转移历史。
"""
return await TransferHistory.async_get(self._db, historyid)
def get_by_title(self, title: str) -> List[TransferHistory]:
"""
按标题查询转移记录
@@ -93,6 +99,12 @@ class TransferHistoryOper(DbOper):
"""
TransferHistory.delete(self._db, historyid)
async def async_delete(self, historyid):
"""
异步删除转移记录。
"""
await TransferHistory.async_delete(self._db, historyid)
def truncate(self):
"""
清空转移记录
+6
View File
@@ -108,6 +108,12 @@ class UserOper(DbOper):
"""
return User.get_by_name(self._db, name)
async def async_get_by_name(self, name: str) -> User:
"""
异步根据用户名获取用户。
"""
return await User.async_get_by_name(self._db, name)
def get_permissions(self, name: str) -> dict:
"""
获取用户权限