<commit_before>class Trade::Filter
  def initialize(options)
    initialize_params(options)
    initialize_query
  end

  def results
    @query.limit(@options[:per_page]).
      offset(@options[:per_page] * (@options[:page] - 1)).all
  end

  def total_cnt
    @query.count
  end

  private
  
  def initialize_params(options)
    @options = Trade::SearchParams.sanitize(options)
    @options.keys.each { |k| instance_variable_set("@#{k}", @options[k]) }
  end

  def initialize_query
    @query = Trade::Shipment.includes([
      :exporter, :importer, :country_of_origin, :purpose,
      :source, :term, :unit, :country_of_origin_permit,
      :import_permit, :export_permits, :taxon_concept
    ]).order('year DESC')

    unless @taxon_concepts_ids.empty?
      @query = @query.where(:taxon_concept_id => @taxon_concepts_ids)
    end

    unless @appendices.empty?
      @query = @query.where(:appendix => @appendices)
    end
    
    unless @time_range_start == 0 || @time_range_end == 0
      @query = @query.where(:year => @time_range_start..@time_range_end)
    end

    unless @terms_ids.empty?
      @query = @query.where(:term_id => @terms_ids)
    end

    unless @units_ids.empty?
      @query = @query.where(:unit_id => @units_ids)
    end

    unless @purposes_ids.empty?
      @query = @query.where(:purpose_id => @purposes_ids)
    end

    unless @sources_ids.empty?
      @query = @query.where(:source_id => @sources_ids)
    end

    unless @importers_ids.empty?
      @query = @query.where(:importer_id => @importers_ids)
    end

    unless @exporters_ids.empty?
      @query = @query.where(:exporter_id => @exporters_ids)
    end

    unless @countries_of_origin_ids.empty?
      @query = @query.where(:country_of_origin_id => @countries_of_origin_ids)
    end

    unless @reporter_type.nil?
      if @reporter_type == 'E'
        @query = @query.where(:reported_by_exporter => true)
      elsif @reporter_type == 'I'
        @query = @query.where(:reported_by_exporter => false)
      else
      end
    end

    unless @permits_ids.empty?
      @query = @query.where("import_permit_id = ? or country_of_origin_permit_id = ?",  @permits_ids , @permits_ids)
    end

    unless @quantity.nil?
      @query = @query.where(:quantity => @quantity)<commit_msg>Fixed bugs on permits_ids and time_range<commit_after>class Trade::Filter
  def initialize(options)
    initialize_params(options)
    initialize_query
  end

  def results
    @query.limit(@options[:per_page]).
      offset(@options[:per_page] * (@options[:page] - 1)).all
  end

  def total_cnt
    @query.count
  end

  private
  
  def initialize_params(options)
    @options = Trade::SearchParams.sanitize(options)
    @options.keys.each { |k| instance_variable_set("@#{k}", @options[k]) }
  end

  def initialize_query
    @query = Trade::Shipment.includes([
      :exporter, :importer, :country_of_origin, :purpose,
      :source, :term, :unit, :country_of_origin_permit,
      :import_permit, :export_permits, :taxon_concept
    ]).order('year DESC')


    # Id's (array)
    unless @taxon_concepts_ids.empty?
      @query = @query.where(:taxon_concept_id => @taxon_concepts_ids)
    end

    unless @appendices.empty?
      @query = @query.where(:appendix => @appendices)
    end
    
    unless @terms_ids.empty?
      @query = @query.where(:term_id => @terms_ids)
    end

    unless @units_ids.empty?
      @query = @query.where(:unit_id => @units_ids)
    end

    unless @purposes_ids.empty?
      @query = @query.where(:purpose_id => @purposes_ids)
    end

    unless @sources_ids.empty?
      @query = @query.where(:source_id => @sources_ids)
    end

    unless @importers_ids.empty?
      @query = @query.where(:importer_id => @importers_ids)
    end

    unless @exporters_ids.empty?
      @query = @query.where(:exporter_id => @exporters_ids)
    end

    unless @countries_of_origin_ids.empty?
      @query = @query.where(:country_of_origin_id => @countries_of_origin_ids)
    end

    # Other cases

    unless @time_range_start == 0 && @time_range_end == 0
      if @time_range_start == 0
        @query = @query.where(:year => 1900..@time_range_end)
      elsif @time_range_end == 0
        @query = @query.where(:year => @time_range_start..2200)
      else
        @query = @query.where(:year => @time_range_start..@time_range_end)
      end
    end


    unless @reporter_type.nil?
      if @reporter_type == 'E'
        @query = @query.where(:reported_by_exporter => true)
      elsif @reporter_type == 'I'
        @query = @query.where(:reported_by_exporter => false)
      else
      end
    end

    unless @permits_ids.empty?
      @query = @query.where("import_permit_id = ? or country_of_origin_permit_id = ? 
                             or trade_shipment_export_permits.trade_permit_id = ?",
                             @permits_ids , @permits_ids, @permits_ids)
                      
    end

    unless @quantity.nil?
      @query = @query.where(:quantity => @quantity)
{'fraction_non_alphanumeric': 0.1278813389456805, 'fraction_numerical': 0.0032070555221487274, 'mean_word_length': 3.0177133655394526, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '892561', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1671, 'n_words': 303}