Compare commits

...

3 Commits

Author SHA1 Message Date
Matt Jankowski
5dbcc13a9f
Merge cf175114d7 into 07912a1cb7 2025-07-15 16:06:00 +00:00
Matt Jankowski
cf175114d7 Brakeman warns on implicit to_s result 2025-07-11 11:16:17 -04:00
Matt Jankowski
8271e9af4c Move annual report percentile data update to model class 2025-07-11 10:34:14 -04:00
5 changed files with 82 additions and 21 deletions

View File

@ -2,15 +2,7 @@
class AnnualReport::Percentiles < AnnualReport::Source class AnnualReport::Percentiles < AnnualReport::Source
def self.prepare(year) def self.prepare(year)
AnnualReport::StatusesPerAccountCount.connection.exec_query(<<~SQL.squish, nil, [year, Mastodon::Snowflake.id_at(DateTime.new(year).beginning_of_year), Mastodon::Snowflake.id_at(DateTime.new(year).end_of_year)]) AnnualReport::StatusesPerAccountCount.refresh(year)
INSERT INTO annual_report_statuses_per_account_counts (year, account_id, statuses_count)
SELECT $1, account_id, count(*)
FROM statuses
WHERE id BETWEEN $2 AND $3
AND (local OR uri IS NULL)
GROUP BY account_id
ON CONFLICT (year, account_id) DO NOTHING
SQL
end end
def generate def generate

View File

@ -11,5 +11,35 @@
# #
class AnnualReport::StatusesPerAccountCount < ApplicationRecord class AnnualReport::StatusesPerAccountCount < ApplicationRecord
# This table facilitates percentile calculations def self.refresh(year)
connection.exec_query(<<~SQL.squish)
INSERT INTO #{table_name} (year, account_id, statuses_count)
#{AccountStatusCountQuery.new(year).to_sql}
ON CONFLICT (year, account_id) DO NOTHING
SQL
end
class AccountStatusCountQuery
def initialize(year)
@year = year
end
def to_sql
Status
.unscoped
.local
.where(id: beginning_of_year..end_of_year)
.group(:account_id)
.select(@year, :account_id, Arel.star.count)
.to_sql
end
def beginning_of_year
Mastodon::Snowflake.id_at(DateTime.new(@year).beginning_of_year, with_random: false)
end
def end_of_year
Mastodon::Snowflake.id_at(DateTime.new(@year).end_of_year, with_random: false)
end
end
end end

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
Fabricator :annual_report_statuses_per_account_count, from: 'AnnualReport::StatusesPerAccountCount' do
year { Time.zone.now.year }
account_id { Fabricate(:account).id }
statuses_count { 123 }
end

View File

@ -7,13 +7,10 @@ RSpec.describe AnnualReport::Percentiles do
subject { described_class.new(account, year) } subject { described_class.new(account, year) }
let(:year) { Time.zone.now.year } let(:year) { Time.zone.now.year }
context 'with an inactive account' do
let(:account) { Fabricate :account } let(:account) { Fabricate :account }
context 'with no status data' do
it 'builds a report for an account' do it 'builds a report for an account' do
described_class.prepare(year)
expect(subject.generate) expect(subject.generate)
.to include( .to include(
percentiles: include( percentiles: include(
@ -23,17 +20,16 @@ RSpec.describe AnnualReport::Percentiles do
end end
end end
context 'with an active account' do context 'with status count data' do
let(:account) { Fabricate :account }
before do before do
Fabricate.times 2, :status # Others as `account` # Simulate scenario where other accounts have each made one status
Fabricate.times 2, :annual_report_statuses_per_account_count, statuses_count: 1
Fabricate.times 2, :status, account: account Fabricate.times 2, :status, account: account
Fabricate :annual_report_statuses_per_account_count, account_id: account.id, statuses_count: 2
end end
it 'builds a report for an account' do it 'builds a report for an account' do
described_class.prepare(year)
expect(subject.generate) expect(subject.generate)
.to include( .to include(
percentiles: include( percentiles: include(

View File

@ -0,0 +1,36 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe AnnualReport::StatusesPerAccountCount do
describe '.refresh' do
subject { described_class.refresh(year) }
let(:year) { Time.zone.now.year }
context 'with an inactive account' do
let(:account) { Fabricate :account }
it 'does not build a status count record' do
expect { subject }
.to not_change(described_class, :count).from(0)
end
end
context 'with an active account' do
let(:account) { Fabricate :account }
before do
Fabricate :status
Fabricate.times 2, :status, account: account
end
it 'builds a status count record' do
expect { subject }
.to change(described_class, :count).by(2)
expect(described_class.where(account_id: account).first)
.to have_attributes(statuses_count: 2)
end
end
end
end