Skip to content

Commit

Permalink
virtual attributize the aggregation mixin methods start
Browse files Browse the repository at this point in the history
  • Loading branch information
d-m-u committed May 10, 2020
1 parent 70f97e6 commit 3aeef98
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 103 deletions.
17 changes: 4 additions & 13 deletions app/models/container_project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class ContainerProject < ApplicationRecord
include MiqPolicyMixin
include TenantIdentityMixin
include CustomActionsMixin
include AggregationMixin
include_concern 'Purging'
belongs_to :ext_management_system, :foreign_key => "ems_id"
has_many :container_groups, -> { active }
Expand Down Expand Up @@ -44,7 +45,6 @@ class ContainerProject < ApplicationRecord

include EventMixin
include Metric::CiMixin
include AggregationMixin::Methods

PERF_ROLLUP_CHILDREN = :all_container_groups

Expand Down Expand Up @@ -73,18 +73,9 @@ def perf_rollup_parents(interval_name = nil)

# required by aggregate_hardware
alias all_computer_system_ids computer_system_ids

def aggregate_memory(targets = nil)
aggregate_hardware(:computer_systems, :memory_mb, targets)
end

def aggregate_cpu_speed(targets = nil)
aggregate_hardware(:computer_systems, :cpu_speed, targets)
end

def aggregate_cpu_total_cores(targets = nil)
aggregate_hardware(:computer_systems, :cpu_total_cores, targets)
end
virtual_aggregate :aggregate_memory, :computer_systems, :sum, :memory_mb
virtual_aggregate :aggregate_cpu_speed, :computer_systems, :sum, :cpu_speed
virtual_aggregate :aggregate_cpu_total_cores, :computer_systems, :sum, :cpu_total_cores

def disconnect_inv
return if archived?
Expand Down
19 changes: 19 additions & 0 deletions app/models/ems_cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ class EmsCluster < ApplicationRecord
virtual_has_many :resource_pools, :uses => :all_relationships
virtual_has_many :lans, :uses => {:hosts => :lans}

alias_method :all_hosts, :hosts
alias_method :all_vms_and_templates, :vms_and_templates
alias_method :all_vm_or_template_ids, :vm_or_template_ids
alias_method :all_vms, :vms
alias_method :all_vm_ids, :vm_ids
alias_method :all_miq_templates, :miq_templates
alias_method :all_miq_template_ids, :miq_template_ids

has_many :host_hardwares, :class_name => 'Hardware', :through => :hosts, :source => :hardware
has_many :vm_hardwares, :class_name => 'Hardware', :through => :vms_and_templates, :source => :hardware

virtual_aggregate :aggregate_vm_memory, :vm_hardwares, :sum, :memory_mb
virtual_aggregate :aggregate_vm_cpus, :vm_hardwares, :sum, :cpu_sockets
virtual_aggregate :aggregate_physical_cpus, :host_hardwares, :sum, :cpu_sockets
virtual_aggregate :aggregate_disk_capacity, :host_hardwares, :sum, :disk_capacity
virtual_aggregate :aggregate_memory, :host_hardwares, :sum, :memory_mb
virtual_aggregate :aggregate_cpu_total_cores, :host_hardwares, :sum, :cpu_total_cores
virtual_aggregate :aggregate_cpu_speed, :host_hardwares, :sum, :aggregate_cpu_speed

has_many :failover_hosts, -> { failover }, :class_name => "Host"

delegate :queue_name_for_ems_operations, :to => :ext_management_system, :allow_nil => true
Expand Down
11 changes: 11 additions & 0 deletions app/models/ext_management_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,17 @@ def validate_zone_not_maintenance_when_ems_enabled?
self.default_relationship_type = "ems_metadata"

include AggregationMixin
# ExtManagementSystem.first.host_hardwares.size
has_many :host_hardwares, :class_name => 'Hardware', :through => :hosts, :source => :hardware
has_many :vm_hardwares, :class_name => 'Hardware', :through => :vms_and_templates, :source => :hardware

virtual_aggregate :aggregate_vm_memory, :vm_hardwares, :sum, :memory_mb
virtual_aggregate :aggregate_vm_cpus, :vm_hardwares, :sum, :cpu_sockets
virtual_aggregate :aggregate_physical_cpus, :host_hardwares, :sum, :cpu_sockets
virtual_aggregate :aggregate_disk_capacity, :host_hardwares, :sum, :disk_capacity
virtual_aggregate :aggregate_memory, :host_hardwares, :sum, :memory_mb
virtual_aggregate :aggregate_cpu_total_cores, :host_hardwares, :sum, :cpu_total_cores
virtual_aggregate :aggregate_cpu_speed, :host_hardwares, :sum, :aggregate_cpu_speed

