summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Lopez <james@jameslopez.es>2016-11-16 15:55:20 +0100
committerJames Lopez <james@jameslopez.es>2016-11-17 08:22:59 +0100
commitcbd7d000395ff60fe3726e67ec351bd4d44582ec (patch)
treef497cc29ef7cb87a9f6bd4334ea536f6b6aa81fd
parent4844476e77f625829d99b7db2680186939ef660a (diff)
downloadgitlab-ce-cbd7d000395ff60fe3726e67ec351bd4d44582ec.tar.gz
added custom date helper and spec and fixed some unrelated spec failures
-rw-r--r--app/serializers/analytics_build_entity.rb2
-rw-r--r--app/serializers/analytics_commit_entity.rb2
-rw-r--r--app/serializers/analytics_generic_entity.rb7
-rw-r--r--app/serializers/entity_date_helper.rb28
-rw-r--r--spec/lib/gitlab/cycle_analytics/events_spec.rb14
-rw-r--r--spec/requests/projects/cycle_analytics_events_spec.rb22
-rw-r--r--spec/serializers/entity_date_helper_spec.rb45
7 files changed, 93 insertions, 27 deletions
diff --git a/app/serializers/analytics_build_entity.rb b/app/serializers/analytics_build_entity.rb
index eb1eef3424a..5fdf2bbf7c3 100644
--- a/app/serializers/analytics_build_entity.rb
+++ b/app/serializers/analytics_build_entity.rb
@@ -13,7 +13,7 @@ class AnalyticsBuildEntity < Grape::Entity
end
expose :duration, as: :total_time do |build|
- distance_of_time_in_words(build[:duration].to_f)
+ distance_of_time_as_hash(build[:duration].to_f)
end
expose :branch do
diff --git a/app/serializers/analytics_commit_entity.rb b/app/serializers/analytics_commit_entity.rb
index a932d612e0f..2b363abbbd2 100644
--- a/app/serializers/analytics_commit_entity.rb
+++ b/app/serializers/analytics_commit_entity.rb
@@ -5,7 +5,7 @@ class AnalyticsCommitEntity < CommitEntity
expose :short_id, as: :short_sha
expose :total_time do |commit|
- distance_of_time_in_words(request.total_time.to_f)
+ distance_of_time_as_hash(request.total_time.to_f)
end
unexpose :author_name
diff --git a/app/serializers/analytics_generic_entity.rb b/app/serializers/analytics_generic_entity.rb
index d7abe3f5f50..203cf39b940 100644
--- a/app/serializers/analytics_generic_entity.rb
+++ b/app/serializers/analytics_generic_entity.rb
@@ -3,12 +3,15 @@ class AnalyticsGenericEntity < Grape::Entity
include EntityDateHelper
expose :title
- expose :iid
expose :state, if: ->(_instance, options) { options[:request].entity == :merge_request }
expose :author, using: UserEntity
+ expose :iid do |object|
+ object[:iid].to_s
+ end
+
expose :total_time do |object|
- distance_of_time_in_words(object[:total_time].to_f)
+ distance_of_time_as_hash(object[:total_time].to_f)
end
expose(:created_at) do |object|
diff --git a/app/serializers/entity_date_helper.rb b/app/serializers/entity_date_helper.rb
index 57817ce1812..b333b3344c3 100644
--- a/app/serializers/entity_date_helper.rb
+++ b/app/serializers/entity_date_helper.rb
@@ -4,4 +4,32 @@ module EntityDateHelper
def interval_in_words(diff)
"#{distance_of_time_in_words(diff.to_f)} ago"
end
+
+ # Converts seconds into a hash such as:
+ # { days: 1, hours: 3, mins: 42, seconds: 40 }
+ #
+ # It returns 0 seconds for zero or negative numbers
+ # It rounds to nearest time unit and does not return zero
+ # i.e { min: 1 } instead of { mins: 1, seconds: 0 }
+ def distance_of_time_as_hash(diff)
+ diff = diff.abs.floor
+
+ return { seconds: 0 } if diff == 0
+
+ mins = (diff / 60).floor
+ seconds = diff % 60
+ hours = (mins / 60).floor
+ mins = mins % 60
+ days = (hours / 24).floor
+ hours = hours % 24
+
+ duration_hash = {}
+
+ duration_hash[:days] = days if days > 0
+ duration_hash[:hours] = hours if hours > 0
+ duration_hash[:mins] = mins if mins > 0
+ duration_hash[:seconds] = seconds if seconds > 0
+
+ duration_hash
+ end
end
diff --git a/spec/lib/gitlab/cycle_analytics/events_spec.rb b/spec/lib/gitlab/cycle_analytics/events_spec.rb
index aa0e54582f1..8e2d2f8b5bd 100644
--- a/spec/lib/gitlab/cycle_analytics/events_spec.rb
+++ b/spec/lib/gitlab/cycle_analytics/events_spec.rb
@@ -16,7 +16,7 @@ describe Gitlab::CycleAnalytics::Events do
describe '#issue_events' do
it 'has the total time' do
- expect(subject.issue_events.first[:total_time]).to eq('2 days')
+ expect(subject.issue_events.first[:total_time]).not_to be_empty
end
it 'has a title' do
@@ -62,7 +62,7 @@ describe Gitlab::CycleAnalytics::Events do
end
it 'has the total time' do
- expect(subject.plan_events.first[:total_time]).to eq('less than a minute')
+ expect(subject.plan_events.first[:total_time]).not_to be_empty
end
it "has the author's URL" do
@@ -84,7 +84,7 @@ describe Gitlab::CycleAnalytics::Events do
end
it 'has the total time' do
- expect(subject.code_events.first[:total_time]).to eq('less than a minute')
+ expect(subject.code_events.first[:total_time]).not_to be_empty
end
it 'has a title' do
@@ -162,7 +162,7 @@ describe Gitlab::CycleAnalytics::Events do
end
it 'has the total time' do
- expect(subject.test_events.first[:total_time]).not_to be_nil
+ expect(subject.test_events.first[:total_time]).not_to be_empty
end
end
@@ -170,7 +170,7 @@ describe Gitlab::CycleAnalytics::Events do
let!(:context) { create(:issue, project: project, created_at: 2.days.ago) }
it 'has the total time' do
- expect(subject.review_events.first[:total_time]).to eq('less than a minute')
+ expect(subject.review_events.first[:total_time]).not_to be_empty
end
it 'has a title' do
@@ -259,7 +259,7 @@ describe Gitlab::CycleAnalytics::Events do
end
it 'has the total time' do
- expect(subject.staging_events.first[:total_time]).not_to be_nil
+ expect(subject.staging_events.first[:total_time]).not_to be_empty
end
it "has the author's URL" do
@@ -284,7 +284,7 @@ describe Gitlab::CycleAnalytics::Events do
end
it 'has the total time' do
- expect(subject.production_events.first[:total_time]).to eq('2 days')
+ expect(subject.production_events.first[:total_time]).not_to be_empty
end
it 'has a title' do
diff --git a/spec/requests/projects/cycle_analytics_events_spec.rb b/spec/requests/projects/cycle_analytics_events_spec.rb
index 1c78cd368db..705dbb7d1c0 100644
--- a/spec/requests/projects/cycle_analytics_events_spec.rb
+++ b/spec/requests/projects/cycle_analytics_events_spec.rb
@@ -3,17 +3,18 @@ require 'spec_helper'
describe 'cycle analytics events' do
let(:user) { create(:user) }
let(:project) { create(:project) }
+ let(:issue) { create(:issue, project: project, created_at: 2.days.ago) }
describe 'GET /:namespace/:project/cycle_analytics/events/issues' do
before do
project.team << [user, :developer]
+ allow_any_instance_of(Gitlab::ReferenceExtractor).to receive(:issues).and_return([issue])
+
3.times { create_cycle }
deploy_master
login_as(user)
-
- allow_any_instance_of(Gitlab::ReferenceExtractor).to receive(:issues).and_return([context])
end
it 'lists the issue events' do
@@ -31,17 +32,7 @@ describe 'cycle analytics events' do
expect(json_response['events']).not_to be_empty
- commits = []
-
- MergeRequest.all.each do |mr|
- mr.merge_request_diff.st_commits.each do |commit|
- commits << { date: commit[:authored_date], sha: commit[:id] }
- end
- end
-
- newest_sha = commits.sort_by { |k| k['date'] }.first[:sha][0...8]
-
- expect(json_response['events'].first['short_sha']).to eq(newest_sha)
+ expect(json_response['events'].first['short_sha']).to eq(MergeRequest.last.commits.first.short_id)
end
it 'lists the code events' do
@@ -49,7 +40,7 @@ describe 'cycle analytics events' do
expect(json_response['events']).not_to be_empty
- first_mr_iid = Issue.order(created_at: :desc).pluck(:iid).first.to_s
+ first_mr_iid = MergeRequest.order(created_at: :desc).pluck(:iid).first.to_s
expect(json_response['events'].first['iid']).to eq(first_mr_iid)
end
@@ -67,7 +58,7 @@ describe 'cycle analytics events' do
expect(json_response['events']).not_to be_empty
- first_mr_iid = Issue.order(created_at: :desc).pluck(:iid).first.to_s
+ first_mr_iid = MergeRequest.order(created_at: :desc).pluck(:iid).first.to_s
expect(json_response['events'].first['iid']).to eq(first_mr_iid)
end
@@ -132,7 +123,6 @@ describe 'cycle analytics events' do
end
def create_cycle
- issue = create(:issue, project: project, created_at: 2.days.ago)
milestone = create(:milestone, project: project)
issue.update(milestone: milestone)
mr = create_merge_request_closing_issue(issue)
diff --git a/spec/serializers/entity_date_helper_spec.rb b/spec/serializers/entity_date_helper_spec.rb
new file mode 100644
index 00000000000..90e57833de7
--- /dev/null
+++ b/spec/serializers/entity_date_helper_spec.rb
@@ -0,0 +1,45 @@
+require 'spec_helper'
+
+describe EntityDateHelper do
+ let(:date_helper_class) { Class.new { include EntityDateHelper }.new }
+
+ it 'converts 0 seconds' do
+ expect(date_helper_class.distance_of_time_as_hash(0)).to eq(seconds: 0)
+ end
+
+ it 'converts 40 seconds' do
+ expect(date_helper_class.distance_of_time_as_hash(40)).to eq(seconds: 40)
+ end
+
+ it 'converts 60 seconds' do
+ expect(date_helper_class.distance_of_time_as_hash(60)).to eq(mins: 1)
+ end
+
+ it 'converts 70 seconds' do
+ expect(date_helper_class.distance_of_time_as_hash(70)).to eq(mins: 1, seconds: 10)
+ end
+
+ it 'converts 3600 seconds' do
+ expect(date_helper_class.distance_of_time_as_hash(3600)).to eq(hours: 1)
+ end
+
+ it 'converts 3750 seconds' do
+ expect(date_helper_class.distance_of_time_as_hash(3750)).to eq(hours: 1, mins: 2, seconds: 30)
+ end
+
+ it 'converts 86400 seconds' do
+ expect(date_helper_class.distance_of_time_as_hash(86400)).to eq(days: 1)
+ end
+
+ it 'converts 86560 seconds' do
+ expect(date_helper_class.distance_of_time_as_hash(86560)).to eq(days: 1, mins: 2, seconds: 40)
+ end
+
+ it 'converts 86760 seconds' do
+ expect(date_helper_class.distance_of_time_as_hash(99760)).to eq(days: 1, hours: 3, mins: 42, seconds: 40)
+ end
+
+ it 'converts 986760 seconds' do
+ expect(date_helper_class.distance_of_time_as_hash(986760)).to eq(days: 11, hours: 10, mins: 6)
+ end
+end \ No newline at end of file