Commit cbd7d000 authored by James Lopez's avatar James Lopez

added custom date helper and spec and fixed some unrelated spec failures

parent 4844476e
...@@ -13,7 +13,7 @@ class AnalyticsBuildEntity < Grape::Entity ...@@ -13,7 +13,7 @@ class AnalyticsBuildEntity < Grape::Entity
end end
expose :duration, as: :total_time do |build| 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 end
expose :branch do expose :branch do
......
...@@ -5,7 +5,7 @@ class AnalyticsCommitEntity < CommitEntity ...@@ -5,7 +5,7 @@ class AnalyticsCommitEntity < CommitEntity
expose :short_id, as: :short_sha expose :short_id, as: :short_sha
expose :total_time do |commit| 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 end
unexpose :author_name unexpose :author_name
......
...@@ -3,12 +3,15 @@ class AnalyticsGenericEntity < Grape::Entity ...@@ -3,12 +3,15 @@ class AnalyticsGenericEntity < Grape::Entity
include EntityDateHelper include EntityDateHelper
expose :title expose :title
expose :iid
expose :state, if: ->(_instance, options) { options[:request].entity == :merge_request } expose :state, if: ->(_instance, options) { options[:request].entity == :merge_request }
expose :author, using: UserEntity expose :author, using: UserEntity
expose :iid do |object|
object[:iid].to_s
end
expose :total_time do |object| 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 end
expose(:created_at) do |object| expose(:created_at) do |object|
......
...@@ -4,4 +4,32 @@ module EntityDateHelper ...@@ -4,4 +4,32 @@ module EntityDateHelper
def interval_in_words(diff) def interval_in_words(diff)
"#{distance_of_time_in_words(diff.to_f)} ago" "#{distance_of_time_in_words(diff.to_f)} ago"
end 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 end
...@@ -16,7 +16,7 @@ describe Gitlab::CycleAnalytics::Events do ...@@ -16,7 +16,7 @@ describe Gitlab::CycleAnalytics::Events do
describe '#issue_events' do describe '#issue_events' do
it 'has the total time' 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 end
it 'has a title' do it 'has a title' do
...@@ -62,7 +62,7 @@ describe Gitlab::CycleAnalytics::Events do ...@@ -62,7 +62,7 @@ describe Gitlab::CycleAnalytics::Events do
end end
it 'has the total time' do 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 end
it "has the author's URL" do it "has the author's URL" do
...@@ -84,7 +84,7 @@ describe Gitlab::CycleAnalytics::Events do ...@@ -84,7 +84,7 @@ describe Gitlab::CycleAnalytics::Events do
end end
it 'has the total time' do 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 end
it 'has a title' do it 'has a title' do
...@@ -162,7 +162,7 @@ describe Gitlab::CycleAnalytics::Events do ...@@ -162,7 +162,7 @@ describe Gitlab::CycleAnalytics::Events do
end end
it 'has the total time' do 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
end end
...@@ -170,7 +170,7 @@ describe Gitlab::CycleAnalytics::Events do ...@@ -170,7 +170,7 @@ describe Gitlab::CycleAnalytics::Events do
let!(:context) { create(:issue, project: project, created_at: 2.days.ago) } let!(:context) { create(:issue, project: project, created_at: 2.days.ago) }
it 'has the total time' do 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 end
it 'has a title' do it 'has a title' do
...@@ -259,7 +259,7 @@ describe Gitlab::CycleAnalytics::Events do ...@@ -259,7 +259,7 @@ describe Gitlab::CycleAnalytics::Events do
end end
it 'has the total time' do 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 end
it "has the author's URL" do it "has the author's URL" do
...@@ -284,7 +284,7 @@ describe Gitlab::CycleAnalytics::Events do ...@@ -284,7 +284,7 @@ describe Gitlab::CycleAnalytics::Events do
end end
it 'has the total time' do 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 end
it 'has a title' do it 'has a title' do
......
...@@ -3,17 +3,18 @@ require 'spec_helper' ...@@ -3,17 +3,18 @@ require 'spec_helper'
describe 'cycle analytics events' do describe 'cycle analytics events' do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project) } let(:project) { create(:project) }
let(:issue) { create(:issue, project: project, created_at: 2.days.ago) }
describe 'GET /:namespace/:project/cycle_analytics/events/issues' do describe 'GET /:namespace/:project/cycle_analytics/events/issues' do
before do before do
project.team << [user, :developer] project.team << [user, :developer]
allow_any_instance_of(Gitlab::ReferenceExtractor).to receive(:issues).and_return([issue])
3.times { create_cycle } 3.times { create_cycle }
deploy_master deploy_master
login_as(user) login_as(user)
allow_any_instance_of(Gitlab::ReferenceExtractor).to receive(:issues).and_return([context])
end end
it 'lists the issue events' do it 'lists the issue events' do
...@@ -31,17 +32,7 @@ describe 'cycle analytics events' do ...@@ -31,17 +32,7 @@ describe 'cycle analytics events' do
expect(json_response['events']).not_to be_empty expect(json_response['events']).not_to be_empty
commits = [] expect(json_response['events'].first['short_sha']).to eq(MergeRequest.last.commits.first.short_id)
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)
end end
it 'lists the code events' do it 'lists the code events' do
...@@ -49,7 +40,7 @@ describe 'cycle analytics events' do ...@@ -49,7 +40,7 @@ describe 'cycle analytics events' do
expect(json_response['events']).not_to be_empty 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) expect(json_response['events'].first['iid']).to eq(first_mr_iid)
end end
...@@ -67,7 +58,7 @@ describe 'cycle analytics events' do ...@@ -67,7 +58,7 @@ describe 'cycle analytics events' do
expect(json_response['events']).not_to be_empty 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) expect(json_response['events'].first['iid']).to eq(first_mr_iid)
end end
...@@ -132,7 +123,6 @@ describe 'cycle analytics events' do ...@@ -132,7 +123,6 @@ describe 'cycle analytics events' do
end end
def create_cycle def create_cycle
issue = create(:issue, project: project, created_at: 2.days.ago)
milestone = create(:milestone, project: project) milestone = create(:milestone, project: project)
issue.update(milestone: milestone) issue.update(milestone: milestone)
mr = create_merge_request_closing_issue(issue) mr = create_merge_request_closing_issue(issue)
......
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment