Skip to content

Commit

Permalink
Merge pull request ManageIQ#16476 from NickLaMuro/services_v_total_vm…
Browse files Browse the repository at this point in the history
…s_arel_model_changes

Improves v_total_vms
  • Loading branch information
Fryguy authored Jan 23, 2018
2 parents 9915f79 + fcb71ff commit c492073
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 3 additions & 1 deletion app/models/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class Service < ApplicationRecord
virtual_has_many :power_states, :uses => :all_vms
virtual_has_many :orchestration_stacks
virtual_has_many :generic_objects
virtual_total :v_total_vms, :vms

virtual_has_one :custom_actions
virtual_has_one :custom_action_buttons
Expand Down Expand Up @@ -70,6 +69,9 @@ class Service < ApplicationRecord
include_concern 'Aggregation'
include_concern 'ResourceLinking'

virtual_total :v_total_vms, :vms,
:arel => aggregate_hardware_arel("v_total_vms", vms_tbl[:id].count, :skip_hardware => true)

virtual_column :has_parent, :type => :boolean
virtual_column :power_state, :type => :string
virtual_column :power_status, :type => :string
Expand Down
7 changes: 5 additions & 2 deletions app/models/service/aggregation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,11 @@ def aggregation_where_clause(arel, subtree_services)
def base_service_aggregation_join(arel, services_tbl, options = {})
arel.join(service_resources_tbl).on(service_resources_tbl[:service_id].eq(services_tbl[:id])
.and(service_resources_tbl[:resource_type].eq(vm_or_template_type)))
.join(vms_tbl).on(vm_join_clause(options))
.join(hardwares_tbl).on(hardwares_tbl[:vm_or_template_id].eq(vms_tbl[:id]))
.join(vms_tbl).on(vm_join_clause(options)).tap do |arel_query|
unless options[:skip_hardware]
arel_query.join(hardwares_tbl).on(hardwares_tbl[:vm_or_template_id].eq(vms_tbl[:id]))
end
end
end

# Generates the following SQL conditional to be used in
Expand Down
13 changes: 13 additions & 0 deletions spec/models/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@
expect(@service.all_vms).to match_array [@vm, @vm1, @vm1, @vm2]
end

it "#v_total_vms" do
expect(@service.v_total_vms).to eq 4
expect(@service.attribute_present?(:v_total_vms)).to eq false
end

it "#v_total_vms with arel" do
service = Service.select(:id, :v_total_vms)
.where(:id => @service.id)
.first
expect(service.v_total_vms).to eq 4
expect(service.attribute_present?(:v_total_vms)).to eq true
end

it "#direct_service" do
expect(@vm.direct_service).to eq(@service)
expect(@vm1.direct_service).to eq(@service_c1)
Expand Down

0 comments on commit c492073

Please sign in to comment.