Skip to content

Commit

Permalink
Merge pull request #20146 from d-m-u/cleanup_the_hardware_query
Browse files Browse the repository at this point in the history
nix the string interpolation on the hardware query in the aggregation mixin

(cherry picked from commit 9be2b9b)
  • Loading branch information
kbrock authored and simaishi committed May 18, 2020
1 parent 3fa99f5 commit b7a4137
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/models/container_project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def perf_rollup_parents(interval_name = nil)

# required by aggregate_hardware
alias all_computer_system_ids computer_system_ids
alias all_computer_systems computer_systems

def aggregate_memory(targets = nil)
aggregate_hardware(:computer_systems, :memory_mb, targets)
Expand Down
6 changes: 2 additions & 4 deletions app/models/mixins/aggregation_mixin/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ def all_storages
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)

targets ||= send("all_#{from.pluralize}")
hdws = Hardware.where(from.singularize => targets).select(select)
hdws.inject(0) { |t, hdw| t + hdw.send(field).to_i }
end

Expand Down
7 changes: 7 additions & 0 deletions spec/models/mixins/aggregation_mixin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b7a4137

Please sign in to comment.