Skip to content

Commit

Permalink
refactor: improve speed of index page rendering with tags
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jan 22, 2018
1 parent 3f13d2b commit 2b788b1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
21 changes: 21 additions & 0 deletions db/migrations/20180122_create_head_pact_publications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Sequel.migration do
change do

# This view includes all the latest pacts, as well as the latest pacts
# for each tag.
# If a pact publication is the latest AND the latest tagged version
# there will be two rows in this view for it - one for the top
# query,and one for the bottom.
create_view(:head_pact_publications,
"select lp.*, null as tag_name, 1 as latest
from latest_pact_publications lp
UNION
select ltp.*, null as latest
from latest_tagged_pact_publications ltp
"
)

end
end
23 changes: 17 additions & 6 deletions lib/pact_broker/index/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class Service
extend PactBroker::Logging

def self.find_index_items options = {}
rows = []

if !options[:tags]
rows = PactBroker::Matrix::LatestRow
.select_all_qualified
.join(:latest_pact_publications, {consumer_id: :consumer_id, provider_id: :provider_id, consumer_version_order: :consumer_version_order})
Expand All @@ -21,21 +24,29 @@ def self.find_index_items options = {}
.order(:consumer_name, :provider_name)
.eager(:consumer_version_tags)
.all
end

if options[:tags]
tagged_rows = PactBroker::Matrix::LatestRow
tagged_rows = PactBroker::Matrix::Row
.select_all_qualified
.select_append(:tag_name)
.join(:head_pact_publications, {id: :pact_publication_id})
.eager(:latest_triggered_webhooks)
.eager(:webhooks)
.order(:consumer_name, :provider_name)
.eager(:consumer_version_tags)
tagged_rows = tagged_rows.join(:latest_tagged_pact_consumer_version_orders, {consumer_id: :consumer_id, provider_id: :provider_id, latest_consumer_version_order: :consumer_version_order})

if options[:tags].is_a?(Array)
tagged_rows = tagged_rows.where(Sequel[:latest_tagged_pact_consumer_version_orders][:tag_name] => options[:tags])
end
if options[:tags].is_a?(Array)
tagged_rows = tagged_rows.where(Sequel[:head_pact_publications][:tag_name] => options[:tags]).or(Sequel[:head_pact_publications][:tag_name] => nil)
end

tagged_rows = tagged_rows.all
.group_by(&:pact_publication_id)
.values
.collect{|group| [group.first, group.collect{|r| r[:tag_name]}.compact] }
.collect{ |(row, tag_names)| row.consumer_head_tag_names = tag_names; row }

rows = (rows + tagged_rows.all).group_by(&:pact_publication_id).values.collect(&:last)
rows = tagged_rows
end

index_items = []
Expand Down
15 changes: 13 additions & 2 deletions lib/pact_broker/matrix/row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ def order_by_names_ascending_most_recent_first
end
end

# tags for which this pact publication is the latest of that tag
# this is set in the code rather than the database
def consumer_head_tag_names
@consumer_head_tag_names ||= []
end

def consumer_head_tag_names= consumer_head_tag_names
@consumer_head_tag_names = consumer_head_tag_names
end

def latest_triggered_webhooks
@latest_triggered_webhooks ||= []
end
Expand All @@ -76,12 +86,12 @@ def provider
end

def consumer_version
@consumer_version ||= OpenStruct.new(number: consumer_version_number, id: consumer_version_id)
@consumer_version ||= OpenStruct.new(number: consumer_version_number, id: consumer_version_id, pacticipant: consumer)
end

def provider_version
if provider_version_number
@provider_version ||= OpenStruct.new(number: provider_version_number, id: provider_version_id)
@provider_version ||= OpenStruct.new(number: provider_version_number, id: provider_version_id, pacticipant: provider)
end
end

Expand All @@ -103,6 +113,7 @@ def latest_verification
success: success,
number: verification_number,
execution_date: verification_executed_at,
created_at: verification_executed_at,
provider_version_number: provider_version_number,
build_url: verification_build_url,
provider_version: provider_version
Expand Down

0 comments on commit 2b788b1

Please sign in to comment.