Skip to content

Commit

Permalink
radio: change Track.NeedReplacement to return whole Song's
Browse files Browse the repository at this point in the history
storage/mariadb: remove leftover RequestsEnabled columns

storage/mariadb: implement the new NeedReplacement
  • Loading branch information
Wessie committed May 31, 2024
1 parent 15aeb65 commit bfe8250
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 21 deletions.
6 changes: 3 additions & 3 deletions mocks/radio.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions radio.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,8 +854,8 @@ type TrackStorage interface {
Delete(TrackID) error
// Unusable returns all tracks that are deemed unusable by the streamer
Unusable() ([]Song, error)
// NeedReplacement returns the IDs that need a replacement
NeedReplacement() ([]TrackID, error)
// NeedReplacement returns the song that need a replacement
NeedReplacement() ([]Song, error)
// Insert inserts a new track, errors if ID or TrackID is set
Insert(song Song) (TrackID, error)
// UpdateMetadata updates track metadata only (artist/title/album/tags/filepath/needreplacement)
Expand Down
2 changes: 1 addition & 1 deletion search/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type partialTrackStorage interface {
Get(radio.TrackID) (*radio.Song, error)
All() ([]radio.Song, error)
Unusable() ([]radio.Song, error)
NeedReplacement() ([]radio.TrackID, error)
NeedReplacement() ([]radio.Song, error)
BeforeLastRequested(before time.Time) ([]radio.Song, error)
QueueCandidates() ([]radio.TrackID, error)
}
Expand Down
4 changes: 0 additions & 4 deletions storage/mariadb/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func (ss StatusStorage) Store(status radio.Status) error {
end_time,
trackid,
thread,
requesting,
djname
) VALUES (
1,
Expand All @@ -43,7 +42,6 @@ func (ss StatusStorage) Store(status radio.Status) error {
UNIX_TIMESTAMP(:songinfo.end),
:song.trackid,
:thread,
:requestsenabled,
:streamername
) ON DUPLICATE KEY UPDATE
djid=:user.dj.id,
Expand All @@ -54,7 +52,6 @@ func (ss StatusStorage) Store(status radio.Status) error {
end_time=UNIX_TIMESTAMP(:songinfo.end),
trackid=:song.trackid,
thread=:thread,
requesting=:requestsenabled,
djname=:streamername,
lastset=NOW();
`
Expand All @@ -81,7 +78,6 @@ func (ss StatusStorage) Load() (*radio.Status, error) {
from_unixtime(end_time) AS 'songinfo.end',
trackid AS 'song.trackid',
thread,
IF(requesting = 1, 'true', 'false') AS requestsenabled,
djname AS streamername
FROM
streamstatus
Expand Down
15 changes: 10 additions & 5 deletions storage/mariadb/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,21 +618,26 @@ type TrackStorage struct {
handle handle
}

var trackNeedReplacementQuery = `
var trackNeedReplacementQuery = expand(`
SELECT
tracks.id AS trackid
{trackColumns},
{maybeSongColumns},
{lastplayedSelect},
NOW() AS synctime
FROM
tracks
LEFT JOIN
esong ON esong.hash = tracks.hash
WHERE
tracks.need_reupload = 1;
`
`)

func (ts TrackStorage) NeedReplacement() ([]radio.TrackID, error) {
func (ts TrackStorage) NeedReplacement() ([]radio.Song, error) {
const op errors.Op = "mariadb/TrackStorage.NeedReplacement"
handle, deferFn := ts.handle.span(op)
defer deferFn()

var songs []radio.TrackID
var songs []radio.Song

err := sqlx.Select(handle, &songs, trackNeedReplacementQuery)
if err != nil {
Expand Down
71 changes: 71 additions & 0 deletions storage/test/track.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package storagetest

import (
"reflect"
"slices"
"strconv"
"testing"
"time"

radio "github.com/R-a-dio/valkyrie"
"github.com/leanovate/gopter"
"github.com/leanovate/gopter/arbitrary"
"github.com/leanovate/gopter/gen"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -237,3 +241,70 @@ func (suite *Suite) TestSongFavoritesOf(t *testing.T) {
require.NoError(t, err)
require.Len(t, db, 0)
}

func (suite *Suite) TestTrackNeedReplacement(t *testing.T) {
s := suite.Storage(t)
ts := s.Track(suite.ctx)

var entries []radio.Song
for i := 0; i < 50; i++ {
song := generateTrack()
song.NeedReplacement = i%2 == 0
tid, err := ts.Insert(song)
require.NoError(t, err)
song.TrackID = tid
entries = append(entries, song)
}

ret, err := ts.NeedReplacement()
require.NoError(t, err)

assert.Len(t, ret, 25)
for _, song := range ret {
assert.True(t, song.NeedReplacement, "should have NeedReplacement set")
exists := slices.ContainsFunc(entries, func(a radio.Song) bool {
return a.EqualTo(song)
})
assert.True(t, exists, "song should be one we inserted")
}
}

func generateTrack() radio.Song {
a := arbitrary.DefaultArbitraries()
generator := gen.Struct(reflect.TypeFor[radio.Song](), map[string]gopter.Gen{
"ID": genForType[radio.SongID](a),
"Length": genDuration(),
"LastPlayed": genTime(),
"LastPlayedBy": gen.PtrOf(genUser()),
"DatabaseTrack": gen.StructPtr(reflect.TypeFor[radio.DatabaseTrack](), map[string]gopter.Gen{
"Artist": gen.AlphaString(),
"Title": gen.AlphaString(),
"Album": gen.AlphaString(),
"Tags": gen.AlphaString(),
"FilePath": gen.AlphaString(),
}),
})

song := OneOff[radio.Song](generator)
song.Hydrate()
return song
}

func genForType[T any](a *arbitrary.Arbitraries) gopter.Gen {
return a.GenForType(reflect.TypeFor[T]())
}

func genDuration() gopter.Gen {
g := gen.Int64Range(0, int64(time.Hour*24))
return genAsType[int64, time.Duration](g)
}

func genAsType[F, T any](g gopter.Gen) gopter.Gen {
g = g.WithShrinker(nil)
return gopter.Gen(func(gp *gopter.GenParameters) *gopter.GenResult {
res := g(gp)
v := res.Result.(F)
vt := reflect.ValueOf(v).Convert(reflect.TypeFor[T]()).Interface()
return gopter.NewGenResult(vt, nil)
}).WithShrinker(nil)
}
10 changes: 5 additions & 5 deletions website/public/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,11 @@ type SubmissionForm struct {
// "cooldown": indicates the user was not permitted to upload yet, they need to wait longer
Errors map[string]string
// form fields
OriginalFilename string // name="track" The filename of the uploaded file
Daypass string // name="daypass"
Comment string // name="comment"
Replacement *radio.TrackID // name="replacement"
NeedReplacementList []radio.TrackID // possible values for Replacement field
OriginalFilename string // name="track" The filename of the uploaded file
Daypass string // name="daypass"
Comment string // name="comment"
Replacement *radio.TrackID // name="replacement"
NeedReplacementList []radio.Song // possible songs for Replacement field

// after processing fields

Expand Down
2 changes: 1 addition & 1 deletion website/public/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestPostSubmit(t *testing.T) {
}
storage.TrackFunc = func(contextMoqParam context.Context) radio.TrackStorage {
return &mocks.TrackStorageMock{
NeedReplacementFunc: func() ([]radio.TrackID, error) {
NeedReplacementFunc: func() ([]radio.Song, error) {
return nil, nil
},
}
Expand Down

0 comments on commit bfe8250

Please sign in to comment.