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

Only show active users on leaderboards #8012

Merged
merged 6 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
40 changes: 17 additions & 23 deletions app/models/statistics/leaderboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Leaderboard
def all_time_requesters
InfoRequest.is_public.
joins(:user).
merge(User.active).
group(:user).
order(count_info_requests_all: :desc).
limit(10).
Expand All @@ -15,40 +16,33 @@ def last_28_day_requesters
InfoRequest.is_public.
where('info_requests.created_at >= ?', 28.days.ago).
joins(:user).
merge(User.active).
group(:user).
order(count_info_requests_all: :desc).
limit(10).
count
end

def all_time_commenters
commenters = Comment.visible.
joins(:user).
group('comments.user_id').
order(count_all: :desc).
limit(10).
count
# TODO: Have user objects automatically instantiated like the InfoRequest
# queries above
result = {}
commenters.each { |user_id, count| result[User.find(user_id)] = count }
result
Comment.visible.
joins(:user).
merge(User.active).
group(:user).
order(count_all: :desc).
limit(10).
count
end

def last_28_day_commenters
# TODO: Refactor as it's basically the same as all_time_commenters
commenters = Comment.visible.
where('comments.created_at >= ?', 28.days.ago).
joins(:user).
group('comments.user_id').
order(count_all: :desc).
limit(10).
count
# TODO: Have user objects automatically instantiated like the InfoRequest
# queries above
result = {}
commenters.each { |user_id, count| result[User.find(user_id)] = count }
result
Comment.visible.
where('comments.created_at >= ?', 28.days.ago).
joins(:user).
merge(User.active).
group(:user).
order(count_all: :desc).
limit(10).
count
end
end
end
2 changes: 2 additions & 0 deletions doc/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* Add admin view of unmasked version of main body part attachments (Gareth Rees)
* Add internal ID number to authority CSV download (Alex Parsons, Graeme
Porteous)
* Don't show users that have closed their account or been banned on leaderboards
(Chris Mytton)

# 0.44.0.0

Expand Down
29 changes: 24 additions & 5 deletions spec/models/statistics/leaderboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@

RSpec.describe Statistics::Leaderboard do
let(:statistics) { described_class.new }
before { User.destroy_all }

describe '#all_time_requesters' do
it 'gets most frequent requesters' do
User.destroy_all

user1 = FactoryBot.create(:user)
user2 = FactoryBot.create(:user)
user3 = FactoryBot.create(:user)
banned_user = FactoryBot.create(:user, :banned)
closed_account_user = FactoryBot.create(:user, :closed)

travel_to(6.months.ago) do
5.times { FactoryBot.create(:info_request, user: user1) }
2.times { FactoryBot.create(:info_request, user: user2) }
FactoryBot.create(:info_request, user: user3)
10.times { FactoryBot.create(:info_request, user: banned_user) }
10.times { FactoryBot.create(:info_request, user: closed_account_user) }
3.times { FactoryBot.create(:info_request, :hidden, user: user1) }
3.times { FactoryBot.create(:embargoed_request, user: user1) }
end

expect(statistics.all_time_requesters).
Expand All @@ -36,6 +41,14 @@
FactoryBot.create(:info_request,
user: user_with_an_old_request,
created_at: 2.months.ago)
banned_user = FactoryBot.create(:user, :banned)
10.times { FactoryBot.create(:info_request, user: banned_user) }
closed_account_user = FactoryBot.create(:user, :closed)
10.times { FactoryBot.create(:info_request, user: closed_account_user) }
user_with_embargoed_request = FactoryBot.create(:user)
FactoryBot.create(:embargoed_request, user: user_with_embargoed_request)
user_with_hidden_request = FactoryBot.create(:user)
FactoryBot.create(:info_request, :hidden, user: user_with_hidden_request)

expect(statistics.last_28_day_requesters).
to eql({ user_with_3_requests => 3,
Expand All @@ -48,6 +61,8 @@
let(:many_comments) { FactoryBot.create(:user) }
let(:some_comments) { FactoryBot.create(:user) }
let!(:none_comments) { FactoryBot.create(:user) }
let(:banned_user) { FactoryBot.create(:user, :banned) }
let(:closed_account_user) { FactoryBot.create(:user, :closed) }

before do
FactoryBot.create(:comment, user: many_comments)
Expand All @@ -56,14 +71,14 @@
FactoryBot.create(:comment, user: many_comments)
FactoryBot.create(:comment, user: some_comments)
FactoryBot.create(:comment, user: many_comments)
10.times { FactoryBot.create(:comment, user: banned_user) }
10.times { FactoryBot.create(:comment, user: closed_account_user) }
end

it 'gets most frequent commenters' do
# FIXME: This uses fixtures. Change it to use factories when we can.
expect(statistics.all_time_commenters).
to eql({ many_comments => 4,
some_comments => 2,
users(:silly_name_user) => 1 })
some_comments => 2 })
end
end

Expand All @@ -79,6 +94,10 @@
FactoryBot.create(:comment,
user: user_with_an_old_comment,
created_at: 2.months.ago)
banned_user = FactoryBot.create(:user, :banned)
10.times { FactoryBot.create(:comment, user: banned_user) }
closed_account_user = FactoryBot.create(:user, :closed)
10.times { FactoryBot.create(:comment, user: closed_account_user) }

expect(statistics.last_28_day_commenters).
to eql({ user_with_3_comments => 3,
Expand Down