Extract method to DRY up month/year grouping in AnnualReport::TimeSeries class (#35113)

This commit is contained in:
Matt Jankowski 2025-06-23 08:18:29 -04:00 committed by GitHub
parent b08ccaa5b3
commit ebc6897afb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -17,14 +17,34 @@ class AnnualReport::TimeSeries < AnnualReport::Source
private private
def statuses_per_month def statuses_per_month
@statuses_per_month ||= report_statuses.group(:period).pluck(Arel.sql("date_part('month', created_at)::int AS period, count(*)")).to_h @statuses_per_month ||= report_statuses.group(:period).pluck(date_part_month.as('period'), Arel.star.count).to_h
end end
def following_per_month def following_per_month
@following_per_month ||= @account.active_relationships.where("date_part('year', created_at) = ?", @year).group(:period).pluck(Arel.sql("date_part('month', created_at)::int AS period, count(*)")).to_h @following_per_month ||= annual_relationships_by_month(@account.active_relationships)
end end
def followers_per_month def followers_per_month
@followers_per_month ||= @account.passive_relationships.where("date_part('year', created_at) = ?", @year).group(:period).pluck(Arel.sql("date_part('month', created_at)::int AS period, count(*)")).to_h @followers_per_month ||= annual_relationships_by_month(@account.passive_relationships)
end
def date_part_month
Arel.sql(<<~SQL.squish)
DATE_PART('month', created_at)::int
SQL
end
def annual_relationships_by_month(relationships)
relationships
.where(created_in_year, @year)
.group(:period)
.pluck(date_part_month.as('period'), Arel.star.count)
.to_h
end
def created_in_year
Arel.sql(<<~SQL.squish)
DATE_PART('year', created_at) = ?
SQL
end end
end end