<commit_before>class Tree
  def initialize(db, server, name)
    @db = db
    @server = server
    @name = name
  end

  def get_tree
    highest_patron = find_highest_patron
    return "{}" if highest_patron.nil?

    tree = {'name' => highest_patron}
    walk_tree(tree)

    tree
  end

  def find_highest_patron
    highest_patron = nil

    # Find the character
    character = @db['characters'].find({:server => @server, :name => @name})

    # Return early if we're done
    return nil if character.count != 1

    # Grab the character directly
    character = character.to_a[0]

    # Return early if no patron
    return character['name'] if !character['patron']

    highest_patron = character['patron']['name']

    # Do the finding
    patron = @db['characters'].find({:server => @server, :name => character['patron']['name']}) if character['patron']

    # Trarverse upward toward the ultimate patron
    while patron.count == 1
      patron = patron.to_a[0]

      highest_patron = patron['name']

      if !patron['patron'].nil?
        patron = @db['characters'].find({:server => @server, :name => patron['patron']['name']})
      else
        return highest_patron
      end
    end

    highest_patron
  end

  def walk_tree(current)
    character = @db['characters'].find({:server => @server, :name => current['name']})

    return if character.count != 1
    
    character = character.to_a[0]

    if !character['vassals'].nil?
      current.merge!({'children' => []})

      character['vassals'].each_with_index do |vassal, i|
        current['children'] << { 'name' => vassal['name'] }
        walk_tree(current['children'][i])
      end<commit_msg>Convert tree helper to Mongoid<commit_after>class Tree
  def initialize(server, name)
    @server = server
    @name = name
  end

  def get_tree
    highest_patron = find_highest_patron
    return "{}" if highest_patron.nil?

    tree = {'name' => highest_patron}
    walk_tree(tree)

    tree
  end

  def find_highest_patron
    highest_patron = nil

    # Find the character
    character = Character.find_by(server: @server, name: @name)

    # Return early if we're done
    return nil if character.nil?

    # Return early if no patron
    return character.name if !character.patron
    
    highest_patron = character.patron['name']
    
    # Do the finding
    patron = Character.find_by(server: @server, name: highest_patron)
    
    # Trarverse upward toward the ultimate patron
    while patron
      highest_patron = patron.name

      if(patron.patron)
        patron = Character.find_by(server: @server, name: patron.patron.name)
      else
        return highest_patron
      end
    end

    highest_patron
  end

  def walk_tree(current)
    character = Character.find_by(server: @server, name: current['name'])
    
    return if character.nil?

    if(character['vassals'])
      current.merge!({'children' => []})

      character['vassals'].each_with_index do |vassal, i|
        current['children'] << { 'name' => vassal['name'] }
        walk_tree(current['children'][i])
      end
{'fraction_non_alphanumeric': 0.12471581682364404, 'fraction_numerical': 0.001948684637869438, 'mean_word_length': 2.963963963963964, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, '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': '456147', 'n_tokens_mistral': 984, 'n_tokens_neox': 945, 'n_words': 237}