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

stage_query_helpers.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: c9a75b399590829a2106fac05dd7c7ce2376e3f2 (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
# frozen_string_literal: true

module Gitlab
  module Analytics
    module CycleAnalytics
      module StageQueryHelpers
        def execute_query(query)
          ActiveRecord::Base.connection.execute(query.to_sql)
        end

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

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

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

        # rubocop: disable CodeReuse/ActiveRecord
        def order_by_end_event(query)
          ordered_query = query.reorder(stage.end_event.timestamp_projection.desc)

          # 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 = [
              ordered_query.arel_table[:id],
              *stage.end_event.column_list,
              *stage.start_event.column_list
            ]

            ordered_query = ordered_query.group(column_list)
          end

          ordered_query
        end
        # rubocop: enable CodeReuse/ActiveRecord

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