summaryrefslogtreecommitdiff
path: root/app/finders/packages/build_infos_for_many_packages_finder.rb
blob: 8f9805f51d0a97f53e1d7f777307604117a27bbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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