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.

58 lines
1.5 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 unavailable?
  27. unavailable_domain.present?
  28. end
  29. def failing?
  30. failure_days.present? || unavailable?
  31. end
  32. def to_param
  33. domain
  34. end
  35. delegate :exhausted_deliveries_days, to: :delivery_failure_tracker
  36. def availability_over_days(num_days, end_date = Time.now.utc.to_date)
  37. failures_map = exhausted_deliveries_days.index_with { true }
  38. period_end_at = exhausted_deliveries_days.last || end_date
  39. period_start_at = period_end_at - num_days.days
  40. (period_start_at..period_end_at).map do |date|
  41. [date, failures_map[date]]
  42. end
  43. end
  44. end