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: 5648984ecbb042311e1804dc16298e408bbfb9ff (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
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true

module Gitlab
  module Analytics
    module CycleAnalytics
      module StageQueryHelpers
        def execute_query(query)
          ApplicationRecord.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(
            end_event_timestamp_projection,
            stage.start_event.timestamp_projection
          )
        end

        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

        # rubocop: disable CodeReuse/ActiveRecord
        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
        # rubocop: enable CodeReuse/ActiveRecord

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

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