Fix scrobble error spam in the logs.

Relates to #2831 and #2975
This commit is contained in:
Deluan 2024-04-26 16:59:14 -04:00
parent 18143fa5a1
commit f5df948eb1
4 changed files with 44 additions and 15 deletions

View File

@ -0,0 +1,30 @@
package migrations
import (
"context"
"database/sql"
"github.com/pressly/goose/v3"
)
func init() {
goose.AddMigrationContext(upAddIdToScrobbleBuffer, downAddIdToScrobbleBuffer)
}
func upAddIdToScrobbleBuffer(ctx context.Context, tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, `
delete from scrobble_buffer where user_id <> '';
alter table scrobble_buffer add id varchar not null default '';
create unique index scrobble_buffer_id_ix
on scrobble_buffer (id);
`)
return err
}
func downAddIdToScrobbleBuffer(ctx context.Context, tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, `
drop index scrobble_buffer_id_ix;
alter table scrobble_buffer drop id;
`)
return err
}

View File

@ -3,11 +3,13 @@ package model
import "time" import "time"
type ScrobbleEntry struct { type ScrobbleEntry struct {
MediaFile ID string
Service string Service string
UserID string `structs:"user_id"` UserID string
PlayTime time.Time PlayTime time.Time
EnqueueTime time.Time EnqueueTime time.Time
MediaFileID string
MediaFile
} }
type ScrobbleEntries []ScrobbleEntry type ScrobbleEntries []ScrobbleEntry

View File

@ -6,6 +6,7 @@ import (
"time" "time"
. "github.com/Masterminds/squirrel" . "github.com/Masterminds/squirrel"
"github.com/google/uuid"
"github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/model"
"github.com/pocketbase/dbx" "github.com/pocketbase/dbx"
) )
@ -37,6 +38,7 @@ func (r *scrobbleBufferRepository) UserIDs(service string) ([]string, error) {
func (r *scrobbleBufferRepository) Enqueue(service, userId, mediaFileId string, playTime time.Time) error { func (r *scrobbleBufferRepository) Enqueue(service, userId, mediaFileId string, playTime time.Time) error {
ins := Insert(r.tableName).SetMap(map[string]interface{}{ ins := Insert(r.tableName).SetMap(map[string]interface{}{
"id": uuid.NewString(),
"user_id": userId, "user_id": userId,
"service": service, "service": service,
"media_file_id": mediaFileId, "media_file_id": mediaFileId,
@ -48,7 +50,8 @@ func (r *scrobbleBufferRepository) Enqueue(service, userId, mediaFileId string,
} }
func (r *scrobbleBufferRepository) Next(service string, userId string) (*model.ScrobbleEntry, error) { func (r *scrobbleBufferRepository) Next(service string, userId string) (*model.ScrobbleEntry, error) {
sql := Select().Columns("s.*, m.*"). // Put `s.*` last or else m.id overrides s.id
sql := Select().Columns("m.*, s.*").
From(r.tableName+" s"). From(r.tableName+" s").
LeftJoin("media_file m on m.id = s.media_file_id"). LeftJoin("media_file m on m.id = s.media_file_id").
Where(And{ Where(And{
@ -57,24 +60,20 @@ func (r *scrobbleBufferRepository) Next(service string, userId string) (*model.S
}). }).
OrderBy("play_time", "s.rowid").Limit(1) OrderBy("play_time", "s.rowid").Limit(1)
res := model.ScrobbleEntries{} res := &model.ScrobbleEntry{}
// TODO Rewrite queryOne to use QueryRows, to workaround the recursive embedded structs issue err := r.queryOne(sql, res)
err := r.queryAll(sql, &res) if errors.Is(err, model.ErrNotFound) {
if errors.Is(err, model.ErrNotFound) || len(res) == 0 {
return nil, nil return nil, nil
} }
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &res[0], nil res.MediaFile.ID = res.MediaFileID
return res, nil
} }
func (r *scrobbleBufferRepository) Dequeue(entry *model.ScrobbleEntry) error { func (r *scrobbleBufferRepository) Dequeue(entry *model.ScrobbleEntry) error {
return r.delete(And{ return r.delete(Eq{"id": entry.ID})
Eq{"service": entry.Service},
Eq{"media_file_id": entry.MediaFile.ID},
Eq{"play_time": entry.PlayTime},
})
} }
func (r *scrobbleBufferRepository) Length() (int64, error) { func (r *scrobbleBufferRepository) Length() (int64, error) {

View File

@ -157,8 +157,6 @@ func (r sqlRepository) toSQL(sq Sqlizer) (string, dbx.Params, error) {
return query, params, nil return query, params, nil
} }
// Note: Due to a bug in the QueryRow method, this function does not map any embedded structs (ex: annotations)
// In this case, use the queryAll method and get the first item of the returned list
func (r sqlRepository) queryOne(sq Sqlizer, response interface{}) error { func (r sqlRepository) queryOne(sq Sqlizer, response interface{}) error {
query, args, err := r.toSQL(sq) query, args, err := r.toSQL(sq)
if err != nil { if err != nil {