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



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

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

#end_event_timestamp_projectionObject



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

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)


59
60
61
# File 'lib/gitlab/analytics/cycle_analytics/stage_query_helpers.rb', line 59

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



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

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)


55
56
57
# File 'lib/gitlab/analytics/cycle_analytics/stage_query_helpers.rb', line 55

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

#round_duration_to_secondsObject



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

def round_duration_to_seconds
  Arel::Nodes::NamedFunction.new('ROUND', [
    Arel::Nodes::NamedFunction.new('CAST', [Arel::Nodes::Extract.new(duration, :epoch).as('double precision')])
  ])
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