diff --git a/app/lib/annual_report/time_series.rb b/app/lib/annual_report/time_series.rb index 65a188eda..3f9f0d52e 100644 --- a/app/lib/annual_report/time_series.rb +++ b/app/lib/annual_report/time_series.rb @@ -17,14 +17,34 @@ class AnnualReport::TimeSeries < AnnualReport::Source private 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 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 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