diff options
Diffstat (limited to 'app/finders/packages')
-rw-r--r-- | app/finders/packages/build_infos_for_many_packages_finder.rb | 92 | ||||
-rw-r--r-- | app/finders/packages/group_packages_finder.rb | 6 | ||||
-rw-r--r-- | app/finders/packages/packages_finder.rb | 6 |
3 files changed, 102 insertions, 2 deletions
diff --git a/app/finders/packages/build_infos_for_many_packages_finder.rb b/app/finders/packages/build_infos_for_many_packages_finder.rb new file mode 100644 index 00000000000..8f9805f51d0 --- /dev/null +++ b/app/finders/packages/build_infos_for_many_packages_finder.rb @@ -0,0 +1,92 @@ +# frozen_string_literal: true + +module Packages + # TODO rename to BuildInfosFinder when cleaning up packages_graphql_pipelines_resolver + # https://gitlab.com/gitlab-org/gitlab/-/issues/358432 + class BuildInfosForManyPackagesFinder + include ActiveRecord::ConnectionAdapters::Quoting + + MAX_PAGE_SIZE = 100 + + def initialize(package_ids, params) + @package_ids = package_ids + @params = params + end + + def execute + return Packages::BuildInfo.none if @package_ids.blank? + + # This is a highly custom query that + # will not be re-used elsewhere + # rubocop: disable CodeReuse/ActiveRecord + query = Packages::Package.id_in(@package_ids) + .select('build_infos.*') + .from([Packages::Package.arel_table, lateral_query.arel.lateral.as('build_infos')]) + .order('build_infos.id DESC') + + # We manually select build_infos fields from the lateral query. + # Thus, we need to instruct ActiveRecord that returned rows are + # actually Packages::BuildInfo objects + Packages::BuildInfo.find_by_sql(query.to_sql) + # rubocop: enable CodeReuse/ActiveRecord + end + + private + + def lateral_query + order_direction = last ? :asc : :desc + + # This is a highly custom query that + # will not be re-used elsewhere + # rubocop: disable CodeReuse/ActiveRecord + where_condition = Packages::BuildInfo.arel_table[:package_id] + .eq(Arel.sql("#{Packages::Package.table_name}.id")) + build_infos = ::Packages::BuildInfo.without_empty_pipelines + .where(where_condition) + .order(id: order_direction) + .limit(max_rows_per_package_id) + # rubocop: enable CodeReuse/ActiveRecord + apply_cursor(build_infos) + end + + def max_rows_per_package_id + limit = [first, last, max_page_size, MAX_PAGE_SIZE].compact.min + limit += 1 if support_next_page + limit + end + + def apply_cursor(build_infos) + if before + build_infos.with_pipeline_id_greater_than(before) + elsif after + build_infos.with_pipeline_id_less_than(after) + else + build_infos + end + end + + def first + @params[:first] + end + + def last + @params[:last] + end + + def max_page_size + @params[:max_page_size] + end + + def before + @params[:before] + end + + def after + @params[:after] + end + + def support_next_page + @params[:support_next_page] + end + end +end diff --git a/app/finders/packages/group_packages_finder.rb b/app/finders/packages/group_packages_finder.rb index 23b0e71d836..1d1ae59674a 100644 --- a/app/finders/packages/group_packages_finder.rb +++ b/app/finders/packages/group_packages_finder.rb @@ -22,11 +22,11 @@ module Packages def packages_for_group_projects(installable_only: false) packages = ::Packages::Package - .preload_pipelines .including_project_route .including_tags .for_projects(group_projects_visible_to_current_user.select(:id)) .sort_by_attribute("#{params[:order_by]}_#{params[:sort]}") + packages = packages.preload_pipelines if preload_pipelines packages = filter_with_version(packages) packages = filter_by_package_type(packages) @@ -59,5 +59,9 @@ module Packages def exclude_subgroups? params[:exclude_subgroups] end + + def preload_pipelines + params.fetch(:preload_pipelines, true) + end end end diff --git a/app/finders/packages/packages_finder.rb b/app/finders/packages/packages_finder.rb index 3bc348c8dc8..b3d14e15953 100644 --- a/app/finders/packages/packages_finder.rb +++ b/app/finders/packages/packages_finder.rb @@ -14,9 +14,9 @@ module Packages def execute packages = project.packages - .preload_pipelines .including_project_route .including_tags + packages = packages.preload_pipelines if preload_pipelines packages = filter_with_version(packages) packages = filter_by_package_type(packages) @@ -32,5 +32,9 @@ module Packages def order_packages(packages) packages.sort_by_attribute("#{params[:order_by]}_#{params[:sort]}") end + + def preload_pipelines + params.fetch(:preload_pipelines, true) + end end end |