Class: FlagApi

Inherits:
Object
  • Object
show all
Defined in:
lib/wingify/api/get_flag.rb

Instance Method Summary collapse

Instance Method Details

#get(feature_key, settings, context, hooks_service) ⇒ GetFlagResponse

Get the flag for a given feature key and context

Parameters:

  • feature_key (String)

    The key of the feature to get the flag for

  • settings (SettingsModel)

    The settings for the VWO instance

  • context (ContextModel)

    The context for the evaluation

  • hooks_service (HooksService)

    The hooks service for the VWO instance

Returns:



38
39
40
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
82
83
84
85
86
87
88
89
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
128
129
130
131
132
133
134
135
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/wingify/api/get_flag.rb', line 38

def get(feature_key, settings, context, hooks_service)
  is_enabled = false
  rollout_variation_to_return = nil
  experiment_variation_to_return = nil
  should_check_for_experiments_rules = false
  
  passed_rules_information = {} # for storing integration callback
  evaluated_feature_map = {}

  # Fetch feature object using the feature key
  feature = get_feature_from_key(settings, feature_key)
  
  decision = {
    feature_name: feature&.get_name,
    feature_id: feature&.get_id,
    feature_key: feature&.get_key,
    user_id: context&.get_id,
    api: ApiEnum::GET_FLAG
  }
  
  storage_service = StorageService.new
  if Storage.instance.is_storage_enabled
    stored_data = StorageDecorator.new.get_feature_from_storage(feature_key, context, storage_service)

    if stored_data && stored_data[:experiment_variation_id]
      if stored_data[:experiment_key]
        variation = CampaignUtil.get_variation_from_campaign_key(settings, stored_data[:experiment_key], stored_data[:experiment_variation_id])

        if variation
          LoggerService.log(LogLevelEnum::INFO, "STORED_VARIATION_FOUND", {variationKey: variation.get_key, userId: context.get_id, experimentKey: stored_data[:experiment_key], experimentType: "experiment"})
          return GetFlagResponse.new(true, variation.get_variables, context.get_uuid, context.get_session_id)
        end
      end
    elsif stored_data && stored_data[:rollout_key] && stored_data[:rollout_id]
      variation = CampaignUtil.get_variation_from_campaign_key(settings, stored_data[:rollout_key], stored_data[:rollout_variation_id])

      if variation
        LoggerService.log(LogLevelEnum::INFO, "STORED_VARIATION_FOUND", {variationKey: variation.get_key, userId: context.get_id, experimentKey: stored_data[:rollout_key], experimentType: "rollout"})
        LoggerService.log(LogLevelEnum::DEBUG, "EXPERIMENTS_EVALUATION_WHEN_ROLLOUT_PASSED", {userId: context.get_id})

        is_enabled = true
        should_check_for_experiments_rules = true
        rollout_variation_to_return = variation
        feature_info = {
          rollout_id: stored_data[:rollout_id],
          rollout_key: stored_data[:rollout_key],
          rollout_variation_id: stored_data[:rollout_variation_id]
        }
        evaluated_feature_map[feature_key] = feature_info
        passed_rules_information.merge!(feature_info)
      end
    end
  end
  
  if feature.nil?
    LoggerService.log(LogLevelEnum::ERROR, "FEATURE_NOT_FOUND", {featureKey: feature_key, an: ApiEnum::GET_FLAG, sId: context.get_session_id, uuid: context.get_uuid})
    return GetFlagResponse.new(false, [], context.get_uuid, context.get_session_id)
  end
  
  # Segmentation evaluation
  SegmentationManager.instance.set_contextual_data(settings, feature, context)
  
  # Get all rollout rules
  rollout_rules = get_specific_rules_based_on_type(feature, CampaignTypeEnum::ROLLOUT)
  
  if rollout_rules.any? && !is_enabled
    rollout_rules_to_evaluate = []
  
    rollout_rules.each do |rule|
      result = evaluate_rule(settings, feature, rule, context, evaluated_feature_map, nil, storage_service, decision)
      pre_segmentation_result = result[:pre_segmentation_result]
      updated_decision = result[:updated_decision]
      decision.merge!(updated_decision) if updated_decision
  
      if pre_segmentation_result
        rollout_rules_to_evaluate << rule
        evaluated_feature_map[feature_key] = {
          rollout_id: rule.get_id,
          rollout_key: rule.get_key,
          rollout_variation_id: rule.get_variations.first&.get_id
        }
        break
      end
    end
  
    if rollout_rules_to_evaluate.any?
      passed_rollout_campaign = CampaignModel.new.model_from_dictionary(rollout_rules_to_evaluate.first)
      variation = DecisionUtil.evaluate_traffic_and_get_variation(settings, passed_rollout_campaign, context)
  
      if variation
        is_enabled = true
        should_check_for_experiments_rules = true
        rollout_variation_to_return = variation
        update_integrations_decision_object(passed_rollout_campaign, variation, passed_rules_information, decision)
  
        create_and_send_impression_for_variation_shown(settings, passed_rollout_campaign.get_id, variation.get_id, context, feature_key)
      end
    end
  elsif rollout_rules.empty?
    LoggerService.log(LogLevelEnum::DEBUG, "EXPERIMENTS_EVALUATION_WHEN_NO_ROLLOUT_PRESENT")
    should_check_for_experiments_rules = true
  end
  
  if should_check_for_experiments_rules
    experiment_rules = get_all_experiment_rules(feature)
    experiment_rules_to_evaluate = []
    meg_group_winner_campaigns = {}
  
    experiment_rules.each do |rule|
      result = evaluate_rule(settings, feature, rule, context, evaluated_feature_map, meg_group_winner_campaigns, storage_service, decision)
      pre_segmentation_result = result[:pre_segmentation_result]
      whitelisted_object = result[:whitelisted_object]
      updated_decision = result[:updated_decision]
      decision.merge!(updated_decision) if updated_decision

      if pre_segmentation_result
        if whitelisted_object.nil?
          experiment_rules_to_evaluate << rule
        else
          is_enabled = true
          experiment_variation_to_return = whitelisted_object[:variation]
          passed_rules_information.merge!(
            experiment_id: rule.get_id,
            experiment_key: rule.get_key,
            experiment_variation_id: whitelisted_object[:variation_id]
          )
        end
        break
      end
    end
  
    if experiment_rules_to_evaluate.any?
      campaign = CampaignModel.new.model_from_dictionary(experiment_rules_to_evaluate.first)
      variation = DecisionUtil.evaluate_traffic_and_get_variation(settings, campaign, context)
  
      if variation
        is_enabled = true
        experiment_variation_to_return = variation
        update_integrations_decision_object(campaign, variation, passed_rules_information, decision)
  
        create_and_send_impression_for_variation_shown(settings, campaign.get_id, variation.get_id, context, feature_key)
      end
    end
  end
  
  # Store evaluated feature in storage
  if is_enabled
    begin
      StorageDecorator.new.set_data_in_storage(
        { feature_key: feature_key, context: context }.merge(passed_rules_information),
        storage_service
      )
    rescue StandardError => e
      LoggerService.log(LogLevelEnum::ERROR, "ERROR_STORING_DATA_IN_STORAGE", { err: e.message, an: ApiEnum::GET_FLAG, sId: context.get_session_id, uuid: context.get_uuid})
    end
  end
  
  # Execute hooks
  hooks_service.set(decision)
  hooks_service.execute(hooks_service.get)

  if feature&.get_impact_campaign&.get_campaign_id
    LoggerService.log(
      LogLevelEnum::INFO,
      "IMPACT_ANALYSIS",
      {
        userId: context.get_id,
        featureKey: feature_key,
        status: is_enabled ? 'enabled' : 'disabled'
      }
    )
  
    variation_id = is_enabled ? 2 : 1 # 2 for Variation(flag enabled), 1 for Control(flag disabled)
    
    create_and_send_impression_for_variation_shown(
      settings,
      feature&.get_impact_campaign&.get_campaign_id,
      variation_id,
      context,
      feature_key
    )
  end
  
  # Return final evaluated feature flag
  return GetFlagResponse.new(is_enabled, experiment_variation_to_return&.get_variables || rollout_variation_to_return&.get_variables || [], context.get_uuid, context.get_session_id)
end