From cbeaadf8e253994260d8ce54c157a6fb223f9535 Mon Sep 17 00:00:00 2001 From: Deluan Date: Sat, 20 Nov 2021 12:29:09 -0500 Subject: [PATCH] Fix logging smart playlist's song count --- model/playlist.go | 1 - persistence/playlist_repository.go | 24 +++++++++++++++--------- persistence/playlist_track_repository.go | 4 +--- scanner/playlist_importer.go | 4 ++-- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/model/playlist.go b/model/playlist.go index a5c17a619..366801654 100644 --- a/model/playlist.go +++ b/model/playlist.go @@ -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 } diff --git a/persistence/playlist_repository.go b/persistence/playlist_repository.go index d1c7258e2..40fc54549 100644 --- a/persistence/playlist_repository.go +++ b/persistence/playlist_repository.go @@ -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) { @@ -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 @@ -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 } @@ -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 { @@ -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) { diff --git a/persistence/playlist_track_repository.go b/persistence/playlist_track_repository.go index a4c6f014a..06d921e37 100644 --- a/persistence/playlist_track_repository.go +++ b/persistence/playlist_track_repository.go @@ -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 } diff --git a/scanner/playlist_importer.go b/scanner/playlist_importer.go index e0b497fa7..601461caa 100644 --- a/scanner/playlist_importer.go +++ b/scanner/playlist_importer.go @@ -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++ }