diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 17:34:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 17:34:42 +0300 |
commit | 9f46488805e86b1bc341ea1620b866016c2ce5ed (patch) | |
tree | f9748c7e287041e37d6da49e0a29c9511dc34768 /lib/gitlab/cycle_analytics | |
parent | dfc92d081ea0332d69c8aca2f0e745cb48ae5e6d (diff) |
Add latest changes from gitlab-org/gitlab@13-0-stable-ee
Diffstat (limited to 'lib/gitlab/cycle_analytics')
-rw-r--r-- | lib/gitlab/cycle_analytics/group_stage_summary.rb | 54 | ||||
-rw-r--r-- | lib/gitlab/cycle_analytics/stage_summary.rb | 5 | ||||
-rw-r--r-- | lib/gitlab/cycle_analytics/summary/commit.rb | 6 | ||||
-rw-r--r-- | lib/gitlab/cycle_analytics/summary/deploy.rb | 16 | ||||
-rw-r--r-- | lib/gitlab/cycle_analytics/summary/deployment_frequency.rb | 3 | ||||
-rw-r--r-- | lib/gitlab/cycle_analytics/summary/group/base.rb | 26 | ||||
-rw-r--r-- | lib/gitlab/cycle_analytics/summary/group/deploy.rb | 31 | ||||
-rw-r--r-- | lib/gitlab/cycle_analytics/summary/group/deployment_frequency.rb | 33 | ||||
-rw-r--r-- | lib/gitlab/cycle_analytics/summary/group/issue.rb | 44 | ||||
-rw-r--r-- | lib/gitlab/cycle_analytics/summary/issue.rb | 11 | ||||
-rw-r--r-- | lib/gitlab/cycle_analytics/summary/value.rb | 42 | ||||
-rw-r--r-- | lib/gitlab/cycle_analytics/summary_helper.rb | 5 |
12 files changed, 70 insertions, 206 deletions
diff --git a/lib/gitlab/cycle_analytics/group_stage_summary.rb b/lib/gitlab/cycle_analytics/group_stage_summary.rb deleted file mode 100644 index 09b33d01846..00000000000 --- a/lib/gitlab/cycle_analytics/group_stage_summary.rb +++ /dev/null @@ -1,54 +0,0 @@ -# frozen_string_literal: true - -module Gitlab - module CycleAnalytics - class GroupStageSummary - attr_reader :group, :current_user, :options - - def initialize(group, options:) - @group = group - @current_user = options[:current_user] - @options = options - end - - def data - [issue_stats, - deploy_stats, - deployment_frequency_stats] - end - - private - - def issue_stats - serialize( - Summary::Group::Issue.new( - group: group, current_user: current_user, options: options) - ) - end - - def deployments_summary - @deployments_summary ||= - Summary::Group::Deploy.new(group: group, options: options) - end - - def deploy_stats - serialize deployments_summary - end - - def deployment_frequency_stats - serialize( - Summary::Group::DeploymentFrequency.new( - deployments: deployments_summary.value, - group: group, - options: options), - with_unit: true - ) - end - - def serialize(summary_object, with_unit: false) - AnalyticsSummarySerializer.new.represent( - summary_object, with_unit: with_unit) - end - end - end -end diff --git a/lib/gitlab/cycle_analytics/stage_summary.rb b/lib/gitlab/cycle_analytics/stage_summary.rb index 564feb0319f..7559cd376bf 100644 --- a/lib/gitlab/cycle_analytics/stage_summary.rb +++ b/lib/gitlab/cycle_analytics/stage_summary.rb @@ -28,8 +28,7 @@ module Gitlab end def deployments_summary - @deployments_summary ||= - Summary::Deploy.new(project: @project, from: @from, to: @to) + @deployments_summary ||= Summary::Deploy.new(project: @project, from: @from, to: @to) end def deploy_stats @@ -39,7 +38,7 @@ module Gitlab def deployment_frequency_stats serialize( Summary::DeploymentFrequency.new( - deployments: deployments_summary.value, + deployments: deployments_summary.value.raw_value, from: @from, to: @to), with_unit: true diff --git a/lib/gitlab/cycle_analytics/summary/commit.rb b/lib/gitlab/cycle_analytics/summary/commit.rb index 76049c6b742..1f426b81800 100644 --- a/lib/gitlab/cycle_analytics/summary/commit.rb +++ b/lib/gitlab/cycle_analytics/summary/commit.rb @@ -9,7 +9,7 @@ module Gitlab end def value - @value ||= count_commits + @value ||= commits_count ? Value::PrettyNumeric.new(commits_count) : Value::None.new end private @@ -18,10 +18,10 @@ module Gitlab # a limit. Since we need a commit count, we _can't_ enforce a limit, so # the easiest way forward is to replicate the relevant portions of the # `log` function here. - def count_commits + def commits_count return unless ref - gitaly_commit_client.commit_count(ref, after: @from, before: @to) + @commits_count ||= gitaly_commit_client.commit_count(ref, after: @from, before: @to) end def gitaly_commit_client diff --git a/lib/gitlab/cycle_analytics/summary/deploy.rb b/lib/gitlab/cycle_analytics/summary/deploy.rb index 5ff8d881143..8544ea1a91e 100644 --- a/lib/gitlab/cycle_analytics/summary/deploy.rb +++ b/lib/gitlab/cycle_analytics/summary/deploy.rb @@ -4,18 +4,20 @@ module Gitlab module CycleAnalytics module Summary class Deploy < Base - include Gitlab::Utils::StrongMemoize - def title n_('Deploy', 'Deploys', value) end def value - strong_memoize(:value) do - query = @project.deployments.success.where("created_at >= ?", @from) - query = query.where("created_at <= ?", @to) if @to - query.count - end + @value ||= Value::PrettyNumeric.new(deployments_count) + end + + private + + def deployments_count + query = @project.deployments.success.where("created_at >= ?", @from) + query = query.where("created_at <= ?", @to) if @to + query.count end end end diff --git a/lib/gitlab/cycle_analytics/summary/deployment_frequency.rb b/lib/gitlab/cycle_analytics/summary/deployment_frequency.rb index 436dc91bd6b..00676a02a6f 100644 --- a/lib/gitlab/cycle_analytics/summary/deployment_frequency.rb +++ b/lib/gitlab/cycle_analytics/summary/deployment_frequency.rb @@ -17,8 +17,7 @@ module Gitlab end def value - @value ||= - frequency(@deployments, @from, @to || Time.now) + @value ||= frequency(@deployments, @from, @to || Time.now) end def unit diff --git a/lib/gitlab/cycle_analytics/summary/group/base.rb b/lib/gitlab/cycle_analytics/summary/group/base.rb deleted file mode 100644 index f1d20d5aefa..00000000000 --- a/lib/gitlab/cycle_analytics/summary/group/base.rb +++ /dev/null @@ -1,26 +0,0 @@ -# frozen_string_literal: true - -module Gitlab - module CycleAnalytics - module Summary - module Group - class Base - attr_reader :group, :options - - def initialize(group:, options:) - @group = group - @options = options - end - - def title - raise NotImplementedError.new("Expected #{self.name} to implement title") - end - - def value - raise NotImplementedError.new("Expected #{self.name} to implement value") - end - end - end - end - end -end diff --git a/lib/gitlab/cycle_analytics/summary/group/deploy.rb b/lib/gitlab/cycle_analytics/summary/group/deploy.rb deleted file mode 100644 index 11a9152cf0c..00000000000 --- a/lib/gitlab/cycle_analytics/summary/group/deploy.rb +++ /dev/null @@ -1,31 +0,0 @@ -# frozen_string_literal: true - -module Gitlab - module CycleAnalytics - module Summary - module Group - class Deploy < Group::Base - include GroupProjectsProvider - - def title - n_('Deploy', 'Deploys', value) - end - - def value - @value ||= find_deployments - end - - private - - def find_deployments - deployments = Deployment.joins(:project).merge(Project.inside_path(group.full_path)) - deployments = deployments.where(projects: { id: options[:projects] }) if options[:projects] - deployments = deployments.where("deployments.created_at > ?", options[:from]) - deployments = deployments.where("deployments.created_at < ?", options[:to]) if options[:to] - deployments.success.count - end - end - end - end - end -end diff --git a/lib/gitlab/cycle_analytics/summary/group/deployment_frequency.rb b/lib/gitlab/cycle_analytics/summary/group/deployment_frequency.rb deleted file mode 100644 index 9fbbbb5a1ec..00000000000 --- a/lib/gitlab/cycle_analytics/summary/group/deployment_frequency.rb +++ /dev/null @@ -1,33 +0,0 @@ -# frozen_string_literal: true - -module Gitlab - module CycleAnalytics - module Summary - module Group - class DeploymentFrequency < Group::Base - include GroupProjectsProvider - include SummaryHelper - - def initialize(deployments:, group:, options:) - @deployments = deployments - - super(group: group, options: options) - end - - def title - _('Deployment Frequency') - end - - def value - @value ||= - frequency(@deployments, options[:from], options[:to] || Time.now) - end - - def unit - _('per day') - end - end - end - end - end -end diff --git a/lib/gitlab/cycle_analytics/summary/group/issue.rb b/lib/gitlab/cycle_analytics/summary/group/issue.rb deleted file mode 100644 index 4d5ee1d43ca..00000000000 --- a/lib/gitlab/cycle_analytics/summary/group/issue.rb +++ /dev/null @@ -1,44 +0,0 @@ -# frozen_string_literal: true - -module Gitlab - module CycleAnalytics - module Summary - module Group - class Issue < Group::Base - attr_reader :group, :current_user, :options - - def initialize(group:, current_user:, options:) - @group = group - @current_user = current_user - @options = options - end - - def title - n_('New Issue', 'New Issues', value) - end - - def value - @value ||= find_issues - end - - private - - def find_issues - issues = IssuesFinder.new(current_user, finder_params).execute - issues = issues.where(projects: { id: options[:projects] }) if options[:projects] - issues.count - end - - def finder_params - { - group_id: group.id, - include_subgroups: true, - created_after: options[:from], - created_before: options[:to] - }.compact - end - end - end - end - end -end diff --git a/lib/gitlab/cycle_analytics/summary/issue.rb b/lib/gitlab/cycle_analytics/summary/issue.rb index 52892eb5a1a..ce7788590b9 100644 --- a/lib/gitlab/cycle_analytics/summary/issue.rb +++ b/lib/gitlab/cycle_analytics/summary/issue.rb @@ -16,7 +16,16 @@ module Gitlab end def value - @value ||= IssuesFinder.new(@current_user, project_id: @project.id, created_after: @from, created_before: @to).execute.count + @value ||= Value::PrettyNumeric.new(issues_count) + end + + private + + def issues_count + IssuesFinder + .new(@current_user, project_id: @project.id, created_after: @from, created_before: @to) + .execute + .count end end end diff --git a/lib/gitlab/cycle_analytics/summary/value.rb b/lib/gitlab/cycle_analytics/summary/value.rb new file mode 100644 index 00000000000..ce32132e048 --- /dev/null +++ b/lib/gitlab/cycle_analytics/summary/value.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +module Gitlab + module CycleAnalytics + module Summary + class Value + attr_reader :value + + def raw_value + value + end + + def to_s + raise NotImplementedError + end + + class None < self + def to_s + '-' + end + end + + class Numeric < self + def initialize(value) + @value = value + end + + def to_s + value.zero? ? '0' : value.to_s + end + end + + class PrettyNumeric < Numeric + def to_s + # 0 is shown as - + (value || 0).nonzero? ? super : None.new.to_s + end + end + end + end + end +end diff --git a/lib/gitlab/cycle_analytics/summary_helper.rb b/lib/gitlab/cycle_analytics/summary_helper.rb index 06abcd151d4..3cf9f463024 100644 --- a/lib/gitlab/cycle_analytics/summary_helper.rb +++ b/lib/gitlab/cycle_analytics/summary_helper.rb @@ -4,10 +4,11 @@ module Gitlab module CycleAnalytics module SummaryHelper def frequency(count, from, to) - return count if count.zero? + return Summary::Value::None.new if count.zero? freq = (count / days(from, to)).round(1) - freq.zero? ? '0' : freq + + Summary::Value::Numeric.new(freq) end def days(from, to) |