Module: Gitlab::Analytics::CycleAnalytics::StageQueryHelpers

Included in:
Aggregated::RecordsFetcher, Average, BaseQueryBuilder, Median, RecordsFetcher, Sorting
Defined in:
lib/gitlab/analytics/cycle_analytics/stage_query_helpers.rb

Instance Method Summary collapse

Instance Method Details

#durationObject


19
20
21
22
23
24
# File 'lib/gitlab/analytics/cycle_analytics/stage_query_helpers.rb', line 19

def duration
  Arel::Nodes::Subtraction.new(
    end_event_timestamp_projection,
    stage.start_event.timestamp_projection
  )
end

#end_event_timestamp_projectionObject


26
27
28
29
30
31
32
# File 'lib/gitlab/analytics/cycle_analytics/stage_query_helpers.rb', line 26

def end_event_timestamp_projection
  if in_progress?
    Arel::Nodes::NamedFunction.new('TO_TIMESTAMP', [Time.current.to_i])
  else
    stage.end_event.timestamp_projection
  end
end

#execute_query(query) ⇒ Object


7
8
9
# File 'lib/gitlab/analytics/cycle_analytics/stage_query_helpers.rb', line 7

def execute_query(query)
  ApplicationRecord.connection.execute(query.to_sql)
end

#in_progress?Boolean

Returns:

  • (Boolean)

57
58
59
# File 'lib/gitlab/analytics/cycle_analytics/stage_query_helpers.rb', line 57

def in_progress?
  params[:end_event_filter] == :in_progress
end

#order_by(query, sort, direction, extra_columns_to_select = [:id]) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gitlab/analytics/cycle_analytics/stage_query_helpers.rb', line 35

def order_by(query, sort, direction, extra_columns_to_select = [:id])
  ordered_query = Gitlab::Analytics::CycleAnalytics::Sorting.new(stage: stage, query: query, params: params).apply(sort, direction)

  # When filtering for more than one label, postgres requires the columns in ORDER BY to be present in the GROUP BY clause
  if requires_grouping?
    column_list = [].tap do |array|
      array.concat(extra_columns_to_select)
      array.concat(stage.end_event.column_list) unless in_progress?
      array.concat(stage.start_event.column_list)
    end

    ordered_query = ordered_query.group(column_list)
  end

  ordered_query
end

#requires_grouping?Boolean

rubocop: enable CodeReuse/ActiveRecord

Returns:

  • (Boolean)

53
54
55
# File 'lib/gitlab/analytics/cycle_analytics/stage_query_helpers.rb', line 53

def requires_grouping?
  Array(params[:label_name]).size > 1
end

#round_duration_to_secondsObject


15
16
17
# File 'lib/gitlab/analytics/cycle_analytics/stage_query_helpers.rb', line 15

def round_duration_to_seconds
  Arel::Nodes::NamedFunction.new('ROUND', [Arel::Nodes::Extract.new(duration, :epoch)])
end

#zero_intervalObject


11
12
13
# File 'lib/gitlab/analytics/cycle_analytics/stage_query_helpers.rb', line 11

def zero_interval
  Arel::Nodes::NamedFunction.new('CAST', [Arel.sql("'0' AS INTERVAL")])
end