aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/simple_importer.rb
blob: bf6f3b406bb14485439afd46882e1bd060bcac62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
class SimpleImporter < ActiveRecord::Base
  attr_accessor :configuration

  def self.define name
    @importers ||= {}
    configuration = Configuration.new name
    yield configuration
    configuration.validate!
    @importers[name.to_sym] = configuration
  end

  def self.find_configuration name
    @importers ||= {}
    configuration = @importers[name.to_sym]
    raise "Importer not found: #{name}" unless configuration
    configuration
  end

  def initialize *args
    super *args
    self.configuration = self.class.find_configuration self.configuration_name
    self.journal ||= []
  end

  def configure
    new_config = configuration.duplicate
    yield new_config
    new_config.validate!
    self.configuration = new_config
  end

  def context
    self.configuration.context || {}
  end

  def resolve col_name, value, &block
    val = block.call(value)
    return val if val.present?
    @resolution_queue[[col_name.to_s, value]].push({record: @current_record, attribute: @current_attribute, block: block})
    nil
  end

  def import opts={}
    @verbose = opts.delete :verbose
    @resolution_queue = Hash.new{|h,k| h[k] = []}
    number_of_lines = 0
    padding = 1
    fail_with_error "File not found: #{self.filepath}" do
      number_of_lines = CSV.read(self.filepath, self.configuration.csv_options).length
      padding = [1, Math.log(number_of_lines, 10).ceil()].max
    end

    current_line = 0
    status = :success
    statuses = ""
    log "#{"%#{padding}d" % 0}/#{number_of_lines}", clear: true
    ActiveRecord::Base.transaction do
      self.configuration.before_actions(:all).each do |action| action.call self end

      CSV.foreach(filepath, self.configuration.csv_options) do |row|
        status = handle_row row, status

        fail_with_error ->(){ @current_record.errors.messages } do
          new_record = @current_record.new_record?
          self.configuration.before_actions(:each_save).each do |action|
            action.call self, @current_record
          end
          ### This could fail if the record has a mandatory relation which is not yet resolved
          ### TODO: do not attempt to save if the current record if waiting for resolution
          ###       and fail at the end if there remains unresolved relations
          @current_record.save!
          self.journal.push({event: (new_record ? :creation : :update), kind: :log})
          statuses += new_record ? colorize("✓", :green) : colorize("-", :orange)
        end
        self.configuration.columns.each do |col|
          if col.name && @resolution_queue.any?
            val = @current_record.send col[:attribute]
            (@resolution_queue.delete([col.name, val]) || []).each do |res|
              record = res[:record]
              attribute = res[:attribute]
              value = res[:block].call(val, record)
              record.send "#{attribute}=", value
              record.save!
            end
          end
        end
        current_line += 1
        log "#{"%#{padding}d" % current_line}/#{number_of_lines}: #{statuses}", clear: true
      end
      self.configuration.after_actions(:all).each do |action|
        action.call self
      end
    end
    self.update_attribute :status, status
  rescue FailedImport
    self.update_attribute :status, :failed
  ensure
    self.save!
  end

  def fail_with_error msg
    begin
      yield
    rescue => e
      msg = msg.call if msg.is_a?(Proc)
      log "\nFAILED: \n errors: #{msg}\n exception: #{e.message}\n#{e.backtrace.join("\n")}", color: :red
      self.journal.push({message: msg, error: e.message, event: :error, kind: :error})
      raise FailedImport
    end
  end

  protected

  def handle_row row, status
    if self.configuration.get_custom_handler
      instance_exec(row, &self.configuration.get_custom_handler)
    else
      @current_record = self.configuration.find_record row
      self.configuration.columns.each do |col|
        @current_attribute = col[:attribute]
        val = col[:value]
        if val.nil? || val.is_a?(Proc)
          if row.has_key? col.name
            if val.is_a?(Proc)
              val = instance_exec(row[col.name], &val)
            else
              val = row[col.name]
            end
          else
            self.journal.push({event: :column_not_found, message: "Column not found: #{col.name}", kind: :warning})
            status = :success_with_warnings
          end
        end
        @current_record.send "#{@current_attribute}=", val if val
      end
    end
    status
  end

  def colorize txt, color
    color = {
      red: "31",
      green: "32",
      orange: "33",
    }[color] || "33"
    "\e[#{color}m#{txt}\e[0m"
  end

  def log msg, opts={}
    return unless @verbose
    out = ""
    msg = colorize(msg, opts[:color]) if opts[:color]
    if opts[:clear] && @prev_msg_size
      out += "\b"*@prev_msg_size
    end
    out += msg
    print out
    @prev_msg_size = msg.size
  end

  class FailedImport < RuntimeError
  end

  class Configuration
    attr_accessor :model, :headers, :separator, :key, :context, :encoding
    attr_reader :columns

    def initialize import_name, opts={}
      @import_name = import_name
      @key = opts[:key] || "id"
      @headers = opts.has_key?(:headers) ? opts[:headers] : true
      @separator = opts[:separator] || ","
      @encoding = opts[:encoding]
      @columns = opts[:columns] || []
      @model = opts[:model]
      @custom_handler = opts[:custom_handler]
    end

    def duplicate
      Configuration.new @import_name, self.options
    end

    def options
      {
        key: @key,
        headers: @headers,
        separator: @separator,
        encoding: @encoding,
        columns: @columns.map(&:duplicate),
        model: model,
        custom_handler: @custom_handler
      }
    end

    def validate!
      raise "Incomplete configuration, missing model for #{@import_name}" unless model.present?
    end

    def attribute_for_col col_name
      column = self.columns.find{|c| c.name == col_name}
      column && column[:attribute] || col_name
    end

    def find_record attrs
      model.find_or_initialize_by(attribute_for_col(@key) => attrs[@key.to_s])
    end

    def csv_options
      {
        headers: self.headers,
        col_sep: self.separator,
        encoding: self.encoding
      }
    end

    def add_column name, opts={}
      @columns.push Column.new({name: name.to_s}.update(opts))
    end

    def add_value attribute, value
      @columns.push Column.new({attribute: attribute, value: value})
    end

    def before group=:all, &block
      @before ||= Hash.new{|h, k| h[k] = []}
      @before[group].push block
    end

    def after group=:all, &block
      @after ||= Hash.new{|h, k| h[k] = []}
      @after[group].push block
    end

    def before_actions group=:all
      @before ||= Hash.new{|h, k| h[k] = []}
      @before[group]
    end

    def after_actions group=:all
      @after ||= Hash.new{|h, k| h[k] = []}
      @after[group]
    end

    def custom_handler &block
      @custom_handler = block
    end

    def get_custom_handler
      @custom_handler
    end

    class Column
      attr_accessor :name
      def initialize opts={}
        @name = opts[:name]
        @options = opts
        @options[:attribute] ||= @name
      end

      def duplicate
        Column.new @options.dup
      end

      def [](key)
        @options[key]
      end
    end
  end
end