Class: Statsig::HashUtils
- Inherits:
-
Object
- Object
- Statsig::HashUtils
- Defined in:
- lib/hash_utils.rb
Class Method Summary collapse
- .bigquery_hash(string) ⇒ Object
- .compute_dedupe_key_for_config(config_name, rule_id, user_id, custom_ids = nil) ⇒ Object
- .compute_dedupe_key_for_gate(gate_name, rule_id, value, user_id, custom_ids = nil) ⇒ Object
- .compute_dedupe_key_for_layer(layer_name, experiment_name, parameter_name, rule_id, user_id, custom_ids = nil) ⇒ Object
- .compute_user_key(user_id, custom_ids = nil) ⇒ Object
- .djb2(input_str) ⇒ Object
- .djb2ForHash(input_hash) ⇒ Object
- .is_hash_in_sampling_rate(key, sampling_rate) ⇒ Object
- .md5(input_str) ⇒ Object
- .sha256(input_str) ⇒ Object
- .sortHash(input_hash) ⇒ Object
Class Method Details
.bigquery_hash(string) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/hash_utils.rb', line 40 def self.bigquery_hash(string) digest = Digest::SHA256.digest(string) num = digest[0...8].unpack('Q>')[0] if num >= TWO_TO_THE_63 num - TWO_TO_THE_64 else num end end |
.compute_dedupe_key_for_config(config_name, rule_id, user_id, custom_ids = nil) ⇒ Object
61 62 63 64 |
# File 'lib/hash_utils.rb', line 61 def self.compute_dedupe_key_for_config(config_name, rule_id, user_id, custom_ids = nil) user_key = compute_user_key(user_id, custom_ids) "n:#{config_name};u:#{user_key}r:#{rule_id}" end |
.compute_dedupe_key_for_gate(gate_name, rule_id, value, user_id, custom_ids = nil) ⇒ Object
56 57 58 59 |
# File 'lib/hash_utils.rb', line 56 def self.compute_dedupe_key_for_gate(gate_name, rule_id, value, user_id, custom_ids = nil) user_key = compute_user_key(user_id, custom_ids) "n:#{gate_name};u:#{user_key}r:#{rule_id};v:#{value}" end |
.compute_dedupe_key_for_layer(layer_name, experiment_name, parameter_name, rule_id, user_id, custom_ids = nil) ⇒ Object
66 67 68 69 |
# File 'lib/hash_utils.rb', line 66 def self.compute_dedupe_key_for_layer(layer_name, experiment_name, parameter_name, rule_id, user_id, custom_ids = nil) user_key = compute_user_key(user_id, custom_ids) "n:#{layer_name};e:#{experiment_name};p:#{parameter_name};u:#{user_key}r:#{rule_id}" end |
.compute_user_key(user_id, custom_ids = nil) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/hash_utils.rb', line 71 def self.compute_user_key(user_id, custom_ids = nil) user_key = "u:#{user_id};" if custom_ids custom_ids.each do |k, v| user_key += "#{k}:#{v};" end end user_key end |
.djb2(input_str) ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'lib/hash_utils.rb', line 8 def self.djb2(input_str) hash = 0 input_str.each_char.each do |c| hash = (hash << 5) - hash + c.ord hash &= hash end hash &= 0xFFFFFFFF # Convert to unsigned 32-bit integer return hash.to_s end |
.djb2ForHash(input_hash) ⇒ Object
18 19 20 |
# File 'lib/hash_utils.rb', line 18 def self.djb2ForHash(input_hash) return djb2(input_hash.to_json) end |
.is_hash_in_sampling_rate(key, sampling_rate) ⇒ Object
51 52 53 54 |
# File 'lib/hash_utils.rb', line 51 def self.is_hash_in_sampling_rate(key, sampling_rate) hash_key = bigquery_hash(key) hash_key % sampling_rate == 0 end |
.md5(input_str) ⇒ Object
26 27 28 |
# File 'lib/hash_utils.rb', line 26 def self.md5(input_str) return Digest::MD5.base64digest(input_str) end |
.sha256(input_str) ⇒ Object
22 23 24 |
# File 'lib/hash_utils.rb', line 22 def self.sha256(input_str) return Digest::SHA256.base64digest(input_str) end |
.sortHash(input_hash) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/hash_utils.rb', line 30 def self.sortHash(input_hash) dictionary = input_hash.clone.sort_by { |key| key }.to_h; input_hash.each do |key, value| if value.is_a?(Hash) dictionary[key] = self.sortHash(value) end end return dictionary end |