|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Featurevisor |
| 4 | + # Bucketer module for handling feature flag bucketing |
| 5 | + module Bucketer |
| 6 | + # Maximum bucketed number (100% * 1000 to include three decimal places) |
| 7 | + MAX_BUCKETED_NUMBER = 100_000 |
| 8 | + |
| 9 | + # Hash seed for consistent bucketing |
| 10 | + HASH_SEED = 1 |
| 11 | + |
| 12 | + # Maximum hash value for 32-bit integers |
| 13 | + MAX_HASH_VALUE = 2**32 |
| 14 | + |
| 15 | + # Default separator for bucket keys |
| 16 | + DEFAULT_BUCKET_KEY_SEPARATOR = "." |
| 17 | + |
| 18 | + # Get bucketed number from a bucket key |
| 19 | + # @param bucket_key [String] The bucket key to hash |
| 20 | + # @return [Integer] Bucket value between 0 and 100000 |
| 21 | + def self.get_bucketed_number(bucket_key) |
| 22 | + hash_value = Featurevisor.murmur_hash_v3(bucket_key, HASH_SEED) |
| 23 | + ratio = hash_value.to_f / MAX_HASH_VALUE |
| 24 | + |
| 25 | + (ratio * MAX_BUCKETED_NUMBER).floor |
| 26 | + end |
| 27 | + |
| 28 | + # Get bucket key from feature configuration and context |
| 29 | + # @param options [Hash] Options hash containing: |
| 30 | + # - feature_key [String] The feature key |
| 31 | + # - bucket_by [String, Array<String>, Hash] Bucketing strategy |
| 32 | + # - context [Hash] User context |
| 33 | + # - logger [Logger] Logger instance |
| 34 | + # @return [String] The bucket key |
| 35 | + # @raise [StandardError] If bucket_by is invalid |
| 36 | + def self.get_bucket_key(options) |
| 37 | + feature_key = options[:feature_key] |
| 38 | + bucket_by = options[:bucket_by] |
| 39 | + context = options[:context] |
| 40 | + logger = options[:logger] |
| 41 | + |
| 42 | + type, attribute_keys = parse_bucket_by(bucket_by, logger, feature_key) |
| 43 | + |
| 44 | + bucket_key = build_bucket_key(attribute_keys, context, type, feature_key) |
| 45 | + |
| 46 | + bucket_key.join(DEFAULT_BUCKET_KEY_SEPARATOR) |
| 47 | + end |
| 48 | + |
| 49 | + private |
| 50 | + |
| 51 | + # Parse bucket_by configuration to determine type and attribute keys |
| 52 | + # @param bucket_by [String, Array<String>, Hash] Bucketing strategy |
| 53 | + # @param logger [Logger] Logger instance |
| 54 | + # @param feature_key [String] Feature key for error logging |
| 55 | + # @return [Array] Tuple of [type, attribute_keys] |
| 56 | + def self.parse_bucket_by(bucket_by, logger, feature_key) |
| 57 | + if bucket_by.is_a?(String) |
| 58 | + ["plain", [bucket_by]] |
| 59 | + elsif bucket_by.is_a?(Array) |
| 60 | + ["and", bucket_by] |
| 61 | + elsif bucket_by.is_a?(Hash) && bucket_by[:or].is_a?(Array) |
| 62 | + ["or", bucket_by[:or]] |
| 63 | + else |
| 64 | + logger.error("invalid bucketBy", { feature_key: feature_key, bucket_by: bucket_by }) |
| 65 | + raise StandardError, "invalid bucketBy" |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + # Build bucket key array from attribute keys and context |
| 70 | + # @param attribute_keys [Array<String>] Array of attribute keys |
| 71 | + # @param context [Hash] User context |
| 72 | + # @param type [String] Bucketing type ("plain", "and", "or") |
| 73 | + # @param feature_key [String] Feature key to append |
| 74 | + # @return [Array] Array of bucket key components |
| 75 | + def self.build_bucket_key(attribute_keys, context, type, feature_key) |
| 76 | + bucket_key = [] |
| 77 | + |
| 78 | + attribute_keys.each do |attribute_key| |
| 79 | + attribute_value = Featurevisor::Conditions.get_value_from_context(context, attribute_key) |
| 80 | + |
| 81 | + next if attribute_value.nil? |
| 82 | + |
| 83 | + if type == "plain" || type == "and" |
| 84 | + bucket_key << attribute_value |
| 85 | + elsif type == "or" && bucket_key.empty? |
| 86 | + # For "or" type, only take the first available value |
| 87 | + bucket_key << attribute_value |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + bucket_key << feature_key |
| 92 | + bucket_key |
| 93 | + end |
| 94 | + end |
| 95 | +end |
0 commit comments