Class: Statsig::Evaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/evaluator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, options, persistent_storage_utils) ⇒ Evaluator

Returns a new instance of Evaluator.



27
28
29
30
31
32
33
34
35
36
# File 'lib/evaluator.rb', line 27

def initialize(store, options, persistent_storage_utils)
  UAParser.initialize_async
  CountryLookup.initialize_async

  @spec_store = store
  @gate_overrides = {}
  @config_overrides = {}
  @options = options
  @persistent_storage_utils = persistent_storage_utils
end

Instance Attribute Details

#config_overridesObject

Returns the value of attribute config_overrides.



21
22
23
# File 'lib/evaluator.rb', line 21

def config_overrides
  @config_overrides
end

#gate_overridesObject

Returns the value of attribute gate_overrides.



19
20
21
# File 'lib/evaluator.rb', line 19

def gate_overrides
  @gate_overrides
end

#optionsObject

Returns the value of attribute options.



23
24
25
# File 'lib/evaluator.rb', line 23

def options
  @options
end

#persistent_storage_utilsObject

Returns the value of attribute persistent_storage_utils.



25
26
27
# File 'lib/evaluator.rb', line 25

def persistent_storage_utils
  @persistent_storage_utils
end

#spec_storeObject

Returns the value of attribute spec_store.



17
18
19
# File 'lib/evaluator.rb', line 17

def spec_store
  @spec_store
end

Instance Method Details

#check_gate(user, gate_name, end_result, ignore_local_overrides: false) ⇒ Object



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
# File 'lib/evaluator.rb', line 74

def check_gate(user, gate_name, end_result, ignore_local_overrides: false)
  unless ignore_local_overrides
    local_override = lookup_gate_override(gate_name)
    unless local_override.nil?
      end_result.gate_value = local_override.gate_value
      end_result.rule_id = local_override.rule_id
      unless end_result.disable_evaluation_details
        end_result.evaluation_details = local_override.evaluation_details
      end
      return
    end
  end

  if @spec_store.init_reason == EvaluationReason::UNINITIALIZED
    unless end_result.disable_evaluation_details
      end_result.evaluation_details = EvaluationDetails.uninitialized
    end
    return
  end

  unless @spec_store.has_gate?(gate_name)
    unsupported_or_unrecognized(gate_name, end_result)
    return
  end

  eval_spec(user, @spec_store.get_gate(gate_name), end_result)
end

#clear_config_overridesObject



276
277
278
# File 'lib/evaluator.rb', line 276

def clear_config_overrides
  @config_overrides.clear
end

#clear_gate_overridesObject



264
265
266
# File 'lib/evaluator.rb', line 264

def clear_gate_overrides
  @gate_overrides.clear
end

#eval_spec(user, config, end_result) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/evaluator.rb', line 280

def eval_spec(user, config, end_result)
  unless config.enabled
    finalize_eval_result(config, end_result, did_pass: false, rule: nil)
    return
  end

  config.rules.each do |rule|
    eval_rule(user, rule, end_result)

    if end_result.gate_value
      if eval_delegate(config.name, user, rule, end_result)
        return
      end

      pass = eval_pass_percent(user, rule, config.salt)
      finalize_eval_result(config, end_result, did_pass: pass, rule: rule)
      return
    end
  end

  finalize_eval_result(config, end_result, did_pass: false, rule: nil)
end

#get_client_initialize_response(user, hash_algo, client_sdk_key, include_local_overrides) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/evaluator.rb', line 200

