0
0
Fork 0

Fix performance on instances list in admin UI (#15282)

- Reduce duplicate queries
- Remove n+1 queries
- Add accounts count to detailed view
- Add separate action log entry for updating existing domain blocks
This commit is contained in:
Eugen Rochko 2020-12-14 09:06:34 +01:00 committed by GitHub
parent a3b5675aa8
commit 216b85b053
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 331 additions and 171 deletions

View file

@ -1,26 +1,63 @@
# frozen_string_literal: true
# == Schema Information
#
# Table name: instances
#
# domain :string primary key
# accounts_count :bigint(8)
#
class Instance
include ActiveModel::Model
class Instance < ApplicationRecord
self.primary_key = :domain
attr_accessor :domain, :accounts_count, :domain_block
has_many :accounts, foreign_key: :domain, primary_key: :domain
def initialize(resource)
@domain = resource.domain
@accounts_count = resource.respond_to?(:accounts_count) ? resource.accounts_count : nil
@domain_block = resource.is_a?(DomainBlock) ? resource : DomainBlock.rule_for(domain)
@domain_allow = resource.is_a?(DomainAllow) ? resource : DomainAllow.rule_for(domain)
belongs_to :domain_block, foreign_key: :domain, primary_key: :domain
belongs_to :domain_allow, foreign_key: :domain, primary_key: :domain
scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
def self.refresh
Scenic.database.refresh_materialized_view(table_name, concurrently: true, cascade: false)
end
def countable?
@accounts_count.present?
def readonly?
true
end
def delivery_failure_tracker
@delivery_failure_tracker ||= DeliveryFailureTracker.new(domain)
end
def following_count
@following_count ||= Follow.where(account: accounts).count
end
def followers_count
@followers_count ||= Follow.where(target_account: accounts).count
end
def reports_count
@reports_count ||= Report.where(target_account: accounts).count
end
def blocks_count
@blocks_count ||= Block.where(target_account: accounts).count
end
def public_comment
domain_block&.public_comment
end
def private_comment
domain_block&.private_comment
end
def media_storage
@media_storage ||= MediaAttachment.where(account: accounts).sum(:file_file_size)
end
def to_param
domain
end
def cache_key
domain
end
end