Skip to content

Commit

Permalink
Merge pull request ManageIQ#22585 from kbrock/capture_vim_performance
Browse files Browse the repository at this point in the history
Keep archived out of VimPerformanceState
  • Loading branch information
Fryguy authored Aug 11, 2023
2 parents 3eeba62 + 3ddd7d6 commit 900fb1b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
35 changes: 22 additions & 13 deletions app/models/vim_performance_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,9 @@ def capture_total(field)
def capture_assoc_ids
result = {}
ASSOCIATIONS.each do |assoc|
method = if assoc == :vms
if resource.kind_of?(EmsCluster)
:all_vms_and_templates
elsif resource.kind_of?(Service)
:vms
else
:vms_and_templates
end
else
assoc
end
next unless resource.respond_to?(method)
assoc_recs = resource.send(method)
assoc_recs = fetch_assoc_records(resource, assoc)
next if assoc_recs == false

has_state = assoc_recs[0] && assoc_recs[0].respond_to?(:state)

r = result[assoc] = {:on => [], :off => []}
Expand All @@ -231,6 +221,25 @@ def capture_assoc_ids
self.assoc_ids = result.presence
end

def fetch_assoc_records(resource, assoc)
method = if assoc == :vms
if resource.kind_of?(EmsCluster)
:all_vms_and_templates
elsif resource.kind_of?(Service)
:vms
else
:vms_and_templates
end
else
assoc
end
return false unless resource.respond_to?(method)

records = resource.send(method)
records = records.not_archived_before(timestamp) if records.try(:klass).respond_to?(:not_archived_before)
records
end

def capture_parent_cluster
if resource.kind_of?(Host) || resource.kind_of?(VmOrTemplate)
self.parent_ems_cluster_id = resource.parent_cluster.try(:id)
Expand Down
39 changes: 39 additions & 0 deletions spec/models/vim_performance_state_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
RSpec.describe VimPerformanceState do
let(:capture_time) { Time.now.utc.beginning_of_hour }

describe ".capture" do
it "uses now" do
host = FactoryBot.create(:host)
Expand All @@ -9,6 +11,17 @@
end
end

describe "#capture_assoc_ids" do
it "captures running containers" do
container_image = FactoryBot.create(:container_image)
containers = create_past_present_future(:container, {:container_image => container_image})

state = VimPerformanceState.capture(container_image)
expect(state.get_assoc(:containers).map(&:to_i)).to match_array(containers.map(&:id))
expect(state.containers.map(&:id)).to match_array(containers.map(&:id))
end
end

describe "#containers" do
let(:capture_time) { 5.hours.ago.utc.beginning_of_hour }

Expand Down Expand Up @@ -122,6 +135,27 @@
end
end

describe "#fetch_assoc_records (private)" do
it "fetches infra record" do
host = FactoryBot.create(:host)
vms = FactoryBot.create_list(:vm, 1, :host => host)

results = fetch_records(host, :vms)
expect(results).to eq(vms)
end

it "fetches active container objects" do
container_image = FactoryBot.create(:container_image, :created_on => 6.hours.ago)
containers = create_past_present_future(:container, {:container_image => container_image})

results = fetch_records(container_image, :containers, capture_time)
expect(results).to eq(containers)

state = VimPerformanceState.capture(container_image)
expect(state.get_assoc(:containers).map(&:to_i)).to match_array(containers.map(&:id))
end
end

private

def create_past_present_future(factory, params)
Expand All @@ -144,4 +178,9 @@ def create_past_present_future_both(factory, params)
def deleted_record_ids(objs)
[objs.last.id + 1]
end

def fetch_records(rec, association, timestamp = Time.now.utc)
vps = VimPerformanceState.new(:timestamp => timestamp)
vps.send(:fetch_assoc_records, rec, association)
end
end

0 comments on commit 900fb1b

Please sign in to comment.