2019-12-21 20:55:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Analytics
|
|
|
|
module CycleAnalytics
|
|
|
|
class Median
|
|
|
|
include StageQueryHelpers
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
def initialize(stage:, query:, params: {})
|
2019-12-21 20:55:43 +05:30
|
|
|
@stage = stage
|
|
|
|
@query = query
|
2021-06-08 01:23:25 +05:30
|
|
|
@params = params
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2019-12-21 20:55:43 +05:30
|
|
|
def seconds
|
2020-06-23 00:09:42 +05:30
|
|
|
@query = @query.select(median_duration_in_seconds.as('median')).reorder(nil)
|
2019-12-21 20:55:43 +05:30
|
|
|
result = execute_query(@query).first || {}
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
result['median'] || nil
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
def days
|
|
|
|
seconds ? seconds.fdiv(1.day) : nil
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
attr_reader :stage, :params
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
def percentile_cont
|
|
|
|
percentile_cont_ordering = Arel::Nodes::UnaryOperation.new(Arel::Nodes::SqlLiteral.new('ORDER BY'), duration)
|
|
|
|
Arel::Nodes::NamedFunction.new(
|
|
|
|
'percentile_cont(0.5) WITHIN GROUP',
|
|
|
|
[percentile_cont_ordering]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def median_duration_in_seconds
|
|
|
|
Arel::Nodes::Extract.new(percentile_cont, :epoch)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|