1
0

annual_report.rb 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 self.table_name_prefix
  17. 'annual_report_'
  18. end
  19. def initialize(account, year)
  20. @account = account
  21. @year = year
  22. end
  23. def self.prepare(year)
  24. SOURCES.each do |klass|
  25. klass.prepare(year)
  26. end
  27. end
  28. def generate
  29. return if GeneratedAnnualReport.exists?(account: @account, year: @year)
  30. GeneratedAnnualReport.create(
  31. account: @account,
  32. year: @year,
  33. schema_version: SCHEMA,
  34. data: data
  35. )
  36. end
  37. private
  38. def data
  39. with_read_replica do
  40. SOURCES.each_with_object({}) { |klass, hsh| hsh.merge!(klass.new(@account, @year).generate) }
  41. end
  42. end
  43. end