feat(widgets): add backend source adapters and per-widget data endpoint

PR 2 of 4 for configurable dashboard widgets.

- Add grafana_url and prometheus_url settings (config.py + compose/env).
- Create WidgetSource protocol and adapters for jellyfin, backups, grafana,
  prometheus, ssh_task, and static sources.
- Add GET /api/widgets/instances/{id}/data endpoint.
- Extract shared dashboard helpers into domain/dashboard.py so widgets and
  the dashboard router reuse the same logic.
- Add adapter and data-endpoint tests.
- Update apply-progress.md.

Verification: ruff clean; backend pytest 200 passed; frontend lint/build green.
This commit is contained in:
Developer
2026-06-21 10:09:45 +00:00
parent 1a52dfb087
commit 1cd8e926de
10 changed files with 607 additions and 85 deletions
@@ -51,9 +51,50 @@ Focused widget test output: `12 passed`.
- None significant for Slice 1. The implementation follows the design's backend CRUD layout.
- Used `HTTP_422_UNPROCESSABLE_CONTENT` instead of the deprecated `HTTP_422_UNPROCESSABLE_ENTITY`.
## Completed tasks (Slice 2)
All Slice 2 tasks are marked `- [x]` in `tasks.md`:
- [x] 2.1 Add observability URL settings (`grafana_url`, `prometheus_url`)
- [x] 2.2 Create source adapters (`jellyfin`, `backups`, `grafana`, `prometheus`, `ssh_task`, `static`)
- [x] 2.3 Add per-widget data endpoint (`GET /api/widgets/instances/{id}/data`)
- [x] 2.4 Extract shared backup/Jellyfin dashboard helpers into `domain/dashboard.py`
- [x] 2.5 Add adapter + data endpoint tests
## Files changed (Slice 2)
### New files
- `backend/src/media_library_viewer_api/widgets/sources.py``WidgetSource` protocol and six source adapters.
- `backend/src/media_library_viewer_api/domain/dashboard.py` — Shared dashboard helpers (`_map_sessions_to_activity_rows`, `build_backup_dashboard_summary`).
### Modified files
- `backend/src/media_library_viewer_api/config.py` — Added `grafana_url` and `prometheus_url` settings.
- `backend/src/media_library_viewer_api/routers/widgets.py` — Added `GET /api/widgets/instances/{id}/data`.
- `backend/src/media_library_viewer_api/routers/dashboard.py` — Delegated to shared `domain/dashboard.py` helpers.
- `backend/tests/test_widgets.py` — Added adapter and data endpoint tests.
- `docker-compose.yml`, `docker-compose.dev.yml`, `.env.example` — Wired `GRAFANA_URL` and `PROMETHEUS_URL` for the new adapters.
## Verification (Slice 2)
```bash
cd backend
.venv/bin/python -m ruff check . # All checks passed
PYTHONPATH=src .venv/bin/python -m pytest # 200 passed, 2 warnings
cd ../frontend
npm run lint # 2 pre-existing warnings, 0 errors
npm run build # Built successfully
```
Focused widget test output: `27 passed`.
## Deviations from design (Slice 2)
- Adapters currently call `get_settings_store()` internally for `backups`/`ssh_task` sources. The router-level endpoint uses FastAPI DI, but adapter unit tests patch `get_settings_store` to inject a test store. A future refactor can pass `store` and `settings` explicitly into `adapter.fetch()` for cleaner testability.
## Remaining work
- Slice 2: Backend source adapters + `GET /api/widgets/instances/{id}/data`
- Slice 3: Frontend types/API/hooks/registry/components
- Slice 4: Dashboard loop + configuration UI + addon pages