Skip to content

Commit

Permalink
feat: optimise matrix by applying specified limit to pact publication…
Browse files Browse the repository at this point in the history
…s before joining to verifications
  • Loading branch information
bethesque committed Sep 4, 2023
1 parent b98f5d1 commit c61c324
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
32 changes: 19 additions & 13 deletions lib/pact_broker/matrix/matrix_row_dataset_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ module MatrixRowDatasetModule

# The matrix query used to determine the final dataset
# @param [Array<PactBroker::Matrix::ResolvedSelector>] resolved_selectors
def matching_selectors(resolved_selectors)
def matching_selectors(resolved_selectors, limit:)
if resolved_selectors.size == 1
matching_one_selector_for_either_consumer_or_provider(resolved_selectors)
matching_one_selector_for_either_consumer_or_provider(resolved_selectors, limit: limit)
else
matching_only_selectors_joining_verifications(resolved_selectors)
matching_only_selectors_joining_verifications(resolved_selectors, limit: limit)
end
end

Expand Down Expand Up @@ -55,18 +55,18 @@ def default_scope
# When we have one selector, we need to join ALL the verifications to find out
# what integrations exist
# @private
def matching_one_selector_for_either_consumer_or_provider(resolved_selectors)
def matching_one_selector_for_either_consumer_or_provider(resolved_selectors, limit: )
if resolved_selectors.size != 1
raise ArgumentError.new("Expected one selector to be provided, but received #{resolved_selectors.size}: #{resolved_selectors}")
end

# consumer
pact_publication_matching_consumer = select_pact_columns_with_aliases.from_self(alias: :p).inner_join_versions_for_selectors_as_consumer(resolved_selectors)
pact_publication_matching_consumer = select_pact_columns_with_aliases.most_recent(limit).from_self(alias: :p).inner_join_versions_for_selectors_as_consumer(resolved_selectors)
rows_where_selector_matches_consumer = pact_publication_matching_consumer.left_outer_join_verifications.select_all_columns_after_join

# provider
verifications_matching_provider = verification_dataset.matching_selectors_as_provider_for_any_consumer(resolved_selectors)
rows_where_selector_matches_provider = select_pact_columns_with_aliases.from_self(alias: :p).inner_join_verifications_dataset(verifications_matching_provider).select_all_columns_after_join
rows_where_selector_matches_provider = select_pact_columns_with_aliases.most_recent(limit).from_self(alias: :p).inner_join_verifications_dataset(verifications_matching_provider).select_all_columns_after_join

# union
rows_where_selector_matches_consumer.union(rows_where_selector_matches_provider)
Expand All @@ -86,8 +86,8 @@ def matching_one_selector_for_either_consumer_or_provider(resolved_selectors)
# @private
# @param [Array<PactBroker::Matrix::ResolvedSelector>] resolved_selectors
# @return [Sequel::Dataset<MatrixRow>]
def matching_only_selectors_joining_verifications(resolved_selectors)
pact_publications = matching_only_selectors_as_consumer(resolved_selectors)
def matching_only_selectors_joining_verifications(resolved_selectors, limit: )
pact_publications = matching_only_selectors_as_consumer(resolved_selectors, limit: limit)
verifications = verification_dataset.matching_only_selectors_as_provider(resolved_selectors)

specified_pacticipant_ids = resolved_selectors.select(&:specified?).collect(&:pacticipant_id).uniq
Expand All @@ -103,10 +103,10 @@ def matching_only_selectors_joining_verifications(resolved_selectors)
# @private
# @param [Array<PactBroker::Matrix::ResolvedSelector>] resolved_selectors
# @return [Sequel::Dataset<MatrixRow>]
def matching_only_selectors_as_consumer(resolved_selectors)
def matching_only_selectors_as_consumer(resolved_selectors, limit: )
[
matching_only_selectors_as_consumer_where_only_pacticipant_name_in_selector(resolved_selectors),
matching_only_selectors_as_consumer_where_not_only_pacticipant_name_in_selector(resolved_selectors),
matching_only_selectors_as_consumer_where_only_pacticipant_name_in_selector(resolved_selectors, limit: limit),
matching_only_selectors_as_consumer_where_not_only_pacticipant_name_in_selector(resolved_selectors, limit: limit),
].compact.reduce(&:union)
end

Expand All @@ -117,14 +117,15 @@ def matching_only_selectors_as_consumer(resolved_selectors)
# @private
# @param [Array<PactBroker::Matrix::ResolvedSelector>] resolved_selectors
# @return [Sequel::Dataset<MatrixRow>, nil]
def matching_only_selectors_as_consumer_where_only_pacticipant_name_in_selector(resolved_selectors)
def matching_only_selectors_as_consumer_where_only_pacticipant_name_in_selector(resolved_selectors, limit:)
all_pacticipant_ids = resolved_selectors.collect(&:pacticipant_id).uniq
pacticipant_ids_for_pacticipant_only_selectors = resolved_selectors.select(&:only_pacticipant_name_specified?).collect(&:pacticipant_id).uniq

if pacticipant_ids_for_pacticipant_only_selectors.any?
select_pact_columns_with_aliases
.where(consumer_id: pacticipant_ids_for_pacticipant_only_selectors)
.where(provider_id: all_pacticipant_ids)
.most_recent(limit)
end
end

Expand All @@ -136,14 +137,15 @@ def matching_only_selectors_as_consumer_where_only_pacticipant_name_in_selector(
# @private
# @param [Array<PactBroker::Matrix::ResolvedSelector>] resolved_selectors
# @return [Sequel::Dataset<MatrixRow>, nil]
def matching_only_selectors_as_consumer_where_not_only_pacticipant_name_in_selector(resolved_selectors)
def matching_only_selectors_as_consumer_where_not_only_pacticipant_name_in_selector(resolved_selectors, limit:)
all_pacticipant_ids = resolved_selectors.collect(&:pacticipant_id).uniq
resolved_selectors_with_versions_specified = resolved_selectors.reject(&:only_pacticipant_name_specified?)

if resolved_selectors_with_versions_specified.any?
select_pact_columns_with_aliases
.inner_join_versions_for_selectors_as_consumer(resolved_selectors_with_versions_specified)
.where(provider_id: all_pacticipant_ids)
.most_recent(limit)
end
end

Expand Down Expand Up @@ -174,6 +176,10 @@ def left_outer_join_verifications_dataset(verifications)
def inner_join_verifications_dataset(verifications_dataset)
join(verifications_dataset, { Sequel[:p][:pact_version_id] => Sequel[:v][:pact_version_id] }, { table_alias: :v } )
end

def most_recent(limit)
order(Sequel.desc(:created_at)).limit(limit)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/pact_broker/matrix/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def apply_latestby(options, lines)

def query_matrix(all_resolved_selectors, options)
query = base_model(options)
.matching_selectors(all_resolved_selectors)
.matching_selectors(all_resolved_selectors, limit: options[:limit])
.order_by_last_action_date

query = query.limit(options[:limit]) if options[:limit]
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact_broker/matrix/every_row_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module Matrix

let(:selectors) { [selector_1, selector_2] }

subject { EveryRow.matching_selectors(selectors).all }
subject { EveryRow.matching_selectors(selectors, limit: 100).all }

let(:un_verified_row) { subject.find{ |r| r.provider_id == bar.id && !r.has_verification? } }
let(:verified_row) { subject.find{ |r| r.provider_id == bar.id && r.has_verification? } }
Expand Down

0 comments on commit c61c324

Please sign in to comment.