<commit_before>require 'csv'
class CsvDb
  class << self

    def convert_save(csv_data)
      p csv_data
      csv_file = csv_data.read
      CSV.parse(csv_file) do |row|
        if row[0] == "Store :"
          # First row of the receipt
          p "New purchase. Customer: #{row[11]}"
          @user = User.find_or_create_by(email: row[11])
          @purchase = Purchase.new({
                                    vendor:           'Durham Coop Market',
                                    sender_email:     'CSV Import',
                                    order_unique_id:  row[13],
                                    order_date:       row[3]
                                  })
        elsif row[0] == "total"
          # Last row of the receipt, before next receipt starts
          @purchase.total_price = row[4]
          p "User: #{@user.email}, Order: #{@purchase.order_unique_id}, Total: #{@purchase.total_price}"
          @user.purchases << @purchase
          p @user.purchases
          @user.save
        else
          @purchase.itemizables << build_itemizable(row)
        end

      end
    end

    def build_itemizable(row)
      item = Item.find_or_create_by({name: row[1], description: "ID: #{row[0]}"})

      itemizable = Itemizable.new({quantity: row[2], total_price: row[4], price_breakdown: "#{row[2]} x $#{row[3]}"})
      itemizable.item = item

      return itemizable
    end
  end
end
<commit_msg>Cleanup logging.<commit_after>require 'csv'
class CsvDb
  class << self

    def convert_save(csv_data)
      csv_file = csv_data.read
      CSV.parse(csv_file) do |row|
        if row[0] == "Store :"
          # First row of the receipt
          @user = User.find_or_create_by(email: row[11])
          @purchase = Purchase.new({
                                    vendor:           'Durham Coop Market',
                                    sender_email:     'CSV Import',
                                    order_unique_id:  row[13],
                                    order_date:       row[3],
                                    raw_message:      "Filename: #{csv_data.original_filename}"
                                  })
        elsif row[0] == "total"
          # Last row of the receipt, before next receipt starts
          @purchase.total_price = row[4]
          @user.purchases << @purchase
          @user.save
          p "Purchase Imported @ #{Time.now} -> User: #{@user.email}, Order: #{@purchase.order_unique_id}, Items: #{@purchase.itemizables.length}, Total: #{@purchase.total_price}, Filename: #{csv_data.original_filename}"

        else
          @purchase.itemizables << build_itemizable(row)
        end

      end
    end

    def build_itemizable(row)
      item = Item.find_or_create_by({name: row[1], description: "ID: #{row[0]}"})

      itemizable = Itemizable.new({quantity: row[2], total_price: row[4], price_breakdown: "#{row[2]} x $#{row[3]}"})
      itemizable.item = item

      return itemizable
    end
  end
end

{'fraction_non_alphanumeric': 0.11627129969929836, 'fraction_numerical': 0.010023387905111928, 'mean_word_length': 1.6779964221824686, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1736030', 'n_tokens_mistral': 926, 'n_tokens_neox': 878, 'n_words': 202}