Skip to content

Commit

Permalink
[ISSUE-318] Filter ids first before running the big index query
Browse files Browse the repository at this point in the history
  • Loading branch information
bangn committed Jun 17, 2021
1 parent c5b5338 commit 6a7345d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
34 changes: 22 additions & 12 deletions lib/pact_broker/index/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,11 @@ def self.db
def self.head_pact_publications(options = {})
base = base_query(options)

ids_query = pact_publication_ids_query(base.overall_latest, options[:tags])
if options[:pacticipant_name]
base = base.where(Sequel[:pact_publications][:id] => pact_pacticipant_ids_by_name(options[:pacticipant_name]))
end

ids_query = query_pact_publication_ids_by_tags(base, options[:tags])
query = PactBroker::Pacts::PactPublication
.select_all_qualified
.where(Sequel[:pact_publications][:id] => ids_query)
Expand All @@ -156,8 +159,6 @@ def self.head_pact_publications(options = {})
{ table_alias: :cv }
)

query = query.where(pacticipant_name_query(options[:pacticipant_name])) if options[:pacticipant_name]

order_columns = [
Sequel.asc(Sequel.function(:lower, Sequel[:consumers][:name])),
Sequel.desc(Sequel[:cv][:order]),
Expand Down Expand Up @@ -185,6 +186,14 @@ def self.latest_verifications_for_consumer_version_tags(options)
end
end

def self.query_pact_publication_ids_by_tags(base, tags)
latest = base.overall_latest
return latest.union(base.latest_for_consumer_tag(tags)) if tags.is_a?(Array)
return latest.union(base.latest_by_consumer_tag) if tags

latest
end

def self.base_query(options)
query = PactBroker::Pacts::PactPublication.select(Sequel[:pact_publications][:id])

Expand All @@ -201,22 +210,23 @@ def self.base_query(options)
query
end

def self.pact_publication_ids_query(query, tags)
return query.union(query.latest_for_consumer_tag(tags)) if tags.is_a?(Array)
return query.union(query.latest_by_consumer_tag) if tags

query
end

def self.pacticipant_name_query(pacticipant_name)
def self.pact_pacticipant_ids_by_name(pacticipant_name)
terms = pacticipant_name.split.map { |v| v.gsub("_", '\\_') }
Sequel.|(
string_match_query = Sequel.|(
Sequel.|( *terms.map { |term| Sequel.ilike(Sequel[:providers][:name], "%#{term}%") }),
Sequel.|( *terms.map { |term| Sequel.ilike(Sequel[:consumers][:name], "%#{term}%") })
)

PactBroker::Pacts::PactPublication
.join_consumers(:consumers)
.join_providers(:providers)
.where(string_match_query)
.select(Sequel[:pact_publications][:id])
.map(:id)
end

private_class_method :base_query, :pacticipant_name_query, :pact_publication_ids_query
private_class_method :base_query, :query_pact_publication_ids_by_tags, :pact_pacticipant_ids_by_name
end
end
end
15 changes: 1 addition & 14 deletions lib/pact_broker/ui/views/index/show-with-tags.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,10 @@
!= render :haml, :'index/_css_and_js', :layout => false
.container
!= render :haml, :'index/_navbar', :layout => false, locals: {tag_toggle: false, base_url: base_url}
- if index_items.empty? && pacticipant_name.blank?
- if index_items.empty?
!= render :haml, :'index/_getting-started', :layout => false
%h1.page-header
Pacts

- unless errors.blank?
- errors.each do | error |
%div.alert.alert-danger
= error

%form{action: "#{base_url}", onsubmit:'return onSubmit()'}
.field
%label{for: 'pacticipant_name'}
Pacticipant name
%input{name: 'pacticipant_name', id: 'pacticipant_name', class: 'pacticipant_name'}
%div.top-of-group
%input{type: 'submit', value: 'Search'}
%table.table.table-bordered.table-striped{ id: 'relationships' }
%thead
%tr
Expand Down
15 changes: 7 additions & 8 deletions spec/lib/pact_broker/ui/controllers/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,30 +97,29 @@ module Controllers
context "when parameter pacticipant_name presents" do
context "when it is blank" do
it "ignores it" do
expect(PactBroker::Index::Service).not_to receive(:filter_item_by_pacticipant_name)
get "/", { pacticipant_name_name: "" }

expect(last_response.body).to include("Example App")
expect(last_response.status).to eq(200)
end
end

context "when it is NOT blank and the pacticipant name exist" do
it "filters out the pacticipant which name does not match the pacticipant name" do
expect(PactBroker::Index::Service).to receive(:filter_item_by_pacticipant_name).and_call_original
it "returns the pacticipant which matches the query" do
get "/", { pacticipant_name: "example app" }

expect(last_response.body).to include("Example App")
expect(last_response.body).not_to include("Test App")
expect(last_response.status).to eq(200)
end
end

context "when it is NOT blank but the pacticipant name does NOT exist" do
it "returns all the pact items" do
expect(PactBroker::Index::Service).to receive(:filter_item_by_pacticipant_name).and_call_original
it "returns no pacts" do
get "/", { pacticipant_name: "does not exist" }

expect(last_response.body).not_to include("does not exit")
expect(last_response.body).to include("Example App")
expect(last_response.body).to include("Test App")
expect(last_response.body).not_to include("Example App")
expect(last_response.body).not_to include("Test App")
expect(last_response.status).to eq(200)
end
end
Expand Down

0 comments on commit 6a7345d

Please sign in to comment.