def get_client_initialize_response(user, hash_algo, client_sdk_key, include_local_overrides)
  if @spec_store.is_ready_for_checks == false
    return nil
  end

  evaluated_keys = {}
  if user.user_id.nil? == false
    evaluated_keys[:userID] = user.user_id
  end

  if user.custom_ids.nil? == false
    evaluated_keys[:customIDs] = user.custom_ids
  end

  {
    feature_gates: Statsig::ResponseFormatter
                     .get_responses(@spec_store.gates, self, user, client_sdk_key, hash_algo, include_local_overrides: include_local_overrides),
    dynamic_configs: Statsig::ResponseFormatter
                       .get_responses(@spec_store.configs, self, user, client_sdk_key, hash_algo, include_local_overrides: include_local_overrides),
    layer_configs: Statsig::ResponseFormatter
                     .get_responses(@spec_store.layers, self, user, client_sdk_key, hash_algo, include_local_overrides: include_local_overrides),
    sdkParams: {},
    has_updates: true,
    generator: Const::STATSIG_RUBY_SDK,
    evaluated_keys: evaluated_keys,
    time: 0,
    hash_used: hash_algo,
    user_hash: user.to_hash_without_stable_id
  }
end

#get_config(user, config_name, end_result, user_persisted_values: nil, ignore_local_overrides: false) ⇒ Object



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
# File 'lib/evaluator.rb', line 102

def get_config(user, config_name, end_result, user_persisted_values: nil, ignore_local_overrides: false)
  unless ignore_local_overrides
    local_override = lookup_config_override(config_name)
    unless local_override.nil?
      end_result.id_type = local_override.id_type
      end_result.rule_id = local_override.rule_id
      end_result.json_value = local_override.json_value
      unless end_result.disable_evaluation_details
        end_result.evaluation_details = local_override.evaluation_details
      end
      return
    end
  end

  if @spec_store.init_reason == EvaluationReason::UNINITIALIZED
    unless end_result.disable_evaluation_details
      end_result.evaluation_details = EvaluationDetails.uninitialized
    end
    return
  end

  unless @spec_store.has_config?(config_name)
    unsupported_or_unrecognized(config_name, end_result)
    return
  end

  config = @spec_store.get_config(config_name)

  # If persisted values is provided and the experiment is active, return sticky values if exists.
  if !user_persisted_values.nil? && config.is_active == true
    sticky_values = user_persisted_values[config_name]
    unless sticky_values.nil?
      end_result.gate_value = sticky_values[Statsig::Const::GATE_VALUE]
      end_result.json_value = sticky_values[Statsig::Const::JSON_VALUE]
      end_result.rule_id = sticky_values[Statsig::Const::RULE_ID]
      end_result.secondary_exposures = sticky_values[Statsig::Const::SECONDARY_EXPOSURES]
      end_result.group_name = sticky_values[Statsig::Const::GROUP_NAME]
      end_result.id_type = sticky_values[Statsig::Const::ID_TYPE]
      end_result.target_app_ids = sticky_values[Statsig::Const::TARGET_APP_IDS]
      unless end_result.disable_evaluation_details
        end_result.evaluation_details = EvaluationDetails.persisted(
          sticky_values[Statsig::Const::CONFIG_SYNC_TIME],
          sticky_values[Statsig::Const::INIT_TIME]
        )
      end
      return
    end

    # If it doesn't exist, then save to persisted storage if the user was assigned to an experiment group.
    eval_spec(user, config, end_result)
    if end_result.is_experiment_group
      @persistent_storage_utils.add_evaluation_to_user_persisted_values(user_persisted_values, config_name,
                                                                        end_result)
      @persistent_storage_utils.save_to_storage(user, config.id_type, user_persisted_values)
    end
    # Otherwise, remove from persisted storage
  else
    @persistent_storage_utils.remove_experiment_from_storage(user, config.id_type, config_name)
    eval_spec(user, config, end_result)
  end
end

#get_layer(user, layer_name, end_result) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/evaluator.rb', line 164

def get_layer(user, layer_name, end_result)
  if @spec_store.init_reason == EvaluationReason::UNINITIALIZED
    unless end_result.disable_evaluation_details
      end_result.evaluation_details = EvaluationDetails.uninitialized
    end
    return
  end

  unless @spec_store.has_layer?(layer_name)
    unsupported_or_unrecognized(layer_name, end_result)
    return
  end

  eval_spec(user, @spec_store.get_layer(layer_name), end_result)
