<commit_before>module Amr
  class TariffUpserter
    def initialize(array_of_price_hashes, array_of_standing_charge_hashes, import_log)
      @array_of_price_hashes = array_of_price_hashes
      @array_of_standing_charge_hashes = array_of_standing_charge_hashes
      @import_log = import_log
    end

    def perform
      ActiveRecord::Base.transaction do
        upsert_prices
        upsert_standing_charges
      end
    end

    def upsert_prices
      records_count_before = TariffPrice.count
      data_for_upsert = add_import_log_id_and_dates_to_hash(@array_of_price_hashes)

      result = TariffPrice.upsert_all(data_for_upsert, unique_by: [:meter_id, :tariff_date])

      inserted_count = TariffPrice.count - records_count_before
      updated_count = result.rows.flatten.size - inserted_count

      @import_log.update(prices_imported: inserted_count, prices_updated: updated_count)

      Rails.logger.info "Updated #{updated_count} Inserted #{inserted_count}"
    end

    def upsert_standing_charges
      records_count_before = TariffStandingCharge.count
      data_for_upsert = add_import_log_id_and_dates_to_hash(@array_of_standing_charge_hashes)

      result = TariffStandingCharge.upsert_all(data_for_upsert, unique_by: [:meter_id, :start_date])

      inserted_count = TariffStandingCharge.count - records_count_before
      updated_count = result.rows.flatten.size - inserted_count

      @import_log.update(standing_charges_imported: inserted_count, standing_charges_updated: updated_count)

      Rails.logger.info "Updated #{updated_count} Inserted #{inserted_count}"
    end

    private

    def add_import_log_id_and_dates_to_hash(arr)
      created_at = DateTime.now.utc
      updated_at = DateTime.now.utc
      arr.each do |reading|
        reading[:tariff_import_log_id] = @import_log.id
        reading[:created_at] = created_at
        reading[:updated_at] = updated_at
      end
      arr
    end
  end
end
<commit_msg>Import log naming<commit_after>module Amr
  class TariffUpserter
    def initialize(array_of_price_hashes, array_of_standing_charge_hashes, tariff_import_log)
      @array_of_price_hashes = array_of_price_hashes
      @array_of_standing_charge_hashes = array_of_standing_charge_hashes
      @tariff_import_log = tariff_import_log
    end

    def perform
      ActiveRecord::Base.transaction do
        upsert_prices
        upsert_standing_charges
      end
    end

    def upsert_prices
      records_count_before = TariffPrice.count
      data_for_upsert = add_import_log_id_and_dates_to_hash(@array_of_price_hashes)

      result = TariffPrice.upsert_all(data_for_upsert, unique_by: [:meter_id, :tariff_date])

      inserted_count = TariffPrice.count - records_count_before
      updated_count = result.rows.flatten.size - inserted_count

      @tariff_import_log.update(prices_imported: inserted_count, prices_updated: updated_count)

      Rails.logger.info "Updated #{updated_count} Inserted #{inserted_count}"
    end

    def upsert_standing_charges
      records_count_before = TariffStandingCharge.count
      data_for_upsert = add_import_log_id_and_dates_to_hash(@array_of_standing_charge_hashes)

      result = TariffStandingCharge.upsert_all(data_for_upsert, unique_by: [:meter_id, :start_date])

      inserted_count = TariffStandingCharge.count - records_count_before
      updated_count = result.rows.flatten.size - inserted_count

      @tariff_import_log.update(standing_charges_imported: inserted_count, standing_charges_updated: updated_count)

      Rails.logger.info "Updated #{updated_count} Inserted #{inserted_count}"
    end

    private

    def add_import_log_id_and_dates_to_hash(arr)
      created_at = DateTime.now.utc
      updated_at = DateTime.now.utc
      arr.each do |reading|
        reading[:tariff_import_log_id] = @tariff_import_log.id
        reading[:created_at] = created_at
        reading[:updated_at] = updated_at
      end
      arr
    end
  end
end

{'fraction_non_alphanumeric': 0.06313131313131314, 'fraction_numerical': 0.0, 'mean_word_length': 4.007585335018963, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5545428', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1286, 'n_words': 198}