Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 15 additions & 0 deletions app/models/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ class Service < ApplicationRecord
hide_attribute "display"

virtual_total :v_total_vms, :vms, :arel => aggregate_hardware_arel("v_total_vms", vms_tbl[:id].count, :skip_hardware => true)
virtual_total :v_total_direct_service_children, :direct_service_children,
:arel => lambda { |t|
child_tbl = Arel::Table.new(:services, :as => "direct_service_children_services")
id_cast = Arel::Nodes::NamedFunction.new("CAST", [t[:id].as("VARCHAR")])
child_anc = Arel::Nodes::Case.new
.when(t[:ancestry].eq(nil))
.then(id_cast)
.else(Arel::Nodes::Concat.new(
Arel::Nodes::Concat.new(t[:ancestry], Arel.sql("'/'")),
id_cast
))
subquery = child_tbl.project(Arel.star.count)
.where(child_tbl[:ancestry].eq(child_anc))
Arel::Nodes::NamedFunction.new("COALESCE", [t.grouping(subquery), Arel.sql("0")])
}

virtual_column :has_parent, :type => :boolean
virtual_column :power_state, :type => :string
Expand Down
26 changes: 26 additions & 0 deletions spec/models/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,32 @@
end
end

describe "#v_total_direct_service_children" do

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"direct" in our parlance means "directly under the service", so in the example tree below there are only 2 direct service children, c1, and c2. Are you looking for all service children (i.e. the entire tree?)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok yeah - spoke with @GilbertCherrie and that is what this is doing - the test didn't make that clear to me.

# create_deep_tree builds:
# @service
# ├── @service_c1
# │ ├── @service_c11
# │ └── @service_c12
# │ └── @service_c121
# └── @service_c2
before { create_deep_tree }

it "sql" do
[[@service, 2], [@service_c1, 2], [@service_c2, 0]].each do |svc, count|
actual = Service.select(:id, :v_total_direct_service_children).find_by(:id => svc.id)
expect(actual.v_total_direct_service_children).to eq(count)
expect(actual.attribute_present?(:v_total_direct_service_children)).to eq(true)
end
end

it "ruby" do
[[@service, 2], [@service_c1, 2], [@service_c2, 0]].each do |svc, count|
expect(svc.v_total_direct_service_children).to eq(count)
expect(svc.attribute_present?(:v_total_direct_service_children)).to eq(false)
end
end
end

describe "#indirect_service_children" do
it "returns 1 level down children" do
create_deep_tree
Expand Down
Loading