From 56c088de3d7246d42002d4e531af6dd98166f541 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Tue, 27 Nov 2018 16:30:01 -0500 Subject: [PATCH 1/2] EmsCluster#direct_vm_rels use sql to filter A. It was bringing back all descendants Now it is just bringing back the records at the second layer B. These records are staying as an AR scope longer So count will not bring back all records --- app/models/ems_cluster.rb | 2 +- app/models/host.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/ems_cluster.rb b/app/models/ems_cluster.rb index 683fcf4c50d..eef1f28d2a3 100644 --- a/app/models/ems_cluster.rb +++ b/app/models/ems_cluster.rb @@ -110,7 +110,7 @@ def total_vcpus # Direct Vm relationship methods def direct_vm_rels # Look for only the Vms at the second depth (default RP + 1) - descendant_rels(:of_type => 'VmOrTemplate').select { |r| (r.depth - depth) == 2 } + grandchild_rels(:of_type => 'VmOrTemplate') end def direct_vms diff --git a/app/models/host.rb b/app/models/host.rb index 03819fa0353..aba3b343d24 100644 --- a/app/models/host.rb +++ b/app/models/host.rb @@ -667,7 +667,7 @@ def disconnect_storage(s) # Vm relationship methods def direct_vms # Look for only the Vms at the second depth (default RP + 1) - rels = descendant_rels(:of_type => 'Vm').select { |r| (r.depth - depth) == 2 } + rels = grandchild_rels(:of_type => 'Vm') Relationship.resources(rels).sort_by { |r| r.name.downcase } end From e87aac66a4afbdb77f117b792bb00e1a98b718a3 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Wed, 9 Jan 2019 12:20:14 -0500 Subject: [PATCH 2/2] Introduce relationship#grandchildren --- app/models/host.rb | 3 +-- app/models/mixins/relationship_mixin.rb | 4 ++++ spec/models/mixins/relationship_mixin_spec.rb | 8 ++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/models/host.rb b/app/models/host.rb index aba3b343d24..0add9466228 100644 --- a/app/models/host.rb +++ b/app/models/host.rb @@ -667,8 +667,7 @@ def disconnect_storage(s) # Vm relationship methods def direct_vms # Look for only the Vms at the second depth (default RP + 1) - rels = grandchild_rels(:of_type => 'Vm') - Relationship.resources(rels).sort_by { |r| r.name.downcase } + grandchildren(:of_type => 'Vm').sort_by { |r| r.name.downcase } end # Resource Pool relationship methods diff --git a/app/models/mixins/relationship_mixin.rb b/app/models/mixins/relationship_mixin.rb index 9ee7dd91b98..a31e7957660 100644 --- a/app/models/mixins/relationship_mixin.rb +++ b/app/models/mixins/relationship_mixin.rb @@ -405,6 +405,10 @@ def grandchild_rels(*args) Relationship.filter_by_resource_type(rels, options) end + def grandchildren(*args) + Relationship.resources(grandchild_rels(*args)) + end + def child_and_grandchild_rels(*args) options = args.extract_options! rels = relationships.inject(Relationship.none) do |stmt, r| diff --git a/spec/models/mixins/relationship_mixin_spec.rb b/spec/models/mixins/relationship_mixin_spec.rb index f0d930a2ae8..5ce5d5c977f 100644 --- a/spec/models/mixins/relationship_mixin_spec.rb +++ b/spec/models/mixins/relationship_mixin_spec.rb @@ -848,6 +848,14 @@ end end + describe "#grandchildren" do + it "works with relationships" do + vms[0].with_relationship_type(test_rel_type) do + expect(vms[0].grandchildren).to match_array([vms[3], vms[4], vms[5], vms[6], vms[7]]) + end + end + end + describe "#child_and_grandchild_rels" do it "works with relationships" do vms[0].with_relationship_type(test_rel_type) do