<commit_before>
class LockrError < RuntimeError
end

def _get_lockable_traits(node)
    ([node[:persist][:traits]].flatten) - (node.keys.select { |k|
          k.to_s != 'not_lockrable_traits' and k.to_s.start_with?('not_lockrable_trait')
    }.map { |k| node[k] }.flatten)

end

def _get_lock_list(node)
    lockable_traits = _get_lockable_traits(node)
    lock_list = Hash[lockable_traits.map {|trait| [trait, node[:persist][:fqdn]]}]
    [lockable_traits, lock_list]
end

def acquire_lockr(node)
    lockable_traits, lock_list = _get_lock_list(node)
    Chef::Log.debug("Just stating: lock_list is >#{lock_list.inspect}>")

    trait_max = Hash[lock_list.map { |trait,fqdn|
        count = fakesearch(:traits => trait, :attributes => 'count(*)')/10
        [trait, (count >= 1 ? count : 1) ]}]
    Chef::Log.debug("Max number of locks that can be acquired for each trait: >#{trait_max.inspect}>")
<commit_msg>make lockr's trait_max ratio divisor configurable<commit_after>
class LockrError < RuntimeError
end

def _get_lockable_traits(node)
    ([node[:persist][:traits]].flatten) - (node.keys.select { |k|
          k.to_s != 'not_lockrable_traits' and k.to_s.start_with?('not_lockrable_trait')
    }.map { |k| node[k] }.flatten)

end

def _get_lock_list(node)
    lockable_traits = _get_lockable_traits(node)
    lock_list = Hash[lockable_traits.map {|trait| [trait, node[:persist][:fqdn]]}]
    [lockable_traits, lock_list]
end

def acquire_lockr(node)
    lockable_traits, lock_list = _get_lock_list(node)
    Chef::Log.debug("Just stating: lock_list is >#{lock_list.inspect}>")

    ratio_divisor = Chef::Config[:lockr_ratio_divisor] ? Chef::Config[:lockr_ratio_divisor] : 10

    trait_max = Hash[lock_list.map { |trait,fqdn|
        count = fakesearch(:traits => trait, :attributes => 'count(*)')/ratio_divisor
        [trait, (count >= 1 ? count : 1) ]}]
    Chef::Log.debug("Max number of locks that can be acquired for each trait: >#{trait_max.inspect}>")

{'fraction_non_alphanumeric': 0.14829862874555613, 'fraction_numerical': 0.004062976130015236, 'mean_word_length': 4.628571428571429, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1676153', 'n_tokens_mistral': 760, 'n_tokens_neox': 737, 'n_words': 141}