refactor: isolate transfer and download history queries

This commit is contained in:
jxxghp
2026-08-23 13:59:12 +08:00
parent 03c03fa27d
commit 4074fa4e42
10 changed files with 505 additions and 348 deletions
+94 -38
View File
@@ -18,20 +18,28 @@ class DownloadHistoryOper(DbOper):
按路径查询下载记录
:param path: 数据key
"""
return DownloadHistory.get_by_path(self._db, path)
return self._execute_sync_query(
lambda session: DownloadHistory.get_by_path(session, path)
)
def get_by_hash(self, download_hash: str) -> Optional[DownloadHistory]:
"""
按Hash查询下载记录
:param download_hash: 数据key
"""
return DownloadHistory.get_by_hash(self._db, download_hash)
return self._execute_sync_query(
lambda session: DownloadHistory.get_by_hash(session, 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)
histories = self._execute_sync_query(
lambda session: DownloadHistory.get_by_hashes(
session, download_hashes
)
)
return {
history.download_hash: history
for history in histories
@@ -48,11 +56,13 @@ class DownloadHistoryOper(DbOper):
:param media_id: 数据源原生 ID
:param music_type: 音乐实体类型
"""
return DownloadHistory.get_by_media_identity(
self._db,
media_source=media_source,
media_id=media_id,
music_type=music_type,
return self._execute_sync_query(
lambda session: DownloadHistory.get_by_media_identity(
session,
media_source=media_source,
media_id=media_id,
music_type=music_type,
)
)
def add(self, **kwargs):
@@ -97,30 +107,48 @@ class DownloadHistoryOper(DbOper):
:param download_hash: 数据key
:param state: 删除状态
"""
return DownloadFiles.get_by_hash(self._db, download_hash, state)
return self._execute_sync_query(
lambda session: DownloadFiles.get_by_hash(
session, download_hash, state
)
)
def get_file_by_fullpath(self, fullpath: str) -> Optional[DownloadFiles]:
"""
按fullpath查询下载文件记录
:param fullpath: 数据key
"""
return cast(Optional[DownloadFiles],
DownloadFiles.get_by_fullpath(self._db, fullpath=fullpath, all_files=False))
return self._execute_sync_query(
lambda session: cast(
Optional[DownloadFiles],
DownloadFiles.get_by_fullpath(
session, fullpath=fullpath, all_files=False
),
)
)
def get_files_by_fullpath(self, fullpath: str) -> List[DownloadFiles]:
"""
按fullpath查询下载文件记录
:param fullpath: 数据key
"""
return cast(List[DownloadFiles],
DownloadFiles.get_by_fullpath(self._db, fullpath=fullpath, all_files=True))
return self._execute_sync_query(
lambda session: cast(
List[DownloadFiles],
DownloadFiles.get_by_fullpath(
session, fullpath=fullpath, all_files=True
),
)
)
def get_files_by_savepath(self, fullpath: str) -> List[DownloadFiles]:
"""
按savepath查询下载文件记录
:param fullpath: 数据key
"""
return DownloadFiles.get_by_savepath(self._db, fullpath)
return self._execute_sync_query(
lambda session: DownloadFiles.get_by_savepath(session, fullpath)
)
def delete_file_by_fullpath(self, fullpath: str):
"""
@@ -147,8 +175,14 @@ class DownloadHistoryOper(DbOper):
按fullpath查询下载文件记录hash
:param fullpath: 数据key
"""
fileinfo = cast(Optional[DownloadFiles],
DownloadFiles.get_by_fullpath(self._db, fullpath=fullpath, all_files=False))
fileinfo = self._execute_sync_query(
lambda session: cast(
Optional[DownloadFiles],
DownloadFiles.get_by_fullpath(
session, fullpath=fullpath, all_files=False
),
)
)
if fileinfo:
return fileinfo.download_hash
return ""
@@ -157,7 +191,9 @@ class DownloadHistoryOper(DbOper):
"""
分页查询下载历史
"""
return DownloadHistory.list_by_page(self._db, page, count)
return self._execute_sync_query(
lambda session: DownloadHistory.list_by_page(session, page, count)
)
async def async_list_by_page(
self,
@@ -165,7 +201,11 @@ class DownloadHistoryOper(DbOper):
count: int = 30,
) -> List[DownloadHistory]:
"""异步分页查询下载历史。"""
return await DownloadHistory.async_list_by_page(self._db, page, count)
return await self._execute_async_query(
lambda session: DownloadHistory.async_list_by_page(
session, page, count
)
)
async def async_delete_history(self, historyid: int):
"""
@@ -187,22 +227,30 @@ class DownloadHistoryOper(DbOper):
按类型、标题、年份、季集查询下载记录
媒体身份 + mtype 或 title + year
"""
return DownloadHistory.get_last_by(db=self._db,
mtype=mtype,
title=title,
year=year,
season=season,
episode=episode,
media_source=media_source,
media_id=media_id)
return self._execute_sync_query(
lambda session: DownloadHistory.get_last_by(
db=session,
mtype=mtype,
title=title,
year=year,
season=season,
episode=episode,
media_source=media_source,
media_id=media_id,
)
)
def list_by_user_date(self, date: str, username: Optional[str] = None) -> List[DownloadHistory]:
"""
查询某用户某时间之前的下载历史
"""
return DownloadHistory.list_by_user_date(db=self._db,
date=date,
username=username)
return self._execute_sync_query(
lambda session: DownloadHistory.list_by_user_date(
db=session,
date=date,
username=username,
)
)
def list_by_date(
self, date: str, type: str, media_source: MediaSource, media_id: str,
@@ -211,20 +259,28 @@ class DownloadHistoryOper(DbOper):
"""
查询某时间之后的下载历史
"""
return DownloadHistory.list_by_date(db=self._db,
date=date,
type=type,
media_source=media_source,
media_id=media_id,
seasons=seasons)
return self._execute_sync_query(
lambda session: DownloadHistory.list_by_date(
db=session,
date=date,
type=type,
media_source=media_source,
media_id=media_id,
seasons=seasons,
)
)
def list_by_type(self, mtype: str, days: int = 7) -> List[DownloadHistory]:
"""
获取指定类型的下载历史
"""
return DownloadHistory.list_by_type(db=self._db,
mtype=mtype,
days=days)
return self._execute_sync_query(
lambda session: DownloadHistory.list_by_type(
db=session,
mtype=mtype,
days=days,
)
)
def delete_history(self, historyid):
"""
+92 -50
View File
@@ -19,13 +19,17 @@ class TransferHistoryOper(DbOper):
获取转移历史
:param historyid: 转移历史id
"""
return TransferHistory.get(self._db, historyid)
return self._execute_sync_query(
lambda session: TransferHistory.get(session, historyid)
)
async def async_get(self, historyid: int) -> Optional[TransferHistory]:
"""
异步获取转移历史。
"""
return await TransferHistory.async_get(self._db, historyid)
return await self._execute_async_query(
lambda session: TransferHistory.async_get(session, historyid)
)
async def async_list_by_title(
self,
@@ -38,13 +42,15 @@ class TransferHistoryOper(DbOper):
"""
异步按标题分页查询转移记录。
"""
return await TransferHistory.async_list_by_title(
self._db,
title=title,
page=page,
count=count,
status=status,
wildcard=wildcard,
return await self._execute_async_query(
lambda session: TransferHistory.async_list_by_title(
session,
title=title,
page=page,
count=count,
status=status,
wildcard=wildcard,
)
)
async def async_list_by_page(
@@ -56,15 +62,19 @@ class TransferHistoryOper(DbOper):
"""
异步分页查询转移记录。
"""
return await TransferHistory.async_list_by_page(
self._db, page=page, count=count, status=status
return await self._execute_async_query(
lambda session: TransferHistory.async_list_by_page(
session, page=page, count=count, status=status
)
)
async def async_count(self, status: Optional[bool] = None) -> Optional[int]:
"""
异步统计转移记录数量。
"""
return await TransferHistory.async_count(self._db, status=status)
return await self._execute_async_query(
lambda session: TransferHistory.async_count(session, status=status)
)
async def async_count_by_title(
self,
@@ -75,11 +85,13 @@ class TransferHistoryOper(DbOper):
"""
异步按标题统计转移记录数量。
"""
return await TransferHistory.async_count_by_title(
self._db,
title=title,
status=status,
wildcard=wildcard,
return await self._execute_async_query(
lambda session: TransferHistory.async_count_by_title(
session,
title=title,
status=status,
wildcard=wildcard,
)
)
def get_by_title(self, title: str) -> List[TransferHistory]:
@@ -87,7 +99,9 @@ class TransferHistoryOper(DbOper):
按标题查询转移记录
:param title: 数据key
"""
return TransferHistory.list_by_title(self._db, title)
return self._execute_sync_query(
lambda session: TransferHistory.list_by_title(session, title)
)
def get_by_src(
self, src: str, storage: Optional[str] = None
@@ -98,7 +112,9 @@ class TransferHistoryOper(DbOper):
:param storage: 存储类型
:return: 命中的整理记录,未命中时返回 None
"""
return TransferHistory.get_by_src(self._db, src, storage)
return self._execute_sync_query(
lambda session: TransferHistory.get_by_src(session, src, storage)
)
def get_success_by_src(
self, src: str, storage: Optional[str] = None
@@ -109,7 +125,11 @@ class TransferHistoryOper(DbOper):
:param storage: 存储类型
:return: 命中的成功整理记录,未命中时返回 None
"""
return TransferHistory.get_success_by_src(self._db, src, storage)
return self._execute_sync_query(
lambda session: TransferHistory.get_success_by_src(
session, src, storage
)
)
def get_by_dest(
self, dest: str, storage: Optional[str] = None
@@ -119,7 +139,9 @@ class TransferHistoryOper(DbOper):
:param dest: 数据key
:param storage: 存储类型
"""
return TransferHistory.get_by_dest(self._db, dest, storage)
return self._execute_sync_query(
lambda session: TransferHistory.get_by_dest(session, dest, storage)
)
def list_success_by_src(
self,
@@ -135,11 +157,13 @@ class TransferHistoryOper(DbOper):
:param recursive: 是否递归匹配目录子项
:return: 命中的成功整理记录
"""
return TransferHistory.list_success_by_src(
self._db,
src=src,
storage=storage,
recursive=recursive,
return self._execute_sync_query(
lambda session: TransferHistory.list_success_by_src(
session,
src=src,
storage=storage,
recursive=recursive,
)
)
def list_success_move_by_dest(
@@ -156,11 +180,13 @@ class TransferHistoryOper(DbOper):
:param recursive: 是否递归匹配目录子项
:return: 命中的成功移动记录
"""
return TransferHistory.list_success_move_by_dest(
self._db,
dest=dest,
storage=storage,
recursive=recursive,
return self._execute_sync_query(
lambda session: TransferHistory.list_success_move_by_dest(
session,
dest=dest,
storage=storage,
recursive=recursive,
)
)
def list_by_hash(self, download_hash: str) -> List[TransferHistory]:
@@ -168,7 +194,9 @@ class TransferHistoryOper(DbOper):
按种子hash查询转移记录
:param download_hash: 种子hash
"""
return TransferHistory.list_by_hash(self._db, download_hash)
return self._execute_sync_query(
lambda session: TransferHistory.list_by_hash(session, download_hash)
)
def add(self, **kwargs):
"""
@@ -183,15 +211,21 @@ class TransferHistoryOper(DbOper):
"""
统计最近days天的下载历史数量
"""
return TransferHistory.statistic(self._db, days)
return self._execute_sync_query(
lambda session: TransferHistory.statistic(session, days)
)
async def async_statistic(self, days: int = 7) -> List[Any]:
"""异步统计最近若干天的整理历史数量。"""
return await TransferHistory.async_statistic(self._db, days)
return await self._execute_async_query(
lambda session: TransferHistory.async_statistic(session, days)
)
def monthly_media_statistics(self) -> tuple[int, int, int, int]:
"""统计本月成功整理的电影、剧集、单集和音乐数量。"""
return TransferHistory.monthly_media_statistics(self._db)
return self._execute_sync_query(
TransferHistory.monthly_media_statistics
)
def get_by(self, title: Optional[str] = None, year: Optional[str] = None, mtype: Optional[str] = None,
season: Optional[str] = None, episode: Optional[str] = None,
@@ -200,26 +234,32 @@ class TransferHistoryOper(DbOper):
"""
按类型、标题、年份、季集查询转移记录
"""
return TransferHistory.list_by(db=self._db,
mtype=mtype,
title=title,
dest=dest,
year=year,
season=season,
episode=episode,
media_source=media_source,
media_id=media_id)
return self._execute_sync_query(
lambda session: TransferHistory.list_by(
db=session,
mtype=mtype,
title=title,
dest=dest,
year=year,
season=season,
episode=episode,
media_source=media_source,
media_id=media_id,
)
)
def get_by_media_identity(
self, media_source: MediaSource, media_id: str,
mtype: Optional[str] = None,
) -> Optional[TransferHistory]:
"""按规范媒体身份和类型查询整理记录。"""
return TransferHistory.get_by_media_identity(
db=self._db,
media_source=media_source,
media_id=media_id,
mtype=mtype,
return self._execute_sync_query(
lambda session: TransferHistory.get_by_media_identity(
db=session,
media_source=media_source,
media_id=media_id,
mtype=mtype,
)
)
def delete(self, historyid):
@@ -312,4 +352,6 @@ class TransferHistoryOper(DbOper):
查询某时间之后的转移历史
:param date: 日期
"""
return TransferHistory.list_by_date(self._db, date)
return self._execute_sync_query(
lambda session: TransferHistory.list_by_date(session, date)
)