<commit_before>class UpdateBuffersForExistingLesses < ActiveRecord::Migration
  def fetch_buffer_for_technology(testing_ground, technology)
    testing_ground.technology_profile.each_tech.detect do |tech|
      (technology.buffer == tech.composite_value) ||
        tech.includes.include?(technology.type) # This is due to some LES's with broken buffers
    end
  end

  def update_technologies(testing_ground, technologies)
    technologies.each_with_index.flat_map do |technology, index|
      if technology.position_relative_to_buffer
        buffer = fetch_buffer_for_technology(testing_ground, technology)

        cloned_buffer                 = buffer.dup
        cloned_buffer.composite_index = index
        cloned_buffer.composite_value = "#{ cloned_buffer.type }_#{ index }"
        cloned_buffer.units           = technology.units
        technology.buffer             = cloned_buffer.composite_value

        [ cloned_buffer, technology ]
      elsif !technology.composite
        technology
      end
    end
  end

  def up
    TestingGround.all.each do |testing_ground|
      list = Hash[testing_ground.technology_profile.list.map do |node, technologies|
        [node, update_technologies(testing_ground, technologies).compact]
      end]
<commit_msg>Migration assigns unique index to each composite<commit_after>class UpdateBuffersForExistingLesses < ActiveRecord::Migration
  def fetch_buffer_for_technology(testing_ground, technology)
    testing_ground.technology_profile.each_tech.detect do |tech|
      (technology.buffer == tech.composite_value) ||
        tech.includes.include?(technology.type) # This is due to some LES's with broken buffers
    end
  end

  def update_technologies(testing_ground, technologies, indices)
    technologies.flat_map do |technology|
      if technology.position_relative_to_buffer
        buffer = fetch_buffer_for_technology(testing_ground, technology)

        index = indices[buffer.type] += 1

        cloned_buffer                 = buffer.dup
        cloned_buffer.composite_index = index
        cloned_buffer.composite_value = "#{ cloned_buffer.type }_#{ index }"
        cloned_buffer.units           = technology.units
        technology.buffer             = cloned_buffer.composite_value

        [ cloned_buffer, technology ]
      elsif !technology.composite
        technology
      end
    end
  end

  def up
    TestingGround.all.each do |testing_ground|
      indices = Hash.new { |h, k| h[k] = 0 }

      list = Hash[testing_ground.technology_profile.list.map do |node, technologies|
        [node, update_technologies(testing_ground, technologies, indices).compact]
      end]

{'fraction_non_alphanumeric': 0.07306967984934086, 'fraction_numerical': 0.0007532956685499058, 'mean_word_length': 3.1761006289308176, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2013921', 'n_tokens_mistral': 752, 'n_tokens_neox': 717, 'n_words': 152}