Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/models/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,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) }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are handy scopes to have, only the active scope here is used but both are generally helpful


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
Original file line number Diff line number Diff line change
Expand Up @@ -488,15 +488,9 @@ def self.zone_is_modifiable?
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
Original file line number Diff line number Diff line change
Expand Up @@ -464,4 +464,19 @@
expect(described_class.zone_is_modifiable?).to be_falsey
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