include AuthenticationMixin
include Metric::CiMixin
Expand Down
11 changes: 11 additions & 0 deletions app/models/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ class Host < ApplicationRecord
virtual_total :v_total_vms, :vms
virtual_total :v_total_miq_templates, :miq_templates

has_many :host_hardwares, :class_name => 'Hardware', :source => :hardware
has_many :vm_hardwares, :class_name => 'Hardware', :through => :vms_and_templates, :source => :hardware

virtual_aggregate :aggregate_vm_memory, :vm_hardwares, :sum, :memory_mb
virtual_aggregate :aggregate_vm_cpus, :vm_hardwares, :sum, :cpu_sockets
virtual_aggregate :aggregate_physical_cpus, :host_hardwares, :sum, :cpu_sockets
virtual_aggregate :aggregate_disk_capacity, :host_hardwares, :sum, :disk_capacity
virtual_aggregate :aggregate_memory, :host_hardwares, :sum, :memory_mb
virtual_aggregate :aggregate_cpu_total_cores, :host_hardwares, :sum, :cpu_total_cores
virtual_aggregate :aggregate_cpu_speed, :host_hardwares, :sum, :aggregate_cpu_speed

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

Expand Down
2 changes: 2 additions & 0 deletions app/models/miq_region.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class MiqRegion < ApplicationRecord
virtual_has_many :miq_templates
virtual_has_many :vms

virtual_aggregate :aggregate_physical_cpus, :host_hardwares, :sum, :cpu_sockets

after_save :clear_my_region_cache

acts_as_miq_taggable
Expand Down
4 changes: 2 additions & 2 deletions app/models/miq_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -491,15 +491,15 @@ def self.managed_resources
{
:vms => Vm.active.count,
:hosts => Host.active.count,
:aggregate_physical_cpus => MiqRegion.my_region.aggregate_physical_cpus(Host.active),
:aggregate_physical_cpus => Host.active.first.aggregate_physical_cpus,
}
end

def self.unmanaged_resources
{
:vms => Vm.not_active.count,
:hosts => Host.archived.count,
:aggregate_physical_cpus => MiqRegion.my_region.aggregate_physical_cpus(Host.archived),
:aggregate_physical_cpus => Host.archived.first.aggregate_physical_cpus,
}
end

Expand Down
33 changes: 17 additions & 16 deletions app/models/mixins/aggregation_mixin.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
module AggregationMixin
extend ActiveSupport::Concern
included do
virtual_column :aggregate_cpu_speed, :type => :integer, :uses => :hosts
virtual_column :aggregate_cpu_total_cores, :type => :integer, :uses => :hosts
virtual_column :aggregate_physical_cpus, :type => :integer, :uses => :hosts
virtual_column :aggregate_memory, :type => :integer, :uses => :hosts
virtual_column :aggregate_vm_cpus, :type => :integer, :uses => :vms_and_templates
virtual_column :aggregate_vm_memory, :type => :integer, :uses => :vms_and_templates
virtual_column :aggregate_disk_capacity, :type => :integer, :uses => :hosts
def all_storages
MiqPreloader.preload(hosts, :storages)
hosts.collect(&:storages).flatten.compact.uniq
end

alias_method :all_hosts, :hosts
alias_method :all_host_ids, :host_ids
alias_method :all_vms_and_templates, :vms_and_templates
alias_method :all_vm_or_template_ids, :vm_or_template_ids
alias_method :all_vms, :vms
alias_method :all_vm_ids, :vm_ids
alias_method :all_miq_templates, :miq_templates
alias_method :all_miq_template_ids, :miq_template_ids
def aggregate_hardware(from, field, targets = nil)
from = from.to_s.singularize
select = field == :aggregate_cpu_speed ? "cpu_total_cores, cpu_speed" : field
targets ||= send("all_#{from}_ids")
targets = targets.collect(&:id) unless targets.first.kind_of?(Integer)
hdws = Hardware.where("#{from}_id".to_sym => targets).select(select)

include AggregationMixin::Methods
hdws.inject(0) { |t, hdw| t + hdw.send(field).to_i }
end

def lans
MiqPreloader.preload(hosts, :lans)
hosts.flat_map(&:lans).compact.uniq
end
end
end
57 changes: 0 additions & 57 deletions app/models/mixins/aggregation_mixin/methods.rb

This file was deleted.

2 changes: 0 additions & 2 deletions app/models/mixins/relationships_aggregation_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ module RelationshipsAggregationMixin
virtual_column :aggregate_vm_cpus, :type => :integer, :uses => :all_relationships
virtual_column :aggregate_vm_memory, :type => :integer, :uses => :all_relationships
virtual_column :aggregate_disk_capacity, :type => :integer, :uses => :all_relationships

include AggregationMixin::Methods
end

