Files
minstrel/internal/api/admin_library_missing.go
T
bvandeusenandClaude Opus 5.5 55c7497ca7
release / go (push) Successful in 1m54s
release / web (push) Failing after 5s
release / integration (push) Failing after 3m44s
release / govulncheck (push) Successful in 20s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / Build signed APK (push) Canceled after 19s
release / android (push) Canceled after 2m37s
feat: Minstrel stops acquiring music and drops Discover
Minstrel is now a viewer of the library it is pointed at. Finding,
downloading and filing music moves out of it.

Removed from the server:
- the request flow (requests, approval, auto-approve) and the wanted list,
  follows, release profiles and release blocks;
- searching indexers, the download clients, the importer, the album
  lifecycle and missing-file re-acquisition;
- the catalogue's discography, search and art (the MusicBrainz release and
  track-list cache stays, for the duplicate resolver);
- Discover: its suggestions of artists not in the library, snoozes, the
  candidate-artist tag enrichment and its tuning scope;
- writes to acquisition tables from moving the library folder and from
  merging duplicates, the want-list join on Suspect sources, the sealing of
  acquisition credentials, and the release-group lookup only
  re-acquisition used.

Settling a wrong-slot duplicate no longer sets the release aside: the
copies go and their slots show as missing until a good copy is in the
library.

Removed from the web: Admin > Acquisition and Requests, /requests,
/discover, Follow on the artist page, the auto-approve toggle, and the
Collection's wanted-list tabs. The Collection is now the library's health:
an overview, Reports, Duplicates, Suspect sources and Missing files. The
README and docs no longer describe getting music.

The tables stay for now; a later migration drops them. The request
notification kinds and the Android screens are still to go.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 20:51:14 -05:00

137 lines
5.0 KiB
Go

package api
import (
"net/http"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
)
// missingTrackView is one track whose file the scan could not find.
//
// The text fields come from the tracks row, not from the filesystem, which is
// the point: the recording is still a known thing with a title, an artist and
// a play history — only its bytes are absent. LastPlayedAt is nullable because
// plenty of missing files were never played, and that is exactly the signal an
// operator wants when deciding whether a lost file is worth replacing.
type missingTrackView struct {
TrackID string `json:"track_id"`
Title string `json:"title"`
ArtistID string `json:"artist_id"`
ArtistName string `json:"artist_name"`
AlbumID string `json:"album_id"`
AlbumTitle string `json:"album_title"`
FilePath string `json:"file_path"`
DurationSec int32 `json:"duration_sec"`
MissingSince string `json:"missing_since"`
LastPlayedAt *string `json:"last_played_at"`
}
// missingGroupView is a directory's worth of missing tracks.
//
// Grouping is the whole ergonomic argument for this surface: a reorganised
// album shows up as dozens of individually missing files, which presented flat
// reads as dozens of problems and presented by folder reads as one.
// MissingSince is the EARLIEST mark in the group, so a directory sorts and
// reads by when it first went away.
type missingGroupView struct {
Directory string `json:"directory"`
MissingSince string `json:"missing_since"`
Tracks []missingTrackView `json:"tracks"`
}
// adminMissingResponse is the paged envelope. Total counts TRACKS, not
// groups — it is what the nav badge shows, and "12 files missing" is the
// honest number even when they happen to sit in two folders.
type adminMissingResponse struct {
Total int64 `json:"total"`
Limit int `json:"limit"`
Offset int `json:"offset"`
Groups []missingGroupView `json:"groups"`
}
// handleListMissingTracks implements GET /api/admin/library/missing.
//
// Read-only by design. Nothing on this surface deletes a track: a missing file
// keeps its row, its history and its likes because it may come back, and if it
// comes back renamed the scanner adopts it. The surface exists so an admin
// can SEE what the library has lost and act on it deliberately.
func (h *handlers) handleListMissingTracks(w http.ResponseWriter, r *http.Request) {
limit, offset, err := parsePaging(r.URL.Query())
if err != nil {
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_paging")
return
}
q := dbq.New(h.pool)
total, err := q.CountMissingTracks(r.Context())
if err != nil {
h.logger.Error("admin: count missing tracks", "err", err)
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
return
}
rows, err := q.ListMissingTracks(r.Context(), dbq.ListMissingTracksParams{
PageLimit: int32(limit),
PageOffset: int32(offset),
})
if err != nil {
h.logger.Error("admin: list missing tracks", "err", err)
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
return
}
groups := groupMissingByDirectory(rows)
out := adminMissingResponse{
Total: total,
Limit: limit,
Offset: offset,
Groups: groups,
}
writeJSON(w, http.StatusOK, out)
}
// groupMissingByDirectory folds the ordered rows into per-directory groups.
//
// It relies on ListMissingTracks ordering by directory, so a simple run-length
// fold is enough and no map is needed — which also preserves the query's
// ordering in the response instead of Go's random map iteration. A page
// boundary can split one directory across two pages; that is accepted rather
// than paging by group, because the alternative costs a second query to find
// the page's directories and this surface's realistic N is small.
func groupMissingByDirectory(rows []dbq.ListMissingTracksRow) []missingGroupView {
groups := make([]missingGroupView, 0, 8)
for _, row := range rows {
t := missingTrackView{
TrackID: uuidToString(row.ID),
Title: row.Title,
ArtistID: uuidToString(row.ArtistID),
ArtistName: row.ArtistName,
AlbumID: uuidToString(row.AlbumID),
AlbumTitle: row.AlbumTitle,
FilePath: row.FilePath,
DurationSec: row.DurationMs / 1000,
MissingSince: formatTimestamp(row.MissingSince),
}
if row.LastPlayedAt.Valid {
s := formatTimestamp(row.LastPlayedAt)
t.LastPlayedAt = &s
}
if n := len(groups); n > 0 && groups[n-1].Directory == row.Directory {
groups[n-1].Tracks = append(groups[n-1].Tracks, t)
continue
}
groups = append(groups, missingGroupView{
Directory: row.Directory,
// First row of a run carries the group's timestamp. Rows are
// ordered within a directory by disc/track, not by mark time, so
// this is "the mark on the first track" rather than the minimum —
// they are the same value in the case that matters (a whole folder
// vanishing at once) and close enough otherwise.
MissingSince: t.MissingSince,
Tracks: []missingTrackView{t},
})
}
return groups
}