Class: NdrPseudonymise::ProgressPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/ndr_pseudonymise/progress_printer.rb

Overview

Log percentage progress on pseudonymisation Starts logging after 1 minute or 5%, then at 5% / 5 minute intervals

Instance Method Summary collapse

Constructor Details

#initialize(dest = $stdout, verbose = false) ⇒ ProgressPrinter

Logs progress to the given stream (default $stdout) If verbose = false, only log percentages on a single line If verbose = true, log verbose output If verbose = :dynamic, act like verbose = false, but if the total time is more than 5 minutes, move into verbose = true mode



10
11
12
13
14
15
# File 'lib/ndr_pseudonymise/progress_printer.rb', line 10

def initialize(dest = $stdout, verbose = false)
  @dest = dest
  @verbose = verbose
  @last_percent = 0
  @last_log = Time.current - (60 * 4) # First log entry after 1 minute
end

Instance Method Details

#log_progress(start_time, time_now, _csv_row, progress, total) ⇒ Object

Returns a lambda that prints progress to stdout (or another stream). parameter _csv_row is not used.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ndr_pseudonymise/progress_printer.rb', line 19

def log_progress(start_time, time_now, _csv_row, progress, total)
  current_percentage = total == 0 ? 0 : (progress * 100 / total).to_i
  now = Time.current
  if (current_percentage / 5 > @last_percent / 5) || # Log at 5% / 5 minute intervals
     (now - @last_log >= 60 * 5) || current_percentage == 100
    if @verbose == :dynamic && (time_now - start_time >= 60 * 5)
      @verbose = true
      @dest << '...'
    end
    if @verbose == true
      # TODO: Add estimated completion time
      tfin = if progress > 0
               time_now + (time_now - start_time) * (total - progress) / progress
             end
      completion = tfin ? ', expected completion' : ''
      @dest << format("Completed %s%% in %.1f minutes%s\n",
                      current_percentage, (now - start_time) / 60.0, completion)

      # @dest << ("Completed %s%% in %.1f minutes#{", expected completion #{tfin}" if tfin}\n" %
      #            [current_percentage, (now - start_time) / 60.0])

    else
      @dest << "#{'...' if @last_percent > 0}#{current_percentage}%"
      @dest << "\n" if current_percentage == 100
    end
    # if current_percentage == 100 # Uncomment for performance debugging
    #   @dest << "Finished %s rows in %.3f secs\n" % [csv_row, time_now - start_time]
    # end
    @dest.flush
    @last_percent = current_percentage
    @last_log = now
  end
end