summaryrefslogtreecommitdiff
path: root/app
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 /app
parent4844476e77f625829d99b7db2680186939ef660a (diff)
downloadgitlab-ce-cbd7d000395ff60fe3726e67ec351bd4d44582ec.tar.gz
added custom date helper and spec and fixed some unrelated spec failures
Diffstat (limited to 'app')
-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
4 files changed, 35 insertions, 4 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