Skip to content

Commit

Permalink
Merge pull request #19835 from agrare/improve_audit_managed_resources…
Browse files Browse the repository at this point in the history
…_performance

Use a single query to get count of active VMs and Hosts

(cherry picked from commit cee35bf)

https://bugzilla.redhat.com/show_bug.cgi?id=1805915
  • Loading branch information
chessbyte authored and simaishi committed Feb 21, 2020
1 parent 6fc121e commit a0a4e44
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
3 changes: 3 additions & 0 deletions app/models/host.rb
Expand Up @@ -154,6 +154,9 @@ class Host < ApplicationRecord
virtual_total :v_total_vms, :vms
virtual_total :v_total_miq_templates, :miq_templates

scope :active, -> { where.not(:ems_id => nil) }
scope :archived, -> { where(:ems_id => nil) }

alias_method :datastores, :storages # Used by web-services to return datastores as the property name

alias_method :parent_cluster, :ems_cluster
Expand Down
12 changes: 3 additions & 9 deletions app/models/miq_server.rb
Expand Up @@ -641,15 +641,9 @@ def self.display_name(number = 1)
end

def self.audit_managed_resources
total_vms = 0
total_hosts = 0

ExtManagementSystem.all.each do |e|
vms = e.all_vms_and_templates.count
hosts = e.all_hosts.count
total_vms += vms
total_hosts += hosts
end
total_vms = VmOrTemplate.active.count
total_hosts = Host.active.count

totals = {"vms" => total_vms, "hosts" => total_hosts}
$audit_log.info("Under Management: #{totals.to_json}")
end
Expand Down
15 changes: 15 additions & 0 deletions spec/models/miq_server_spec.rb
Expand Up @@ -481,4 +481,19 @@
expect(s.description).to eq(s.name)
end
end

context ".audit_managed_resources" do
let(:ems) { FactoryBot.create(:ems_infra) }
let!(:active_vm) { FactoryBot.create(:vm_infra, :ext_management_system => ems) }
let!(:archived_vm) { FactoryBot.create(:vm_infra) }
let!(:active_host) { FactoryBot.create(:host, :ext_management_system => ems) }
let!(:archived_host) { FactoryBot.create(:host) }

it "with active and archived vms and hosts" do
expected_message = "Under Management: #{{"vms" => 1, "hosts" => 1}.to_json}"

expect($audit_log).to receive(:info).with(expected_message)
described_class.audit_managed_resources
end
end
end

0 comments on commit a0a4e44

Please sign in to comment.