mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-05 23:47:41 +08:00
refactor: 推进后端分层架构治理
This commit is contained in:
@@ -4,7 +4,15 @@ from typing import Any, List, Optional, Annotated
|
||||
from fastapi import Depends
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app import schemas
|
||||
from app.schemas.dashboard import DashboardMemoryInfo as _SchemaDashboardMemoryInfo
|
||||
from app.schemas.dashboard import DashboardSystemInfo as _SchemaDashboardSystemInfo
|
||||
from app.schemas.dashboard import DownloaderInfo as _SchemaDownloaderInfo
|
||||
from app.schemas.dashboard import ProcessInfo as _SchemaProcessInfo
|
||||
from app.schemas.dashboard import ScheduleInfo as _SchemaScheduleInfo
|
||||
from app.schemas.dashboard import ScheduleProgress as _SchemaScheduleProgress
|
||||
from app.schemas.dashboard import Statistic as _SchemaStatistic
|
||||
from app.schemas.dashboard import Storage as _SchemaStorage
|
||||
from app.schemas.response import Response as _SchemaResponse
|
||||
from app.api.response import ResponseAPIRouter
|
||||
from app.chain.dashboard import DashboardChain
|
||||
from app.chain.storage import StorageChain
|
||||
@@ -21,16 +29,16 @@ from app.adapters.system.host import SystemUtils
|
||||
router = ResponseAPIRouter()
|
||||
|
||||
|
||||
def _build_statistic(db: Session, name: Optional[str] = None) -> schemas.Statistic:
|
||||
def _build_statistic(db: Session, name: Optional[str] = None) -> _SchemaStatistic:
|
||||
"""
|
||||
构建媒体数量统计信息。
|
||||
"""
|
||||
media_statistics: Optional[List[schemas.Statistic]] = (
|
||||
media_statistics: Optional[List[_SchemaStatistic]] = (
|
||||
DashboardChain().media_statistic(name)
|
||||
)
|
||||
if media_statistics:
|
||||
# 汇总各媒体库统计信息
|
||||
ret_statistic = schemas.Statistic()
|
||||
ret_statistic = _SchemaStatistic()
|
||||
has_episode_count = False
|
||||
for media_statistic in media_statistics:
|
||||
ret_statistic.movie_count += media_statistic.movie_count or 0
|
||||
@@ -44,7 +52,7 @@ def _build_statistic(db: Session, name: Optional[str] = None) -> schemas.Statist
|
||||
# 所有媒体服务都未提供剧集统计时,返回 None 供前端展示“未获取”。
|
||||
ret_statistic.episode_count = None
|
||||
else:
|
||||
ret_statistic = schemas.Statistic()
|
||||
ret_statistic = _SchemaStatistic()
|
||||
|
||||
movie_count_month, tv_count_month, episode_count_month, music_count_month = (
|
||||
TransferHistory.monthly_media_statistics(db)
|
||||
@@ -56,14 +64,14 @@ def _build_statistic(db: Session, name: Optional[str] = None) -> schemas.Statist
|
||||
return ret_statistic
|
||||
|
||||
|
||||
def _build_storage() -> schemas.Storage:
|
||||
def _build_storage() -> _SchemaStorage:
|
||||
"""
|
||||
构建本地存储空间信息。
|
||||
"""
|
||||
total, available = 0, 0
|
||||
dirs = DirectoryHelper().get_dirs()
|
||||
if not dirs:
|
||||
return schemas.Storage(total_storage=total, used_storage=total - available)
|
||||
return _SchemaStorage(total_storage=total, used_storage=total - available)
|
||||
# 下载目录按 storage、媒体库目录按 library_storage 汇总存储集合,
|
||||
# 用 set 去重存储名,避免同一存储被重复统计;
|
||||
# 各存储的 usage 内部已按磁盘(st_dev / Btrfs FSID)去重,相同磁盘的不同目录不会重复累加。
|
||||
@@ -77,10 +85,10 @@ def _build_storage() -> schemas.Storage:
|
||||
if _usage:
|
||||
total += _usage.get("total") or 0
|
||||
available += _usage.get("available") or 0
|
||||
return schemas.Storage(total_storage=total, used_storage=total - available)
|
||||
return _SchemaStorage(total_storage=total, used_storage=total - available)
|
||||
|
||||
|
||||
def _build_downloader(name: Optional[str] = None) -> schemas.DownloaderInfo:
|
||||
def _build_downloader(name: Optional[str] = None) -> _SchemaDownloaderInfo:
|
||||
"""
|
||||
构建下载器统计信息。
|
||||
"""
|
||||
@@ -91,7 +99,7 @@ def _build_downloader(name: Optional[str] = None) -> schemas.DownloaderInfo:
|
||||
btrfs_fsid_dedup=settings.BTRFS_FSID_DEDUP,
|
||||
)
|
||||
# 下载器信息
|
||||
downloader_info = schemas.DownloaderInfo()
|
||||
downloader_info = _SchemaDownloaderInfo()
|
||||
transfer_infos = DashboardChain().downloader_info(name)
|
||||
if transfer_infos:
|
||||
for transfer_info in transfer_infos:
|
||||
@@ -103,7 +111,7 @@ def _build_downloader(name: Optional[str] = None) -> schemas.DownloaderInfo:
|
||||
return downloader_info
|
||||
|
||||
|
||||
@router.get("/statistic", summary="媒体数量统计", response_model=schemas.Statistic)
|
||||
@router.get("/statistic", summary="媒体数量统计", response_model=_SchemaStatistic)
|
||||
def statistic(
|
||||
name: Optional[str] = None,
|
||||
db: Session = Depends(get_db),
|
||||
@@ -116,7 +124,7 @@ def statistic(
|
||||
|
||||
|
||||
@router.get(
|
||||
"/statistic2", summary="媒体数量统计(API_TOKEN)", response_model=schemas.Statistic
|
||||
"/statistic2", summary="媒体数量统计(API_TOKEN)", response_model=_SchemaStatistic
|
||||
)
|
||||
def statistic2(
|
||||
_: Annotated[str, Depends(verify_apitoken)],
|
||||
@@ -128,7 +136,7 @@ def statistic2(
|
||||
return _build_statistic(db)
|
||||
|
||||
|
||||
@router.get("/storage", summary="本地存储空间", response_model=schemas.Storage)
|
||||
@router.get("/storage", summary="本地存储空间", response_model=_SchemaStorage)
|
||||
def storage(_: Any = Depends(get_current_active_superuser)) -> Any:
|
||||
"""
|
||||
查询本地存储空间信息
|
||||
@@ -137,7 +145,7 @@ def storage(_: Any = Depends(get_current_active_superuser)) -> Any:
|
||||
|
||||
|
||||
@router.get(
|
||||
"/storage2", summary="本地存储空间(API_TOKEN)", response_model=schemas.Storage
|
||||
"/storage2", summary="本地存储空间(API_TOKEN)", response_model=_SchemaStorage
|
||||
)
|
||||
def storage2(_: Annotated[str, Depends(verify_apitoken)]) -> Any:
|
||||
"""
|
||||
@@ -146,7 +154,7 @@ def storage2(_: Annotated[str, Depends(verify_apitoken)]) -> Any:
|
||||
return _build_storage()
|
||||
|
||||
|
||||
@router.get("/processes", summary="进程信息", response_model=List[schemas.ProcessInfo])
|
||||
@router.get("/processes", summary="进程信息", response_model=List[_SchemaProcessInfo])
|
||||
def processes(_: Any = Depends(get_current_active_superuser)) -> Any:
|
||||
"""
|
||||
查询进程信息
|
||||
@@ -154,7 +162,7 @@ def processes(_: Any = Depends(get_current_active_superuser)) -> Any:
|
||||
return SystemUtils.processes()
|
||||
|
||||
|
||||
@router.get("/system", summary="系统摘要信息", response_model=schemas.DashboardSystemInfo)
|
||||
@router.get("/system", summary="系统摘要信息", response_model=_SchemaDashboardSystemInfo)
|
||||
def system_info(_: Any = Depends(get_current_active_superuser)) -> Any:
|
||||
"""
|
||||
查询仪表板系统摘要信息
|
||||
@@ -162,7 +170,7 @@ def system_info(_: Any = Depends(get_current_active_superuser)) -> Any:
|
||||
return SystemUtils.dashboard_system_info()
|
||||
|
||||
|
||||
@router.get("/downloader", summary="下载器信息", response_model=schemas.DownloaderInfo)
|
||||
@router.get("/downloader", summary="下载器信息", response_model=_SchemaDownloaderInfo)
|
||||
def downloader(
|
||||
name: Optional[str] = None, _: Any = Depends(get_current_active_superuser)
|
||||
) -> Any:
|
||||
@@ -175,7 +183,7 @@ def downloader(
|
||||
@router.get(
|
||||
"/downloader2",
|
||||
summary="下载器信息(API_TOKEN)",
|
||||
response_model=schemas.DownloaderInfo,
|
||||
response_model=_SchemaDownloaderInfo,
|
||||
)
|
||||
def downloader2(_: Annotated[str, Depends(verify_apitoken)]) -> Any:
|
||||
"""
|
||||
@@ -184,7 +192,7 @@ def downloader2(_: Annotated[str, Depends(verify_apitoken)]) -> Any:
|
||||
return _build_downloader()
|
||||
|
||||
|
||||
@router.get("/schedule", summary="后台服务", response_model=List[schemas.ScheduleInfo])
|
||||
@router.get("/schedule", summary="后台服务", response_model=List[_SchemaScheduleInfo])
|
||||
async def schedule(_: Any = Depends(get_current_active_superuser)) -> Any:
|
||||
"""
|
||||
查询后台服务信息
|
||||
@@ -195,7 +203,7 @@ async def schedule(_: Any = Depends(get_current_active_superuser)) -> Any:
|
||||
@router.get(
|
||||
"/schedule/{job_id}/progress",
|
||||
summary="后台服务进度",
|
||||
response_model=schemas.Response[schemas.ScheduleProgress],
|
||||
response_model=_SchemaResponse[_SchemaScheduleProgress],
|
||||
)
|
||||
async def schedule_progress(
|
||||
job_id: str, _: Any = Depends(get_current_active_superuser)
|
||||
@@ -205,14 +213,14 @@ async def schedule_progress(
|
||||
"""
|
||||
progress = Scheduler().get_progress(job_id)
|
||||
if not progress:
|
||||
return schemas.Response(success=False, message="后台服务不存在")
|
||||
return schemas.Response(success=True, data=progress.model_dump())
|
||||
return _SchemaResponse(success=False, message="后台服务不存在")
|
||||
return _SchemaResponse(success=True, data=progress.model_dump())
|
||||
|
||||
|
||||
@router.get(
|
||||
"/schedule2",
|
||||
summary="后台服务(API_TOKEN)",
|
||||
response_model=List[schemas.ScheduleInfo],
|
||||
response_model=List[_SchemaScheduleInfo],
|
||||
)
|
||||
async def schedule2(_: Annotated[str, Depends(verify_apitoken)]) -> Any:
|
||||
"""
|
||||
@@ -224,7 +232,7 @@ async def schedule2(_: Annotated[str, Depends(verify_apitoken)]) -> Any:
|
||||
@router.get(
|
||||
"/schedule2/{job_id}/progress",
|
||||
summary="后台服务进度(API_TOKEN)",
|
||||
response_model=schemas.Response[schemas.ScheduleProgress],
|
||||
response_model=_SchemaResponse[_SchemaScheduleProgress],
|
||||
)
|
||||
async def schedule_progress2(
|
||||
job_id: str, _: Annotated[str, Depends(verify_apitoken)]
|
||||
@@ -234,8 +242,8 @@ async def schedule_progress2(
|
||||
"""
|
||||
progress = Scheduler().get_progress(job_id)
|
||||
if not progress:
|
||||
return schemas.Response(success=False, message="后台服务不存在")
|
||||
return schemas.Response(success=True, data=progress.model_dump())
|
||||
return _SchemaResponse(success=False, message="后台服务不存在")
|
||||
return _SchemaResponse(success=True, data=progress.model_dump())
|
||||
|
||||
|
||||
@router.get("/transfer", summary="文件整理统计", response_model=List[int])
|
||||
@@ -270,7 +278,7 @@ def cpu2(_: Annotated[str, Depends(verify_apitoken)]) -> Any:
|
||||
@router.get(
|
||||
"/memory",
|
||||
summary="获取当前应用与系统内存信息",
|
||||
response_model=schemas.DashboardMemoryInfo,
|
||||
response_model=_SchemaDashboardMemoryInfo,
|
||||
)
|
||||
def memory(_: Any = Depends(get_current_active_superuser)) -> Any:
|
||||
"""
|
||||
@@ -282,7 +290,7 @@ def memory(_: Any = Depends(get_current_active_superuser)) -> Any:
|
||||
@router.get(
|
||||
"/memory2",
|
||||
summary="获取当前应用与系统内存信息(API_TOKEN)",
|
||||
response_model=schemas.DashboardMemoryInfo,
|
||||
response_model=_SchemaDashboardMemoryInfo,
|
||||
)
|
||||
def memory2(_: Annotated[str, Depends(verify_apitoken)]) -> Any:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user