feat(jellyseer): stats backend — provider, stat widgets, router (slice 1/3)
Groundwork for Jellyseerr request stats in the Jellyfin service, behind a
small reusable abstraction so future stats services (Sonarr/Radarr) reuse it.
Backend:
- JellyseerrClient.request_count() -> /api/v1/request/count (normalized
total/pending/approved/declined/processing/available) and recent_requests()
-> /api/v1/request mapped to {name,type,status,media_status,created_at}
with numeric status enums labelled.
- widgets/stats_provider.py: StatsProvider protocol + registry keyed by
service_type (StatValue/StatsResult). A thin generic interface.
- widgets/jellyseerr_stats.py: JellyseerrStatsProvider registered for the
Jellyfin service; reuses one authenticated client per service (lru_cache) and
caches the StatsResult for ~10s under a lock, so multiple widgets + the tab
collapse onto one Jellyseerr fetch (same lesson as the qBittorrent client).
Accepts jellyseerr_api_key from secrets OR config during the upcoming
config->secret migration.
- Jellyfin service gains two widget kinds: `stat` (a Literal selector over the
six stats — the "extract one value into a widget" affordance) and
`stats_overview` (all stats + recent list).
- widgets router routes widget_kind in {stat, stats_overview} to a generic
StatsWidgetSource (dispatches to the service type's provider), independent of
service type.
- new /api/jellyseerr/stats router endpoint for the Requests tab (resolves the
Jellyfin service by id or first-enabled; shares the provider cache).
Tests: provider normalization, not-configured, TTL caching; stat selector +
overview + unknown-stat widget dispatch; 7 new tests. 400/400 backend pass;
ruff clean.
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
"""Jellyseerr request stats — powers the Requests tab on the Jellyfin page.
|
||||
|
||||
Jellyseerr is an optional companion of the Jellyfin service. This router
|
||||
resolves the Jellyfin service instance (by ``jellyfin_service_id`` or the first
|
||||
enabled one) and delegates to the registered Jellyseerr stats provider, which
|
||||
shares its short-TTL cache with the ``stat`` / ``stats_overview`` widgets so
|
||||
the tab and the widgets don't each hit Jellyseerr.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
|
||||
from media_library_viewer_api.dependencies import get_settings_store
|
||||
from media_library_viewer_api.services.service_resolution import resolve_service_record
|
||||
from media_library_viewer_api.services.settings_store import SettingsStore
|
||||
from media_library_viewer_api.widgets import jellyseerr_stats # noqa: F401 — ensure provider registration
|
||||
from media_library_viewer_api.widgets.stats_provider import get_stats_provider
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/api/jellyseerr", tags=["jellyseerr"])
|
||||
|
||||
|
||||
def _serialize(result) -> dict:
|
||||
return {
|
||||
"stats": [{"key": s.key, "label": s.label, "value": s.value} for s in result.stats],
|
||||
"recent": result.recent,
|
||||
"detail": result.detail,
|
||||
}
|
||||
|
||||
|
||||
@router.get("/stats")
|
||||
def get_jellyseerr_stats(
|
||||
jellyfin_service_id: str | None = None,
|
||||
store: SettingsStore = Depends(get_settings_store),
|
||||
) -> dict:
|
||||
"""Return Jellyseerr request counts + a recent-requests list."""
|
||||
service = resolve_service_record(store, "jellyfin", jellyfin_service_id)
|
||||
if service is None:
|
||||
raise HTTPException(status_code=503, detail="No Jellyfin service is configured.")
|
||||
provider = get_stats_provider("jellyfin")
|
||||
if provider is None: # pragma: no cover - registered at import
|
||||
raise HTTPException(status_code=503, detail="Jellyseerr stats provider is not available.")
|
||||
try:
|
||||
result = provider.fetch_stats(service)
|
||||
except Exception as exc: # pragma: no cover - provider guards internally
|
||||
logger.exception("Jellyseerr stats endpoint failed")
|
||||
raise HTTPException(status_code=502, detail=f"Jellyseerr fetch failed: {exc}") from exc
|
||||
return _serialize(result)
|
||||
Reference in New Issue
Block a user