# Default implementations which can be overridden with something more optimized
Expand Down
59 changes: 50 additions & 9 deletions spec/models/ext_management_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,18 +353,59 @@
@ems = FactoryBot.create(:ems_vmware)
2.times do
FactoryBot.create(:vm_vmware,
:ext_management_system => @ems,
:hardware => FactoryBot.create(:hardware,
:cpu1x2,
:ram1GB))
:ext_management_system => @ems,
:hardware => FactoryBot.create(:hardware,
:cpu1x2,
:ram1GB))
end
2.times do
FactoryBot.create(:host,
:ext_management_system => @ems,
:hardware => FactoryBot.create(:hardware,
:cpu2x2,
:ram1GB))
:ext_management_system => @ems,
:hardware => FactoryBot.create(:hardware,
:cpu2x2,
:ram1GB))
end
2.times do
FactoryBot.create(:host,
:ext_management_system => @ems,
:hardware => FactoryBot.build(:hardware,
:cpu_total_cores => 4,
:cpu_speed => 1000))
end
2.times do
FactoryBot.create(:host,
:ext_management_system => @ems,
:hardware => FactoryBot.create(:hardware,
:disk_capacity => 300))
end
end

it "#aggregate_vm_memory" do
expect(@ems.aggregate_vm_memory).to eq(2048)
end

it "#aggregate_vm_cpus" do
expect(@ems.aggregate_vm_cpus).to eq(2)
end

it "#aggregate_physical_cpus" do
expect(@ems.aggregate_physical_cpus).to eq(8)
end

it "#aggregate_disk_capacity" do
expect(@ems.aggregate_disk_capacity).to eq(0.6e3)
end

it "#aggregate_memory" do
expect(@ems.aggregate_memory).to eq(2048)
end

it "#aggregate_cpu_total_cores" do
expect(@ems.aggregate_cpu_total_cores).to eq(16)
end

it "#aggregate_cpu_speed" do
expect(@ems.aggregate_cpu_speed).to eq(8000)
end

it "#total_cloud_vcpus" do
Expand All @@ -376,7 +417,7 @@
end

it "#total_vcpus" do
expect(@ems.total_vcpus).to eq(8)
expect(@ems.total_vcpus).to eq(16)
end

it "#total_memory" do
Expand Down
24 changes: 20 additions & 4 deletions spec/models/mixins/aggregation_mixin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
it "calculates from objects" do
cluster = cluster_2_1_host(hardware_args)
partial_cluster = cluster.hosts[1..1] + cluster.hosts[2..2]
expect(cluster.aggregate_cpu_speed(partial_cluster)).to eq(cpu_speed)

MiqPreloader.preload(partial_cluster, :aggregate_cpu_speed)
expect {
expect(partial_cluster.first.aggregate_cpu_speed).to eq(cpu_speed)
expect(partial_cluster.last.aggregate_cpu_speed).to eq(0)
}.to match_query_limit_of(0)
end

it "calculates from ids" do
Expand All @@ -42,7 +47,12 @@
it "calculates from objects" do
cluster = cluster_2_1_host(hardware_args)
partial_cluster = cluster.hosts[1..1] + cluster.hosts[2..2]
expect(cluster.aggregate_cpu_total_cores(partial_cluster)).to eq(8)

MiqPreloader.preload(partial_cluster, :aggregate_cpu_speed)
expect {
expect(partial_cluster.first.aggregate_cpu_total_cores).to eq(8)
expect(partial_cluster.last.aggregate_cpu_total_cores).to eq(0)
}.to match_query_limit_of(0)
end

it "calculates from ids" do
Expand All @@ -69,7 +79,12 @@
it "calculates from objects" do
cluster = cluster_2_1_host(hardware_args)
partial_cluster = cluster.hosts[1..1] + cluster.hosts[2..2]
expect(cluster.aggregate_memory(partial_cluster)).to eq(memory)

MiqPreloader.preload(partial_cluster, :aggregate_memory)
expect {
expect(partial_cluster.first.aggregate_memory).to eq(memory)
expect(partial_cluster.last.aggregate_memory).to eq(0)
}.to match_query_limit_of(0)
end

it "calculates from ids" do
Expand Down Expand Up @@ -102,7 +117,8 @@
it "calculates from vms" do
cluster = cluster_2_1_host(hardware_args)
partial_cluster = cluster.hosts[1..1] + cluster.hosts[2..2]
expect(cluster.aggregate_disk_capacity(partial_cluster)).to eq(40)
expect(partial_cluster.first.aggregate_disk_capacity).to eq(40)
expect(partial_cluster.last.aggregate_disk_capacity).to eq(0.0)
end

it "calculates from vm ids" do
Expand Down

0 comments on commit 3aeef98

Please sign in to comment.