1
0

instance.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. delegate :exhausted_deliveries_days, to: :delivery_failure_tracker
  39. def availability_over_days(num_days, end_date = Time.now.utc.to_date)
  40. failures_map = exhausted_deliveries_days.index_with { true }
  41. period_end_at = exhausted_deliveries_days.last || end_date
  42. period_start_at = period_end_at - num_days.days
  43. (period_start_at..period_end_at).map do |date|
  44. [date, failures_map[date]]
  45. end
  46. end
  47. end