2019-12-21 20:55:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Analytics
|
|
|
|
module CycleAnalytics
|
|
|
|
module StageQueryHelpers
|
|
|
|
def execute_query(query)
|
2021-10-27 15:23:28 +05:30
|
|
|
ApplicationRecord.connection.execute(query.to_sql)
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def zero_interval
|
2020-01-01 13:55:28 +05:30
|
|
|
Arel::Nodes::NamedFunction.new('CAST', [Arel.sql("'0' AS INTERVAL")])
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def round_duration_to_seconds
|
2020-01-01 13:55:28 +05:30
|
|
|
Arel::Nodes::NamedFunction.new('ROUND', [Arel::Nodes::Extract.new(duration, :epoch)])
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def duration
|
|
|
|
Arel::Nodes::Subtraction.new(
|
2021-06-08 01:23:25 +05:30
|
|
|
end_event_timestamp_projection,
|
2019-12-21 20:55:43 +05:30
|
|
|
stage.start_event.timestamp_projection
|
|
|
|
)
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
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
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2021-04-17 20:07:23 +05:30
|
|
|
def order_by(query, sort, direction, extra_columns_to_select = [:id])
|
2021-06-08 01:23:25 +05:30
|
|
|
ordered_query = Gitlab::Analytics::CycleAnalytics::Sorting.new(stage: stage, query: query, params: params).apply(sort, direction)
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
# 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?
|
2021-06-08 01:23:25 +05:30
|
|
|
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
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
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
|
2021-06-08 01:23:25 +05:30
|
|
|
|
|
|
|
def in_progress?
|
|
|
|
params[:end_event_filter] == :in_progress
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|