instance_accounts_measure.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # frozen_string_literal: true
  2. class Admin::Metrics::Measure::InstanceAccountsMeasure < Admin::Metrics::Measure::BaseMeasure
  3. def self.with_params?
  4. true
  5. end
  6. def key
  7. 'instance_accounts'
  8. end
  9. def total_in_time_range?
  10. false
  11. end
  12. protected
  13. def perform_total_query
  14. Account.where(domain: params[:domain]).count
  15. end
  16. def perform_previous_total_query
  17. nil
  18. end
  19. def perform_data_query
  20. sql = <<-SQL.squish
  21. SELECT axis.*, (
  22. WITH new_accounts AS (
  23. SELECT accounts.id
  24. FROM accounts
  25. WHERE date_trunc('day', accounts.created_at)::date = axis.period
  26. AND accounts.domain = $3::text
  27. )
  28. SELECT count(*) FROM new_accounts
  29. ) AS value
  30. FROM (
  31. SELECT generate_series(date_trunc('day', $1::timestamp)::date, date_trunc('day', $2::timestamp)::date, interval '1 day') AS period
  32. ) AS axis
  33. SQL
  34. rows = ActiveRecord::Base.connection.select_all(sql, nil, [[nil, @start_at], [nil, @end_at], [nil, params[:domain]]])
  35. rows.map { |row| { date: row['period'], value: row['value'].to_s } }
  36. end
  37. def time_period
  38. (@start_at.to_date..@end_at.to_date)
  39. end
  40. def previous_time_period
  41. ((@start_at.to_date - length_of_period)..(@end_at.to_date - length_of_period))
  42. end
  43. def params
  44. @params.permit(:domain)
  45. end
  46. end