diff options
author | James Lopez <james@jameslopez.es> | 2016-11-22 13:46:02 +0300 |
---|---|---|
committer | James Lopez <james@jameslopez.es> | 2017-01-17 13:32:54 +0300 |
commit | fc6f8f20562ad761c034ffff076d329a3e9e8f4d (patch) | |
tree | 401c48a79209f656fa74dffe102d4385caefea76 /lib/gitlab/cycle_analytics | |
parent | dc6ea14b0d11a5e73e81c95ef723f0c1af69215b (diff) |
added new summary serializers and refactor all of the summary stuff into separate logical classes
Diffstat (limited to 'lib/gitlab/cycle_analytics')
-rw-r--r-- | lib/gitlab/cycle_analytics/summary.rb | 36 | ||||
-rw-r--r-- | lib/gitlab/cycle_analytics/summary/base.rb | 20 | ||||
-rw-r--r-- | lib/gitlab/cycle_analytics/summary/commit.rb | 36 | ||||
-rw-r--r-- | lib/gitlab/cycle_analytics/summary/deploy.rb | 11 | ||||
-rw-r--r-- | lib/gitlab/cycle_analytics/summary/issue.rb | 15 |
5 files changed, 118 insertions, 0 deletions
diff --git a/lib/gitlab/cycle_analytics/summary.rb b/lib/gitlab/cycle_analytics/summary.rb new file mode 100644 index 00000000000..7d172855a94 --- /dev/null +++ b/lib/gitlab/cycle_analytics/summary.rb @@ -0,0 +1,36 @@ +module Gitlab + module CycleAnalytics + module Summary + extend self + + def initialize(project, from:) + @project = project + @from = from + end + + def data + [serialize(issue), + serialize(commit), + serialize(deploy)] + end + + private + + def serialize(summary_object) + AnalyticsSummarySerializer.new.represent(summary_object).as_json + end + + def issue + Summary::Issue.new(project: @project, from: @from) + end + + def deploy + Summary::Deploy.new(project: @project, from: @from) + end + + def commit + Summary::Commit.new(project: @project, from: @from) + end + end + end +end diff --git a/lib/gitlab/cycle_analytics/summary/base.rb b/lib/gitlab/cycle_analytics/summary/base.rb new file mode 100644 index 00000000000..1bc4ff00b99 --- /dev/null +++ b/lib/gitlab/cycle_analytics/summary/base.rb @@ -0,0 +1,20 @@ +module Gitlab + module CycleAnalytics + module Summary + class Base + def initialize(project:, from:) + @project = project + @from = from + end + + def title + self.name + end + + def value + raise NotImplementedError.new("Expected #{self.name} to implement value") + end + end + end + end +end diff --git a/lib/gitlab/cycle_analytics/summary/commit.rb b/lib/gitlab/cycle_analytics/summary/commit.rb new file mode 100644 index 00000000000..ec3c067c0be --- /dev/null +++ b/lib/gitlab/cycle_analytics/summary/commit.rb @@ -0,0 +1,36 @@ +module Gitlab + module CycleAnalytics + module Summary + class Commit < Base + def value + @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 + + repository = @project.repository.raw_repository + sha = @project.repository.commit(ref).sha + + cmd = %W(git --git-dir=#{repository.path} log) + cmd << '--format=%H' + cmd << "--after=#{@from.iso8601}" + cmd << sha + + raw_output = IO.popen(cmd) { |io| io.read } + raw_output.lines.count + end + + def ref + @ref ||= @project.default_branch.presence + end + end + end + end +end diff --git a/lib/gitlab/cycle_analytics/summary/deploy.rb b/lib/gitlab/cycle_analytics/summary/deploy.rb new file mode 100644 index 00000000000..06032e9200e --- /dev/null +++ b/lib/gitlab/cycle_analytics/summary/deploy.rb @@ -0,0 +1,11 @@ +module Gitlab + module CycleAnalytics + module Summary + class Deploy < Base + def value + @value ||= @project.deployments.where("created_at > ?", @from).count + end + end + end + end +end diff --git a/lib/gitlab/cycle_analytics/summary/issue.rb b/lib/gitlab/cycle_analytics/summary/issue.rb new file mode 100644 index 00000000000..7d62164aae3 --- /dev/null +++ b/lib/gitlab/cycle_analytics/summary/issue.rb @@ -0,0 +1,15 @@ +module Gitlab + module CycleAnalytics + module Summary + class Issue < Base + def title + 'New Issue' + end + + def value + @value ||= @project.issues.created_after(@from).count + end + end + end + end +end |