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.
+13
View File
@@ -23,6 +23,19 @@ func (q *Queries) CountAlbums(ctx context.Context) (int64, error) {
return count, err
}
const countAlbumsByArtist = `-- name: CountAlbumsByArtist :one
SELECT COUNT(*) FROM albums WHERE artist_id = $1
`
// Used by request-progress reporting to count how many albums have been
// ingested for a matched artist while a request is still in flight.
func (q *Queries) CountAlbumsByArtist(ctx context.Context, artistID pgtype.UUID) (int64, error) {
row := q.db.QueryRow(ctx, countAlbumsByArtist, artistID)
var count int64
err := row.Scan(&count)
return count, err
}
const countAlbumsMatching = `-- name: CountAlbumsMatching :one
SELECT COUNT(*) FROM albums WHERE title ILIKE '%' || $1::text || '%'
`
+14
View File
@@ -22,6 +22,20 @@ func (q *Queries) CountTracksByAlbum(ctx context.Context, albumID pgtype.UUID) (
return count, err
}
const countTracksByArtist = `-- name: CountTracksByArtist :one
SELECT COUNT(*) FROM tracks WHERE artist_id = $1
`
// Used by request-progress reporting to count tracks ingested under a
// matched artist (sum across all the artist's albums) while a request
// is still in flight.
func (q *Queries) CountTracksByArtist(ctx context.Context, artistID pgtype.UUID) (int64, error) {
row := q.db.QueryRow(ctx, countTracksByArtist, artistID)
var count int64
err := row.Scan(&count)
return count, err
}
const countTracksMatching = `-- name: CountTracksMatching :one
SELECT COUNT(*) FROM tracks WHERE title ILIKE '%' || $1::text || '%'
`
+5
View File
@@ -80,3 +80,8 @@ LIMIT $1 OFFSET $2;
-- name: CountAlbums :one
-- M6a: total album count for the /api/library/albums envelope.
SELECT COUNT(*) FROM albums;
-- name: CountAlbumsByArtist :one
-- Used by request-progress reporting to count how many albums have been
-- ingested for a matched artist while a request is still in flight.
SELECT COUNT(*) FROM albums WHERE artist_id = $1;
+6
View File
@@ -90,3 +90,9 @@ WHERE t.artist_id = $1
)
ORDER BY albums.release_date NULLS LAST, albums.sort_title,
t.disc_number NULLS FIRST, t.track_number NULLS FIRST, t.id;
-- name: CountTracksByArtist :one
-- Used by request-progress reporting to count tracks ingested under a
-- matched artist (sum across all the artist's albums) while a request
-- is still in flight.
SELECT COUNT(*) FROM tracks WHERE artist_id = $1;
+3 -1
View File
@@ -66,7 +66,9 @@ const baseRow: LidarrRequest = {
matched_album_id: null,
matched_artist_id: null,
requested_at: '2026-01-01T00:00:00Z',
updated_at: '2026-01-01T00:00:00Z'
updated_at: '2026-01-01T00:00:00Z',
imported_album_count: 0,
imported_track_count: 0
};
beforeEach(() => {
+3 -1
View File
@@ -41,7 +41,9 @@ const mockRow: LidarrRequest = {
matched_album_id: null,
matched_artist_id: null,
requested_at: '2026-01-01T00:00:00Z',
updated_at: '2026-01-01T00:00:00Z'
updated_at: '2026-01-01T00:00:00Z',
imported_album_count: 0,
imported_track_count: 0
};
beforeEach(() => {
+5
View File
@@ -123,6 +123,11 @@ export type LidarrRequest = {
matched_artist_id?: string | null;
requested_at: string;
updated_at: string;
// Live ingest counters. Server fills these from the matched entity's
// children. Useful for in-flight requests so the operator can see
// "5 albums · 47 tracks" while Lidarr is still working on the rest.
imported_album_count: number;
imported_track_count: number;
};
export type LidarrConfig = {
@@ -256,6 +256,18 @@
<div class="truncate text-sm text-text-secondary">
{rowMeta(r)}
</div>
{#if r.imported_album_count > 0 || r.imported_track_count > 0}
<div class="text-sm text-accent" data-testid="ingest-progress">
{#if r.kind === 'artist'}
{r.imported_album_count} {r.imported_album_count === 1 ? 'album' : 'albums'}
· {r.imported_track_count} {r.imported_track_count === 1 ? 'track' : 'tracks'} ingested
{:else if r.kind === 'album'}
{r.imported_track_count} {r.imported_track_count === 1 ? 'track' : 'tracks'} ingested
{:else}
Track ingested
{/if}
</div>
{/if}
{#if r.kind === 'track' && r.album_title}
<div class="text-sm text-text-secondary" data-testid="track-disclosure">
Approving will add the album <em class="font-medium text-text-primary">{r.album_title}</em>.
@@ -37,7 +37,9 @@ const baseRow: LidarrRequest = {
artist_name: 'Boards of Canada',
album_title: 'Geogaddi',
requested_at: '2026-04-29T10:00:00Z',
updated_at: '2026-04-29T10:00:00Z'
updated_at: '2026-04-29T10:00:00Z',
imported_album_count: 0,
imported_track_count: 0
};
afterEach(() => vi.clearAllMocks());
+12
View File
@@ -103,6 +103,18 @@
<div class="truncate text-sm text-text-secondary">
{rowMeta(r)}
</div>
{#if r.imported_album_count > 0 || r.imported_track_count > 0}
<div class="text-sm text-accent" data-testid="ingest-progress">
{#if r.kind === 'artist'}
{r.imported_album_count} {r.imported_album_count === 1 ? 'album' : 'albums'}
· {r.imported_track_count} {r.imported_track_count === 1 ? 'track' : 'tracks'} ingested
{:else if r.kind === 'album'}
{r.imported_track_count} {r.imported_track_count === 1 ? 'track' : 'tracks'} ingested
{:else}
Track ingested
{/if}
</div>
{/if}
{#if r.status === 'rejected' && r.notes}
<div class="text-sm text-text-secondary" data-testid="rejection-notes">
{r.notes}
+2
View File
@@ -60,6 +60,8 @@ function req(over: Partial<LidarrRequest> = {}): LidarrRequest {
matched_artist_id: null,
requested_at: '2026-04-28T12:00:00Z',
updated_at: '2026-04-28T12:00:00Z',
imported_album_count: 0,
imported_track_count: 0,
...over
};
}