Created
October 4, 2017 08:47
-
-
Save formigarafa/7d6dc72062c897cde15f1f039e7308ce to your computer and use it in GitHub Desktop.
console process feedback with ETA in ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module EtaReport | |
def total | |
@total ||= whatever.count | |
end | |
def started_at | |
@started_at ||= Time.now | |
end | |
def progress_feedback(processed) | |
if processed.modulo(1000).zero? && processed.nonzero? | |
time_taken = Time.now - started_at | |
speed = processed/time_taken | |
remaining = total - processed | |
time_remaining = remaining / speed.to_f | |
puts "Completed #{processed}/#{total} #{eta_str(time_remaining)} (#{speed.round(2)} stuffs/s)" | |
else | |
putc "." | |
end | |
end | |
def eta_str(eta_seconds) | |
eta_days = (eta_seconds / 1.day).floor | |
eta_days_str = if eta_days.nonzero? | |
"#{eta_days} days " | |
end | |
eta_h_m_s = eta_seconds.modulo(1.day) | |
"ETA: #{eta_days_str}#{Time.at(eta_h_m_s.modulo(24.hours)).utc.strftime('%H:%M:%S')}" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................ | |
Completed 1000/10000 2 days 23:40:12 (23 stuffs/s) | |
......... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# use | |
def total | |
10000 | |
end | |
def run | |
10000.times do |n| | |
progress_feedback(n) | |
do_stuff | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment