diff --git a/app/models/cycle_analytics.rb b/app/models/cycle_analytics.rb index 52eb07ae7d6..8a57298f824 100644 --- a/app/models/cycle_analytics.rb +++ b/app/models/cycle_analytics.rb @@ -7,8 +7,13 @@ class CycleAnalytics end def all_medians_per_stage - STAGES.each_with_object({}) do |stage_name, hsh| - hsh[stage_name] = self[stage_name].median + medians_per_stage = {} + + # We only need this data for Postgres instances + return medians_per_stage if Gitlab::Database.mysql? + + STAGES.each do |stage_name| + medians_per_stage[stage_name] = self[stage_name].median end end diff --git a/app/serializers/analytics_stage_entity.rb b/app/serializers/analytics_stage_entity.rb index 08fa8b83c24..f87eb78e0e8 100644 --- a/app/serializers/analytics_stage_entity.rb +++ b/app/serializers/analytics_stage_entity.rb @@ -7,6 +7,6 @@ class AnalyticsStageEntity < Grape::Entity expose :description expose :median, as: :value do |stage| - stage.median && !stage.median.blank? ? distance_of_time_in_words(stage.median) : nil + distance_of_time_in_words(stage.median) if stage.median && !(stage.median.blank? || stage.median.zero?) end end diff --git a/lib/gitlab/cycle_analytics/base_query.rb b/lib/gitlab/cycle_analytics/base_query.rb index ed050287a9d..cd3d19db28e 100644 --- a/lib/gitlab/cycle_analytics/base_query.rb +++ b/lib/gitlab/cycle_analytics/base_query.rb @@ -7,14 +7,15 @@ module Gitlab private - def base_query(project_ids = nil) - stage_query(project_ids) + def base_query + @base_query ||= stage_query([@project.id]) end - def stage_query(project_ids = nil) + def stage_query(project_ids) query = mr_closing_issues_table.join(issue_table).on(issue_table[:id].eq(mr_closing_issues_table[:issue_id])) .join(issue_metrics_table).on(issue_table[:id].eq(issue_metrics_table[:issue_id])) - .where(issue_table[:project_id].in(project_ids || @project.id)) # rubocop:disable Gitlab/ModuleWithInstanceVariables + .project(issue_table[:project_id].as("project_id")) + .where(issue_table[:project_id].in(project_ids)) # rubocop:disable Gitlab/ModuleWithInstanceVariables .where(issue_table[:created_at].gteq(@options[:from])) # rubocop:disable Gitlab/ModuleWithInstanceVariables # Load merge_requests @@ -22,7 +23,6 @@ module Gitlab .on(mr_table[:id].eq(mr_closing_issues_table[:merge_request_id])) .join(mr_metrics_table) .on(mr_table[:id].eq(mr_metrics_table[:merge_request_id])) - .project(issue_table[:project_id].as("project_id")) query end diff --git a/lib/gitlab/cycle_analytics/base_stage.rb b/lib/gitlab/cycle_analytics/base_stage.rb index d7c98e41c63..45386fee622 100644 --- a/lib/gitlab/cycle_analytics/base_stage.rb +++ b/lib/gitlab/cycle_analytics/base_stage.rb @@ -29,7 +29,7 @@ module Gitlab # We compute the (end_time - start_time) interval, and give it an alias based on the current # cycle analytics stage. interval_query = Arel::Nodes::As.new(cte_table, - subtract_datetimes(base_query(project_ids), start_time_attrs, end_time_attrs, name.to_s)) + subtract_datetimes(stage_query(project_ids), start_time_attrs, end_time_attrs, name.to_s)) median_datetimes(cte_table, interval_query, name, :project_id)&.each do |project_id, median| loader.call(project_id, median) diff --git a/lib/gitlab/cycle_analytics/production_helper.rb b/lib/gitlab/cycle_analytics/production_helper.rb index ad1b2a007f6..d0ca62e46e4 100644 --- a/lib/gitlab/cycle_analytics/production_helper.rb +++ b/lib/gitlab/cycle_analytics/production_helper.rb @@ -1,7 +1,7 @@ module Gitlab module CycleAnalytics module ProductionHelper - def stage_query(project_ids = nil) + def stage_query(project_ids) super(project_ids) .where(mr_metrics_table[:first_deployed_to_production_at] .gteq(@options[:from])) # rubocop:disable Gitlab/ModuleWithInstanceVariables diff --git a/lib/gitlab/cycle_analytics/test_stage.rb b/lib/gitlab/cycle_analytics/test_stage.rb index f47f313d35d..0e9d235ca79 100644 --- a/lib/gitlab/cycle_analytics/test_stage.rb +++ b/lib/gitlab/cycle_analytics/test_stage.rb @@ -25,7 +25,7 @@ module Gitlab _("Total test time for all commits/merges") end - def stage_query(project_ids = nil) + def stage_query(project_ids) if @options[:branch] super(project_ids).where(build_table[:ref].eq(@options[:branch])) else