Skip to content

Instantly share code, notes, and snippets.

@jvanus
Created July 9, 2020 19:28
Show Gist options
  • Save jvanus/ca0b9dec92d552d39a42edb35bfc83df to your computer and use it in GitHub Desktop.
Save jvanus/ca0b9dec92d552d39a42edb35bfc83df to your computer and use it in GitHub Desktop.
# ETA class is used to calculate the time a script will finish
# It needs to be initilized with the total number of units to be processed
# It needs to be called with the current number of of units processed so far
# eg: eta = Eta.new(things.count)
# things.each_with_index do |thing, index|
# puts eta.calc(index) rescue nil
# do_something(thing)
# end
# eta goes first because on index 0 you've processed 0 units
# Usage:
# Eta.new(total, start_time, note, frequency)
# total: total number of units to iterate over
# start_time: time the first unit started to process
# note: a string to include in the output (helpfull if you have more than 1 eta at a time)
# frequency: sampeling frequency, default should produce 100 lines of output
# eg: frequency of 3 would give results every third index
class Eta
attr_accessor :total, :start_time, :note, :frequency, :index, :display_memory_usage
def initialize(t, st = Time.now, n = nil, f = [t/100, 1].max)
@total = t
@start_time = st
@note = n
@frequency = f
@index = 0
@display_memory_usage = false
end
def calc(idx = nil)
#TODO: Cleanup instance variable associations, remove total = @total lines
n = "#{note.to_s} (Mem: #{NewRelic::Agent::Samplers::MemorySampler.new.sampler.get_sample.to_i}M)" if display_memory_usage
idx ||= @index += 1
if idx > 1 && idx % frequency != 0
return
end
if idx == 1
return "working #{idx} of #{total}%s." % (" #{n}" if n)
elsif idx > total
return "working #{idx} of #{total}%s. Should be done already." % (" #{n}" if n)
else
rate = (idx / (Time.now - start_time))
ttf = (total - idx) / rate
eta = (Time.now + ttf.round.seconds).strftime("%Y-%m-%d %H:%M:%S")
seconds = ttf % 60
ttf = (ttf - seconds) / 60
minutes = ttf % 60
ttf = (ttf - minutes) / 60
hours = ttf
return "working #{idx} of #{total}%s. ETA: #{eta} (%02d:%02d:%02d)" % [(" #{n}" if n), hours, minutes, seconds]
end
end
def out(idx = nil)
begin
output = self.calc(idx)
puts output unless output.nil?
rescue => e
puts "eta error: " + e.full_message
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment