Skip to content

Commit

Permalink
Fix rollup of track genres to albums and artists.
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Jul 20, 2021
1 parent 254e567 commit 95181d7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
33 changes: 26 additions & 7 deletions persistence/album_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (r *albumRepository) GetAll(options ...model.QueryOptions) (model.Albums, e
}

// Return a map of mediafiles that have embedded covers for the given album ids
func (r *albumRepository) getEmbeddedCovers(ids []string) (map[string]model.MediaFile, error) {
func (r *albumRepository) getEmbeddedCoversForAlbums(ids []string) (map[string]model.MediaFile, error) {
var mfs model.MediaFiles
coverSql := Select("album_id", "id", "path").Distinct().From("media_file").
Where(And{Eq{"has_cover_art": true}, Eq{"album_id": ids}}).
Expand All @@ -149,6 +149,23 @@ func (r *albumRepository) getEmbeddedCovers(ids []string) (map[string]model.Medi
return result, nil
}

func (r *albumRepository) getGenresForAlbums(ids []string) (map[string]model.Genres, error) {
sql := Select("mf.album_id", "group_concat(mfg.genre_id, ' ') as genre").From("media_file_genres mfg").
LeftJoin("media_file mf on mf.id = mfg.media_file_id").
Where(Eq{"mf.album_id": ids}).GroupBy("mf.album_id")
var mfs model.MediaFiles
err := r.queryAll(sql, &mfs)
if err != nil {
return nil, err
}

result := map[string]model.Genres{}
for _, mf := range mfs {
result[mf.AlbumID] = getGenres(mf.Genre)
}
return result, nil
}

func (r *albumRepository) Refresh(ids ...string) error {
chunks := utils.BreakUpStringSlice(ids, 100)
for _, chunk := range chunks {
Expand All @@ -168,7 +185,6 @@ type refreshAlbum struct {
SongArtists string
SongArtistIds string
AlbumArtistIds string
GenreIds string
Years string
DiscSubtitles string
Comments string
Expand All @@ -195,8 +211,7 @@ func (r *albumRepository) refresh(ids ...string) error {
group_concat(f.artist, ' ') as song_artists,
group_concat(f.artist_id, ' ') as song_artist_ids,
group_concat(f.album_artist_id, ' ') as album_artist_ids,
group_concat(f.year, ' ') as years,
(select group_concat(genre_id, ' ') from media_file_genres where media_file_id = f.id) as genre_ids`).
group_concat(f.year, ' ') as years`).
From("media_file f").
LeftJoin("album a on f.album_id = a.id").
Where(Eq{"f.album_id": ids}).GroupBy("f.album_id")
Expand All @@ -205,7 +220,12 @@ func (r *albumRepository) refresh(ids ...string) error {
return err
}

covers, err := r.getEmbeddedCovers(ids)
covers, err := r.getEmbeddedCoversForAlbums(ids)
if err != nil {
return nil
}

allGenres, err := r.getGenresForAlbums(ids)
if err != nil {
return nil
}
Expand All @@ -218,7 +238,6 @@ func (r *albumRepository) refresh(ids ...string) error {
al.CoverArtId = embedded.ID
al.CoverArtPath = embedded.Path
}

if !hasCoverArt || !strings.HasPrefix(conf.Server.CoverArtPriority, "embedded") {
if path := getCoverFromPath(al.Path, al.CoverArtPath); path != "" {
al.CoverArtId = "al-" + al.ID
Expand Down Expand Up @@ -252,7 +271,7 @@ func (r *albumRepository) refresh(ids ...string) error {
al.AllArtistIDs = utils.SanitizeStrings(al.SongArtistIds, al.AlbumArtistID, al.ArtistID)
al.FullText = getFullText(al.Name, al.Artist, al.AlbumArtist, al.SongArtists,
al.SortAlbumName, al.SortArtistName, al.SortAlbumArtistName, al.DiscSubtitles)
al.Genres = getGenres(al.GenreIds)
al.Genres = allGenres[al.ID]
err := r.Put(&al.Album)
if err != nil {
return err
Expand Down
25 changes: 23 additions & 2 deletions persistence/artist_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,23 @@ func (r *artistRepository) GetIndex() (model.ArtistIndexes, error) {
return result, nil
}

func (r *artistRepository) getGenresForArtists(ids []string) (map[string]model.Genres, error) {
sql := Select("a.album_artist_id", "group_concat(ag.genre_id, ' ') as genre").From("album_genres ag").
LeftJoin("album a on a.id = ag.album_id").
Where(Eq{"a.album_artist_id": ids}).GroupBy("a.album_artist_id")
var als model.Albums
err := r.queryAll(sql, &als)
if err != nil {
return nil, err
}

result := map[string]model.Genres{}
for _, al := range als {
result[al.AlbumArtistID] = getGenres(al.Genre)
}
return result, nil
}

func (r *artistRepository) Refresh(ids ...string) error {
chunks := utils.BreakUpStringSlice(ids, 100)
for _, chunk := range chunks {
Expand All @@ -190,7 +207,6 @@ func (r *artistRepository) refresh(ids ...string) error {
type refreshArtist struct {
model.Artist
CurrentId string
GenreIds string
}
var artists []refreshArtist
sel := Select("f.album_artist_id as id", "f.album_artist as name", "count(*) as album_count", "a.id as current_id",
Expand All @@ -207,6 +223,11 @@ func (r *artistRepository) refresh(ids ...string) error {
return err
}

allGenres, err := r.getGenresForArtists(ids)
if err != nil {
return err
}

toInsert := 0
toUpdate := 0
for _, ar := range artists {
Expand All @@ -216,7 +237,7 @@ func (r *artistRepository) refresh(ids ...string) error {
toInsert++
}
ar.MbzArtistID = getMostFrequentMbzID(r.ctx, ar.MbzArtistID, r.tableName, ar.Name)
ar.Genres = getGenres(ar.GenreIds)
ar.Genres = allGenres[ar.ID]
err := r.Put(&ar.Artist)
if err != nil {
return err
Expand Down

0 comments on commit 95181d7

Please sign in to comment.