Fix build duration when build is not finished yet

Signed-off-by: Rémy Coutable <remy@rymai.me>
This commit is contained in:
Rémy Coutable 2016-07-18 16:44:10 +02:00
parent 017ae313dc
commit dd4f50b187
No known key found for this signature in database
GPG key ID: 46DF07E5CD9E96AB
4 changed files with 31 additions and 34 deletions

View file

@ -1,14 +1,4 @@
module TimeHelper
def duration_in_words(finished_at, started_at)
if finished_at && started_at
interval_in_seconds = finished_at.to_i - started_at.to_i
elsif started_at
interval_in_seconds = Time.now.to_i - started_at.to_i
end
time_interval_in_words(interval_in_seconds)
end
def time_interval_in_words(interval_in_seconds)
minutes = interval_in_seconds / 60
seconds = interval_in_seconds - minutes * 60
@ -25,9 +15,19 @@ module TimeHelper
end
def duration_in_numbers(finished_at, started_at)
diff_in_seconds = finished_at.to_i - started_at.to_i
time_format = diff_in_seconds < 1.hour ? "%M:%S" : "%H:%M:%S"
interval = interval_in_seconds(started_at, finished_at)
time_format = interval < 1.hour ? "%M:%S" : "%H:%M:%S"
Time.at(diff_in_seconds).utc.strftime(time_format)
Time.at(interval).utc.strftime(time_format)
end
private
def interval_in_seconds(started_at, finished_at = nil)
if started_at && finished_at
finished_at.to_i - started_at.to_i
elsif started_at
Time.now.to_i - started_at.to_i
end
end
end

View file

@ -49,7 +49,7 @@
- if @build.duration
%p.build-detail-row
%span.build-light-text Duration:
#{duration_in_words(@build.finished_at, @build.started_at)}
= time_interval_in_words(@build.duration)
- if @build.finished_at
%p.build-detail-row
%span.build-light-text Finished:

View file

@ -51,7 +51,7 @@
%td.duration
- if generic_commit_status.duration
= icon("clock-o")
#{duration_in_words(generic_commit_status.finished_at, generic_commit_status.started_at)}
= time_interval_in_words(generic_commit_status.duration)
%td.timestamp
- if generic_commit_status.finished_at

View file

@ -1,25 +1,6 @@
require 'spec_helper'
describe TimeHelper do
describe "#duration_in_words" do
it "returns minutes and seconds" do
intervals_in_words = {
100 => "1 minute 40 seconds",
121 => "2 minutes 1 second",
3721 => "62 minutes 1 second",
0 => "0 seconds"
}
intervals_in_words.each do |interval, expectation|
expect(duration_in_words(Time.now + interval, Time.now)).to eq(expectation)
end
end
it "calculates interval from now if there is no finished_at" do
expect(duration_in_words(nil, Time.now - 5)).to eq("5 seconds")
end
end
describe "#time_interval_in_words" do
it "returns minutes and seconds" do
intervals_in_words = {
@ -34,4 +15,20 @@ describe TimeHelper do
end
end
end
describe "#duration_in_numbers" do
it "returns minutes and seconds" do
duration_in_numbers = {
[100, 0] => "01:40",
[121, 0] => "02:01",
[3721, 0] => "01:02:01",
[0, 0] => "00:00",
[nil, Time.now.to_i - 42] => "00:42"
}
duration_in_numbers.each do |interval, expectation|
expect(duration_in_numbers(*interval)).to eq(expectation)
end
end
end
end