2017-06-01 03:38:44 +09:00
|
|
|
# frozen_string_literal: true
|
2020-12-14 17:06:34 +09:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: instances
|
|
|
|
#
|
|
|
|
# domain :string primary key
|
|
|
|
# accounts_count :bigint(8)
|
|
|
|
#
|
2017-06-01 03:38:44 +09:00
|
|
|
|
2020-12-14 17:06:34 +09:00
|
|
|
class Instance < ApplicationRecord
|
|
|
|
self.primary_key = :domain
|
2017-06-01 03:38:44 +09:00
|
|
|
|
2021-05-06 06:39:02 +09:00
|
|
|
attr_accessor :failure_days
|
|
|
|
|
2020-12-14 17:06:34 +09:00
|
|
|
has_many :accounts, foreign_key: :domain, primary_key: :domain
|
2017-06-01 03:38:44 +09:00
|
|
|
|
2020-12-14 17:06:34 +09:00
|
|
|
belongs_to :domain_block, foreign_key: :domain, primary_key: :domain
|
|
|
|
belongs_to :domain_allow, foreign_key: :domain, primary_key: :domain
|
2021-05-06 06:39:02 +09:00
|
|
|
belongs_to :unavailable_domain, foreign_key: :domain, primary_key: :domain # skipcq: RB-RL1031
|
2020-12-14 17:06:34 +09:00
|
|
|
|
|
|
|
scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
|
|
|
|
|
|
|
|
def self.refresh
|
|
|
|
Scenic.database.refresh_materialized_view(table_name, concurrently: true, cascade: false)
|
2019-01-08 21:39:49 +09:00
|
|
|
end
|
|
|
|
|
2020-12-14 17:06:34 +09:00
|
|
|
def readonly?
|
|
|
|
true
|
2019-03-26 08:36:35 +09:00
|
|
|
end
|
|
|
|
|
2020-12-14 17:06:34 +09:00
|
|
|
def delivery_failure_tracker
|
|
|
|
@delivery_failure_tracker ||= DeliveryFailureTracker.new(domain)
|
|
|
|
end
|
|
|
|
|
2022-03-14 13:27:37 +09:00
|
|
|
def purgeable?
|
|
|
|
unavailable? || domain_block&.suspend?
|
|
|
|
end
|
|
|
|
|
2022-03-09 16:52:32 +09:00
|
|
|
def unavailable?
|
2022-03-14 13:27:37 +09:00
|
|
|
unavailable_domain.present?
|
2020-12-14 17:06:34 +09:00
|
|
|
end
|
|
|
|
|
2022-03-09 16:52:32 +09:00
|
|
|
def failing?
|
|
|
|
failure_days.present? || unavailable?
|
2020-12-14 17:06:34 +09:00
|
|
|
end
|
|
|
|
|
2022-03-09 16:52:32 +09:00
|
|
|
def to_param
|
|
|
|
domain
|
2020-12-14 17:06:34 +09:00
|
|
|
end
|
|
|
|
|
2022-03-09 16:52:32 +09:00
|
|
|
delegate :exhausted_deliveries_days, to: :delivery_failure_tracker
|
2020-12-14 17:06:34 +09:00
|
|
|
|
2022-03-09 16:52:32 +09: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 17:06:34 +09:00
|
|
|
|
2022-03-09 16:52:32 +09:00
|
|
|
(period_start_at..period_end_at).map do |date|
|
|
|
|
[date, failures_map[date]]
|
|
|
|
end
|
2017-06-01 03:38:44 +09:00
|
|
|
end
|
|
|
|
end
|