From 1f0be21317295e1f3a47ace6454d5afcc502d077 Mon Sep 17 00:00:00 2001 From: Claire Date: Thu, 9 Feb 2023 20:57:14 +0100 Subject: [PATCH] Fix some performance issues with /admin/instances (#21907) (#23483) /admin/instances?availability=failing remains wholly unefficient --- app/controllers/admin/instances_controller.rb | 2 +- app/lib/delivery_failure_tracker.rb | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/instances_controller.rb b/app/controllers/admin/instances_controller.rb index 5c82331de..7c44e88b7 100644 --- a/app/controllers/admin/instances_controller.rb +++ b/app/controllers/admin/instances_controller.rb @@ -57,7 +57,7 @@ module Admin end def preload_delivery_failures! - warning_domains_map = DeliveryFailureTracker.warning_domains_map + warning_domains_map = DeliveryFailureTracker.warning_domains_map(@instances.map(&:domain)) @instances.each do |instance| instance.failure_days = warning_domains_map[instance.domain] diff --git a/app/lib/delivery_failure_tracker.rb b/app/lib/delivery_failure_tracker.rb index 7c4e28eb7..66c1fd8c0 100644 --- a/app/lib/delivery_failure_tracker.rb +++ b/app/lib/delivery_failure_tracker.rb @@ -65,8 +65,13 @@ class DeliveryFailureTracker domains - UnavailableDomain.all.pluck(:domain) end - def warning_domains_map - warning_domains.index_with { |domain| redis.scard(exhausted_deliveries_key_by(domain)) } + def warning_domains_map(domains = nil) + if domains.nil? + warning_domains.index_with { |domain| redis.scard(exhausted_deliveries_key_by(domain)) } + else + domains -= UnavailableDomain.where(domain: domains).pluck(:domain) + domains.index_with { |domain| redis.scard(exhausted_deliveries_key_by(domain)) }.filter { |_, days| days.positive? } + end end private