Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sorting.rb « cycle_analytics « analytics « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c399bac423b24dc6c0844c191341f01b1a4556f2 (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
# frozen_string_literal: true

module Gitlab
  module Analytics
    module CycleAnalytics
      class Sorting
        include StageQueryHelpers

        def initialize(stage:, query:, params: {})
          @stage = stage
          @query = query
          @params = params
        end

        # rubocop: disable CodeReuse/ActiveRecord
        def apply(sort, direction)
          sorting_options = {
            end_event: {
              asc: -> { query.reorder(end_event_timestamp_projection.asc) },
              desc: -> { query.reorder(end_event_timestamp_projection.desc) }
            },
            duration: {
              asc: -> { query.reorder(duration.asc) },
              desc: -> { query.reorder(duration.desc) }
            }
          }

          sort_lambda = sorting_options.dig(sort, direction) || sorting_options.dig(:end_event, :desc)
          sort_lambda.call
        end
        # rubocop: enable CodeReuse/ActiveRecord

        private

        attr_reader :stage, :query, :params
      end
    end
  end
end