2017-06-01 04:38:44 +10:00
|
|
|
# frozen_string_literal: true
|
2023-02-20 16:58:28 +11:00
|
|
|
|
2020-12-14 19:06:34 +11:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: instances
|
|
|
|
#
|
|
|
|
# domain :string primary key
|
|
|
|
# accounts_count :bigint(8)
|
|
|
|
#
|
2017-06-01 04:38:44 +10:00
|
|
|
|
2020-12-14 19:06:34 +11:00
|
|
|
class Instance < ApplicationRecord
|
|
|
|
self.primary_key = :domain
|
2017-06-01 04:38:44 +10:00
|
|
|
|
2021-05-06 07:39:02 +10:00
|
|
|
attr_accessor :failure_days
|
|
|
|
|
2023-04-30 22:06:53 +10:00
|
|
|
with_options foreign_key: :domain, primary_key: :domain, inverse_of: false do
|
|
|
|
belongs_to :domain_block
|
|
|
|
belongs_to :domain_allow
|
2024-01-25 04:30:28 +11:00
|
|
|
belongs_to :unavailable_domain
|
|
|
|
|
|
|
|
has_many :accounts, dependent: nil
|
2023-04-30 22:06:53 +10:00
|
|
|
end
|
2020-12-14 19:06:34 +11:00
|
|
|
|
2023-07-28 00:11:17 +10:00
|
|
|
scope :searchable, -> { where.not(domain: DomainBlock.select(:domain)) }
|
2020-12-14 19:06:34 +11:00
|
|
|
scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
|
2024-01-19 02:57:10 +11:00
|
|
|
scope :domain_starts_with, ->(value) { where(arel_table[:domain].matches("#{sanitize_sql_like(value)}%", false, true)) }
|
2023-05-31 17:57:24 +10:00
|
|
|
scope :by_domain_and_subdomains, ->(domain) { where("reverse('.' || domain) LIKE reverse(?)", "%.#{domain}") }
|
2024-01-26 02:28:27 +11:00
|
|
|
scope :with_domain_follows, ->(domains) { where(domain: domains).where(domain_account_follows) }
|
2020-12-14 19:06:34 +11:00
|
|
|
|
|
|
|
def self.refresh
|
|
|
|
Scenic.database.refresh_materialized_view(table_name, concurrently: true, cascade: false)
|
2019-01-08 23:39:49 +11:00
|
|
|
end
|
|
|
|
|
2024-01-26 02:28:27 +11:00
|
|
|
def self.domain_account_follows
|
|
|
|
Arel.sql(
|
|
|
|
<<~SQL.squish
|
|
|
|
EXISTS (
|
|
|
|
SELECT 1
|
|
|
|
FROM follows
|
|
|
|
JOIN accounts ON follows.account_id = accounts.id OR follows.target_account_id = accounts.id
|
|
|
|
WHERE accounts.domain = instances.domain
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-12-14 19:06:34 +11:00
|
|
|
def readonly?
|
|
|
|
true
|
2019-03-26 10:36:35 +11:00
|
|
|
end
|
|
|
|
|
2020-12-14 19:06:34 +11:00
|
|
|
def delivery_failure_tracker
|
|
|
|
@delivery_failure_tracker ||= DeliveryFailureTracker.new(domain)
|
|
|
|
end
|
|
|
|
|
2022-03-14 15:27:37 +11:00
|
|
|
def purgeable?
|
|
|
|
unavailable? || domain_block&.suspend?
|
|
|
|
end
|
|
|
|
|
2022-03-09 18:52:32 +11:00
|
|
|
def unavailable?
|
2022-03-14 15:27:37 +11:00
|
|
|
unavailable_domain.present?
|
2020-12-14 19:06:34 +11:00
|
|
|
end
|
|
|
|
|
2022-03-09 18:52:32 +11:00
|
|
|
def failing?
|
|
|
|
failure_days.present? || unavailable?
|
2020-12-14 19:06:34 +11:00
|
|
|
end
|
|
|
|
|
2022-03-09 18:52:32 +11:00
|
|
|
def to_param
|
|
|
|
domain
|
2020-12-14 19:06:34 +11:00
|
|
|
end
|
|
|
|
|
2022-08-26 04:39:40 +10:00
|
|
|
alias to_log_human_identifier to_param
|
|
|
|
|
2022-03-09 18:52:32 +11:00
|
|
|
delegate :exhausted_deliveries_days, to: :delivery_failure_tracker
|
2020-12-14 19:06:34 +11:00
|
|
|
|
2022-03-09 18:52:32 +11:00
|
|
|
def availability_over_days(num_days, end_date = Time.now.utc.to_date)
|
|
|
|
failures_map = exhausted_deliveries_days.index_with { true }
|
|
|
|
period_end_at = exhausted_deliveries_days.last || end_date
|
|
|
|
period_start_at = period_end_at - num_days.days
|
2020-12-14 19:06:34 +11:00
|
|
|
|
2022-03-09 18:52:32 +11:00
|
|
|
(period_start_at..period_end_at).map do |date|
|
|
|
|
[date, failures_map[date]]
|
|
|
|
end
|
2017-06-01 04:38:44 +10:00
|
|
|
end
|
|
|
|
end
|