end

#list_autotunesObject



192
193
194
# File 'lib/evaluator.rb', line 192

def list_autotunes
  @spec_store.configs.map { |name, config| name if config.entity == :autotune }.compact
end

#list_configsObject



184
185
186
# File 'lib/evaluator.rb', line 184

def list_configs
  @spec_store.configs.map { |name, config| name if config.entity == :dynamic_config }.compact
end

#list_experimentsObject



188
189
190
# File 'lib/evaluator.rb', line 188

def list_experiments
  @spec_store.configs.map { |name, config| name if config.entity == :experiment }.compact
end

#list_gatesObject



180
181
182
# File 'lib/evaluator.rb', line 180

def list_gates
  @spec_store.gates.map { |name, _| name }
end

#list_layersObject



196
197
198
# File 'lib/evaluator.rb', line 196

def list_layers
  @spec_store.layers.map { |name, _| name }
end

#lookup_config_override(config_name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/evaluator.rb', line 58

def lookup_config_override(config_name)
  if @config_overrides.key?(config_name)
    return ConfigResult.new(
      name: config_name,
      json_value: @config_overrides[config_name],
      rule_id: Const::OVERRIDE,
      id_type: @spec_store.has_config?(config_name) ? @spec_store.get_config(config_name).id_type : Const::EMPTY_STR,
      evaluation_details: EvaluationDetails.local_override(
        @spec_store.last_config_sync_time,
        @spec_store.initial_config_sync_time
      )
    )
  end
  return nil
end

#lookup_gate_override(gate_name) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/evaluator.rb', line 42

def lookup_gate_override(gate_name)
  if @gate_overrides.key?(gate_name)
    return ConfigResult.new(
      name: gate_name,
      gate_value: @gate_overrides[gate_name],
      rule_id: Const::OVERRIDE,
      id_type: @spec_store.has_gate?(gate_name) ? @spec_store.get_gate(gate_name).id_type : Const::EMPTY_STR,
      evaluation_details: EvaluationDetails.local_override(
        @spec_store.last_config_sync_time,
        @spec_store.initial_config_sync_time
      )
    )
  end
  return nil
end

#maybe_restart_background_threadsObject



38
39
40
# File 'lib/evaluator.rb', line 38

def maybe_restart_background_threads
  @spec_store.maybe_restart_background_threads
end

#override_config(config, value) ⇒ Object



268
269
270
# File 'lib/evaluator.rb', line 268

def override_config(config, value)
  @config_overrides[config] = value
end

#override_gate(gate, value) ⇒ Object



256
257
258
# File 'lib/evaluator.rb', line 256

def override_gate(gate, value)
  @gate_overrides[gate] = value
end

#remove_config_override(config) ⇒ Object



272
273
274
# File 'lib/evaluator.rb', line 272

def remove_config_override(config)
  @config_overrides.delete(config)
end

#remove_gate_override(gate) ⇒ Object



260
261
262
# File 'lib/evaluator.rb', line 260

def remove_gate_override(gate)
  @gate_overrides.delete(gate)
end

#shutdownObject



231
232
233
# File 'lib/evaluator.rb', line 231

def shutdown
  @spec_store.shutdown
end

#unsupported_or_unrecognized(config_name, end_result) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/evaluator.rb', line 235

def unsupported_or_unrecognized(config_name, end_result)
  end_result.rule_id = Const::EMPTY_STR

  if end_result.disable_evaluation_details
    return
  end

  if @spec_store.unsupported_configs.include?(config_name)
    end_result.evaluation_details = EvaluationDetails.unsupported(
      @spec_store.last_config_sync_time,
      @spec_store.initial_config_sync_time
    )
    return
  end

  end_result.evaluation_details = EvaluationDetails.unrecognized(
    @spec_store.last_config_sync_time,
    @spec_store.initial_config_sync_time
  )
end