<commit_before># frozen_string_literal: true

# A class that knows about Elasticsearch configuration and usage.
class Elasticsearch
  KB = 1024
  MB = KB * KB
  GB = MB * KB
  MINMEMORY = 1 * KB
  MAXMEMORY = 26 * KB

  def self.node_memory_in_units(node, _which, unit)
    node[:memory][:total] =~ /^(\d+)kB/
    mem_in_kb = Regexp.last_match(1).to_i
    case unit
    when :kb, :kilobytes
      mem_in_kb
    when :bytes, :b
      mem_in_kb * 1024
    when :mb, :megabytes
      mem_in_kb / KB
    when :gb, :gigabytes
      mem_in_kb / (KB * KB)
    end
  end

  # Supplies the default total heap size for elasticsearch calculated as
  # 25% of the system memory bounded between 1GB - 26GB
  # https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
  def self.heap_size_default(node)
    memory = node_memory_in_units(node, :total, :mb)
    value = [memory / 4, 1024].max
    if value > MAXMEMORY
      value = MAXMEMORY
    elsif value < MINMEMORY
      value = MINMEMORY
    end
    value
  end

  # Defaults to the larger of 1/16th of heap_size,
  # or 32MB.
  def self.new_size_default(node)
    [node['private_chef']['elasticsearch']['heap_size'] / 16, 32].max
  end
end
<commit_msg>make constant more clear and cleanup memory/4<commit_after># frozen_string_literal: true

# A class that knows about Elasticsearch configuration and usage.
class Elasticsearch
  KB = 1024
  MB = KB * KB
  GB = MB * KB
  MIN_HEAP_SIZE = 1 * 1024 # in mb
  MAX_HEAP_SIZE = 26 * 1024 # in mb

  def self.node_memory_in_units(node, _which, unit)
    node[:memory][:total] =~ /^(\d+)kB/
    mem_in_kb = Regexp.last_match(1).to_i
    case unit
    when :kb, :kilobytes
      mem_in_kb
    when :bytes, :b
      mem_in_kb * 1024
    when :mb, :megabytes
      mem_in_kb / KB
    when :gb, :gigabytes
      mem_in_kb / (KB * KB)
    end
  end

  # Supplies the default total heap size for elasticsearch calculated as
  # 25% of the system memory bounded between 1GB - 26GB
  # https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
  def self.heap_size_default(node)
    memory = node_memory_in_units(node, :total, :mb)
    value = memory / 4
    if value > MAX_HEAP_SIZE
      value = MAX_HEAP_SIZE
    elsif value < MIN_HEAP_SIZE
      value = MIN_HEAP_SIZE
    end
    value
  end

  # Defaults to the larger of 1/16th of heap_size,
  # or 32MB.
  def self.new_size_default(node)
    [node['private_chef']['elasticsearch']['heap_size'] / 16, 32].max
  end
end

{'fraction_non_alphanumeric': 0.09743384121892543, 'fraction_numerical': 0.0268644747393745, 'mean_word_length': 2.9229559748427674, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '3112270', 'n_tokens_mistral': 972, 'n_tokens_neox': 885, 'n_words': 249}