class Elastics::ProgBar

Attributes

pbar[R]
total_count[R]

Public Class Methods

new(total_count, batch_size=nil, prefix_message=nil) click to toggle source
# File lib/elastics/prog_bar.rb, line 6
def initialize(total_count, batch_size=nil, prefix_message=nil)
  @successful_count = 0
  @failed_count     = 0
  puts
  message = "#{prefix_message}Processing #{total_count} documents"
  message << " in batches of #{batch_size}" unless batch_size.nil?
  Prompter.say_log message
  @pbar             = ::ProgressBar.create(:title         => title(:ok),
                                           :total         => total_count,
                                           :progress_mark => (Dye.color? ? ' ' : '|'),
                                           :format        => ('%t%c/%C %p%% %E %b' + dye(:background, '%i', '%i')))
end

Public Instance Methods

finish() click to toggle source
# File lib/elastics/prog_bar.rb, line 35
def finish
  @pbar.finish unless @pbar.finished?
  Prompter.say_log     "Processed #{@pbar.total}. "
  Prompter.say_ok      "Successful #{@successful_count}. "
  Prompter.say_notice  "Skipped #{@pbar.total - @successful_count - @failed_count}. "
  Prompter.say_warning "Failed #{@failed_count}.", :mute => true
  Prompter.say_warning "See the log for the details about the #{@failed_count} failures." unless @failed_count == 0
end
process_result(result, inc) click to toggle source
# File lib/elastics/prog_bar.rb, line 19
def process_result(result, inc)
  unless result.nil? || result.empty?
    if result.failed.size > 0
      Conf.logger.error "Failed load:\n#{result.failed.to_yaml}"
      @pbar.title         = title(:failed)
      @pbar.progress_mark = 'F' unless Dye.color?
    end
    @failed_count     += result.failed.size
    @successful_count += result.successful.size
  end
  new_progress   = @pbar.progress + inc
  # avoids an error in case progress > total (may happen in import)
  @pbar.total    = (new_progress + 1) if new_progress > @pbar.total
  @pbar.progress = new_progress
end

Private Instance Methods

color(how) click to toggle source
# File lib/elastics/prog_bar.rb, line 55
def color(how)
  case how
  when :background then :blue
  when :ok         then :green
  when :failed     then :red
  end
end
dye(how, colored, plain) click to toggle source
# File lib/elastics/prog_bar.rb, line 51
def dye(how, colored, plain)
  Dye.dye colored, plain, :reversed, :bold, color(how)
end
title(how) click to toggle source
# File lib/elastics/prog_bar.rb, line 46
def title(how)
  return '' unless Dye.color?
  Dye.sgr(:clear, color(how), :reversed, :bold) + ' '
end