-
Notifications
You must be signed in to change notification settings - Fork 132
Clean up leaderboard code + speed up hot LB API #1413
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
Open
skyfallwastaken
wants to merge
3
commits into
main
Choose a base branch
from
leaderboard-cleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 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
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 |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| class LeaderboardEntries | ||
| CACHE_EXPIRATION = 10.minutes | ||
|
|
||
| def self.fetch(...) = new(...).fetch | ||
| def self.fetch_public(leaderboard:) = new(leaderboard:).fetch_public | ||
| def self.warm_public(leaderboard:) = fetch_public(leaderboard:) | ||
|
|
||
| def initialize(leaderboard:, viewer: nil, scope: :global, country_code: nil, include_active_projects: false) | ||
| @leaderboard = leaderboard | ||
| @viewer = viewer | ||
| @scope = scope.to_sym | ||
| @country_code = country_code | ||
| @include_active_projects = include_active_projects | ||
| end | ||
|
|
||
| def fetch | ||
| return { entries: [], total: 0 } unless @leaderboard&.persisted? | ||
|
|
||
| active_projects = @include_active_projects ? Cache::ActiveProjectsJob.perform_now : nil | ||
| entries = visible_cached_entries.map.with_index(1) do |entry, rank| | ||
| entry_payload(entry, rank:, active_projects:) | ||
| end | ||
|
|
||
| { entries:, total: entries.size } | ||
| end | ||
|
|
||
| def fetch_public | ||
| return { entries: [], total: 0 } unless @leaderboard&.persisted? | ||
|
|
||
| entries = Rails.cache.fetch(public_cache_key, expires_in: CACHE_EXPIRATION) do | ||
| public_entries_from_database | ||
| end | ||
|
|
||
| { entries:, total: entries.size } | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def public_cache_key | ||
| "leaderboard_entries/public/v1/#{LeaderboardPageCache.version}/#{@leaderboard.cache_key_with_version}" | ||
| end | ||
|
|
||
| def public_entries_from_database | ||
| @leaderboard.entries | ||
| .joins(:user) | ||
| .where(users: { leaderboard_shadowbanned: false }) | ||
| .preload(:user) | ||
| .order(total_seconds: :desc, user_id: :asc) | ||
| .map.with_index(1) do |row, rank| | ||
| public_entry_payload(row, rank:) | ||
|
skyfallwastaken marked this conversation as resolved.
|
||
| end | ||
| end | ||
|
|
||
| def public_entry_payload(entry, rank:) | ||
| { | ||
| rank:, | ||
| user_id: entry.user_id, | ||
| total_seconds: entry.total_seconds, | ||
| streak_count: entry.streak_count, | ||
| is_current_user: false, | ||
| user: { | ||
| id: entry.user.id, | ||
| display_name: entry.user.display_name, | ||
| avatar_url: entry.user.avatar_url | ||
| }, | ||
| active_project: nil | ||
| } | ||
| end | ||
|
|
||
| def visible_cached_entries | ||
| cached_entries.reject { |entry| entry_hidden_from_viewer?(entry) } | ||
| end | ||
|
|
||
| def cached_entries | ||
| LeaderboardPageCache.fetch( | ||
| leaderboard: @leaderboard, | ||
| scope: @scope, | ||
| country_code: @country_code | ||
| )[:entries] | ||
| end | ||
|
|
||
| def entry_hidden_from_viewer?(entry) | ||
| entry.dig(:user, :shadowbanned) && entry[:user_id] != @viewer&.id | ||
| end | ||
|
|
||
| def entry_payload(entry, rank:, active_projects:) | ||
| user = entry[:user] | ||
| { | ||
| rank:, | ||
| user_id: entry[:user_id], | ||
| total_seconds: entry[:total_seconds], | ||
| streak_count: entry[:streak_count], | ||
| is_current_user: entry[:user_id] == @viewer&.id, | ||
| user: user_payload(user), | ||
| active_project: active_project_payload(active_projects&.dig(entry[:user_id])) | ||
| } | ||
| end | ||
|
|
||
| def user_payload(user) | ||
| { | ||
| id: user[:id], | ||
| display_name: user[:display_name], | ||
| avatar_url: user[:avatar_url], | ||
| profile_path: user[:profile_path], | ||
| verified: user[:verified], | ||
| country_code: user[:country_code], | ||
| red: user[:red] | ||
| } | ||
| end | ||
|
|
||
| def active_project_payload(active_project) | ||
| return nil unless active_project | ||
|
|
||
| { name: active_project.project_name, repo_url: active_project.repo_url } | ||
| end | ||
| end | ||
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 |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| require "test_helper" | ||
|
|
||
| class LeaderboardEntriesTest < ActiveSupport::TestCase | ||
| setup do | ||
| Rails.cache.clear | ||
| end | ||
|
|
||
| teardown do | ||
| Rails.cache.clear | ||
| end | ||
|
|
||
| test "fetch hides leaderboard shadowbanned entries from public viewers and reranks visible entries" do | ||
| hidden_user = create_user(username: "entries_hidden", leaderboard_shadowbanned: true) | ||
| visible_user = create_user(username: "entries_visible") | ||
| board = create_board | ||
| board.entries.create!(user: hidden_user, total_seconds: 500, streak_count: 1) | ||
| board.entries.create!(user: visible_user, total_seconds: 300, streak_count: 2) | ||
|
|
||
| payload = LeaderboardEntries.fetch(leaderboard: board) | ||
|
|
||
| assert_equal 1, payload[:total] | ||
| assert_equal [ visible_user.id ], payload[:entries].map { |entry| entry[:user_id] } | ||
| assert_equal [ 1 ], payload[:entries].map { |entry| entry[:rank] } | ||
| end | ||
|
|
||
| test "fetch shows a leaderboard shadowbanned entry to its own user" do | ||
| hidden_user = create_user(username: "entries_self", leaderboard_shadowbanned: true) | ||
| board = create_board | ||
| board.entries.create!(user: hidden_user, total_seconds: 500, streak_count: 1) | ||
|
|
||
| payload = LeaderboardEntries.fetch(leaderboard: board, viewer: hidden_user) | ||
|
|
||
| assert_equal 1, payload[:total] | ||
| assert_equal hidden_user.id, payload[:entries].first[:user_id] | ||
| assert_equal true, payload[:entries].first[:is_current_user] | ||
| end | ||
|
|
||
| test "fetch applies country scope" do | ||
| us_user = create_user(username: "entries_us", country_code: "US") | ||
| ca_user = create_user(username: "entries_ca", country_code: "CA") | ||
| board = create_board | ||
| board.entries.create!(user: us_user, total_seconds: 300, streak_count: 1) | ||
| board.entries.create!(user: ca_user, total_seconds: 200, streak_count: 1) | ||
|
|
||
| payload = LeaderboardEntries.fetch(leaderboard: board, scope: :country, country_code: "US") | ||
|
|
||
| assert_equal 1, payload[:total] | ||
| assert_equal [ us_user.id ], payload[:entries].map { |entry| entry[:user_id] } | ||
| end | ||
|
|
||
| test "fetch can include active project enrichment" do | ||
| user = create_user(username: "entries_active") | ||
| board = create_board | ||
| board.entries.create!(user: user, total_seconds: 300, streak_count: 1) | ||
| user.project_repo_mappings.create!(project_name: "active-project") | ||
| Heartbeat.create!( | ||
| user: user, | ||
| project: "active-project", | ||
| category: "coding", | ||
| time: Time.current.to_f, | ||
| source_type: :direct_entry | ||
| ) | ||
|
|
||
| payload = LeaderboardEntries.fetch(leaderboard: board, include_active_projects: true) | ||
|
|
||
| assert_equal({ name: "active-project", repo_url: nil }, payload[:entries].first[:active_project]) | ||
| end | ||
|
|
||
| test "fetch_public uses lean public-visible entries" do | ||
| hidden_user = create_user(username: "public_hidden", leaderboard_shadowbanned: true) | ||
| visible_user = create_user(username: "public_visible", trust_level: :green) | ||
| board = create_board | ||
| board.entries.create!(user: hidden_user, total_seconds: 500, streak_count: 1) | ||
| board.entries.create!(user: visible_user, total_seconds: 300, streak_count: 2) | ||
|
|
||
| payload = LeaderboardEntries.fetch_public(leaderboard: board) | ||
|
|
||
| assert_equal 1, payload[:total] | ||
| assert_equal visible_user.id, payload[:entries].first[:user_id] | ||
| assert_equal 1, payload[:entries].first[:rank] | ||
| assert_equal visible_user.display_name, payload[:entries].first.dig(:user, :display_name) | ||
| end | ||
|
|
||
| test "fetch_public cache follows leaderboard page cache version" do | ||
| original_cache = Rails.cache | ||
| Rails.cache = ActiveSupport::Cache.lookup_store(:memory_store) | ||
| Rails.cache.clear | ||
|
|
||
| user = create_user(username: "public_cache_visible") | ||
| board = create_board | ||
| board.entries.create!(user: user, total_seconds: 300, streak_count: 1) | ||
|
|
||
| assert_equal 1, LeaderboardEntries.fetch_public(leaderboard: board)[:total] | ||
|
|
||
| user.set_leaderboard_shadowban( | ||
| banned: true, | ||
| changed_by_user: User.create!(timezone: "UTC", admin_level: :superadmin), | ||
| reason: "test shadowban" | ||
| ) | ||
|
|
||
| assert_equal 0, LeaderboardEntries.fetch_public(leaderboard: board)[:total] | ||
| ensure | ||
| Rails.cache.clear | ||
| Rails.cache = original_cache | ||
| end | ||
|
skyfallwastaken marked this conversation as resolved.
|
||
|
|
||
| private | ||
|
|
||
| def create_user(username:, country_code: nil, leaderboard_shadowbanned: false, trust_level: :blue) | ||
| User.create!( | ||
| username: username, | ||
| country_code: country_code, | ||
| timezone: "UTC", | ||
| trust_level: trust_level, | ||
| leaderboard_shadowbanned: leaderboard_shadowbanned, | ||
| leaderboard_shadowban_reason: leaderboard_shadowbanned ? "test shadowban" : nil | ||
| ) | ||
| end | ||
|
|
||
| def create_board | ||
| Leaderboard.create!( | ||
| start_date: Date.current, | ||
| period_type: :daily, | ||
| timezone_utc_offset: nil, | ||
| finished_generating_at: Time.current | ||
| ) | ||
| end | ||
| end | ||
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.
Uh oh!
There was an error while loading. Please reload this page.