add all time rank to item pages

This commit is contained in:
Gabe Farrell 2026-01-16 00:30:04 -05:00
parent d08e05220f
commit 89161b5586
17 changed files with 274 additions and 181 deletions

View file

@ -134,6 +134,39 @@ func (q *Queries) GetArtist(ctx context.Context, id int32) (GetArtistRow, error)
return i, err
}
const getArtistAllTimeRank = `-- name: GetArtistAllTimeRank :one
SELECT
artist_id,
rank
FROM (
SELECT
x.artist_id,
RANK() OVER (ORDER BY x.listen_count DESC) AS rank
FROM (
SELECT
at.artist_id,
COUNT(*) AS listen_count
FROM listens l
JOIN tracks t ON l.track_id = t.id
JOIN artist_tracks at ON t.id = at.track_id
GROUP BY at.artist_id
) x
)
WHERE artist_id = $1
`
type GetArtistAllTimeRankRow struct {
ArtistID int32
Rank int64
}
func (q *Queries) GetArtistAllTimeRank(ctx context.Context, artistID int32) (GetArtistAllTimeRankRow, error) {
row := q.db.QueryRow(ctx, getArtistAllTimeRank, artistID)
var i GetArtistAllTimeRankRow
err := row.Scan(&i.ArtistID, &i.Rank)
return i, err
}
const getArtistByImage = `-- name: GetArtistByImage :one
SELECT id, musicbrainz_id, image, image_source FROM artists WHERE image = $1 LIMIT 1
`

View file

@ -141,6 +141,38 @@ func (q *Queries) GetRelease(ctx context.Context, id int32) (GetReleaseRow, erro
return i, err
}
const getReleaseAllTimeRank = `-- name: GetReleaseAllTimeRank :one
SELECT
release_id,
rank
FROM (
SELECT
x.release_id,
RANK() OVER (ORDER BY x.listen_count DESC) AS rank
FROM (
SELECT
t.release_id,
COUNT(*) AS listen_count
FROM listens l
JOIN tracks t ON l.track_id = t.id
GROUP BY t.release_id
) x
)
WHERE release_id = $1
`
type GetReleaseAllTimeRankRow struct {
ReleaseID int32
Rank int64
}
func (q *Queries) GetReleaseAllTimeRank(ctx context.Context, releaseID int32) (GetReleaseAllTimeRankRow, error) {
row := q.db.QueryRow(ctx, getReleaseAllTimeRank, releaseID)
var i GetReleaseAllTimeRankRow
err := row.Scan(&i.ReleaseID, &i.Rank)
return i, err
}
const getReleaseByArtistAndTitle = `-- name: GetReleaseByArtistAndTitle :one
SELECT r.id, r.musicbrainz_id, r.image, r.various_artists, r.image_source, r.title
FROM releases_with_title r

View file

@ -438,6 +438,37 @@ func (q *Queries) GetTrack(ctx context.Context, id int32) (GetTrackRow, error) {
return i, err
}
const getTrackAllTimeRank = `-- name: GetTrackAllTimeRank :one
SELECT
id,
rank
FROM (
SELECT
x.id,
RANK() OVER (ORDER BY x.listen_count DESC) AS rank
FROM (
SELECT
t.id,
COUNT(*) AS listen_count
FROM listens l
JOIN tracks_with_title t ON l.track_id = t.id
GROUP BY t.id) x
) y
WHERE id = $1
`
type GetTrackAllTimeRankRow struct {
ID int32
Rank int64
}
func (q *Queries) GetTrackAllTimeRank(ctx context.Context, id int32) (GetTrackAllTimeRankRow, error) {
row := q.db.QueryRow(ctx, getTrackAllTimeRank, id)
var i GetTrackAllTimeRankRow
err := row.Scan(&i.ID, &i.Rank)
return i, err
}
const getTrackByMbzID = `-- name: GetTrackByMbzID :one
SELECT id, musicbrainz_id, duration, release_id, title FROM tracks_with_title
WHERE musicbrainz_id = $1 LIMIT 1