Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ func NewApiServer(config config.Config) *ApiServer {
g.Get("/tracks", app.v1Tracks)

g.Get("/tracks/trending", app.v1TracksTrending)
g.Get("/tracks/trending/ids", app.v1TracksTrendingIds)
g.Get("/tracks/recommended", app.v1TracksTrending)

g.Use("/tracks/:trackId", app.requireTrackIdMiddleware)
Expand Down
1 change: 1 addition & 0 deletions api/testdata/track_fixtures.csv
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ track_id,genre,owner_id,title,is_unlisted,stream_conditions,download_conditions
300,Electronic,3,Follow Gated Download,f,,"{""follow_user_id"": 3}"
301,Electronic,3,Pay Gated Download,f,,"{""usdc_purchase"": {""price"": 135, ""splits"": [{""user_id"": 3, ""percentage"": 100.0}]}}"
302,Electronic,3,Tip Gated Stream,f," {""tip_user_id"": 3}"," {""tip_user_id"": 3}"
400,Folk,5,Trending Month Folk,f,,
3 changes: 2 additions & 1 deletion api/testdata/track_trending_scores_fixtures.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ track_id,genre,time_range,score
201,Alternative,week,2.0
202,Alternative,week,2.0
300,Electronic,week,3.0
300,Electronic,allTime,3.0
300,Electronic,allTime,3.0
400,Folk,month,10.0
1 change: 1 addition & 0 deletions api/testdata/user_fixtures.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ user_id,handle,handle_lc,is_deactivated,wallet,playlist_library
2,stereosteve,stereosteve,f,0x1234567890abcdef,
3,someseller,someseller,f,0x234567890abcdef1,
4,accesstester,accesstester,f,0x34567890abcdef12,
5,guyintrending,guyintrending,f,0x34567890abcdef13,
91,badguy,badguy,t,0x4567890abcdef123,
38 changes: 7 additions & 31 deletions api/v1_tracks_trending.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,19 @@ package api
import (
"bridgerton.audius.co/api/dbv1"
"github.com/gofiber/fiber/v2"
"github.com/jackc/pgx/v5"
)

func (app *ApiServer) v1TracksTrending(c *fiber.Ctx) error {
myId := app.getMyId(c)

sql := `
SELECT track_trending_scores.track_id
FROM track_trending_scores
LEFT JOIN tracks
ON tracks.track_id = track_trending_scores.track_id
AND tracks.is_delete = false
AND tracks.is_unlisted = false
AND tracks.is_available = true
WHERE type = 'TRACKS'
AND version = 'pnagD'
AND time_range = @time
AND (@genre = '' OR track_trending_scores.genre = @genre)
ORDER BY
score DESC,
track_id DESC
LIMIT @limit
OFFSET @offset
`
trackIds, err := app.getTrendingIds(
c,
c.Query("time", "week"),
c.Query("genre", ""),
c.QueryInt("limit", 100),
c.QueryInt("offset", 0),
)

args := pgx.NamedArgs{}
args["limit"] = c.Query("limit", "100")
args["offset"] = c.Query("offset", "0")
args["time"] = c.Query("time", "week")
args["genre"] = c.Query("genre", "")

rows, err := app.pool.Query(c.Context(), sql, args)
if err != nil {
return err
}

trackIds, err := pgx.CollectRows(rows, pgx.RowTo[int32])
if err != nil {
return err
}
Expand Down
137 changes: 137 additions & 0 deletions api/v1_tracks_trending_ids.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package api

import (
"bridgerton.audius.co/trashid"
"github.com/gofiber/fiber/v2"
"github.com/jackc/pgx/v5"
)

func (app *ApiServer) getTrendingIds(c *fiber.Ctx, timeRange string, genre string, limit int, offset int) ([]int32, error) {
sql := `
SELECT track_trending_scores.track_id
FROM track_trending_scores
LEFT JOIN tracks
ON tracks.track_id = track_trending_scores.track_id
AND tracks.is_delete = false
AND tracks.is_unlisted = false
AND tracks.is_available = true
WHERE type = 'TRACKS'
AND version = 'pnagD'
AND time_range = @time
AND (@genre = '' OR track_trending_scores.genre = @genre)
ORDER BY
score DESC,
track_id DESC
LIMIT @limit
OFFSET @offset
`

args := pgx.NamedArgs{}
args["limit"] = limit
args["offset"] = offset
args["time"] = timeRange
args["genre"] = genre

rows, err := app.pool.Query(c.Context(), sql, args)
if err != nil {
return nil, err
}

trackIds, err := pgx.CollectRows(rows, pgx.RowTo[int32])
if err != nil {
return nil, err
}

return trackIds, nil
}

type hashIdResponse struct {
ID string `json:"id"`
}

func encodeIds(ids []int32) ([]hashIdResponse, error) {
result := make([]hashIdResponse, len(ids))
for i, id := range ids {
encoded, err := trashid.EncodeHashId(int(id))
if err != nil {
return nil, err
}
result[i] = hashIdResponse{ID: encoded}
}
return result, nil
}

func (app *ApiServer) v1TracksTrendingIds(c *fiber.Ctx) error {
limit := c.QueryInt("limit", 100)
offset := c.QueryInt("offset", 0)
genre := ""

weekChan := make(chan []int32)
monthChan := make(chan []int32)
yearChan := make(chan []int32)
errChan := make(chan error)

go func() {
ids, err := app.getTrendingIds(c, "week", genre, limit, offset)
if err != nil {
errChan <- err
return
}
weekChan <- ids
}()

go func() {
ids, err := app.getTrendingIds(c, "month", genre, limit, offset)
if err != nil {
errChan <- err
return
}
monthChan <- ids
}()

go func() {
ids, err := app.getTrendingIds(c, "allTime", genre, limit, offset)
if err != nil {
errChan <- err
return
}
yearChan <- ids
}()

var weekIds, monthIds, yearIds []int32

for i := 0; i < 3; i++ {
select {
case weekIds = <-weekChan:
case monthIds = <-monthChan:
case yearIds = <-yearChan:
case err := <-errChan:
return err
}
}

weekHashedIds, err := encodeIds(weekIds)
if err != nil {
return err
}

monthHashedIds, err := encodeIds(monthIds)
if err != nil {
return err
}

yearHashedIds, err := encodeIds(yearIds)
if err != nil {
return err
}

return c.JSON(fiber.Map{
"data": fiber.Map{
"week": weekHashedIds,
"month": monthHashedIds,
// Note that this is technically all time, but is set as
// year for backwards compatibility
"year": yearHashedIds,
},
})
}
28 changes: 28 additions & 0 deletions api/v1_tracks_trending_ids_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package api

import (
"testing"

"bridgerton.audius.co/trashid"
"github.com/stretchr/testify/assert"
)

func TestGetTrendingIds(t *testing.T) {
var resp struct {
Data struct {
Week []hashIdResponse `json:"week"`
Month []hashIdResponse `json:"month"`
Year []hashIdResponse `json:"year"`
} `json:"data"`
}
status, _ := testGet(t, "/v1/tracks/trending/ids", &resp)
assert.Equal(t, 200, status)

assert.Equal(t, trashid.MustEncodeHashID(300), resp.Data.Week[0].ID)
assert.Equal(t, trashid.MustEncodeHashID(202), resp.Data.Week[1].ID)
assert.Equal(t, trashid.MustEncodeHashID(201), resp.Data.Week[2].ID)
assert.Equal(t, trashid.MustEncodeHashID(200), resp.Data.Week[3].ID)
assert.Equal(t, trashid.MustEncodeHashID(400), resp.Data.Month[0].ID)
assert.Equal(t, trashid.MustEncodeHashID(200), resp.Data.Year[0].ID)
assert.Equal(t, trashid.MustEncodeHashID(300), resp.Data.Year[1].ID)
}
Loading