Skip to content

Commit

Permalink
Remove N+1 from assign_ems_create_on
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryguy committed Oct 20, 2020
1 parent f01759c commit 5d8b658
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions app/models/manageiq/providers/vmware/infra_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -671,16 +671,22 @@ def assign_ems_created_on(vm_ids)

# Of the VMs without a VM create time, filter out the ones for which we
# already have a VM create event
events = EmsEvent
.where(
:vm_or_template_id => vms_to_update.map(&:id),
:event_type => ["VmCreatedEvent", "VmDeployedEvent"])
)
.group_by(&:vm_or_template_id)

vms_to_update = vms_to_update.reject do |v|
event = v.ems_events.find_by(:event_type => ["VmCreatedEvent", "VmDeployedEvent"])
v.update_attribute(:ems_created_on, event.timestamp) if event && v.ems_created_on != event.timestamp
event
events[v.id].tap do |event|
v.update_attribute(:ems_created_on, event.timestamp) if event && v.ems_created_on != event.timestamp
end
end
return if vms_to_update.empty?

# Of the VMs still without an VM create time, use historical events, if
# available, to determine the VM create time

vms_list = vms_to_update.collect { |v| {:id => v.id, :name => v.name, :uid_ems => v.uid_ems} }
found = find_vm_create_events(vms_list)

Expand Down

0 comments on commit 5d8b658

Please sign in to comment.