Skip to content

Commit

Permalink
Fix logging smart playlist's song count
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Nov 20, 2021
1 parent 3e282df commit cbeaadf
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
1 change: 0 additions & 1 deletion model/playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ type PlaylistRepository interface {
GetWithTracks(id string) (*Playlist, error)
GetAll(options ...QueryOptions) (Playlists, error)
FindByPath(path string) (*Playlist, error)
RefreshStatus(playlistId string) error
Delete(id string) error
Tracks(playlistId string) PlaylistTrackRepository
}
Expand Down
24 changes: 15 additions & 9 deletions persistence/playlist_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (r *playlistRepository) Put(p *model.Playlist) error {
if len(pls.Tracks) > 0 {
return r.updateTracks(id, p.MediaFiles())
}
return r.RefreshStatus(id)
return r.refreshCounters(&pls.Playlist)
}

func (r *playlistRepository) Get(id string) (*model.Playlist, error) {
Expand Down Expand Up @@ -224,14 +224,14 @@ func (r *playlistRepository) refreshSmartPlaylist(pls *model.Playlist) bool {
LeftJoin("genre on ag.genre_id = genre.id").GroupBy("media_file.id")
sql = r.addCriteria(sql, rules)
insSql := Insert("playlist_tracks").Columns("id", "playlist_id", "media_file_id").Select(sql)
c, err := r.executeSQL(insSql)
_, err = r.executeSQL(insSql)
if err != nil {
log.Error(r.ctx, "Error refreshing smart playlist tracks", "playlist", pls.Name, "id", pls.ID, err)
return false
}

// Update playlist stats
err = r.RefreshStatus(pls.ID)
err = r.refreshCounters(pls)
if err != nil {
log.Error(r.ctx, "Error updating smart playlist stats", "playlist", pls.Name, "id", pls.ID, err)
return false
Expand All @@ -245,7 +245,7 @@ func (r *playlistRepository) refreshSmartPlaylist(pls *model.Playlist) bool {
return false
}

log.Debug(r.ctx, "Refreshed playlist", "playlist", pls.Name, "id", pls.ID, "numTracks", c, "elapsed", time.Since(start))
log.Debug(r.ctx, "Refreshed playlist", "playlist", pls.Name, "id", pls.ID, "numTracks", pls.SongCount, "elapsed", time.Since(start))

return true
}
Expand Down Expand Up @@ -302,15 +302,15 @@ func (r *playlistRepository) addTracks(playlistId string, startingPos int, media
}
}

return r.RefreshStatus(playlistId)
return r.refreshCounters(&model.Playlist{ID: playlistId})
}

// RefreshStatus updates total playlist duration, size and count
func (r *playlistRepository) RefreshStatus(playlistId string) error {
func (r *playlistRepository) refreshCounters(pls *model.Playlist) error {
statsSql := Select("sum(duration) as duration", "sum(size) as size", "count(*) as count").
From("media_file").
Join("playlist_tracks f on f.media_file_id = media_file.id").
Where(Eq{"playlist_id": playlistId})
Where(Eq{"playlist_id": pls.ID})
var res struct{ Duration, Size, Count float32 }
err := r.queryOne(statsSql, &res)
if err != nil {
Expand All @@ -323,9 +323,15 @@ func (r *playlistRepository) RefreshStatus(playlistId string) error {
Set("size", res.Size).
Set("song_count", res.Count).
Set("updated_at", time.Now()).
Where(Eq{"id": playlistId})
Where(Eq{"id": pls.ID})
_, err = r.executeSQL(upd)
return err
if err != nil {
return err
}
pls.SongCount = int(res.Count)
pls.Duration = res.Duration
pls.Size = int64(res.Size)
return nil
}

func (r *playlistRepository) loadTracks(sel SelectBuilder, id string) (model.PlaylistTracks, error) {
Expand Down
4 changes: 1 addition & 3 deletions persistence/playlist_track_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ func (r *playlistRepository) Tracks(playlistId string) model.PlaylistTrackReposi
if err != nil {
return nil
}
if pls.IsSmartPlaylist() {
r.refreshSmartPlaylist(pls)
}
r.refreshSmartPlaylist(pls)
p.playlist = pls
return p
}
Expand Down
4 changes: 2 additions & 2 deletions scanner/playlist_importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func (s *playlistImporter) processPlaylists(ctx context.Context, dir string) int
continue
}
if pls.IsSmartPlaylist() {
log.Debug("Imported smart playlist", "name", pls.Name, "lastUpdated", pls.UpdatedAt, "path", pls.Path, "numTracks", len(pls.Tracks))
log.Debug("Imported smart playlist", "name", pls.Name, "lastUpdated", pls.UpdatedAt, "path", pls.Path, "numTracks", pls.SongCount)
} else {
log.Debug("Imported playlist", "name", pls.Name, "lastUpdated", pls.UpdatedAt, "path", pls.Path, "numTracks", len(pls.Tracks))
log.Debug("Imported playlist", "name", pls.Name, "lastUpdated", pls.UpdatedAt, "path", pls.Path, "numTracks", pls.SongCount)
}
count++
}
Expand Down

0 comments on commit cbeaadf

Please sign in to comment.