You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.6 KiB

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: instances
  5. #
  6. # domain :string primary key
  7. # accounts_count :bigint(8)
  8. #
  9. class Instance < ApplicationRecord
  10. self.primary_key = :domain
  11. attr_accessor :failure_days
  12. has_many :accounts, foreign_key: :domain, primary_key: :domain
  13. belongs_to :domain_block, foreign_key: :domain, primary_key: :domain
  14. belongs_to :domain_allow, foreign_key: :domain, primary_key: :domain
  15. belongs_to :unavailable_domain, foreign_key: :domain, primary_key: :domain # skipcq: RB-RL1031
  16. scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
  17. def self.refresh
  18. Scenic.database.refresh_materialized_view(table_name, concurrently: true, cascade: false)
  19. end
  20. def readonly?
  21. true
  22. end
  23. def delivery_failure_tracker
  24. @delivery_failure_tracker ||= DeliveryFailureTracker.new(domain)
  25. end
  26. def purgeable?
  27. unavailable? || domain_block&.suspend?
  28. end
  29. def unavailable?
  30. unavailable_domain.present?
  31. end
  32. def failing?
  33. failure_days.present? || unavailable?
  34. end
  35. def to_param
  36. domain
  37. end
  38. alias to_log_human_identifier to_param
  39. delegate :exhausted_deliveries_days, to: :delivery_failure_tracker
  40. def availability_over_days(num_days, end_date = Time.now.utc.to_date)
  41. failures_map = exhausted_deliveries_days.index_with { true }
  42. period_end_at = exhausted_deliveries_days.last || end_date
  43. period_start_at = period_end_at - num_days.days
  44. (period_start_at..period_end_at).map do |date|
  45. [date, failures_map[date]]
  46. end
  47. end
  48. end