Class: DecisionMaker

Inherits:
Object
  • Object
show all
Defined in:
lib/wingify/packages/decision_maker/decision_maker.rb

Constant Summary collapse

SEED_VALUE =

Seed value for the hash function

1

Instance Method Summary collapse

Instance Method Details

#calculate_bucket_value(str, multiplier = 1, max_value = 10000) ⇒ Integer

Calculates the bucket value for a given string with an optional multiplier and maximum value.

Parameters:

  • str (String)

    The input string to calculate the bucket value for.

  • multiplier (Integer) (defaults to: 1)

    Optional multiplier to adjust the value (default: 1).

  • max_value (Integer) (defaults to: 10000)

    The maximum value for bucket scaling (default: 10000).

Returns:

  • (Integer)

    The calculated bucket value.



48
49
50
51
# File 'lib/wingify/packages/decision_maker/decision_maker.rb', line 48

def calculate_bucket_value(str, multiplier = 1, max_value = 10000)
  hash_value = generate_hash_value(str)
  generate_bucket_value(hash_value, max_value, multiplier)
end

#generate_bucket_value(hash_value, max_value, multiplier = 1) ⇒ Integer

Generates a bucket value based on the hash value, maximum value, and an optional multiplier.

Parameters:

  • hash_value (Integer)

    The hash value used for calculation.

  • max_value (Integer)

    The maximum value for bucket scaling.

  • multiplier (Integer) (defaults to: 1)

    Optional multiplier to adjust the value (default: 1).

Returns:

  • (Integer)

    The calculated bucket value.



26
27
28
29
30
# File 'lib/wingify/packages/decision_maker/decision_maker.rb', line 26

def generate_bucket_value(hash_value, max_value, multiplier = 1)
  ratio = hash_value.to_f / (2**32)
  multiplied_value = ((max_value * ratio) + 1) * multiplier
  multiplied_value.floor
end

#generate_hash_value(hash_key) ⇒ Integer

Generates the hash value for a given hash key using MurmurHash v3.

Parameters:

  • hash_key (String)

    The hash key for which the hash value is generated.

Returns:

  • (Integer)

    The generated hash value.



57
58
59
# File 'lib/wingify/packages/decision_maker/decision_maker.rb', line 57

def generate_hash_value(hash_key)
  MurmurHash3::V32.str_hash(hash_key, SEED_VALUE)
end

#get_bucket_value_for_user(hash_key, max_value = 100) ⇒ Integer

Gets the bucket value for a user based on the hash key and maximum value.

Parameters:

  • hash_key (String)

    The hash key for the user.

  • max_value (Integer) (defaults to: 100)

    The maximum value for bucket scaling (default: 100).

Returns:

  • (Integer)

    The calculated bucket value for the user.



37
38
39
40
# File 'lib/wingify/packages/decision_maker/decision_maker.rb', line 37

def get_bucket_value_for_user(hash_key, max_value = 100)
  hash_value = MurmurHash3::V32.str_hash(hash_key, SEED_VALUE)
  generate_bucket_value(hash_value, max_value)
end