instance_statuses_measure.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # frozen_string_literal: true
  2. class Admin::Metrics::Measure::InstanceStatusesMeasure < Admin::Metrics::Measure::BaseMeasure
  3. include Admin::Metrics::Measure::QueryHelper
  4. def self.with_params?
  5. true
  6. end
  7. def key
  8. 'instance_statuses'
  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. Status.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], earliest_status_id: earliest_status_id, latest_status_id: latest_status_id }]
  24. end
  25. def sql_query_string
  26. <<~SQL.squish
  27. SELECT axis.*, (
  28. WITH new_statuses AS (
  29. SELECT statuses.id
  30. FROM statuses
  31. INNER JOIN accounts ON accounts.id = statuses.account_id
  32. WHERE statuses.id BETWEEN :earliest_status_id AND :latest_status_id
  33. AND #{account_domain_sql(params[:include_subdomains])}
  34. AND date_trunc('day', statuses.created_at)::date = axis.period
  35. )
  36. SELECT count(*) FROM new_statuses
  37. ) AS value
  38. FROM (
  39. SELECT generate_series(date_trunc('day', :start_at::timestamp)::date, date_trunc('day', :end_at::timestamp)::date, interval '1 day') AS period
  40. ) AS axis
  41. SQL
  42. end
  43. def earliest_status_id
  44. Mastodon::Snowflake.id_at(@start_at, with_random: false)
  45. end
  46. def latest_status_id
  47. Mastodon::Snowflake.id_at(@end_at, with_random: false)
  48. end
  49. def time_period
  50. (@start_at.to_date..@end_at.to_date)
  51. end
  52. def previous_time_period
  53. ((@start_at.to_date - length_of_period)..(@end_at.to_date - length_of_period))
  54. end
  55. def params
  56. @params.permit(:domain, :include_subdomains)
  57. end
  58. end