instance_languages_dimension.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # frozen_string_literal: true
  2. class Admin::Metrics::Dimension::InstanceLanguagesDimension < Admin::Metrics::Dimension::BaseDimension
  3. include LanguagesHelper
  4. def self.with_params?
  5. true
  6. end
  7. def key
  8. 'instance_languages'
  9. end
  10. protected
  11. def perform_query
  12. sql = <<-SQL.squish
  13. SELECT COALESCE(statuses.language, 'und') AS language, count(*) AS value
  14. FROM statuses
  15. INNER JOIN accounts ON accounts.id = statuses.account_id
  16. WHERE accounts.domain = $1
  17. AND statuses.id BETWEEN $2 AND $3
  18. AND statuses.reblog_of_id IS NULL
  19. GROUP BY COALESCE(statuses.language, 'und')
  20. ORDER BY count(*) DESC
  21. LIMIT $4
  22. SQL
  23. rows = ActiveRecord::Base.connection.select_all(sql, nil, [[nil, params[:domain]], [nil, Mastodon::Snowflake.id_at(@start_at, with_random: false)], [nil, Mastodon::Snowflake.id_at(@end_at, with_random: false)], [nil, @limit]])
  24. rows.map { |row| { key: row['language'], human_key: standard_locale_name(row['language']), value: row['value'].to_s } }
  25. end
  26. def params
  27. @params.permit(:domain)
  28. end
  29. end