Skip to content

Commit 6ae039d

Browse files
committed
feat(matrix): speed up matrix query for latestby=cvpv and latestby=cvp
1 parent 54c346f commit 6ae039d

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

lib/pact_broker/matrix/repository.rb

+22-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class Repository
1515

1616
# TODO move latest verification logic in to database
1717

18+
TP_COLS = PactBroker::Matrix::Row::TP_COLS
19+
1820
GROUP_BY_PROVIDER_VERSION_NUMBER = [:consumer_name, :consumer_version_number, :provider_name, :provider_version_number]
1921
GROUP_BY_PROVIDER = [:consumer_name, :consumer_version_number, :provider_name]
2022
GROUP_BY_PACT = [:consumer_name, :provider_name]
@@ -38,26 +40,41 @@ def find selectors, options = {}
3840
lines.sort
3941
end
4042

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

44+
45+
def apply_latestby options, selectors, lines
46+
return lines unless options[:latestby]
4447
group_by_columns = case options[:latestby]
48+
when 'cvpv' then GROUP_BY_PROVIDER_VERSION_NUMBER
4549
when 'cvp' then GROUP_BY_PROVIDER
4650
when 'cp' then GROUP_BY_PACT
4751
end
4852

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

61+
def remove_overwritten_revisions lines
62+
latest_revisions_keys = {}
63+
latest_revisions = []
64+
lines.each do | line |
65+
key = "#{line.consumer_name}-#{line.provider_name}-#{line.consumer_version_number}"
66+
if !latest_revisions_keys.key?(key) || latest_revisions_keys[key] == line.pact_revision_number
67+
latest_revisions << line
68+
latest_revisions_keys[key] ||= line.pact_revision_number
69+
end
70+
end
71+
latest_revisions
72+
end
73+
5774
def find_for_consumer_and_provider pacticipant_1_name, pacticipant_2_name
5875
selectors = [{ pacticipant_name: pacticipant_1_name }, { pacticipant_name: pacticipant_2_name }]
5976
options = { latestby: 'cvpv' }
60-
query_matrix(resolve_selectors(selectors, options), options).sort
77+
find(selectors, options)
6178
end
6279

6380
def find_compatible_pacticipant_versions selectors
@@ -75,7 +92,7 @@ def query_matrix selectors, options
7592
end
7693

7794
def view_for(options)
78-
options[:latestby] ? LatestRow : Row
95+
Row
7996
end
8097

8198
def resolve_selectors(selectors, options)

lib/pact_broker/matrix/row.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Matrix
77
class Row < Sequel::Model(:materialized_matrix)
88

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

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

0 commit comments

Comments
 (0)