Class: StructuredDataToSql::ProgressReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/structured_data_to_sql/progress_reporter.rb

Overview

Renders converter progress events as terminal output on the diagnostic stream. It is purely a consumer of the throttled event stream the converters already emit, so it adds no per-record work: on a TTY it keeps one live status line updated in place; elsewhere it prints a plain status line at most every min_interval seconds so logs show the run is alive without flooding. Finished files become fixed ledger rows, warnings are printed as they arrive, and a summary closes the run.

Constant Summary collapse

TTY_INTERVAL =
0.5
PLAIN_INTERVAL =
30
ETA_WARMUP_SECONDS =
5
NAME_WIDTH =
28
PLAIN_WIDTH =
100
MIN_WRAP_WIDTH =
40
BLOCK_INDENT =
"    "
MAX_ITEM_LINES =
3
GLYPHS =
{
  unicode: {
    done: "",
    live: "",
    warn: "!"
  },
  ascii: {
    done: "[ok]",
    live: "[..]",
    warn: "[!!]"
  }
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(io:, tty: nil, min_interval: nil, ascii: nil, profile: nil, clock: nil) ⇒ ProgressReporter

Returns a new instance of ProgressReporter.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/structured_data_to_sql/progress_reporter.rb', line 38

def initialize(
  io:,
  tty: nil,
  min_interval: nil,
  ascii: nil,
  profile: nil,
  clock: nil
)
  @io = io
  @tty = tty.nil? ? (io.respond_to?(:tty?) && io.tty?) : tty
  @min_interval = min_interval || (@tty ? TTY_INTERVAL : PLAIN_INTERVAL)
  @glyphs =
    GLYPHS.fetch(
      ascii.nil? ? (@tty ? :unicode : :ascii) : (ascii ? :ascii : :unicode)
    )
  @profile = profile
  @clock = clock || -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
  @live_line = nil
  @last_status_at = nil
  @total_input_bytes = nil
  @last_line_blank = false
end

Instance Method Details

#call(event) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/structured_data_to_sql/progress_reporter.rb', line 67

def call(event)
  case event.type
  when :start
    start(event)
  when :file_start
    file_start(event)
  when :pass_complete
    @last_status_at = nil if @tty
    status(event)
  when :bytes, :rows, :table
    status(event)
  when :file_complete
    file_complete(event)
  when :warning
    warning(event)
  when :complete
    complete(event)
  end
rescue IOError, Errno::EPIPE
  nil
end

#converter_intervalObject

Recommended converter throttle so the event stream matches the display cadence; the converters only build a payload when this interval passes.



63
64
65
# File 'lib/structured_data_to_sql/progress_reporter.rb', line 63

def converter_interval
  @min_interval
end