Skip to content

Commit

Permalink
Use group/count approach in annual report classes (mastodon#32914)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjankowski authored Nov 19, 2024
1 parent 295ad6f commit 4259828
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/lib/annual_report/commonly_interacted_with_accounts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ def generate
private

def commonly_interacted_with_accounts
report_statuses.where.not(in_reply_to_account_id: @account.id).group(:in_reply_to_account_id).having('count(*) > 1').order(total: :desc).limit(SET_SIZE).pluck(Arel.sql('in_reply_to_account_id, count(*) AS total'))
report_statuses.where.not(in_reply_to_account_id: @account.id).group(:in_reply_to_account_id).having('count(*) > 1').order(count_all: :desc).limit(SET_SIZE).count
end
end
2 changes: 1 addition & 1 deletion app/lib/annual_report/most_reblogged_accounts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ def generate
private

def most_reblogged_accounts
report_statuses.where.not(reblog_of_id: nil).joins(reblog: :account).group('accounts.id').having('count(*) > 1').order(total: :desc).limit(SET_SIZE).pluck(Arel.sql('accounts.id, count(*) as total'))
report_statuses.where.not(reblog_of_id: nil).joins(reblog: :account).group('accounts.id').having('count(*) > 1').order(count_all: :desc).limit(SET_SIZE).count
end
end
2 changes: 1 addition & 1 deletion app/lib/annual_report/most_used_apps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ def generate
private

def most_used_apps
report_statuses.joins(:application).group('oauth_applications.name').order(total: :desc).limit(SET_SIZE).pluck(Arel.sql('oauth_applications.name, count(*) as total'))
report_statuses.joins(:application).group('oauth_applications.name').order(count_all: :desc).limit(SET_SIZE).count
end
end
8 changes: 7 additions & 1 deletion app/lib/annual_report/top_hashtags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ def generate
private

def top_hashtags
Tag.joins(:statuses).where(statuses: { id: report_statuses.select(:id) }).group(:id).having('count(*) > 1').order(total: :desc).limit(SET_SIZE).pluck(Arel.sql('COALESCE(tags.display_name, tags.name), count(*) AS total'))
Tag.joins(:statuses).where(statuses: { id: report_statuses.select(:id) }).group(coalesced_tag_names).having('count(*) > 1').order(count_all: :desc).limit(SET_SIZE).count
end

def coalesced_tag_names
Arel.sql(<<~SQL.squish)
COALESCE(tags.display_name, tags.name)
SQL
end
end
13 changes: 11 additions & 2 deletions spec/lib/annual_report/commonly_interacted_with_accounts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,27 @@
let(:account) { Fabricate :account }

let(:other_account) { Fabricate :account }
let(:most_other_account) { Fabricate :account }

before do
_other = Fabricate :status

Fabricate :status, account: account, reply: true, in_reply_to_id: Fabricate(:status, account: other_account).id
Fabricate :status, account: account, reply: true, in_reply_to_id: Fabricate(:status, account: other_account).id

Fabricate :status, account: account, reply: true, in_reply_to_id: Fabricate(:status, account: most_other_account).id
Fabricate :status, account: account, reply: true, in_reply_to_id: Fabricate(:status, account: most_other_account).id
Fabricate :status, account: account, reply: true, in_reply_to_id: Fabricate(:status, account: most_other_account).id
end

it 'builds a report for an account' do
expect(subject.generate)
.to include(
commonly_interacted_with_accounts: contain_exactly(
include(account_id: other_account.id.to_s, count: 2)
commonly_interacted_with_accounts: eq(
[
{ account_id: most_other_account.id.to_s, count: 3 },
{ account_id: other_account.id.to_s, count: 2 },
]
)
)
end
Expand Down
12 changes: 10 additions & 2 deletions spec/lib/annual_report/most_reblogged_accounts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,26 @@
let(:account) { Fabricate :account }

let(:other_account) { Fabricate :account }
let(:most_other_account) { Fabricate :account }

before do
_other = Fabricate :status
Fabricate :status, account: account, reblog: Fabricate(:status, account: other_account)
Fabricate :status, account: account, reblog: Fabricate(:status, account: other_account)

Fabricate :status, account: account, reblog: Fabricate(:status, account: most_other_account)
Fabricate :status, account: account, reblog: Fabricate(:status, account: most_other_account)
Fabricate :status, account: account, reblog: Fabricate(:status, account: most_other_account)
end

it 'builds a report for an account' do
expect(subject.generate)
.to include(
most_reblogged_accounts: contain_exactly(
include(account_id: other_account.id.to_s, count: 2)
most_reblogged_accounts: eq(
[
{ account_id: most_other_account.id.to_s, count: 3 },
{ account_id: other_account.id.to_s, count: 2 },
]
)
)
end
Expand Down
11 changes: 8 additions & 3 deletions spec/lib/annual_report/most_used_apps_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,23 @@
context 'with an active account' do
let(:account) { Fabricate :account }

let(:application) { Fabricate :application }
let(:application) { Fabricate :application, name: 'App' }
let(:most_application) { Fabricate :application, name: 'Most App' }

before do
_other = Fabricate :status
Fabricate.times 2, :status, account: account, application: application
Fabricate.times 3, :status, account: account, application: most_application
end

it 'builds a report for an account' do
expect(subject.generate)
.to include(
most_used_apps: contain_exactly(
include(name: application.name, count: 2)
most_used_apps: eq(
[
{ name: most_application.name, count: 3 },
{ name: application.name, count: 2 },
]
)
)
end
Expand Down
15 changes: 13 additions & 2 deletions spec/lib/annual_report/top_hashtags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,31 @@
let(:account) { Fabricate :account }

let(:tag) { Fabricate :tag }
let(:most_tag) { Fabricate :tag }

before do
_other = Fabricate :status

first = Fabricate :status, account: account
first.tags << tag
first.tags << most_tag

last = Fabricate :status, account: account
last.tags << tag
last.tags << most_tag

middle = Fabricate :status, account: account
middle.tags << most_tag
end

it 'builds a report for an account' do
expect(subject.generate)
.to include(
top_hashtags: contain_exactly(
include(name: tag.name, count: 2)
top_hashtags: eq(
[
{ name: most_tag.name, count: 3 },
{ name: tag.name, count: 2 },
]
)
)
end
Expand Down

0 comments on commit 4259828

Please sign in to comment.