annual_report.rb 901 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # frozen_string_literal: true
  2. class AnnualReport
  3. include DatabaseHelper
  4. SOURCES = [
  5. AnnualReport::Archetype,
  6. AnnualReport::TypeDistribution,
  7. AnnualReport::TopStatuses,
  8. AnnualReport::MostUsedApps,
  9. AnnualReport::CommonlyInteractedWithAccounts,
  10. AnnualReport::TimeSeries,
  11. AnnualReport::TopHashtags,
  12. AnnualReport::MostRebloggedAccounts,
  13. AnnualReport::Percentiles,
  14. ].freeze
  15. SCHEMA = 1
  16. def initialize(account, year)
  17. @account = account
  18. @year = year
  19. end
  20. def generate
  21. return if GeneratedAnnualReport.exists?(account: @account, year: @year)
  22. GeneratedAnnualReport.create(
  23. account: @account,
  24. year: @year,
  25. schema_version: SCHEMA,
  26. data: data
  27. )
  28. end
  29. private
  30. def data
  31. with_read_replica do
  32. SOURCES.each_with_object({}) { |klass, hsh| hsh.merge!(klass.new(@account, @year).generate) }
  33. end
  34. end
  35. end