debian-mirror-gitlab/lib/gitlab/analytics/cycle_analytics/average.rb

50 lines
1.1 KiB
Ruby
Raw Normal View History

2021-04-17 20:07:23 +05:30
# frozen_string_literal: true
module Gitlab
module Analytics
module CycleAnalytics
class Average
include Gitlab::Utils::StrongMemoize
include StageQueryHelpers
2021-06-08 01:23:25 +05:30
def initialize(stage:, query:, params: {})
2021-04-17 20:07:23 +05:30
@stage = stage
@query = query
2021-06-08 01:23:25 +05:30
@params = params
2021-04-17 20:07:23 +05:30
end
def seconds
select_average ? select_average['average'] : nil
end
def days
seconds ? seconds.fdiv(1.day) : nil
end
private
2021-06-08 01:23:25 +05:30
attr_reader :stage, :params
2021-04-17 20:07:23 +05:30
# rubocop: disable CodeReuse/ActiveRecord
def select_average
strong_memoize(:select_average) do
execute_query(@query.select(average_in_seconds.as('average')).reorder(nil)).first
end
end
# rubocop: enable CodeReuse/ActiveRecord
def average
Arel::Nodes::NamedFunction.new(
'AVG',
[duration]
)
end
def average_in_seconds
2023-06-20 00:43:36 +05:30
Arel::Nodes::NamedFunction.new('CAST', [Arel::Nodes::Extract.new(average, :epoch).as('double precision')])
2021-04-17 20:07:23 +05:30
end
end
end
end
end