Use group/count approach in annual report classes (#32914)

This commit is contained in:
Matt Jankowski 2024-11-19 03:51:34 -05:00 committed by GitHub
parent 295ad6f19a
commit 425982841d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 52 additions and 13 deletions

View File

@ -17,6 +17,6 @@ class AnnualReport::CommonlyInteractedWithAccounts < AnnualReport::Source
private private
def commonly_interacted_with_accounts 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
end end

View File

@ -17,6 +17,6 @@ class AnnualReport::MostRebloggedAccounts < AnnualReport::Source
private private
def most_reblogged_accounts 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
end end

View File

@ -17,6 +17,6 @@ class AnnualReport::MostUsedApps < AnnualReport::Source
private private
def most_used_apps 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
end end

View File

@ -17,6 +17,12 @@ class AnnualReport::TopHashtags < AnnualReport::Source
private private
def top_hashtags 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
end end

View File

@ -21,18 +21,27 @@ RSpec.describe AnnualReport::CommonlyInteractedWithAccounts do
let(:account) { Fabricate :account } let(:account) { Fabricate :account }
let(:other_account) { Fabricate :account } let(:other_account) { Fabricate :account }
let(:most_other_account) { Fabricate :account }
before do before do
_other = Fabricate :status _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: 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 end
it 'builds a report for an account' do it 'builds a report for an account' do
expect(subject.generate) expect(subject.generate)
.to include( .to include(
commonly_interacted_with_accounts: contain_exactly( commonly_interacted_with_accounts: eq(
include(account_id: other_account.id.to_s, count: 2) [
{ account_id: most_other_account.id.to_s, count: 3 },
{ account_id: other_account.id.to_s, count: 2 },
]
) )
) )
end end

View File

@ -21,18 +21,26 @@ RSpec.describe AnnualReport::MostRebloggedAccounts do
let(:account) { Fabricate :account } let(:account) { Fabricate :account }
let(:other_account) { Fabricate :account } let(:other_account) { Fabricate :account }
let(:most_other_account) { Fabricate :account }
before do before do
_other = Fabricate :status _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: 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 end
it 'builds a report for an account' do it 'builds a report for an account' do
expect(subject.generate) expect(subject.generate)
.to include( .to include(
most_reblogged_accounts: contain_exactly( most_reblogged_accounts: eq(
include(account_id: other_account.id.to_s, count: 2) [
{ account_id: most_other_account.id.to_s, count: 3 },
{ account_id: other_account.id.to_s, count: 2 },
]
) )
) )
end end

View File

@ -20,18 +20,23 @@ RSpec.describe AnnualReport::MostUsedApps do
context 'with an active account' do context 'with an active account' do
let(:account) { Fabricate :account } let(:account) { Fabricate :account }
let(:application) { Fabricate :application } let(:application) { Fabricate :application, name: 'App' }
let(:most_application) { Fabricate :application, name: 'Most App' }
before do before do
_other = Fabricate :status _other = Fabricate :status
Fabricate.times 2, :status, account: account, application: application Fabricate.times 2, :status, account: account, application: application
Fabricate.times 3, :status, account: account, application: most_application
end end
it 'builds a report for an account' do it 'builds a report for an account' do
expect(subject.generate) expect(subject.generate)
.to include( .to include(
most_used_apps: contain_exactly( most_used_apps: eq(
include(name: application.name, count: 2) [
{ name: most_application.name, count: 3 },
{ name: application.name, count: 2 },
]
) )
) )
end end

View File

@ -21,20 +21,31 @@ RSpec.describe AnnualReport::TopHashtags do
let(:account) { Fabricate :account } let(:account) { Fabricate :account }
let(:tag) { Fabricate :tag } let(:tag) { Fabricate :tag }
let(:most_tag) { Fabricate :tag }
before do before do
_other = Fabricate :status _other = Fabricate :status
first = Fabricate :status, account: account first = Fabricate :status, account: account
first.tags << tag first.tags << tag
first.tags << most_tag
last = Fabricate :status, account: account last = Fabricate :status, account: account
last.tags << tag last.tags << tag
last.tags << most_tag
middle = Fabricate :status, account: account
middle.tags << most_tag
end end
it 'builds a report for an account' do it 'builds a report for an account' do
expect(subject.generate) expect(subject.generate)
.to include( .to include(
top_hashtags: contain_exactly( top_hashtags: eq(
include(name: tag.name, count: 2) [
{ name: most_tag.name, count: 3 },
{ name: tag.name, count: 2 },
]
) )
) )
end end