instance_media_attachments_measure.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # frozen_string_literal: true
  2. class Admin::Metrics::Measure::InstanceMediaAttachmentsMeasure < Admin::Metrics::Measure::BaseMeasure
  3. include ActionView::Helpers::NumberHelper
  4. def self.with_params?
  5. true
  6. end
  7. def key
  8. 'instance_media_attachments'
  9. end
  10. def unit
  11. 'bytes'
  12. end
  13. def value_to_human_value(value)
  14. number_to_human_size(value)
  15. end
  16. def total_in_time_range?
  17. false
  18. end
  19. protected
  20. def perform_total_query
  21. MediaAttachment.joins(:account).merge(Account.where(domain: params[:domain])).sum('COALESCE(file_file_size, 0) + COALESCE(thumbnail_file_size, 0)')
  22. end
  23. def perform_previous_total_query
  24. nil
  25. end
  26. def perform_data_query
  27. sql = <<-SQL.squish
  28. SELECT axis.*, (
  29. WITH new_media_attachments AS (
  30. SELECT COALESCE(media_attachments.file_file_size, 0) + COALESCE(media_attachments.thumbnail_file_size, 0) AS size
  31. FROM media_attachments
  32. INNER JOIN accounts ON accounts.id = media_attachments.account_id
  33. WHERE date_trunc('day', media_attachments.created_at)::date = axis.period
  34. AND accounts.domain = $3::text
  35. )
  36. SELECT SUM(size) FROM new_media_attachments
  37. ) AS value
  38. FROM (
  39. SELECT generate_series(date_trunc('day', $1::timestamp)::date, date_trunc('day', $2::timestamp)::date, interval '1 day') AS period
  40. ) AS axis
  41. SQL
  42. rows = ActiveRecord::Base.connection.select_all(sql, nil, [[nil, @start_at], [nil, @end_at], [nil, params[:domain]]])
  43. rows.map { |row| { date: row['period'], value: row['value'].to_s } }
  44. end
  45. def time_period
  46. (@start_at.to_date..@end_at.to_date)
  47. end
  48. def previous_time_period
  49. ((@start_at.to_date - length_of_period)..(@end_at.to_date - length_of_period))
  50. end
  51. def params
  52. @params.permit(:domain)
  53. end
  54. end