instance_reports_measure.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # frozen_string_literal: true
  2. class Admin::Metrics::Measure::InstanceReportsMeasure < Admin::Metrics::Measure::BaseMeasure
  3. def self.with_params?
  4. true
  5. end
  6. def key
  7. 'instance_reports'
  8. end
  9. def total_in_time_range?
  10. false
  11. end
  12. protected
  13. def perform_total_query
  14. Report.where(target_account: 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_reports AS (
  23. SELECT reports.id
  24. FROM reports
  25. INNER JOIN accounts ON accounts.id = reports.target_account_id
  26. WHERE date_trunc('day', reports.created_at)::date = axis.period
  27. AND accounts.domain = $3::text
  28. )
  29. SELECT count(*) FROM new_reports
  30. ) AS value
  31. FROM (
  32. SELECT generate_series(date_trunc('day', $1::timestamp)::date, date_trunc('day', $2::timestamp)::date, interval '1 day') AS period
  33. ) AS axis
  34. SQL
  35. rows = ActiveRecord::Base.connection.select_all(sql, nil, [[nil, @start_at], [nil, @end_at], [nil, params[:domain]]])
  36. rows.map { |row| { date: row['period'], value: row['value'].to_s } }
  37. end
  38. def time_period
  39. (@start_at.to_date..@end_at.to_date)
  40. end
  41. def previous_time_period
  42. ((@start_at.to_date - length_of_period)..(@end_at.to_date - length_of_period))
  43. end
  44. def params
  45. @params.permit(:domain)
  46. end
  47. end