Class: Transplant::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/transplant/stats.rb

Instance Method Summary collapse

Constructor Details

#initialize(transplanter) ⇒ Stats

Returns a new instance of Stats.



4
5
6
7
# File 'lib/transplant/stats.rb', line 4

def initialize(transplanter)
  @result_set = []
  @planter = transplanter
end

Instance Method Details

#add_to_results(output) ⇒ Object



26
27
28
29
30
# File 'lib/transplant/stats.rb', line 26

def add_to_results(output)
  style = ::Transplant::Configuration.output_style
  puts output if [:both, :output].include?(style)
  @result_set << output if [:both, :file].include?(style)
end

#array_output(header, array, depth = 0, sub_array = false) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/transplant/stats.rb', line 53

def array_output(header, array, depth = 0, sub_array = false)
  add_to_results tabs(depth) + "#{header}:" if array.any? && header.present?
  array.each do |item|
    if item.is_a?(Hash) || item.is_a?(Array)
      output("", item, depth + 1, sub_hash = true)
    else
      add_to_results tabs(depth + 1) + "* #{item}"
    end
  end
end

#failuresObject



86
87
88
89
90
91
92
93
# File 'lib/transplant/stats.rb', line 86

def failures
  if @planter.failures.count <= 0
    add_to_results "\nNo failed record imports!!!! Time to par-tay!!!!\n"
  else
    count = Hash[@planter.failures.map { |key, value| [key.tableize.humanize, value] }]
    output("\nEstimated number failed imports to #{@planter.app_name}", count)
  end
end

#hash_output(header, hash, depth = 0, sub_hash = false) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/transplant/stats.rb', line 42

def hash_output(header, hash, depth = 0, sub_hash = false)
  add_to_results tabs(depth) + "#{header}:" if hash.any? && header.present?
  hash.each_pair do |key, value|
    if value.is_a?(Hash) || value.is_a?(Array)
      output(key, value, depth + 1, sub_hash = true)
    else
      add_to_results tabs(depth + 1) + "* #{key}: #{value}"
    end
  end
end

#output(header, input = {}, depth = 0, sub_output = false) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/transplant/stats.rb', line 32

def output(header, input = {}, depth = 0, sub_output = false)
  if input.is_a? Hash
    hash_output(header, input, depth, sub_output)
  elsif input.is_a? Array
    array_output(header, input, depth, sub_output)
  elsif input.is_a? String
    add_to_results input
  end
end

#output_all_info(opts = {}) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/transplant/stats.rb', line 9

def output_all_info(opts = {})
  total_import_time(opts[:measurement]) if opts[:measurement].present?
  total_import_records
  successes
  failures
  output_to_file
end

#output_to_fileObject



17
18
19
20
21
22
23
24
# File 'lib/transplant/stats.rb', line 17

def output_to_file
  timestamp = Time.now.utc.to_datetime.to_formatted_s(:number)
  Dir.chdir(::Transplant::Configuration.root_path)
  filepath = ".import.#{timestamp}_#{@planter.app_name.downcase}"
  f = File.open(filepath, "w")
  @result_set.each { |output| f.puts output }
  f.close
end

#successesObject



77
78
79
80
81
82
83
84
# File 'lib/transplant/stats.rb', line 77

def successes
  if @planter.successes.count <= 0
    add_to_results "\nNo records were imported! Boo!!!!\n"
  else
    count = Hash[@planter.successes.map { |key, value| [key.tableize.humanize, value] }]
    output("\nEstimated number of successful imports to #{@planter.app_name}", count)
  end
end

#total_import_recordsObject



69
70
71
# File 'lib/transplant/stats.rb', line 69

def total_import_records
  add_to_results "Estimated number of records imported into #{@planter.app_name}: #{@planter.total_successes}"
end

#total_import_time(m) ⇒ Object



73
74
75
# File 'lib/transplant/stats.rb', line 73

def total_import_time(m)
  add_to_results "Total time taken to import everything into #{@planter.app_name}: #{(m.real/60).round(2)} minutes"
end