summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMałgorzata Ksionek <mksionek@gitlab.com>2019-06-26 15:13:10 +0200
committerMałgorzata Ksionek <mksionek@gitlab.com>2019-07-03 16:45:53 +0200
commitb58d32437a924dbd41a0a7f0e1c4e335c10693fe (patch)
tree9a76ca8155bf0783a8853b886705a210e9ca0712
parent0536bd7d850d2258bf09dae5b630e8687b1fcad3 (diff)
downloadgitlab-ce-b58d32437a924dbd41a0a7f0e1c4e335c10693fe.tar.gz
Add group stage
-rw-r--r--lib/gitlab/cycle_analytics/group_stage_summary.rb25
-rw-r--r--lib/gitlab/cycle_analytics/summary/group/base.rb24
-rw-r--r--lib/gitlab/cycle_analytics/summary/group/commit.rb40
-rw-r--r--lib/gitlab/cycle_analytics/summary/group/deploy.rb23
-rw-r--r--lib/gitlab/cycle_analytics/summary/group/issue.rb25
-rw-r--r--spec/lib/gitlab/cycle_analytics/group_stage_summary_spec.rb71
6 files changed, 208 insertions, 0 deletions
diff --git a/lib/gitlab/cycle_analytics/group_stage_summary.rb b/lib/gitlab/cycle_analytics/group_stage_summary.rb
new file mode 100644
index 00000000000..0a415c5ad3f
--- /dev/null
+++ b/lib/gitlab/cycle_analytics/group_stage_summary.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module CycleAnalytics
+ class GroupStageSummary
+ def initialize(group, from:, current_user:)
+ @group = group
+ @from = from
+ @current_user = current_user
+ end
+
+ def data
+ [serialize(Summary::Group::Issue.new(group: @group, from: @from, current_user: @current_user)),
+ serialize(Summary::Group::Commit.new(group: @group, from: @from)),
+ serialize(Summary::Group::Deploy.new(group: @group, from: @from))]
+ end
+
+ private
+
+ def serialize(summary_object)
+ AnalyticsSummarySerializer.new.represent(summary_object)
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/cycle_analytics/summary/group/base.rb b/lib/gitlab/cycle_analytics/summary/group/base.rb
new file mode 100644
index 00000000000..7f18b61d309
--- /dev/null
+++ b/lib/gitlab/cycle_analytics/summary/group/base.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module CycleAnalytics
+ module Summary
+ module Group
+ class Base
+ def initialize(group:, from:)
+ @group = group
+ @from = from
+ 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/commit.rb b/lib/gitlab/cycle_analytics/summary/group/commit.rb
new file mode 100644
index 00000000000..e1357cd0023
--- /dev/null
+++ b/lib/gitlab/cycle_analytics/summary/group/commit.rb
@@ -0,0 +1,40 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module CycleAnalytics
+ module Summary
+ module Group
+ class Commit < Base
+ def title
+ n_('Commit', 'Commits', value)
+ end
+
+ def value
+ 0
+ # @value ||= count_commits
+ end
+
+ private
+
+ # Don't use the `Gitlab::Git::Repository#log` method, because it enforces
+ # 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
+ # return unless ref
+
+ # gitaly_commit_client.commit_count(ref, after: @from)
+ # end
+
+ # def gitaly_commit_client
+ # Gitlab::GitalyClient::CommitService.new(@project.repository.raw_repository)
+ # end
+
+ # def ref
+ # @ref ||= @project.default_branch.presence
+ # 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
new file mode 100644
index 00000000000..820fd48d3af
--- /dev/null
+++ b/lib/gitlab/cycle_analytics/summary/group/deploy.rb
@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module CycleAnalytics
+ module Summary
+ module Group
+ class Deploy < Base
+ def title
+ n_('Deploy', 'Deploys', value)
+ end
+
+ def value
+ @value ||= Deployment.joins(:project)
+ .where(projects: { namespace_id: @group.id })
+ .where("deployments.created_at > ?", @from)
+ .success
+ .count
+ 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
new file mode 100644
index 00000000000..0ed870ead6f
--- /dev/null
+++ b/lib/gitlab/cycle_analytics/summary/group/issue.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module CycleAnalytics
+ module Summary
+ module Group
+ class Issue < Base
+ def initialize(group:, from:, current_user:)
+ @group = group
+ @from = from
+ @current_user = current_user
+ end
+
+ def title
+ n_('New Issue', 'New Issues', value)
+ end
+
+ def value
+ @value ||= IssuesFinder.new(@current_user, group_id: @group.id).execute.created_after(@from).count
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spec/lib/gitlab/cycle_analytics/group_stage_summary_spec.rb b/spec/lib/gitlab/cycle_analytics/group_stage_summary_spec.rb
new file mode 100644
index 00000000000..6f121a17209
--- /dev/null
+++ b/spec/lib/gitlab/cycle_analytics/group_stage_summary_spec.rb
@@ -0,0 +1,71 @@
+require 'spec_helper'
+
+describe Gitlab::CycleAnalytics::GroupStageSummary do
+ let(:group) { create(:group) }
+ let(:project) { create(:project, :repository, namespace: group) }
+ let(:project_2) { create(:project, :repository, namespace: group) }
+ let(:from) { 1.day.ago }
+ let(:user) { create(:user, :admin) }
+ subject { described_class.new(group, from: Time.now, current_user: user).data }
+
+ describe "#new_issues" do
+ it "finds the number of issues created after the 'from date'" do
+ Timecop.freeze(5.days.ago) { create(:issue, project: project) }
+ Timecop.freeze(5.days.ago) { create(:issue, project: project_2) }
+ Timecop.freeze(5.days.from_now) { create(:issue, project: project) }
+ Timecop.freeze(5.days.from_now) { create(:issue, project: project_2) }
+
+ expect(subject.first[:value]).to eq(2)
+ end
+
+ it "doesn't find issues from other projects" do
+ Timecop.freeze(5.days.from_now) { create(:issue, project: create(:project, namespace: create(:group))) }
+ Timecop.freeze(5.days.from_now) { create(:issue, project: project) }
+ Timecop.freeze(5.days.from_now) { create(:issue, project: project_2) }
+
+ expect(subject.first[:value]).to eq(2)
+ end
+ end
+
+ # describe "#commits" do
+ # it "finds the number of commits created after the 'from date'" do
+ # Timecop.freeze(5.days.ago) { create_commit("Test message", project, user, 'master') }
+ # Timecop.freeze(5.days.from_now) { create_commit("Test message", project_2, user, 'master') }
+ # Timecop.freeze(5.days.from_now) { create_commit("Test message", project, user, 'master') }
+
+ # expect(subject.second[:value]).to eq(2)
+ # end
+
+ # it "doesn't find commits from other projects" do
+ # Timecop.freeze(5.days.from_now) { create_commit("Test message", create(:project, :repository, namespace: create(:group)), user, 'master') }
+
+ # expect(subject.second[:value]).to eq(0)
+ # end
+
+ # it "finds a large (> 100) snumber of commits if present" do
+ # Timecop.freeze(5.days.from_now) { create_commit("Test message", project, user, 'master', count: 51) }
+ # Timecop.freeze(5.days.from_now) { create_commit("Test message", project_2, user, 'master', count: 51) }
+
+ # expect(subject.second[:value]).to eq(100)
+ # end
+ # end
+
+ describe "#deploys" do
+ it "finds the number of deploys made created after the 'from date'" do
+ Timecop.freeze(5.days.ago) { create(:deployment, :success, project: project) }
+ Timecop.freeze(5.days.from_now) { create(:deployment, :success, project: project) }
+ Timecop.freeze(5.days.ago) { create(:deployment, :success, project: project_2) }
+ Timecop.freeze(5.days.from_now) { create(:deployment, :success, project: project_2) }
+
+ expect(subject.third[:value]).to eq(2)
+ end
+
+ it "doesn't find commits from other projects" do
+ Timecop.freeze(5.days.from_now) do
+ create(:deployment, :success, project: create(:project, :repository, namespace: create(:group)))
+ end
+
+ expect(subject.third[:value]).to eq(0)
+ end
+ end
+end