time_series.rb 1.1 KB

123456789101112131415161718192021222324252627282930
  1. # frozen_string_literal: true
  2. class AnnualReport::TimeSeries < AnnualReport::Source
  3. def generate
  4. {
  5. time_series: (1..12).map do |month|
  6. {
  7. month: month,
  8. statuses: statuses_per_month[month] || 0,
  9. following: following_per_month[month] || 0,
  10. followers: followers_per_month[month] || 0,
  11. }
  12. end,
  13. }
  14. end
  15. private
  16. def statuses_per_month
  17. @statuses_per_month ||= report_statuses.group(:period).pluck(Arel.sql("date_part('month', created_at)::int AS period, count(*)")).to_h
  18. end
  19. def following_per_month
  20. @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
  21. end
  22. def followers_per_month
  23. @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
  24. end
  25. end