diff options
Diffstat (limited to 'lib')
6 files changed, 62 insertions, 12 deletions
diff --git a/lib/gitlab/analytics/cycle_analytics/base_query_builder.rb b/lib/gitlab/analytics/cycle_analytics/base_query_builder.rb index 0afe2dbd021..163f7e2ccef 100644 --- a/lib/gitlab/analytics/cycle_analytics/base_query_builder.rb +++ b/lib/gitlab/analytics/cycle_analytics/base_query_builder.rb @@ -5,7 +5,6 @@ module Gitlab module CycleAnalytics class BaseQueryBuilder include Gitlab::CycleAnalytics::MetricsTables - include StageQueryHelpers delegate :subject_model, to: :stage @@ -14,6 +13,7 @@ module Gitlab def initialize(stage:, params: {}) @stage = stage @params = params + @duration_filter = DurationFilter.new(stage: stage) end def run @@ -22,16 +22,12 @@ module Gitlab query = filter_by_time_range(query) query = stage.start_event.apply_query_customization(query) query = stage.end_event.apply_query_customization(query) - exclude_negative_durations(query) + duration_filter.apply(query) end private - attr_reader :stage, :params - - def exclude_negative_durations(query) - query.where(duration.gt(zero_interval)) - end + attr_reader :stage, :params, :duration_filter def filter_by_parent_model(query) parent_class = stage.parent.class diff --git a/lib/gitlab/analytics/cycle_analytics/duration_filter.rb b/lib/gitlab/analytics/cycle_analytics/duration_filter.rb new file mode 100644 index 00000000000..c3a2ede4e57 --- /dev/null +++ b/lib/gitlab/analytics/cycle_analytics/duration_filter.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +module Gitlab + module Analytics + module CycleAnalytics + # This class ensures that negative durations won't be returned by the query. Sometimes checking for negative duration is unnecessary, in that case the duration check won't be executed. + # + # Example: issues.closed_at - issues.created_at + # Check is not needed because issues.created_at will be always earlier than closed_at. + class DurationFilter + include StageQueryHelpers + + def initialize(stage:) + @stage = stage + end + + # rubocop: disable CodeReuse/ActiveRecord + def apply(query) + skip_duration_check? ? query : query.where(stage.end_event.timestamp_projection.gteq(stage.start_event.timestamp_projection)) + end + # rubocop: enable CodeReuse/ActiveRecord + + private + + attr_reader :stage + + def skip_duration_check? + starts_with_issue_creation? || + starts_with_mr_creation? || + mr_merged_at_with_deployment? || + mr_build_started_and_finished? + end + + def starts_with_issue_creation? + stage.start_event.is_a?(StageEvents::IssueCreated) + end + + def starts_with_mr_creation? + stage.start_event.is_a?(StageEvents::MergeRequestCreated) + end + + def mr_merged_at_with_deployment? + stage.start_event.is_a?(StageEvents::MergeRequestMerged) && + stage.end_event.is_a?(StageEvents::MergeRequestFirstDeployedToProduction) + end + + def mr_build_started_and_finished? + stage.start_event.is_a?(StageEvents::MergeRequestLastBuildStarted) && + stage.end_event.is_a?(StageEvents::MergeRequestLastBuildFinished) + end + end + end + end +end diff --git a/lib/gitlab/analytics/cycle_analytics/records_fetcher.rb b/lib/gitlab/analytics/cycle_analytics/records_fetcher.rb index 2d4377099aa..9e2a2d57150 100644 --- a/lib/gitlab/analytics/cycle_analytics/records_fetcher.rb +++ b/lib/gitlab/analytics/cycle_analytics/records_fetcher.rb @@ -101,9 +101,9 @@ module Gitlab .joins(ci_build_join) .select(build_table[:id], round_duration_to_seconds.as('total_time')) - result = execute_query(q).to_a + results = execute_query(q).to_a - Gitlab::CycleAnalytics::Updater.update!(result, from: 'id', to: 'build', klass: ::Ci::Build) + Gitlab::CycleAnalytics::Updater.update!(results, from: 'id', to: 'build', klass: ::Ci::Build.includes({ project: [:namespace], user: [], pipeline: [] })) end def ordered_and_limited_query diff --git a/lib/gitlab/analytics/cycle_analytics/stage_events/issue_stage_end.rb b/lib/gitlab/analytics/cycle_analytics/stage_events/issue_stage_end.rb index f09c98e537c..77e4092b9ab 100644 --- a/lib/gitlab/analytics/cycle_analytics/stage_events/issue_stage_end.rb +++ b/lib/gitlab/analytics/cycle_analytics/stage_events/issue_stage_end.rb @@ -26,7 +26,7 @@ module Gitlab # rubocop: disable CodeReuse/ActiveRecord def apply_query_customization(query) - query.joins(:metrics) + query.joins(:metrics).where(issue_metrics_table[:first_added_to_board_at].not_eq(nil).or(issue_metrics_table[:first_associated_with_milestone_at].not_eq(nil))) end # rubocop: enable CodeReuse/ActiveRecord end diff --git a/lib/gitlab/analytics/cycle_analytics/stage_events/merge_request_first_deployed_to_production.rb b/lib/gitlab/analytics/cycle_analytics/stage_events/merge_request_first_deployed_to_production.rb index 56a331c028c..3d7482eaaf0 100644 --- a/lib/gitlab/analytics/cycle_analytics/stage_events/merge_request_first_deployed_to_production.rb +++ b/lib/gitlab/analytics/cycle_analytics/stage_events/merge_request_first_deployed_to_production.rb @@ -23,7 +23,7 @@ module Gitlab # rubocop: disable CodeReuse/ActiveRecord def apply_query_customization(query) - query.joins(:metrics) + query.joins(:metrics).where(timestamp_projection.gteq(mr_table[:created_at])) end # rubocop: enable CodeReuse/ActiveRecord end diff --git a/lib/gitlab/analytics/cycle_analytics/stage_events/production_stage_end.rb b/lib/gitlab/analytics/cycle_analytics/stage_events/production_stage_end.rb index 2fd71216e11..607371a32e8 100644 --- a/lib/gitlab/analytics/cycle_analytics/stage_events/production_stage_end.rb +++ b/lib/gitlab/analytics/cycle_analytics/stage_events/production_stage_end.rb @@ -23,7 +23,7 @@ module Gitlab # rubocop: disable CodeReuse/ActiveRecord def apply_query_customization(query) - query.joins(merge_requests_closing_issues: { merge_request: [:metrics] }) + query.joins(merge_requests_closing_issues: { merge_request: [:metrics] }).where(mr_metrics_table[:first_deployed_to_production_at].gteq(mr_table[:created_at])) end # rubocop: enable CodeReuse/ActiveRecord end |