Refactor random functions

This commit is contained in:
Deluan 2024-05-11 20:04:21 -04:00
parent 30ae468dc1
commit 0ae2944073
10 changed files with 53 additions and 19 deletions

View file

@ -18,6 +18,7 @@ import (
"github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/utils" "github.com/navidrome/navidrome/utils"
. "github.com/navidrome/navidrome/utils/gg" . "github.com/navidrome/navidrome/utils/gg"
"github.com/navidrome/navidrome/utils/random"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )
@ -266,8 +267,8 @@ func (e *externalMetadata) SimilarSongs(ctx context.Context, id string, count in
return nil, ctx.Err() return nil, ctx.Err()
} }
weightedSongs := utils.NewWeightedRandomChooser() weightedSongs := random.NewWeightedRandomChooser()
addArtist := func(a model.Artist, weightedSongs *utils.WeightedChooser, count, artistWeight int) error { addArtist := func(a model.Artist, weightedSongs *random.WeightedChooser, count, artistWeight int) error {
if utils.IsCtxDone(ctx) { if utils.IsCtxDone(ctx) {
log.Warn(ctx, "SimilarSongs call canceled", ctx.Err()) log.Warn(ctx, "SimilarSongs call canceled", ctx.Err())
return ctx.Err() return ctx.Err()

View file

@ -4,7 +4,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/navidrome/navidrome/utils/number" "github.com/navidrome/navidrome/utils/random"
) )
type Share struct { type Share struct {
@ -42,7 +42,7 @@ func (s Share) CoverArtID() ArtworkID {
case "artist": case "artist":
return Artist{ID: ids[0]}.CoverArtID() return Artist{ID: ids[0]}.CoverArtID()
} }
rnd := number.RandomInt64(int64(len(s.Tracks))) rnd := random.Int64(int64(len(s.Tracks)))
return s.Tracks[rnd].CoverArtID() return s.Tracks[rnd].CoverArtID()
} }

View file

@ -12,7 +12,7 @@ import (
"github.com/navidrome/navidrome/consts" "github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/utils/number" "github.com/navidrome/navidrome/utils/random"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@ -51,7 +51,7 @@ func (h *Handler) getRandomImage(ctx context.Context) (string, error) {
if len(list) == 0 { if len(list) == 0 {
return "", errors.New("no images available") return "", errors.New("no images available")
} }
rnd := number.RandomInt64(int64(len(list))) rnd := random.Int64(int64(len(list)))
return list[rnd], nil return list[rnd], nil
} }

View file

@ -5,12 +5,18 @@ import (
"io/fs" "io/fs"
"os" "os"
"path/filepath" "path/filepath"
"testing"
"github.com/navidrome/navidrome/utils/merge" "github.com/navidrome/navidrome/utils/merge"
. "github.com/onsi/ginkgo/v2" . "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
func TestMergeFS(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "MergeFS Suite")
}
var _ = Describe("FS", func() { var _ = Describe("FS", func() {
var baseName, overlayName string var baseName, overlayName string
var mergedDir fs.FS var mergedDir fs.FS

View file

@ -1,18 +1,11 @@
package number package number
import ( import (
"crypto/rand"
"math/big"
"strconv" "strconv"
"golang.org/x/exp/constraints" "golang.org/x/exp/constraints"
) )
func RandomInt64(max int64) int64 {
rnd, _ := rand.Int(rand.Reader, big.NewInt(max))
return rnd.Int64()
}
func ParseInt[T constraints.Integer](s string) T { func ParseInt[T constraints.Integer](s string) T {
r, _ := strconv.ParseInt(s, 10, 64) r, _ := strconv.ParseInt(s, 10, 64)
return T(r) return T(r)

View file

@ -4,6 +4,7 @@ import (
"testing" "testing"
"github.com/navidrome/navidrome/utils/number" "github.com/navidrome/navidrome/utils/number"
"github.com/navidrome/navidrome/utils/random"
. "github.com/onsi/ginkgo/v2" . "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
@ -17,7 +18,7 @@ var _ = Describe("number package", func() {
Describe("RandomInt64", func() { Describe("RandomInt64", func() {
It("should return a random int64", func() { It("should return a random int64", func() {
for i := 0; i < 10000; i++ { for i := 0; i < 10000; i++ {
Expect(number.RandomInt64(100)).To(BeNumerically("<", 100)) Expect(random.Int64(100)).To(BeNumerically("<", 100))
} }
}) })
}) })

11
utils/random/number.go Normal file
View file

@ -0,0 +1,11 @@
package random
import (
"crypto/rand"
"math/big"
)
func Int64(max int64) int64 {
rnd, _ := rand.Int(rand.Reader, big.NewInt(max))
return rnd.Int64()
}

View file

@ -0,0 +1,24 @@
package random_test
import (
"testing"
"github.com/navidrome/navidrome/utils/random"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestRandom(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Random Suite")
}
var _ = Describe("number package", func() {
Describe("Int64", func() {
It("should return a random int64", func() {
for i := 0; i < 10000; i++ {
Expect(random.Int64(100)).To(BeNumerically("<", 100))
}
})
})
})

View file

@ -1,9 +1,7 @@
package utils package random
import ( import (
"errors" "errors"
"github.com/navidrome/navidrome/utils/number"
) )
type WeightedChooser struct { type WeightedChooser struct {
@ -41,7 +39,7 @@ func (w *WeightedChooser) weightedChoice() (int, error) {
if w.totalWeight == 0 { if w.totalWeight == 0 {
return 0, errors.New("no choices available") return 0, errors.New("no choices available")
} }
rnd := number.RandomInt64(int64(w.totalWeight)) rnd := Int64(int64(w.totalWeight))
for i, weight := range w.weights { for i, weight := range w.weights {
rnd -= int64(weight) rnd -= int64(weight)
if rnd < 0 { if rnd < 0 {

View file

@ -1,4 +1,4 @@
package utils package random
import ( import (
. "github.com/onsi/ginkgo/v2" . "github.com/onsi/ginkgo/v2"