summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2016-07-08 15:20:22 +0000
committerRémy Coutable <remy@rymai.me>2016-07-08 15:20:22 +0000
commit8bdb5b6aecdece24a5ee014628ffb154fad969a9 (patch)
treea46ccb393f3202d65187970a6d0393ff77e15aa3
parent4b21b45db1d701231a68e706434a4b5a9d0574f8 (diff)
parent382e8cfef60d7bf1a96946ee902b25bc01d174ca (diff)
downloadgitlab-ce-8bdb5b6aecdece24a5ee014628ffb154fad969a9.tar.gz
Merge branch '1548-average-commits-per-day' into 'master'
Fix: Infinity Bug in Commit Statistics ## What does this MR do? It fixes a logic bug in the commits statistics: The code assumed that the amount of days involved in a commit range is equal to the difference between the first and last date. This is not true, though, as (from a human standpoint), a commit yesterday and a commit today involve two days, not one. Similarly, a fresh project with only commits made today already 'used' one day. Since the number of involved days used to be zero for new projects, the result for commits per day quite often amounted to `Infinity`… ## Are there points in the code the reviewer needs to double check? The test file. I hope it is up to the standards of GitLab. ## Why was this MR needed? The bug occurres especially for new users with their first project while exploring GitLab. ## What are the relevant issue numbers? This bug was reported as #1548. ## Screenshots (if relevant) See merge request !4231
-rw-r--r--lib/gitlab/graphs/commits.rb2
-rw-r--r--spec/lib/gitlab/graphs/commits_spec.rb39
2 files changed, 40 insertions, 1 deletions
diff --git a/lib/gitlab/graphs/commits.rb b/lib/gitlab/graphs/commits.rb
index 2122339d2db..3caf9036459 100644
--- a/lib/gitlab/graphs/commits.rb
+++ b/lib/gitlab/graphs/commits.rb
@@ -18,7 +18,7 @@ module Gitlab
end
def commit_per_day
- @commit_per_day ||= (@commits.size.to_f / @duration).round(1)
+ @commit_per_day ||= @commits.size / (@duration + 1)
end
def collect_data
diff --git a/spec/lib/gitlab/graphs/commits_spec.rb b/spec/lib/gitlab/graphs/commits_spec.rb
new file mode 100644
index 00000000000..f5c064303ad
--- /dev/null
+++ b/spec/lib/gitlab/graphs/commits_spec.rb
@@ -0,0 +1,39 @@
+require 'spec_helper'
+
+describe Gitlab::Graphs::Commits, lib: true do
+ let!(:project) { create(:project, :public, :empty_repo) }
+
+ let!(:commit1) { create(:commit, git_commit: RepoHelpers.sample_commit, project: project, committed_date: Time.now) }
+ let!(:commit1_yesterday) { create(:commit, git_commit: RepoHelpers.sample_commit, project: project, committed_date: 1.day.ago)}
+
+ let!(:commit2) { create(:commit, git_commit: RepoHelpers.another_sample_commit, project: project, committed_date: Time.now) }
+
+ describe '#commit_per_day' do
+ context 'when range is only commits from today' do
+ subject { described_class.new([commit2, commit1]).commit_per_day }
+ it { is_expected.to eq 2 }
+ end
+ end
+
+ context 'when range is only commits from today' do
+ subject { described_class.new([commit2, commit1]) }
+ describe '#commit_per_day' do
+ it { expect(subject.commit_per_day).to eq 2 }
+ end
+
+ describe '#duration' do
+ it { expect(subject.duration).to eq 0 }
+ end
+ end
+
+ context 'with commits from yesterday and today' do
+ subject { described_class.new([commit2, commit1_yesterday]) }
+ describe '#commit_per_day' do
+ it { expect(subject.commit_per_day).to eq 1 }
+ end
+
+ describe '#duration' do
+ it { expect(subject.duration).to eq 1 }
+ end
+ end
+end