mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-11-25 21:33:08 +08:00
feat(widget): 显示下载上传速度和剩余磁盘空间 (#7177)
This commit is contained in:
86
app/api/dependencies/dashboard.py
Normal file
86
app/api/dependencies/dashboard.py
Normal file
@@ -0,0 +1,86 @@
|
||||
"""
|
||||
为仪表盘和受限小组件装配同一下载器与本地目录采样口径。
|
||||
"""
|
||||
|
||||
import math
|
||||
from pathlib import Path
|
||||
|
||||
from app.adapters.system.host import SystemUtils
|
||||
from app.application.directory import DirectoryHelper
|
||||
from app.chain.dashboard import DashboardChain
|
||||
from app.schemas.dashboard import DownloaderInfo
|
||||
|
||||
|
||||
def _download_free_space(*, btrfs_fsid_dedup: bool, require_source: bool) -> float | None:
|
||||
"""
|
||||
复用目录空间去重,严格读取不把未配置或不完整的容量误报为零。
|
||||
|
||||
:param btrfs_fsid_dedup: 原仪表盘使用的文件系统去重配置
|
||||
:param require_source: 小组件需要可确认的完整本地目录来源
|
||||
:return: 可确认的剩余字节数;严格模式的未知容量为 None
|
||||
:raises OSError: 普通仪表盘保留原有的空间读取异常
|
||||
"""
|
||||
try:
|
||||
directories = DirectoryHelper().get_local_download_dirs()
|
||||
paths = [Path(directory.download_path) for directory in directories if directory.download_path]
|
||||
if require_source and (not paths or not all(path.exists() for path in paths)):
|
||||
return None
|
||||
total, available = SystemUtils.space_usage(paths, btrfs_fsid_dedup=btrfs_fsid_dedup)
|
||||
if require_source and (
|
||||
not math.isfinite(total) or not math.isfinite(available) or not 0 <= available <= total or total <= 0
|
||||
):
|
||||
return None
|
||||
return available
|
||||
except OSError:
|
||||
if require_source:
|
||||
return None
|
||||
raise
|
||||
|
||||
|
||||
def _check_speeds(info: DownloaderInfo) -> None:
|
||||
"""
|
||||
拒绝缺失、负数和非有限的原始速度,真实零速率仍是有效采样。
|
||||
|
||||
:param info: 单个下载器或汇总后的原始字节统计
|
||||
:raises ValueError: 速率来源不能代表本次真实采样
|
||||
"""
|
||||
if any(
|
||||
value is None or not math.isfinite(value) or value < 0 for value in (info.download_speed, info.upload_speed)
|
||||
):
|
||||
raise ValueError("下载器速度来源暂不可用")
|
||||
|
||||
|
||||
def sample_downloader(
|
||||
name: str | None = None,
|
||||
*,
|
||||
btrfs_fsid_dedup: bool = False,
|
||||
require_source: bool = False,
|
||||
) -> DownloaderInfo:
|
||||
"""
|
||||
汇总原下载器的字节速率和累计量,包含下载列表之外的做种上传。
|
||||
|
||||
:param name: 原仪表盘可选择的下载器,None 表示全部
|
||||
:param btrfs_fsid_dedup: 本次请求的原目录空间去重配置
|
||||
:param require_source: 仅小组件严格拒绝缺失或非法速度;普通页面保留零值兼容
|
||||
:return: 共用口径的统计,严格模式的未知磁盘容量为 None
|
||||
:raises ValueError: 严格模式下缺少真实下载器采样或速度非法
|
||||
"""
|
||||
free_space = _download_free_space(btrfs_fsid_dedup=btrfs_fsid_dedup, require_source=require_source)
|
||||
transfer_infos = DashboardChain().downloader_info(name)
|
||||
if not transfer_infos:
|
||||
if require_source:
|
||||
raise ValueError("下载器统计来源暂不可用")
|
||||
return DownloaderInfo()
|
||||
if require_source:
|
||||
for info in transfer_infos:
|
||||
_check_speeds(info)
|
||||
result = DownloaderInfo(
|
||||
download_speed=sum(info.download_speed or 0 for info in transfer_infos),
|
||||
upload_speed=sum(info.upload_speed or 0 for info in transfer_infos),
|
||||
download_size=sum(info.download_size or 0 for info in transfer_infos),
|
||||
upload_size=sum(info.upload_size or 0 for info in transfer_infos),
|
||||
free_space=free_space,
|
||||
)
|
||||
if require_source:
|
||||
_check_speeds(result)
|
||||
return result
|
||||
@@ -8,8 +8,10 @@ from typing import NoReturn
|
||||
from fastapi import Depends, HTTPException, Response
|
||||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
||||
|
||||
from app.api.context import get_host_runtime
|
||||
from app.api.context import get_api_runtime_config, get_host_runtime
|
||||
from app.api.dependencies.dashboard import sample_downloader
|
||||
from app.api.dependencies.history import get_dashboard_query_service, get_history_query_service
|
||||
from app.application.configuration import ApiRuntimeConfig
|
||||
from app.application.dashboard import DashboardQueryService
|
||||
from app.application.history import HistoryQueryService
|
||||
from app.application.query import get_configured_data_query_service
|
||||
@@ -74,12 +76,14 @@ def get_widget_grant(
|
||||
def get_widget_snapshot_service(
|
||||
dashboard: DashboardQueryService = Depends(get_dashboard_query_service),
|
||||
history: HistoryQueryService = Depends(get_history_query_service),
|
||||
runtime_config: ApiRuntimeConfig = Depends(get_api_runtime_config),
|
||||
) -> WidgetSnapshotService:
|
||||
"""
|
||||
只连接现有的查询与业务链,普通页面不会调用小组件专用接口。
|
||||
|
||||
:param dashboard: 原仪表板管理员查询服务
|
||||
:param history: 原历史列表查询服务
|
||||
:param runtime_config: 本次请求的空间去重配置,不从组件凭据或参数读取
|
||||
:return: 使用原有 owner、计数和成功筛选的快照投影服务
|
||||
"""
|
||||
return WidgetSnapshotService(
|
||||
@@ -87,5 +91,6 @@ def get_widget_snapshot_service(
|
||||
history=history,
|
||||
dashboard=dashboard,
|
||||
downloads=partial(DownloadChain().downloading, require_source=True),
|
||||
downloader=partial(sample_downloader, btrfs_fsid_dedup=runtime_config.btrfs_fsid_dedup, require_source=True),
|
||||
storage=partial(DashboardChain().storage, require_source=True),
|
||||
)
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
汇总仪表盘所需的服务统计、系统状态与活动数据。
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Annotated, Any, List, Optional
|
||||
|
||||
from fastapi import Depends
|
||||
@@ -11,6 +10,7 @@ from app.adapters.system.host import SystemUtils
|
||||
from app.adapters.web.security.access import verify_apitoken
|
||||
from app.api.context import get_api_runtime_config, resolve_api_runtime_config
|
||||
from app.api.dependencies.auth import get_current_active_superuser
|
||||
from app.api.dependencies.dashboard import sample_downloader
|
||||
from app.api.dependencies.history import get_dashboard_query_service
|
||||
from app.api.response import (
|
||||
CompatibleCountParam,
|
||||
@@ -19,7 +19,6 @@ from app.api.response import (
|
||||
)
|
||||
from app.application.configuration import ApiRuntimeConfig
|
||||
from app.application.dashboard import DashboardQueryService
|
||||
from app.application.directory import DirectoryHelper
|
||||
from app.application.scheduling import get_scheduler
|
||||
from app.chain.dashboard import DashboardChain
|
||||
from app.runtime.execution import run_in_threadpool
|
||||
@@ -49,25 +48,13 @@ def _build_downloader(
|
||||
btrfs_fsid_dedup: bool = False,
|
||||
) -> _SchemaDownloaderInfo:
|
||||
"""
|
||||
构建下载器统计信息。
|
||||
使用共用采样口径构建下载器统计,保持普通页面的宽容来源语义。
|
||||
|
||||
:param name: 下载器名称,None 表示全部
|
||||
:param btrfs_fsid_dedup: 当前请求的 Btrfs 文件系统去重配置
|
||||
:return: 原仪表盘的全局字节统计与本地目录可用容量
|
||||
"""
|
||||
# 下载目录空间
|
||||
download_dirs = DirectoryHelper().get_local_download_dirs()
|
||||
_, free_space = SystemUtils.space_usage(
|
||||
[Path(d.download_path) for d in download_dirs if d.download_path],
|
||||
btrfs_fsid_dedup=btrfs_fsid_dedup,
|
||||
)
|
||||
# 下载器信息
|
||||
downloader_info = _SchemaDownloaderInfo()
|
||||
transfer_infos = DashboardChain().downloader_info(name)
|
||||
if transfer_infos:
|
||||
for transfer_info in transfer_infos:
|
||||
downloader_info.download_speed = (downloader_info.download_speed or 0) + (transfer_info.download_speed or 0)
|
||||
downloader_info.upload_speed = (downloader_info.upload_speed or 0) + (transfer_info.upload_speed or 0)
|
||||
downloader_info.download_size = (downloader_info.download_size or 0) + (transfer_info.download_size or 0)
|
||||
downloader_info.upload_size = (downloader_info.upload_size or 0) + (transfer_info.upload_size or 0)
|
||||
downloader_info.free_space = free_space
|
||||
return downloader_info
|
||||
return sample_downloader(name, btrfs_fsid_dedup=btrfs_fsid_dedup)
|
||||
|
||||
|
||||
@router.get("/statistic", summary="媒体数量统计", response_model=_SchemaStatistic)
|
||||
|
||||
@@ -15,7 +15,7 @@ from app.application.security.widget import WidgetGrant, available_widget_conten
|
||||
from app.foundation.size import format_size
|
||||
from app.runtime.execution import run_in_threadpool
|
||||
from app.runtime.log import logger
|
||||
from app.schemas.dashboard import Storage
|
||||
from app.schemas.dashboard import DownloaderInfo, Storage
|
||||
from app.schemas.query import QueryPageRequest, SubscriptionFilter
|
||||
from app.schemas.transfer import DownloaderTorrent
|
||||
from app.schemas.types import MediaType
|
||||
@@ -84,6 +84,7 @@ class WidgetSnapshotService:
|
||||
history: HistoryQueryService,
|
||||
dashboard: DashboardQueryService,
|
||||
downloads: Callable[[], list[DownloaderTorrent] | None],
|
||||
downloader: Callable[[], DownloaderInfo],
|
||||
storage: Callable[[], Storage],
|
||||
clock: Callable[[], float] = time.time,
|
||||
) -> None:
|
||||
@@ -94,6 +95,7 @@ class WidgetSnapshotService:
|
||||
:param history: 既有整理历史查询,保留成功筛选和数据库分页
|
||||
:param dashboard: 既有管理员媒体统计查询
|
||||
:param downloads: 原有下载链的采样回调,仅在工作线程内执行
|
||||
:param downloader: 共用下载器字节统计;只由管理员在工作线程内读取
|
||||
:param storage: 既有存储汇总的严格读取回调
|
||||
:param clock: 可信 UTC Unix 秒来源
|
||||
"""
|
||||
@@ -101,6 +103,7 @@ class WidgetSnapshotService:
|
||||
self._history = history
|
||||
self._dashboard = dashboard
|
||||
self._downloads = downloads
|
||||
self._downloader = downloader
|
||||
self._storage = storage
|
||||
self._clock = clock
|
||||
|
||||
@@ -208,11 +211,13 @@ class WidgetSnapshotService:
|
||||
|
||||
async def _download_section(self, user: UserSnapshot) -> WidgetSection:
|
||||
"""
|
||||
按原网页精确 owner 规则过滤采样,只投影前六项,不输出 hash 或文件路径。
|
||||
按原网页精确 owner 过滤任务,仅管理员读取全局速率与本地目录空间。
|
||||
|
||||
:param user: 本次重新鉴权的用户与既有权限快照
|
||||
:return: 最多六项任务及同作用域的真实计数,管理员附加三项全局指标
|
||||
:raises ValueError: 任务或统计来源暂不可用,或原始进度超出自然范围
|
||||
"""
|
||||
rows = await run_in_threadpool(self._downloads)
|
||||
if rows is None:
|
||||
raise ValueError("下载来源暂不可用")
|
||||
rows, info = await run_in_threadpool(self._download_sources, user.is_superuser)
|
||||
visible = (
|
||||
rows if user.is_superuser else [row for row in rows if row.userid == user.name or row.username == user.name]
|
||||
)
|
||||
@@ -229,7 +234,48 @@ class WidgetSnapshotService:
|
||||
progress=progress / 100 if progress is not None else None,
|
||||
)
|
||||
)
|
||||
return self._ready("downloads", len(visible), items, icon="download")
|
||||
section = self._ready("downloads", len(visible), items, icon="download")
|
||||
if info is not None:
|
||||
section.metrics = self._download_metrics(info) + section.metrics
|
||||
# 无下载任务仍可正在做种上传,有效速率不能被空列表状态隐藏。
|
||||
section.state = "ready"
|
||||
return section
|
||||
|
||||
def _download_sources(self, administrator: bool) -> tuple[list[DownloaderTorrent], DownloaderInfo | None]:
|
||||
"""
|
||||
在同一个受管工作线程采样任务与统计,普通用户不触发全局读取。
|
||||
|
||||
:param administrator: 当前重新鉴权用户是否为超级管理员
|
||||
:return: 真实任务列表与仅管理员可读的原始下载器统计
|
||||
:raises ValueError: 原下载任务来源尚不可用
|
||||
"""
|
||||
rows = self._downloads()
|
||||
if rows is None:
|
||||
raise ValueError("下载来源暂不可用")
|
||||
return rows, self._downloader() if administrator else None
|
||||
|
||||
def _download_metrics(self, info: DownloaderInfo) -> list[WidgetMetric]:
|
||||
"""
|
||||
投影管理员全局速率与剩余空间,未知容量和真实磁盘满明确区分。
|
||||
|
||||
:param info: 既有下载器的原始字节采样,不从任务展示文字重算
|
||||
:return: 固定标识的上传、下载和空间指标
|
||||
:raises ValueError: 速度缺失、负数或非有限;不能伪造为成功的零值
|
||||
"""
|
||||
download, upload, available = info.download_speed, info.upload_speed, info.free_space
|
||||
if download is None or upload is None or not all(
|
||||
math.isfinite(value) and value >= 0 for value in (download, upload)
|
||||
):
|
||||
raise ValueError("下载器速度来源暂不可用")
|
||||
free_space = (
|
||||
format_size(int(available))
|
||||
if available is not None and math.isfinite(available) and available >= 0 else "—"
|
||||
)
|
||||
return [
|
||||
_metric("downloadSpeed", "下载速度", f"{format_size(int(download))}/s", "download"),
|
||||
_metric("uploadSpeed", "上传速度", f"{format_size(int(upload))}/s", "upload"),
|
||||
_metric("freeSpace", "剩余空间", free_space, "storage"),
|
||||
]
|
||||
|
||||
async def _overview(self) -> WidgetSection:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user