feat(requests): show ingest progress while requests are in flight

After Lidarr accepts a request the local reconciler imports albums and
tracks as they arrive — but until the request hit 'completed' the
operator had no way to see "is anything happening?" The /requests and
/admin/requests rows now surface a live progress line whenever the
request's matched entity has children in our library.

Backend:
- New CountAlbumsByArtist + CountTracksByArtist sqlc queries.
- requestView gains imported_album_count and imported_track_count.
- New fillProgress helper computes them from the matched entity:
  - kind=artist  → counts albums + tracks under matched_artist_id
  - kind=album   → counts tracks under matched_album_id
  - kind=track   → 1 once matched_track_id is set
  N+1 in the list endpoints; acceptable at admin scale.
- handleListRequests, handleGetRequest, and handleListAdminRequests
  populate the new fields on every response.

Frontend:
- LidarrRequest TS type extended with the two counters.
- Both the operator's /requests page and the admin /admin/requests
  page render an accent-colored line under the row meta when at
  least one counter is non-zero, e.g.:
    Artist:  "5 albums · 47 tracks ingested"
    Album:   "12 tracks ingested"
    Track:   "Track ingested"
- Updated test fixtures to include the new required fields.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-02 13:04:38 -04:00
parent 70d3d60d21
commit f77245bc41
13 changed files with 134 additions and 6 deletions
+5 -1
View File
@@ -9,6 +9,7 @@ import (
"github.com/go-chi/chi/v5"
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/lidarr"
"git.fabledsword.com/bvandeusen/minstrel/internal/lidarrrequests"
)
@@ -55,9 +56,12 @@ func (h *handlers) handleListAdminRequests(w http.ResponseWriter, r *http.Reques
return
}
q := dbq.New(h.pool)
out := make([]requestView, 0, len(rows))
for _, row := range rows {
out = append(out, requestViewFrom(row))
v := requestViewFrom(row)
fillProgress(r.Context(), q, row, &v)
out = append(out, v)
}
writeJSON(w, http.StatusOK, out)
}
+51 -2
View File
@@ -1,6 +1,7 @@
package api
import (
"context"
"encoding/json"
"errors"
"net/http"
@@ -38,6 +39,12 @@ type requestView struct {
MatchedArtistID pgtype.UUID `json:"matched_artist_id,omitempty"`
RequestedAt pgtype.Timestamptz `json:"requested_at"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
// Progress counters, computed at response time from the matched
// entity's children. 0 when nothing has been ingested yet (or when
// the request has no matched entity yet). For artist requests both
// counters fill; for album/track requests only ImportedTrackCount.
ImportedAlbumCount int `json:"imported_album_count"`
ImportedTrackCount int `json:"imported_track_count"`
}
func requestViewFrom(row dbq.LidarrRequest) requestView {
@@ -66,6 +73,43 @@ func requestViewFrom(row dbq.LidarrRequest) requestView {
}
}
// fillProgress fills ImportedAlbumCount + ImportedTrackCount on a view by
// querying the count of children for the request's matched entity.
//
// kind=artist → counts all albums + tracks under the matched artist.
// kind=album → counts tracks under the matched album.
// kind=track → ImportedTrackCount = 1 once matched_track_id is set.
//
// N+1 against the request list: acceptable at admin scale (dozens of
// in-flight requests at most). Errors are swallowed — progress is a
// reporting nicety, not a correctness gate.
func fillProgress(ctx context.Context, q *dbq.Queries, row dbq.LidarrRequest, view *requestView) {
switch row.Kind {
case dbq.LidarrRequestKindArtist:
if !row.MatchedArtistID.Valid {
return
}
if n, err := q.CountAlbumsByArtist(ctx, row.MatchedArtistID); err == nil {
view.ImportedAlbumCount = int(n)
}
if n, err := q.CountTracksByArtist(ctx, row.MatchedArtistID); err == nil {
view.ImportedTrackCount = int(n)
}
case dbq.LidarrRequestKindAlbum:
if !row.MatchedAlbumID.Valid {
return
}
view.ImportedAlbumCount = 1
if n, err := q.CountTracksByAlbum(ctx, row.MatchedAlbumID); err == nil {
view.ImportedTrackCount = int(n)
}
case dbq.LidarrRequestKindTrack:
if row.MatchedTrackID.Valid {
view.ImportedTrackCount = 1
}
}
}
// createRequestBody is the decoded JSON for POST /api/requests.
type createRequestBody struct {
Kind string `json:"kind"`
@@ -128,9 +172,12 @@ func (h *handlers) handleListRequests(w http.ResponseWriter, r *http.Request) {
return
}
q := dbq.New(h.pool)
out := make([]requestView, 0, len(rows))
for _, row := range rows {
out = append(out, requestViewFrom(row))
v := requestViewFrom(row)
fillProgress(r.Context(), q, row, &v)
out = append(out, v)
}
writeJSON(w, http.StatusOK, out)
}
@@ -167,7 +214,9 @@ func (h *handlers) handleGetRequest(w http.ResponseWriter, r *http.Request) {
return
}
writeJSON(w, http.StatusOK, requestViewFrom(row))
v := requestViewFrom(row)
fillProgress(r.Context(), dbq.New(h.pool), row, &v)
writeJSON(w, http.StatusOK, v)
}
// handleCancelRequest implements DELETE /api/requests/:id.