Class: VWO::DecisionService

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

Constant Summary

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?

Methods included from Common::CampaignUtils

#get_campaign, #get_campaign_goal, #set_variation_allocation

Constructor Details

#initialize(settings_file, user_profile_service = nil) ⇒ DecisionService

Initializes DecisionService with settings_file, UserProfileService and logger. @param - Settings file of the project. @param - Class instance having the capability of

lookup and save.


20
21
22
23
24
25
26
27
# File 'lib/vwo/decision_service.rb', line 20

def initialize(settings_file,  = nil)
  @logger = CustomLogger.get_instance
  @user_profile_service = 
  # Check if user_profile_service provided is valid or not
  @user_profile_service =  if valid_utility?(, 'user_profile_service')
  @bucketer = VWO::BucketingService.new
  @settings_file = settings_file
end

Instance Method Details

#get(user_id, campaign, campaign_test_key) ⇒ Object

Returns variation for the user for required campaign First lookup in the UPS, if user_profile is found, return from there Else, calculates the variation with helper method

@param :user_id The unique ID assigned to User @param :campaign Campaign in which user is participating @param :campaign_test_key The unique ID of the campaign passed @return (variation_name|Nil): Tuple of

variation_id and variation_name if variation allotted, else nil


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/vwo/decision_service.rb', line 41

def get(user_id, campaign, campaign_test_key)
  campaign_bucket_map = resolve_campaign_bucket_map(user_id)
  variation = get_stored_variation(user_id, campaign_test_key, campaign_bucket_map) if valid_hash?(campaign_bucket_map)

  if variation
    @logger.log(
      LogLevelEnum::INFO,
      format(
        LogMessageEnum::InfoMessages::GOT_STORED_VARIATION,
        file: FILE,
        campaign_test_key: campaign_test_key,
        user_id: user_id,
        variation_name: variation['name']
      )
    )
    return variation['id'], variation['name']
  end

  variation_id, variation_name = get_variation_allotted(user_id, campaign)

  if variation_name
    (user_id, campaign_test_key, variation_name) if variation_name

    @logger.log(
      LogLevelEnum::INFO,
      format(
        LogMessageEnum::InfoMessages::VARIATION_ALLOCATED,
        file: FILE,
        campaign_test_key: campaign_test_key,
        user_id: user_id,
        variation_name: variation_name
      )
    )
  else
    @logger.log(
      LogLevelEnum::INFO,
      format(LogMessageEnum::InfoMessages::NO_VARIATION_ALLOCATED, file: FILE, campaign_test_key: campaign_test_key, user_id: user_id)
    )
  end
  [variation_id, variation_name]
end

#get_variation_allotted(user_id, campaign) ⇒ Object

Returns the Variation Allotted to User

@param :user_id The unique ID assigned to User @param :campaign Campaign Object

@return Variation Object allotted to User



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/vwo/decision_service.rb', line 90

def get_variation_allotted(user_id, campaign)
  variation_id, variation_name = nil
  unless valid_value?(user_id)
    @logger.log(
      LogLevelEnum::ERROR,
      format(LogMessageEnum::ErrorMessages::INVALID_USER_ID, file: FILE, user_id: user_id, method: 'get_variation_alloted')
    )
    return variation_id, variation_name
  end

  if @bucketer.user_part_of_campaign?(user_id, campaign)
    variation_id, variation_name = get_variation_of_campaign_for_user(user_id, campaign)
    @logger.log(
      LogLevelEnum::DEBUG,
      format(
        LogMessageEnum::DebugMessages::GOT_VARIATION_FOR_USER,
        file: FILE,
        variation_name: variation_name,
        user_id: user_id,
        campaign_test_key: campaign['key'],
        method: 'get_variation_allotted'
      )
    )
  else
    # not part of campaign
    @logger.log(
      LogLevelEnum::DEBUG,
      format(
        LogMessageEnum::DebugMessages::USER_NOT_PART_OF_CAMPAIGN,
        file: FILE,
        user_id: user_id,
        campaign_test_key: nil,
        method: 'get_variation_allotted'
      )
    )
  end
  [variation_id, variation_name]
end

#get_variation_of_campaign_for_user(user_id, campaign) ⇒ Object

Assigns random variation ID to a particular user Depending on the PercentTraffic. Makes user a part of campaign if user’s included in Traffic.

@param :user_id The unique ID assigned to a user @param :campaign The Campaign of which user is to be made a part of @return Variation allotted to User



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/vwo/decision_service.rb', line 136

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

  variation = @bucketer.bucket_user_to_variation(user_id, campaign)

  if variation && variation['name']
    @logger.log(
      LogLevelEnum::INFO,
      format(
        LogMessageEnum::InfoMessages::GOT_VARIATION_FOR_USER,
        file: FILE,
        variation_name: variation['name'],
        user_id: user_id,
        campaign_test_key: campaign['key']
      )
    )
    return variation['id'], variation['name']
  end

  @logger.log(
    LogLevelEnum::INFO,
    format(
      LogMessageEnum::InfoMessages::USER_GOT_NO_VARIATION,
      file: FILE,
      user_id: user_id,
      campaign_test_key: campaign['key']
    )
  )
  [nil, nil]
end