diff --git a/lib/pact_broker/matrix/matrix_row_dataset_module.rb b/lib/pact_broker/matrix/matrix_row_dataset_module.rb index 1983ef777..fd9b7946b 100644 --- a/lib/pact_broker/matrix/matrix_row_dataset_module.rb +++ b/lib/pact_broker/matrix/matrix_row_dataset_module.rb @@ -12,11 +12,11 @@ module MatrixRowDatasetModule # The matrix query used to determine the final dataset # @param [Array] 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 @@ -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) @@ -86,8 +86,8 @@ def matching_one_selector_for_either_consumer_or_provider(resolved_selectors) # @private # @param [Array] resolved_selectors # @return [Sequel::Dataset] - 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 @@ -103,10 +103,10 @@ def matching_only_selectors_joining_verifications(resolved_selectors) # @private # @param [Array] resolved_selectors # @return [Sequel::Dataset] - 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 @@ -117,7 +117,7 @@ def matching_only_selectors_as_consumer(resolved_selectors) # @private # @param [Array] resolved_selectors # @return [Sequel::Dataset, 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 @@ -125,6 +125,7 @@ def matching_only_selectors_as_consumer_where_only_pacticipant_name_in_selector( 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 @@ -136,7 +137,7 @@ def matching_only_selectors_as_consumer_where_only_pacticipant_name_in_selector( # @private # @param [Array] resolved_selectors # @return [Sequel::Dataset, 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?) @@ -144,6 +145,7 @@ def matching_only_selectors_as_consumer_where_not_only_pacticipant_name_in_selec 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 @@ -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 diff --git a/lib/pact_broker/matrix/repository.rb b/lib/pact_broker/matrix/repository.rb index a2ddadf37..603a9be96 100644 --- a/lib/pact_broker/matrix/repository.rb +++ b/lib/pact_broker/matrix/repository.rb @@ -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] diff --git a/spec/lib/pact_broker/matrix/every_row_spec.rb b/spec/lib/pact_broker/matrix/every_row_spec.rb index 05bf9a33b..825935a5b 100644 --- a/spec/lib/pact_broker/matrix/every_row_spec.rb +++ b/spec/lib/pact_broker/matrix/every_row_spec.rb @@ -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? } }