Class: VWO::BucketingService

Inherits:
Object
  • Object
show all
Includes:
Common::CONSTANTS, Common::Enums, Common::Validations
Defined in:
lib/vwo/bucketing_service.rb

Constant Summary collapse

U_MAX_32_BIT =
0xFFFFFFFF
MAX_HASH_VALUE =
2**32
FILE =
FileNameEnum::BucketingService

Constants included from Common::Validations

Common::Validations::UTILITIES

Constants included from Common::CONSTANTS

Common::CONSTANTS::API_VERSION, Common::CONSTANTS::HTTPS_PROTOCOL, Common::CONSTANTS::HTTP_PROTOCOL, Common::CONSTANTS::LIBRARY_PATH, Common::CONSTANTS::MAX_TRAFFIC_PERCENT, Common::CONSTANTS::MAX_TRAFFIC_VALUE, Common::CONSTANTS::PLATFORM, Common::CONSTANTS::SDK_VERSION, Common::CONSTANTS::SEED_VALUE, Common::CONSTANTS::STATUS_RUNNING, Common::CONSTANTS::URL_NAMESPACE

Instance Method Summary collapse

Methods included from Common::Validations

#valid_hash?, #valid_number?, #valid_settings_file?, #valid_string?, #valid_utility?, #valid_value?

Constructor Details

#initializeBucketingService

Returns a new instance of BucketingService.



20
21
22
# File 'lib/vwo/bucketing_service.rb', line 20

def initialize
  @logger = CustomLogger.get_instance
end

Instance Method Details

#bucket_user_to_variation(user_id, campaign) ⇒ Object

Validates the User ID and Generates Variation into which the User is bucketed in.

@param :user_id The unique ID assigned to User @param :campaign The Campaign of which User is a part of

@return[Hash|nil} Variation data into which user is bucketed in

or nil if not


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/vwo/bucketing_service.rb', line 65

def bucket_user_to_variation(user_id, campaign)
  unless valid_value?(user_id)
    @logger.log(
      LogLevelEnum::ERROR,
      format(LogMessageEnum::ErrorMessages::INVALID_USER_ID, file: FILE, user_id: user_id, method: 'bucket_user_to_variation')
    )
    return
  end

  unless campaign
    @logger.log(
      LogLevelEnum::ERROR,
      format(LogMessageEnum::ErrorMessages::INVALID_CAMPAIGN, file: FILE, method: 'is_user_part_of_campaign')
    )
    return
  end

  hash_value = MurmurHash3::V32.str_hash(user_id, SEED_VALUE) & U_MAX_32_BIT
  normalize = MAX_TRAFFIC_VALUE / campaign['percentTraffic']
  multiplier = normalize / 100
  bucket_value = generate_bucket_value(
    hash_value,
    MAX_TRAFFIC_VALUE,
    multiplier
  )

  @logger.log(
    LogLevelEnum::DEBUG,
    format(
      LogMessageEnum::DebugMessages::VARIATION_HASH_BUCKET_VALUE,
      file: FILE,
      user_id: user_id,
      campaign_test_key: campaign['key'],
      percent_traffic: campaign['percentTraffic'],
      bucket_value: bucket_value,
      hash_value: hash_value
    )
  )
  get_variation(campaign, bucket_value)
end

#user_part_of_campaign?(user_id, campaign) ⇒ Boolean

Calculate if this user should become part of the campaign or not @param :user_id The unique ID assigned to a user @param :campaign For getting traffic allotted to the campaign @return If User is a part of Campaign or not

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vwo/bucketing_service.rb', line 29

def user_part_of_campaign?(user_id, campaign)
  unless valid_value?(user_id)
    @logger.log(
      LogLevelEnum::ERROR,
      format(LogMessageEnum::ErrorMessages::INVALID_USER_ID, file: FILE, user_id: user_id, method: 'is_user_part_of_campaign')
    )
    return false
  end

  if campaign.nil?
    @logger.log(
      LogLevelEnum::ERROR,
      format(LogMessageEnum::ErrorMessages::INVALID_CAMPAIGN, file: FILE, method: 'is_user_part_of_campaign')
    )
    return false
  end

  traffic_allocation = campaign['percentTraffic']

  value_assigned_to_user = get_bucket_value_for_user(user_id)
  is_user_part = (value_assigned_to_user != 0) && value_assigned_to_user <= traffic_allocation
  @logger.log(
    LogLevelEnum::INFO,
    format(LogMessageEnum::InfoMessages::USER_ELIGIBILITY_FOR_CAMPAIGN, file: FILE, user_id: user_id, is_user_part: is_user_part)
  )
  is_user_part
end