diff --git a/app/models/mixins/aggregation_mixin/methods.rb b/app/models/mixins/aggregation_mixin/methods.rb index 866f2025ffc8..41395060f741 100644 --- a/app/models/mixins/aggregation_mixin/methods.rb +++ b/app/models/mixins/aggregation_mixin/methods.rb @@ -39,12 +39,11 @@ def all_storages end def aggregate_hardware(from, field, targets = nil) - from = from.to_s.singularize + from = from.to_s.singularize.to_sym 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) - + hdws = Hardware.where(from => targets).select(select) hdws.inject(0) { |t, hdw| t + hdw.send(field).to_i } end diff --git a/spec/models/mixins/aggregation_mixin_spec.rb b/spec/models/mixins/aggregation_mixin_spec.rb index d56125b8d69e..eed443d7cc87 100644 --- a/spec/models/mixins/aggregation_mixin_spec.rb +++ b/spec/models/mixins/aggregation_mixin_spec.rb @@ -112,6 +112,13 @@ end end + describe "aggregate_hardware" do + it "calculates from hosts" do + cluster = cluster_2_1_host(hardware_args) + expect(cluster.aggregate_hardware("host", :aggregate_cpu_speed)).to eq(cpu_speed * 2) + end + end + private def cluster_2_1_host(hardware_args)