mirror of
https://github.com/navidrome/navidrome.git
synced 2026-08-31 07:30:32 +00:00
WIP
# Conflicts: # persistence/artist_repository.go
This commit is contained in:
parent
8d4feb242b
commit
f0270dc48c
@ -448,9 +448,9 @@ func (r *artistRepository) RefreshStats(allArtists bool) (int64, error) {
|
||||
library_artist_counters AS (
|
||||
SELECT artist_id,
|
||||
library_id,
|
||||
json_group_object(
|
||||
jsonb_object_agg(
|
||||
role,
|
||||
json_object('a', album_count, 'm', count, 's', size)
|
||||
jsonb_build_object('a', album_count, 'm', count, 's', size)
|
||||
) AS counters
|
||||
FROM combined_counters
|
||||
GROUP BY artist_id, library_id
|
||||
@ -458,7 +458,7 @@ func (r *artistRepository) RefreshStats(allArtists bool) (int64, error) {
|
||||
UPDATE library_artist
|
||||
SET stats = coalesce((SELECT counters FROM library_artist_counters lac
|
||||
WHERE lac.artist_id = library_artist.artist_id
|
||||
AND lac.library_id = library_artist.library_id), '{}')
|
||||
AND lac.library_id = library_artist.library_id), '{}'::jsonb)
|
||||
WHERE library_artist.artist_id IN (ROLE_IDS_PLACEHOLDER);` // Will replace with actual placeholders
|
||||
|
||||
var totalRowsAffected int64 = 0
|
||||
|
||||
@ -207,17 +207,17 @@ func (r *libraryRepository) RefreshStats(id int) error {
|
||||
}), &foldersRes)
|
||||
},
|
||||
func() error {
|
||||
return r.queryOne(Select("ifnull(sum(num_audio_files + num_playlists + json_array_length(image_files)),0) as count").
|
||||
return r.queryOne(Select("coalesce(sum(num_audio_files + num_playlists + jsonb_array_length(image_files)),0) as count").
|
||||
From("folder").Where(Eq{"library_id": id, "missing": false}), &filesRes)
|
||||
},
|
||||
func() error {
|
||||
return r.queryOne(Select("count(*) as count").From("media_file").Where(Eq{"library_id": id, "missing": true}), &missingRes)
|
||||
},
|
||||
func() error {
|
||||
return r.queryOne(Select("ifnull(sum(size),0) as sum").From("album").Where(Eq{"library_id": id, "missing": false}), &sizeRes)
|
||||
return r.queryOne(Select("coalesce(sum(size),0) as sum").From("album").Where(Eq{"library_id": id, "missing": false}), &sizeRes)
|
||||
},
|
||||
func() error {
|
||||
return r.queryOne(Select("ifnull(sum(duration),0) as sum").From("album").Where(Eq{"library_id": id, "missing": false}), &durationRes)
|
||||
return r.queryOne(Select("coalesce(sum(duration),0) as sum").From("album").Where(Eq{"library_id": id, "missing": false}), &durationRes)
|
||||
},
|
||||
)()
|
||||
if err != nil {
|
||||
|
||||
@ -53,12 +53,13 @@ func (r *tagRepository) Add(libraryID int, tags ...model.Tag) error {
|
||||
func (r *tagRepository) UpdateCounts() error {
|
||||
template := `
|
||||
INSERT INTO library_tag (tag_id, library_id, %[1]s_count)
|
||||
SELECT jt.value as tag_id, %[1]s.library_id, count(distinct %[1]s.id) as %[1]s_count
|
||||
FROM %[1]s
|
||||
JOIN json_tree(%[1]s.tags, '$.genre') as jt ON jt.atom IS NOT NULL AND jt.key = 'id'
|
||||
JOIN tag ON tag.id = jt.value
|
||||
GROUP BY jt.value, %[1]s.library_id
|
||||
ON CONFLICT (tag_id, library_id)
|
||||
SELECT elem->>'id' as tag_id, %[1]s.library_id, count(distinct %[1]s.id) as %[1]s_count
|
||||
FROM %[1]s,
|
||||
LATERAL jsonb_array_elements(%[1]s.tags->'genre') AS elem
|
||||
WHERE elem->>'id' IS NOT NULL
|
||||
AND EXISTS (SELECT 1 FROM tag WHERE tag.id = elem->>'id')
|
||||
GROUP BY elem->>'id', %[1]s.library_id
|
||||
ON CONFLICT (tag_id, library_id)
|
||||
DO UPDATE SET %[1]s_count = excluded.%[1]s_count;
|
||||
`
|
||||
|
||||
|
||||
@ -207,10 +207,11 @@ func (s *scannerImpl) runUpdateLibraries(ctx context.Context, state *scanState)
|
||||
}
|
||||
if state.changesDetected.Load() {
|
||||
log.Debug(ctx, "Scanner: Refreshing library stats", "lib", lib.Name)
|
||||
if err := tx.Library(ctx).RefreshStats(lib.ID); err != nil {
|
||||
log.Error(ctx, "Scanner: Error refreshing library stats", "lib", lib.Name, err)
|
||||
return fmt.Errorf("refreshing library stats: %w", err)
|
||||
}
|
||||
// FIXME
|
||||
// if err := tx.Library(ctx).RefreshStats(lib.ID); err != nil {
|
||||
// log.Error(ctx, "Scanner: Error refreshing library stats", "lib", lib.Name, err)
|
||||
// return fmt.Errorf("refreshing library stats: %w", err)
|
||||
// }
|
||||
} else {
|
||||
log.Debug(ctx, "Scanner: No changes detected, skipping library stats refresh", "lib", lib.Name)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user