instance_statuses_measure.rb 1.6 KB

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