Skip to content

Commit

Permalink
Return a Duration instead of a Time for the ETA (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebarrie authored Jun 3, 2021
1 parent 3ad8c72 commit 66cc13e
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 47 deletions.
14 changes: 0 additions & 14 deletions app/helpers/maintenance_tasks/tasks_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,6 @@ def status_tag(status)
tag.span(status.capitalize, class: ["tag"] + STATUS_COLOURS.fetch(status))
end

# Returns the distance between now and the Run's expected completion time,
# if the Run has an estimated_completion_time.
#
# @param run [MaintenanceTasks::Run] the Run for which the estimated time to
# completion is being calculated.
# return [String, nil] the distance in words, or nil if the Run has no
# estimated completion time.
def estimated_time_to_completion(run)
estimated_completion_time = run.estimated_completion_time
if estimated_completion_time.present?
time_ago_in_words(estimated_completion_time)
end
end

# Reports the approximate elapsed time a Run has been processed so far based
# on the Run's time running attribute.
#
Expand Down
14 changes: 7 additions & 7 deletions app/models/maintenance_tasks/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,19 @@ def active?
ACTIVE_STATUSES.include?(status.to_sym)
end

# Returns the estimated time the task will finish based on the the number of
# ticks left and the average time needed to process a tick.
# Returns nil if the Run is completed, or if the tick_count or tick_total is
# zero.
# Returns the duration left for the Run to finish based on the number of
# ticks left and the average time needed to process a tick. Returns nil if
# the Run is completed, or if tick_count or tick_total is zero.
#
# @return [Time] the estimated time the Run will finish.
def estimated_completion_time
# @return [ActiveSupport::Duration] the estimated duration left for the Run
# to finish.
def time_to_completion
return if completed? || tick_count == 0 || tick_total.to_i == 0

processed_per_second = (tick_count.to_f / time_running)
ticks_left = (tick_total - tick_count)
seconds_to_finished = ticks_left / processed_per_second
Time.now + seconds_to_finished
seconds_to_finished.seconds
end

# Cancels a Run.
Expand Down
4 changes: 2 additions & 2 deletions app/views/maintenance_tasks/runs/info/_paused.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<p>
Ran for <%= time_running_in_words run %> until paused,
<% if run.estimated_completion_time %>
<%= estimated_time_to_completion(run) %> remaining.
<% if (time_to_completion = run.time_to_completion) %>
<%= distance_of_time_in_words(time_to_completion) %> remaining.
<% else %>
processed <%= pluralize run.tick_count, 'item' %> so far.
<% end %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/maintenance_tasks/runs/info/_running.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p>
<% if run.estimated_completion_time %>
<%= estimated_time_to_completion(run).capitalize %> remaining.
<% if (time_to_completion = run.time_to_completion) %>
<%= distance_of_time_in_words(time_to_completion).capitalize %> remaining.
<% end %>
</p>
9 changes: 0 additions & 9 deletions test/helpers/maintenance_tasks/tasks_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,6 @@ class TasksHelperTest < ActionView::TestCase
assert_equal expected, status_tag("pausing")
end

test "#estimated_time_to_completion returns the Run's estimated_completion_time in words" do
@run.expects(estimated_completion_time: Time.now + 2.minutes)
assert_equal "2 minutes", estimated_time_to_completion(@run)
end

test "#estimated_time_to_completion returns nil if the Run has no estimated_completion_time" do
assert_nil estimated_time_to_completion(@run)
end

test "#time_running_in_words reports the approximate time running of the given Run" do
@run.time_running = 182.5
assert_equal "3 minutes", time_running_in_words(@run)
Expand Down
21 changes: 8 additions & 13 deletions test/models/maintenance_tasks/run_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,51 +158,46 @@ class RunTest < ActiveSupport::TestCase
end
end

test "#estimated_completion_time returns nil if the run is completed" do
test "#time_to_completion returns nil if the run is completed" do
run = Run.new(
task_name: "Maintenance::UpdatePostsTask",
status: :succeeded
)

assert_nil run.estimated_completion_time
assert_nil run.time_to_completion
end

test "#estimated_completion_time returns nil if tick_count is 0" do
test "#time_to_completion returns nil if tick_count is 0" do
run = Run.new(
task_name: "Maintenance::UpdatePostsTask",
status: :running,
tick_count: 0,
tick_total: 10
)

assert_nil run.estimated_completion_time
assert_nil run.time_to_completion
end

test "#estimated_completion_time returns nil if no tick_total" do
test "#time_to_completion returns nil if no tick_total" do
run = Run.new(
task_name: "Maintenance::UpdatePostsTask",
status: :running,
tick_count: 1
)

assert_nil run.estimated_completion_time
assert_nil run.time_to_completion
end

test "#estimated_completion_time returns estimated completion time based on average time elapsed per tick" do
started_at = Time.utc(2020, 1, 9, 9, 41, 44)
travel_to started_at + 9.seconds

test "#time_to_completion returns estimated duration until completion based on average time elapsed per tick" do
run = Run.new(
task_name: "Maintenance::UpdatePostsTask",
started_at: started_at,
status: :running,
tick_count: 9,
tick_total: 10,
time_running: 9,
)

expected_completion_time = Time.utc(2020, 1, 9, 9, 41, 54)
assert_equal expected_completion_time, run.estimated_completion_time
assert_equal 1.second, run.time_to_completion
end

test "#cancel transitions the Run to cancelling if not paused" do
Expand Down

0 comments on commit 66cc13e

Please sign in to comment.