Class: StructuredDataToSql::MysqlDumpXml::TableDiscovery::DiscoveryProgress

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

Instance Method Summary collapse

Constructor Details

#initialize(callback:, on_progress:, interval:, total_input_bytes:) ⇒ DiscoveryProgress

Returns a new instance of DiscoveryProgress.



165
166
167
168
169
170
171
172
173
174
# File 'lib/structured_data_to_sql/mysql_dump_xml/table_discovery.rb', line 165

def initialize(callback:, on_progress:, interval:, total_input_bytes:)
  @callback = callback
  @on_progress = on_progress
  @interval = interval
  @total_input_bytes = total_input_bytes
  @started_at = Time.now
  @last_progress_at = nil
  @bytes_scanned = 0
  @current_file = nil
end

Instance Method Details

#advance(bytes, tables:) ⇒ Object



195
196
197
198
199
# File 'lib/structured_data_to_sql/mysql_dump_xml/table_discovery.rb', line 195

def advance(bytes, tables:)
  @bytes_scanned += bytes
  @current_file[:bytes_scanned] += bytes if @current_file
  report(:bytes, tables: tables)
end

#finish_file(force:) ⇒ Object



201
202
203
204
# File 'lib/structured_data_to_sql/mysql_dump_xml/table_discovery.rb', line 201

def finish_file(force:)
  report(:file_complete, force: force)
  @current_file = nil
end

#report(event, force: false, **extra) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/structured_data_to_sql/mysql_dump_xml/table_discovery.rb', line 206

def report(event, force: false, **extra)
  return unless @callback || @on_progress

  now = Time.now
  if !force && @last_progress_at && now - @last_progress_at < @interval
    return
  end

  @last_progress_at = now
  payload = {
    event: event,
    elapsed: now - @started_at,
    bytes_scanned: @bytes_scanned,
    total_input_bytes: @total_input_bytes,
    current_file: @current_file&.dup
  }.merge(extra)
  @callback&.call(payload)
  @on_progress&.call(
    ProgressEvent.new(event, payload.reject { |key, _| key == :event })
  )
end

#start_file(file, index:, count:) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/structured_data_to_sql/mysql_dump_xml/table_discovery.rb', line 176

def start_file(file, index:, count:)
  @current_file = {
    path: file.to_s,
    name: file.basename.to_s,
    index: index,
    count: count,
    size:
      (
        begin
          file.size
        rescue StandardError
          0
        end
      ),
    bytes_scanned: 0
  }
  report(:file_start, force: true)
end