blob: c7987d631534524878e771182ea563346aee8705 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# frozen_string_literal: true
module Gitlab
module Analytics
module CycleAnalytics
class BaseQueryBuilder
include Gitlab::CycleAnalytics::MetricsTables
include StageQueryHelpers
delegate :subject_class, to: :stage
FINDER_CLASSES = {
MergeRequest.to_s => MergeRequestsFinder,
Issue.to_s => IssuesFinder
}.freeze
DEFAULT_END_EVENT_FILTER = :finished
def initialize(stage:, params: {})
@stage = stage
@params = build_finder_params(params)
@params[:state] = :opened if in_progress?
end
# rubocop: disable CodeReuse/ActiveRecord
def build
query = finder.execute
query = stage.start_event.apply_query_customization(query)
apply_end_event_query_customization(query)
end
# rubocop: enable CodeReuse/ActiveRecord
private
attr_reader :stage, :params
def duration_condition
stage.end_event.timestamp_projection.gteq(stage.start_event.timestamp_projection)
end
def finder
FINDER_CLASSES.fetch(subject_class.to_s).new(params[:current_user], params)
end
def parent_class
stage.parent.class
end
def build_finder_params(params)
{}.tap do |finder_params|
finder_params[:current_user] = params[:current_user]
finder_params[:end_event_filter] = params[:end_event_filter] || DEFAULT_END_EVENT_FILTER
add_parent_model_params!(finder_params)
add_time_range_params!(finder_params, params[:from], params[:to])
end
end
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
end
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
# 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
end
end
end
end
Gitlab::Analytics::CycleAnalytics::BaseQueryBuilder.prepend_mod_with('Gitlab::Analytics::CycleAnalytics::BaseQueryBuilder')
|