~cytrogen/masto-fe

ref: 53f5b27bd10e5f471b59cd0597d67ea12587c95a masto-fe/app/lib/admin/metrics/dimension/instance_accounts_dimension.rb -rw-r--r-- 926 bytes
53f5b27b — Claire Merge commit '640421f661ee4d7e76a2aab607e7b15687940b6f' into glitch-soc/merge-upstream 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true

class Admin::Metrics::Dimension::InstanceAccountsDimension < Admin::Metrics::Dimension::BaseDimension
  include Admin::Metrics::Dimension::QueryHelper
  include LanguagesHelper

  def self.with_params?
    true
  end

  def key
    'instance_accounts'
  end

  protected

  def perform_query
    dimension_data_rows.map { |row| { key: row['username'], human_key: row['username'], value: row['value'].to_s } }
  end

  def sql_array
    [sql_query_string, { domain: params[:domain], limit: @limit }]
  end

  def sql_query_string
    <<~SQL.squish
      SELECT accounts.username, count(follows.*) AS value
      FROM accounts
      LEFT JOIN follows ON follows.target_account_id = accounts.id
      WHERE accounts.domain = :domain
      GROUP BY accounts.id, follows.target_account_id
      ORDER BY value DESC
      LIMIT :limit
    SQL
  end

  def params
    @params.permit(:domain)
  end
end