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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

86 lines
2.6 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
2021-06-08 01:23:25 +05:30
include StageQueryHelpers
2019-12-21 20:55:43 +05:30
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
2021-06-08 01:23:25 +05:30
DEFAULT_END_EVENT_FILTER = :finished
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)
2021-06-08 01:23:25 +05:30
@params[:state] = :opened if in_progress?
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)
2021-06-08 01:23:25 +05:30
apply_end_event_query_customization(query)
2019-12-21 20:55:43 +05:30
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]
2021-06-08 01:23:25 +05:30
finder_params[:end_event_filter] = params[:end_event_filter] || DEFAULT_END_EVENT_FILTER
2020-06-23 00:09:42 +05:30
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
2021-06-08 01:23:25 +05:30
# rubocop: disable CodeReuse/ActiveRecord
def apply_end_event_query_customization(query)
if in_progress?
stage.end_event.apply_negated_query_customization(query)
else
query = stage.end_event.apply_query_customization(query)
query.where(duration_condition)
end
end
# rubocop: enable CodeReuse/ActiveRecord
2019-12-21 20:55:43 +05:30
end
end
end
end
2019-12-26 22:10:19 +05:30
2021-06-08 01:23:25 +05:30
Gitlab::Analytics::CycleAnalytics::BaseQueryBuilder.prepend_mod_with('Gitlab::Analytics::CycleAnalytics::BaseQueryBuilder')