Skip to content

[WIP] Add include_for_find support via 'expand' #877

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

Closed
Closed
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
10 changes: 10 additions & 0 deletions app/controllers/api/base_controller/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ def sort_directive(klass, attr, order, options)
end
arel
end

def determine_include_for_find(klass)
return nil unless klass.respond_to?(:reflect_on_association)

relations = @req.derived_include_for_find.select do |relation|
klass.reflect_on_association(relation)
end

relations.empty? ? nil : relations
end
end
end
end
1 change: 1 addition & 0 deletions app/controllers/api/base_controller/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def collection_filterer(res, type, klass, is_subcollection = false)
options[:filter] = miq_expression if miq_expression
options[:offset] = params['offset'] if params['offset']
options[:limit] = params['limit'] if params['limit']
options[:include_for_find] = determine_include_for_find(klass)
Copy link
Member

Choose a reason for hiding this comment

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

😍


filter_results(miq_expression, res, options)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/api/request_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def attributes
@attributes ||= @params['attributes'].to_s.split(',')
end

def derived_include_for_find
@derived_include_for_find ||= expand_requested.reject { |item| item == "resource" }
end

def base
url.partition(fullpath)[0] # http://target
end
Expand Down
5 changes: 5 additions & 0 deletions spec/requests/vms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ def update_raw_power_state(state, *vms)
[{"vendor" => "openstack"},
{"vendor" => "openstack"}])
end

include_examples "for 'expand' query optimizations" do
let!(:includes) { "hardware" }
let!(:attributes) { "num_cpu,name" }
end
end

context 'Vm edit' do
Expand Down
29 changes: 29 additions & 0 deletions spec/support/shared_examples/expand_resources_includes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
RSpec.shared_context "for 'expand' query optimizations" do
context "expand attribute query optimizations" do
let(:resource) { :vms }
let(:index_url) { api_vms_url }

it "removes N+1's from the index query for subcollections/virtual_attributes" do
api_basic_authorize action_identifier(resource, :read, :resource_actions, :get)

expands = ["resources"]
from_match = [resource]

if defined?(includes)
expands << includes
from_match << includes
end

attrs = defined?(attributes) ? attributes : "resources"
query_match = /SELECT.*FROM\s"(?:#{from_match.join("|")})"/m
expected_query_count = defined?(query_count) ? query_count : 10

expect {
get index_url, :params => {
:expand => expands.join(','),
:attributes => attrs
}
}.to make_database_queries(:count => expected_query_count, :matching => query_match)
end
end
end