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

71 lines
2 KiB
Ruby
Raw Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
module Gitlab
module Analytics
module CycleAnalytics
class BaseQueryBuilder
include Gitlab::CycleAnalytics::MetricsTables
delegate :subject_class, to: :stage
2020-06-23 00:09:42 +05:30
FINDER_CLASSES = {
MergeRequest.to_s => MergeRequestsFinder,
Issue.to_s => IssuesFinder
}.freeze
2019-12-21 20:55:43 +05:30
def initialize(stage:, params: {})
@stage = stage
2020-06-23 00:09:42 +05:30
@params = build_finder_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 build
2020-06-23 00:09:42 +05:30
query = finder.execute
2019-12-21 20:55:43 +05:30
query = stage.start_event.apply_query_customization(query)
query = stage.end_event.apply_query_customization(query)
query.where(duration_condition)
end
2020-06-23 00:09:42 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2019-12-21 20:55:43 +05:30
private
attr_reader :stage, :params
def duration_condition
stage.end_event.timestamp_projection.gteq(stage.start_event.timestamp_projection)
end
2020-06-23 00:09:42 +05:30
def finder
FINDER_CLASSES.fetch(subject_class.to_s).new(params[:current_user], params)
2019-12-21 20:55:43 +05:30
end
2020-06-23 00:09:42 +05:30
def parent_class
stage.parent.class
2019-12-21 20:55:43 +05:30
end
2020-06-23 00:09:42 +05:30
def build_finder_params(params)
{}.tap do |finder_params|
finder_params[:current_user] = params[:current_user]
add_parent_model_params!(finder_params)
add_time_range_params!(finder_params, params[:from], params[:to])
end
2019-12-21 20:55:43 +05:30
end
2020-06-23 00:09:42 +05:30
def add_parent_model_params!(finder_params)
raise(ArgumentError, "unknown parent_class: #{parent_class}") unless parent_class.eql?(Project)
finder_params[:project_id] = stage.parent_id
2019-12-21 20:55:43 +05:30
end
2020-06-23 00:09:42 +05:30
def add_time_range_params!(finder_params, from, to)
finder_params[:created_after] = from || 30.days.ago
finder_params[:created_before] = to if to
end
2019-12-21 20:55:43 +05:30
end
end
end
end
2019-12-26 22:10:19 +05:30
Gitlab::Analytics::CycleAnalytics::BaseQueryBuilder.prepend_if_ee('EE::Gitlab::Analytics::CycleAnalytics::BaseQueryBuilder')