Class: Gitlab::Analytics::CycleAnalytics::Sorting

Inherits:
Object
  • Object
show all
Includes:
StageQueryHelpers
Defined in:
lib/gitlab/analytics/cycle_analytics/sorting.rb

Instance Method Summary collapse

Methods included from StageQueryHelpers

#duration, #end_event_timestamp_projection, #execute_query, #in_progress?, #order_by, #requires_grouping?, #round_duration_to_seconds, #zero_interval

Constructor Details

#initialize(stage:, query:, params: {}) ⇒ Sorting

Returns a new instance of Sorting.


9
10
11
12
13
# File 'lib/gitlab/analytics/cycle_analytics/sorting.rb', line 9

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

Instance Method Details

#apply(sort, direction) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gitlab/analytics/cycle_analytics/sorting.rb', line 16

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