-
Notifications
You must be signed in to change notification settings - Fork 1
Trending and tests #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| track_id,owner_id,title,is_unlisted,stream_conditions,download_conditions | ||
| 200,2,Culca Canyon,f,, | ||
| 201,2,Turkey Time DEMO,t,, | ||
| 300,3,Follow Gated Download,f,,"{""follow_user_id"": 3}" | ||
| 301,3,Pay Gated Download,f,,"{""usdc_purchase"": {""price"": 135, ""splits"": [{""user_id"": 3, ""percentage"": 100.0}]}}" | ||
| 302,3,Tip Gated Stream,f," {""tip_user_id"": 859175075}", | ||
| track_id,genre,owner_id,title,is_unlisted,stream_conditions,download_conditions | ||
| 200,Electronic,2,Culca Canyon,f,, | ||
| 201,Alternative,2,Turkey Time DEMO,t,, | ||
| 202,Alternative,2,Turkey Time (live),f,, | ||
| 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"": 859175075}", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| track_id,genre,time_range,score | ||
| 200,Electronic,week,1.0 | ||
| 200,Electronic,allTime,5.0 | ||
| 201,Alternative,week,2.0 | ||
| 202,Alternative,week,2.0 | ||
| 300,Electronic,week,3.0 | ||
| 300,Electronic,allTime,3.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "bridgerton.audius.co/api/dbv1" | ||
| "github.com/gofiber/fiber/v2" | ||
| "github.com/jackc/pgx/v5" | ||
| ) | ||
|
|
||
| func (app *ApiServer) v1Trending(c *fiber.Ctx) error { | ||
| myId := c.Locals("myId") | ||
|
|
||
| // SQL query with conditional genre filter | ||
| 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 = @timeRange | ||
| AND (@genre = '' OR track_trending_scores.genre = @genre) | ||
| ORDER BY | ||
| score DESC, | ||
| track_id DESC | ||
| LIMIT @limit | ||
| OFFSET @offset | ||
| ` | ||
|
|
||
| args := pgx.NamedArgs{} | ||
| args["limit"] = c.Query("limit", "100") | ||
| args["offset"] = c.Query("offset", "0") | ||
| args["timeRange"] = c.Query("timeRange", "week") | ||
| args["genre"] = c.Query("genre", "") | ||
|
|
||
| rows, err := app.pool.Query(c.Context(), sql, args) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| type trackTrendingRow struct { | ||
| TrackId int32 `json:"track_id"` | ||
| } | ||
|
|
||
| trackIds, err := pgx.CollectRows(rows, pgx.RowTo[int32]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| tracks, err := app.queries.FullTracks(c.Context(), dbv1.GetTracksParams{ | ||
| Ids: trackIds, | ||
| MyID: myId, | ||
| }) | ||
|
|
||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return v1TracksResponse(c, tracks) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "bridgerton.audius.co/api/dbv1" | ||
| "bridgerton.audius.co/trashid" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestGetTrending(t *testing.T) { | ||
| var resp struct { | ||
| Data []dbv1.FullTrack | ||
| } | ||
| status, _ := testGet(t, "/v1/tracks/trending", &resp) | ||
| assert.Equal(t, 200, status) | ||
|
|
||
| assert.Equal(t, trashid.MustEncodeHashID(300), resp.Data[0].ID) | ||
| assert.Equal(t, "Electronic", resp.Data[0].Genre.String) | ||
|
|
||
| assert.Equal(t, trashid.MustEncodeHashID(202), resp.Data[1].ID) | ||
| assert.Equal(t, "Alternative", resp.Data[1].Genre.String) | ||
|
|
||
| assert.Equal(t, trashid.MustEncodeHashID(200), resp.Data[2].ID) | ||
| assert.Equal(t, "Electronic", resp.Data[2].Genre.String) | ||
| } | ||
|
|
||
| func TestGetTrendingElectronic(t *testing.T) { | ||
| var resp struct { | ||
| Data []dbv1.FullTrack | ||
| } | ||
| status, _ := testGet(t, "/v1/tracks/trending?genre=Electronic", &resp) | ||
| assert.Equal(t, 200, status) | ||
|
|
||
| assert.Equal(t, "eYRWn", resp.Data[0].ID) | ||
| assert.Equal(t, "Electronic", resp.Data[0].Genre.String) | ||
|
|
||
| assert.Equal(t, "eYJyn", resp.Data[1].ID) | ||
| assert.Equal(t, "Electronic", resp.Data[1].Genre.String) | ||
| } | ||
|
|
||
| func TestGetTrendingAllTime(t *testing.T) { | ||
| var resp struct { | ||
| Data []dbv1.FullTrack | ||
| } | ||
| status, _ := testGet(t, "/v1/tracks/trending?timeRange=allTime", &resp) | ||
| assert.Equal(t, 200, status) | ||
|
|
||
| assert.Equal(t, "eYJyn", resp.Data[0].ID) | ||
| assert.Equal(t, "Electronic", resp.Data[0].Genre.String) | ||
|
|
||
| assert.Equal(t, "eYRWn", resp.Data[1].ID) | ||
| assert.Equal(t, "Electronic", resp.Data[1].Genre.String) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't this be a full join?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i dont think so. if there's no scores for a track, we probably don't care about it