opened_reports_measure.rb 974 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. # frozen_string_literal: true
  2. class Admin::Metrics::Measure::OpenedReportsMeasure < Admin::Metrics::Measure::BaseMeasure
  3. include Admin::Metrics::Measure::QueryHelper
  4. def key
  5. 'opened_reports'
  6. end
  7. protected
  8. def perform_total_query
  9. Report.where(created_at: time_period).count
  10. end
  11. def perform_previous_total_query
  12. Report.where(created_at: previous_time_period).count
  13. end
  14. def sql_array
  15. [sql_query_string, { start_at: @start_at, end_at: @end_at }]
  16. end
  17. def sql_query_string
  18. <<~SQL.squish
  19. SELECT axis.*, (
  20. WITH new_reports AS (
  21. SELECT reports.id
  22. FROM reports
  23. WHERE date_trunc('day', reports.created_at)::date = axis.period
  24. )
  25. SELECT count(*) FROM new_reports
  26. ) AS value
  27. FROM (
  28. SELECT generate_series(date_trunc('day', :start_at::timestamp)::date, date_trunc('day', :end_at::timestamp)::date, interval '1 day') AS period
  29. ) AS axis
  30. SQL
  31. end
  32. end