instance_followers_measure.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # frozen_string_literal: true
  2. class Admin::Metrics::Measure::InstanceFollowersMeasure < Admin::Metrics::Measure::BaseMeasure
  3. include Admin::Metrics::Measure::QueryHelper
  4. def self.with_params?
  5. true
  6. end
  7. def key
  8. 'instance_followers'
  9. end
  10. def total_in_time_range?
  11. false
  12. end
  13. protected
  14. def perform_total_query
  15. domain = params[:domain]
  16. domain = Instance.by_domain_and_subdomains(params[:domain]).select(:domain) if params[:include_subdomains]
  17. Follow.joins(:account).merge(Account.where(domain: domain)).count
  18. end
  19. def perform_previous_total_query
  20. nil
  21. end
  22. def sql_array
  23. [sql_query_string, { start_at: @start_at, end_at: @end_at, domain: params[:domain] }]
  24. end
  25. def sql_query_string
  26. <<~SQL.squish
  27. SELECT axis.*, (
  28. WITH new_followers AS (
  29. SELECT follows.id
  30. FROM follows
  31. INNER JOIN accounts ON follows.account_id = accounts.id
  32. WHERE date_trunc('day', follows.created_at)::date = axis.period
  33. AND #{account_domain_sql(params[:include_subdomains])}
  34. )
  35. SELECT count(*) FROM new_followers
  36. ) AS value
  37. FROM (
  38. SELECT generate_series(date_trunc('day', :start_at::timestamp)::date, date_trunc('day', :end_at::timestamp)::date, interval '1 day') AS period
  39. ) AS axis
  40. SQL
  41. end
  42. def time_period
  43. (@start_at.to_date..@end_at.to_date)
  44. end
  45. def previous_time_period
  46. ((@start_at.to_date - length_of_period)..(@end_at.to_date - length_of_period))
  47. end
  48. def params
  49. @params.permit(:domain, :include_subdomains)
  50. end
  51. end