diff options
Diffstat (limited to 'app/models/simple_interface.rb')
| -rw-r--r-- | app/models/simple_interface.rb | 89 | 
1 files changed, 79 insertions, 10 deletions
| diff --git a/app/models/simple_interface.rb b/app/models/simple_interface.rb index 11b9d0dcd..c20b70875 100644 --- a/app/models/simple_interface.rb +++ b/app/models/simple_interface.rb @@ -1,5 +1,5 @@  class SimpleInterface < ActiveRecord::Base -  attr_accessor :configuration +  attr_accessor :configuration, :interfaces_group    class << self      def configuration_class @@ -39,6 +39,8 @@ class SimpleInterface < ActiveRecord::Base      @padding = 1      @current_line = -1      @padding = [1, Math.log([@number_of_lines, 1].max, 10).ceil()].max +    @output_dir = opts[:output_dir] || Rails.root.join('tmp', self.class.name.tableize) +    @start_time = Time.now    end    def configure @@ -70,16 +72,56 @@ class SimpleInterface < ActiveRecord::Base    def log msg, opts={}      msg = msg.to_s      msg = colorize msg, opts[:color] if opts[:color] +    @start_time ||= Time.now +    time = Time.now - @start_time +    @messages ||= []      if opts[:append] -      @messages[-1] = (@messages[-1] || "") + msg +      _time, _msg = @messages.pop || [] +      _time ||= time +      _msg ||= "" +      @messages.push [_time, _msg+msg] +    elsif opts[:replace] +      @messages.pop +      @messages << [time, msg]      else -      @messages << msg +      @messages << [time, msg] +    end +    print_state true +  end + +  def output_filepath +    @output_filepath ||= File.join @output_dir, "#{self.configuration_name}_#{Time.now.strftime "%y%m%d%H%M"}_out.csv" +  end + +  def write_output_to_csv +    filepath = +    cols = %i(line kind event message error) +    if self.journal.size > 0 && self.journal.first[:row].present? +      log "Writing output log" +      FileUtils.mkdir_p @output_dir +      keys = self.journal.first[:row].map(&:first) +      CSV.open(output_filepath, "w") do |csv| +        csv << cols + keys +        self.journal.each do |j| +          csv << cols.map{|c| j[c]} + j[:row].map(&:last) +        end +      end +      log "Output written in #{filepath}", replace: true      end -    print_state    end    protected +  def task_finished +    log "Saving..." +    self.save! +    log "Saved", replace: true +    write_output_to_csv +    log "FINISHED, status: " +    log status, color: SimpleInterface.status_color(status), append: true +    print_state true +  end +    def push_in_journal data      line = (@current_line || 0) + 1      line += 1 if configuration.headers @@ -90,7 +132,7 @@ class SimpleInterface < ActiveRecord::Base      end    end -  def colorize txt, color +  def self.colorize txt, color      color = {        red: "31",        green: "32", @@ -99,12 +141,24 @@ class SimpleInterface < ActiveRecord::Base      "\e[#{color}m#{txt}\e[0m"    end -  def print_state +  def self.status_color status +    color = :green +    color = :orange if status.to_s == "success_with_warnings" +    color = :red if status.to_s == "error" +    color +  end + +  def colorize txt, color +    SimpleInterface.colorize txt, color +  end + +  def print_state force=false      return unless @verbose +    return if !@last_repaint.nil? && (Time.now - @last_repaint < 0.5) && !force      @status_width ||= begin -      term_width = %x(tput cols).to_i -      term_width - @padding - 10 +      @term_width = %x(tput cols).to_i +      @term_width - @padding - 10      rescue        100      end @@ -116,12 +170,24 @@ class SimpleInterface < ActiveRecord::Base        50      end +    msg = "" + +    if @banner.nil? && interfaces_group.present? +      @banner = interfaces_group.banner @status_width +      @status_height -= @banner.lines.count + 2 +    end + +    if @banner.present? +      msg += @banner +      msg += "\n" + "-"*@term_width + "\n" +    end +      full_status = @statuses || ""      full_status = full_status.last(@status_width*10) || ""      padding_size = [(@number_of_lines - @current_line - 1), (@status_width - full_status.size/10)].min      full_status = "#{full_status}#{"."*[padding_size, 0].max}" -    msg = "#{"%#{@padding}d" % (@current_line + 1)}/#{@number_of_lines}: #{full_status}" +    msg += "#{"%#{@padding}d" % (@current_line + 1)}/#{@number_of_lines}: #{full_status}"      lines_count = [(@status_height / 2) - 3, 1].max @@ -129,7 +195,9 @@ class SimpleInterface < ActiveRecord::Base        msg += "\n\n"        msg += colorize "=== MESSAGES (#{@messages.count}) ===\n", :green        msg += "[...]\n" if @messages.count > lines_count -      msg += @messages.last(lines_count).map{|m| m.truncate(@status_width)}.join("\n") +      msg += @messages.last(lines_count).map do |m| +        "[#{"%.5f" % m[0]}]\t" + m[1].truncate(@status_width - 10) +      end.join("\n")        msg += "\n"*[lines_count-@messages.count, 0].max      end @@ -146,6 +214,7 @@ class SimpleInterface < ActiveRecord::Base        end.join("\n")      end      custom_print msg, clear: true +    @last_repaint = Time.now    end    def custom_print msg, opts={} | 
