<commit_before># frozen_string_literal: true

module BudgetsFactory

  extend ActiveSupport::Concern

  INDEXES = {
    forecast: GobiertoBudgets::SearchEngineConfiguration::BudgetLine.index_forecast,
    forecast_updated: GobiertoBudgets::SearchEngineConfiguration::BudgetLine.index_forecast_updated,
    executed: GobiertoBudgets::SearchEngineConfiguration::BudgetLine.index_executed
  }

  class_methods do
    def default_amount
      123_456.789
    end

    def default_population
      100
    end

    def default_place
      INE::Places::Place.find_by_slug("madrid")
    end

    def default_organization_id
      default_place.id
    end

    def default_code
      "1"
    end

    def default_kind
      GobiertoData::GobiertoBudgets::EXPENSE
    end

    def default_area
      GobiertoData::GobiertoBudgets::ECONOMIC_AREA_NAME
    end

    def default_year
      Time.now.year
    end

    def base_data(params = {})
      place_attributes(params).merge(
        organization_id: params[:organization_id] || default_organization_id,
        year: params[:year] || default_year
      )
    end

    def place_attributes(params = {})
      place = params[:place] || default_place

      {
        ine_code: place.id,
        province_id: place.province_id,
        autonomy_id: place.province.autonomous_region_id
      }
    end
  end

  attr_accessor :client, :created_documents

  def initialize(params = {})
    self.client = GobiertoBudgets::SearchEngine.client
    elasticsearch_url = client.instance_variable_get(:@options)[:url]

    unless (Rails.env.development? || Rails.env.test?) && elasticsearch_url.include?("localhost")
      raise "ERROR: it's not safe to run a BudgetsFactory in #{Rails.env} pointing to #{elasticsearch_url}"
    end

    indexes = if params[:indexes]
                params[:indexes].map { |index_alias| INDEXES[index_alias] }
              else
                INDEXES.values
              end
    params.delete(:indexes)

    documents = indexes.map { |index| build_document(index, params) }
    @result = bulk(body: documents)
  end

  def teardown
    bulk(body: teardown_body)
  end

  private

  def bulk(params = {})
    client.bulk(params)
  end

  def teardown_body
    created_documents = @result["items"].map do |doc|
      doc["index"].slice("_index", "_type", "_id")
    end

    created_documents.map { |doc_summary| { delete: doc_summary } }
  end
end<commit_msg>Revert "Speedup factory"<commit_after># frozen_string_literal: true

module BudgetsFactory

  extend ActiveSupport::Concern

  INDEXES = {
    forecast: GobiertoBudgets::SearchEngineConfiguration::BudgetLine.index_forecast,
    forecast_updated: GobiertoBudgets::SearchEngineConfiguration::BudgetLine.index_forecast_updated,
    executed: GobiertoBudgets::SearchEngineConfiguration::BudgetLine.index_executed
  }

  class_methods do
    def default_amount
      123_456.789
    end

    def default_population
      100
    end

    def default_place
      INE::Places::Place.find_by_slug("madrid")
    end

    def default_organization_id
      default_place.id
    end

    def default_code
      "1"
    end

    def default_kind
      GobiertoData::GobiertoBudgets::EXPENSE
    end

    def default_area
      GobiertoData::GobiertoBudgets::ECONOMIC_AREA_NAME
    end

    def default_year
      Time.now.year
    end

    def base_data(params = {})
      place_attributes(params).merge(
        organization_id: params[:organization_id] || default_organization_id,
        year: params[:year] || default_year
      )
    end

    def place_attributes(params = {})
      place = params[:place] || default_place

      {
        ine_code: place.id,
        province_id: place.province_id,
        autonomy_id: place.province.autonomous_region_id
      }
    end
  end

  attr_accessor :client, :created_documents

  def initialize(params = {})
    self.client = GobiertoBudgets::SearchEngine.client
    elasticsearch_url = client.instance_variable_get(:@options)[:url]

    unless (Rails.env.development? || Rails.env.test?) && elasticsearch_url.include?("localhost")
      raise "ERROR: it's not safe to run a BudgetsFactory in #{Rails.env} pointing to #{elasticsearch_url}"
    end

    indexes = if params[:indexes]
                params[:indexes].map { |index_alias| INDEXES[index_alias] }
              else
                INDEXES.values
              end
    params.delete(:indexes)

    documents = indexes.map { |index| build_document(index, params) }
    result = bulk(body: documents)

    self.created_documents = result["items"].map do |doc|
      doc["index"].slice("_index", "_type", "_id")
    end
  end

  def teardown
    bulk(body: teardown_body)
  end

  private

  def bulk(params = {})
    result = client.bulk(params)
    sleep 1 # wait for search index to be ready
    result
  end

  def teardown_body
    created_documents.map { |doc_summary| { delete: doc_summary } }
  end

end
{'fraction_non_alphanumeric': 0.09179331306990882, 'fraction_numerical': 0.00547112462006079, 'mean_word_length': 3.0592105263157894, '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': '3726106', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1509, 'n_words': 300}