instance_accounts_measure.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # frozen_string_literal: true
  2. class Admin::Metrics::Measure::InstanceAccountsMeasure < Admin::Metrics::Measure::BaseMeasure
  3. include Admin::Metrics::Measure::QueryHelper
  4. def self.with_params?
  5. true
  6. end
  7. def key
  8. 'instance_accounts'
  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. 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_accounts AS (
  29. SELECT accounts.id
  30. FROM accounts
  31. WHERE date_trunc('day', accounts.created_at)::date = axis.period
  32. AND #{account_domain_sql(params[:include_subdomains])}
  33. )
  34. SELECT count(*) FROM new_accounts
  35. ) AS value
  36. FROM (
  37. SELECT generate_series(date_trunc('day', :start_at::timestamp)::date, date_trunc('day', :end_at::timestamp)::date, interval '1 day') AS period
  38. ) AS axis
  39. SQL
  40. end
  41. def time_period
  42. (@start_at.to_date..@end_at.to_date)
  43. end
  44. def previous_time_period
  45. ((@start_at.to_date - length_of_period)..(@end_at.to_date - length_of_period))
  46. end
  47. def params
  48. @params.permit(:domain, :include_subdomains)
  49. end
  50. end