Skip to content

Commit

Permalink
Log warning when artist has a MBID of Various Artists
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Jul 18, 2021
1 parent e61cf32 commit 44e7502
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 28 deletions.
7 changes: 4 additions & 3 deletions consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ var (
)

var (
VariousArtists = "Various Artists"
VariousArtistsID = fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(VariousArtists))))
UnknownArtist = "[Unknown Artist]"
VariousArtists = "Various Artists"
VariousArtistsID = fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(VariousArtists))))
UnknownArtist = "[Unknown Artist]"
VariousArtistsMbzId = "89ad4ac3-39f7-470e-963a-56509c546377"

ServerStart = time.Now()
)
2 changes: 1 addition & 1 deletion persistence/album_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (r *albumRepository) refresh(ids ...string) error {

al.AlbumArtistID, al.AlbumArtist = getAlbumArtist(al)
al.MinYear = getMinYear(al.Years)
al.MbzAlbumID = getMbzId(r.ctx, al.MbzAlbumID, r.tableName, al.Name)
al.MbzAlbumID = getMostFrequentMbzID(r.ctx, al.MbzAlbumID, r.tableName, al.Name)
al.Comment = getComment(al.Comments, zwsp)
if al.CurrentId != "" {
toUpdate++
Expand Down
2 changes: 1 addition & 1 deletion persistence/artist_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (r *artistRepository) refresh(ids ...string) error {
} else {
toInsert++
}
ar.MbzArtistID = getMbzId(r.ctx, ar.MbzArtistID, r.tableName, ar.Name)
ar.MbzArtistID = getMostFrequentMbzID(r.ctx, ar.MbzArtistID, r.tableName, ar.Name)
err := r.Put(&ar.Artist)
if err != nil {
return err
Expand Down
38 changes: 20 additions & 18 deletions persistence/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"regexp"
"strings"

"github.com/Masterminds/squirrel"
"github.com/navidrome/navidrome/consts"

"github.com/Masterminds/squirrel"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/utils"
Expand Down Expand Up @@ -59,31 +60,32 @@ func (e existsCond) ToSql() (string, []interface{}, error) {
return sql, args, err
}

func getMbzId(ctx context.Context, mbzIDS, entityName, name string) string {
ids := strings.Fields(mbzIDS)
func getMostFrequentMbzID(ctx context.Context, mbzIDs, entityName, name string) string {
ids := strings.Fields(mbzIDs)
if len(ids) == 0 {
return ""
}
if len(ids) == 1 {
return ids[0]
}
idCounts := map[string]int{}
var topId string
var topCount int
for _, id := range ids {
if c, ok := idCounts[id]; ok {
idCounts[id] = c + 1
} else {
idCounts[id] = 1
c := idCounts[id] + 1
idCounts[id] = c
if c > topCount {
topId = id
topCount = c
}
}

var topKey string
var topCount int
for k, v := range idCounts {
if v > topCount {
topKey = k
topCount = v
if name != consts.VariousArtists {
if topId == consts.VariousArtistsMbzId {
log.Warn(ctx, "Artist with mbid of Various Artists", "name", name, "mbid", topId)
} else {
log.Warn(ctx, "Multiple MBIDs found for "+entityName, "name", name, "mbids", idCounts)
}
}

if len(idCounts) > 1 && name != consts.VariousArtists {
log.Warn(ctx, "Multiple MBIDs found for "+entityName, "name", name, "mbids", idCounts)
}
return topKey
return topId
}
10 changes: 5 additions & 5 deletions persistence/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var _ = Describe("Helpers", func() {
})
})

Describe("Exists", func() {
Describe("exists", func() {
It("constructs the correct EXISTS query", func() {
e := exists("album", squirrel.Eq{"id": 1})
sql, args, err := e.ToSql()
Expand All @@ -63,15 +63,15 @@ var _ = Describe("Helpers", func() {
})
})

Describe("getMbzId", func() {
Describe("getMostFrequentMbzID", func() {
It(`returns "" when no ids are passed`, func() {
Expect(getMbzId(context.TODO(), " ", "", "")).To(Equal(""))
Expect(getMostFrequentMbzID(context.TODO(), " ", "", "")).To(Equal(""))
})
It(`returns the only id passed`, func() {
Expect(getMbzId(context.TODO(), "1234 ", "", "")).To(Equal("1234"))
Expect(getMostFrequentMbzID(context.TODO(), "111 ", "", "")).To(Equal("111"))
})
It(`returns the id with higher frequency`, func() {
Expect(getMbzId(context.TODO(), "1 2 3 4 1", "", "")).To(Equal("1"))
Expect(getMostFrequentMbzID(context.TODO(), "1 2 3 4 2", "", "")).To(Equal("2"))
})
})
})

0 comments on commit 44e7502

Please sign in to comment.