From 023a6aa2e873ec5e4b47c20dc4c760b019005bd6 Mon Sep 17 00:00:00 2001 From: briri Date: Mon, 17 Jun 2024 09:27:27 -0700 Subject: [PATCH] updates to the registry_orgs controller to improve Org typeahead performance --- app/controllers/registry_orgs_controller.rb | 33 ++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/app/controllers/registry_orgs_controller.rb b/app/controllers/registry_orgs_controller.rb index 87cbcfde59..00769fdef6 100644 --- a/app/controllers/registry_orgs_controller.rb +++ b/app/controllers/registry_orgs_controller.rb @@ -73,7 +73,13 @@ def find_by_search_term(term:, **options) registry_matches.reject { |match| match.org_id.nil? } if restricting # Filter out any RegistryOrgs that are also in the Orgs, we only want to return one! - registry_matches = registry_matches.reject { |r_org| org_matches.map(&:id).include?(r_org.org_id) } + + # briley - 06/17 + # fix inefficient query causing 502 errors + # + # registry_matches = registry_matches.reject { |r_org| org_matches.map(&:id).include?(r_org.org_id) } + registry_matches = (registry_matches - org_matches) + org_matches + matches = (registry_matches + org_matches).flatten.compact.uniq matches = deduplicate(term: term, list: matches) matches.map(&:name).flatten.compact.uniq @@ -85,7 +91,16 @@ def find_by_search_term(term:, **options) def orgs_search(term:, **options) return [] if options[:unknown_only] - matches = Org.includes(:users).search(term) + # briley - 06/17 + # fix inefficient query causing 502 errors. Add limit and user count + # + # matches = Org.includes(:users).search(term).limit(100) + matches = Org.left_joins(:users) + .search(term) + .select('orgs.*, COUNT(users.id) AS user_count') + .group('orgs.id') + .limit(500) + if options[:template_owner_only] matches = matches.joins(:templates) .where('templates.published = true') @@ -107,7 +122,16 @@ def registry_orgs_search(term:, **options) # If we only want Orgs with a template, skip the RegistryOrg search return [] if options[:template_owner_only] - matches = RegistryOrg.includes(org: :users).search(term) + # briley - 06/17 + # fix inefficient query causing 502 errors. Add limit and user count + # + #matches = RegistryOrg.includes(org: :users).search(term).limit(100) + matches = RegistryOrg.left_joins(org: :users) + .search(term) + .select('registry_orgs.*, COUNT(users.id) AS user_count') + .group('registry_orgs.id') + .limit(500) + # If we are only allowing known Orgs then filter by org_id presence matches = matches.where.not(org_id: nil) if options.fetch(:known_only, false) matches = matches.where(org_id: nil) if options.fetch(:unknown_only, false) @@ -159,7 +183,8 @@ def deduplicate(term:, list: []) hashes = list.map do |item| { normalized: item.name.downcase.strip, - user_count: item.respond_to?(:users) ? item.users.count : 0, + # user_count: item.respond_to?(:users) ? item.users.count : 0, + user_count: item.respond_to?(:user_count) ? item.user_count : 0, weight: weigh(term: term, org: item), original: item }