From 6257d580c87bd5b06a4cb0de72dcd935933b2b30 Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Wed, 2 May 2018 17:20:00 -0500 Subject: [PATCH] Load Lans effeciently in MiqProvisionVirtWorkflow Instead of preloading Lan records, splitting them up based on if they are shareable and instantiating entire ActiveRecord to only use the name column in a hash, use a specific query to gather and filter those values and generate a hash from that. --- app/models/miq_provision_virt_workflow.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/models/miq_provision_virt_workflow.rb b/app/models/miq_provision_virt_workflow.rb index ae9643595e18..068056e79bc8 100644 --- a/app/models/miq_provision_virt_workflow.rb +++ b/app/models/miq_provision_virt_workflow.rb @@ -230,8 +230,6 @@ def available_vlans_and_hosts(options = {}) hosts = get_selected_hosts(src) unless @vlan_options[:vlans] == false rails_logger('allowed_vlans', 0) - # TODO: Use Active Record to preload this data? - MiqPreloader.preload(hosts, :lans => :switches) load_allowed_vlans(hosts, vlans) rails_logger('allowed_vlans', 1) end @@ -244,9 +242,11 @@ def load_allowed_vlans(hosts, vlans) end def load_hosts_vlans(hosts, vlans) - hosts.each do |h| - h.lans.each { |l| vlans[l.name] = l.name unless l.switch.shared? } - end + Lan.joins(:switch => :hosts) + .where(:hosts => {:id => hosts.map(&:id)}) + .where(:switches => {:shared => [nil, false]}) + .distinct.pluck(:name) + .each_with_object(vlans) { |v, h| h[v] = v } end def filter_by_tags(target, options)