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

Keep archived out of VimPerformanceState #22585

Merged
merged 2 commits into from
Aug 11, 2023
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
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)
Copy link
Member Author

@kbrock kbrock Aug 10, 2023

Choose a reason for hiding this comment

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

this was a minor change to return something so the parent knows we need to next
(codeclimate this code to be its own method)


records = resource.send(method)
records = records.not_archived_before(timestamp) if records.try(:klass).respond_to?(:not_archived_before)
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the only actual changed line

Copy link
Member Author

Choose a reason for hiding this comment

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

Why do we have the try here?
-- @Fryguy

If the association is a virtual attribute, like it is for MiqRegion, then it will return an array that does not have a klass.

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