Skip to content

Commit

Permalink
feat(matrix): speed up matrix query for latestby=cvpv and latestby=cvp
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Feb 6, 2018
1 parent 54c346f commit 6ae039d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
27 changes: 22 additions & 5 deletions lib/pact_broker/matrix/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Repository

# TODO move latest verification logic in to database

TP_COLS = PactBroker::Matrix::Row::TP_COLS

GROUP_BY_PROVIDER_VERSION_NUMBER = [:consumer_name, :consumer_version_number, :provider_name, :provider_version_number]
GROUP_BY_PROVIDER = [:consumer_name, :consumer_version_number, :provider_name]
GROUP_BY_PACT = [:consumer_name, :provider_name]
Expand All @@ -38,26 +40,41 @@ def find selectors, options = {}
lines.sort
end

def apply_latestby options, selectors, lines
return lines unless options[:latestby] == 'cvp' || options[:latestby] == 'cp'


def apply_latestby options, selectors, lines
return lines unless options[:latestby]
group_by_columns = case options[:latestby]
when 'cvpv' then GROUP_BY_PROVIDER_VERSION_NUMBER
when 'cvp' then GROUP_BY_PROVIDER
when 'cp' then GROUP_BY_PACT
end

# The group with the nil provider_version_numbers will be the results of the left outer join
# that don't have verifications, so we need to include them all.
lines.group_by{|line| group_by_columns.collect{|key| line.send(key) }}
remove_overwritten_revisions(lines).group_by{|line| group_by_columns.collect{|key| line.send(key) }}
.values
.collect{ | lines | lines.first.provider_version_number.nil? ? lines : lines.first }
.flatten
end

def remove_overwritten_revisions lines
latest_revisions_keys = {}
latest_revisions = []
lines.each do | line |
key = "#{line.consumer_name}-#{line.provider_name}-#{line.consumer_version_number}"
if !latest_revisions_keys.key?(key) || latest_revisions_keys[key] == line.pact_revision_number
latest_revisions << line
latest_revisions_keys[key] ||= line.pact_revision_number
end
end
latest_revisions
end

def find_for_consumer_and_provider pacticipant_1_name, pacticipant_2_name
selectors = [{ pacticipant_name: pacticipant_1_name }, { pacticipant_name: pacticipant_2_name }]
options = { latestby: 'cvpv' }
query_matrix(resolve_selectors(selectors, options), options).sort
find(selectors, options)
end

def find_compatible_pacticipant_versions selectors
Expand All @@ -75,7 +92,7 @@ def query_matrix selectors, options
end

def view_for(options)
options[:latestby] ? LatestRow : Row
Row
end

def resolve_selectors(selectors, options)
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/matrix/row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Matrix
class Row < Sequel::Model(:materialized_matrix)

# Used when using table_print to output query results
TP_COLS = [:consumer_id, :consumer_version_id, :provider_id, :provider_version_id]
TP_COLS = [ :consumer_version_number, :pact_revision_number, :provider_version_number, :verification_number]

associate(:one_to_many, :latest_triggered_webhooks, :class => "PactBroker::Webhooks::LatestTriggeredWebhook", primary_key: :pact_publication_id, key: :pact_publication_id)
associate(:one_to_many, :webhooks, :class => "PactBroker::Webhooks::Webhook", primary_key: [:consumer_id, :provider_id], key: [:consumer_id, :provider_id])
Expand Down

0 comments on commit 6ae039d

Please sign in to comment.