Skip to content
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

Add endpoint to get play stats #340

Merged
merged 4 commits into from
Jun 30, 2022
Merged

Add endpoint to get play stats #340

merged 4 commits into from
Jun 30, 2022

Conversation

robbevp
Copy link
Member

@robbevp robbevp commented May 26, 2022

This PR adds an extra endpoint: /plays/stats through which a user is able to get their aggregated play stats.

this endpoint should be enough for most use cases we have for plays. It can replace the calculations to show the simple overall play count, and simplify many calculations for the recently played albums, artists or the stats page.

These stats can be filtered by album or artist (for smaller requests) or the played_at date (for detailed stats)

TODO:

  • Create some real life tests with a high volume of plays
  • Maybe add the total played length of a track?

Update about real life tests

I did some tests in my development env to verify whether this approach can scale.
This test db contains:

  • ± 45k tracks (though only a small portion contain an actual audio file)
  • ±60k plays
    This seemed to be no problem - most pages with stats are returned between 200 and 350ms. I don't really expect requests to take considerably longer when there are more plays

@robbevp robbevp added the enhancement New feature or request label May 26, 2022
@robbevp robbevp self-assigned this May 26, 2022
@@ -14,7 +14,7 @@ class ApplicationController < ActionController::API
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
rescue_from ActiveRecord::RecordNotFound, with: :model_not_found

has_scope :sorted, default: nil, allow_blank: true
# has_scope :sorted, default: nil, allow_blank: true
Copy link
Member Author

@robbevp robbevp May 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chvp I had to tmp comment this line, since it was causing problems.
When the line was included our query also tried to order per id, which was not present in the GROUP BY clause. (Even if the request didn't use any scopes). Do we still use/need this somewhere?

ERROR:  column "plays.id" must appear in the GROUP BY clause or be used in an aggregate function at character 188
STATEMENT:  SELECT COUNT(*), MAX(played_at) as last_played_at, "plays"."track_id", "plays"."user_id" FROM "plays" WHERE "plays"."user_id" = $1 GROUP BY "plays"."track_id", "plays"."user_id" ORDER BY "plays"."id" DESC, "plays"."track_id" DESC LIMIT $2 OFFSET $3

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clients implicitly rely on sorting by id to handle the edge case where new data appears while syncing. It needs at least some ordering, otherwise the database might decide to start ordering differently, leaving the clients with some missed data (even if some data disappears). The reverse by id is the order where there is the least chance for older data to disappear.

@robbevp robbevp marked this pull request as draft May 26, 2022 12:34
@robbevp robbevp mentioned this pull request May 26, 2022
3 tasks
@chvp chvp changed the title WIP: Add endpoint to get play stats Add endpoint to get play stats May 26, 2022
@robbevp robbevp marked this pull request as ready for review May 29, 2022 09:44
@robbevp robbevp requested a review from chvp May 29, 2022 09:53
@chvp chvp force-pushed the enhc/add-play-stats-endpoint branch from 899ce50 to 8015363 Compare June 30, 2022 17:26
@chvp chvp merged commit 0735a9a into main Jun 30, 2022
@chvp chvp deleted the enhc/add-play-stats-endpoint branch June 30, 2022 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants