Class: Statsig::Evaluator
- Inherits:
-
Object
- Object
- Statsig::Evaluator
- Defined in:
- lib/evaluator.rb
Instance Attribute Summary collapse
-
#config_overrides ⇒ Object
Returns the value of attribute config_overrides.
-
#gate_overrides ⇒ Object
Returns the value of attribute gate_overrides.
-
#options ⇒ Object
Returns the value of attribute options.
-
#persistent_storage_utils ⇒ Object
Returns the value of attribute persistent_storage_utils.
-
#spec_store ⇒ Object
Returns the value of attribute spec_store.
Instance Method Summary collapse
- #check_gate(user, gate_name, end_result, ignore_local_overrides: false, is_nested: false) ⇒ Object
- #clear_config_overrides ⇒ Object
- #clear_gate_overrides ⇒ Object
- #eval_spec(config_name, user, config, end_result, is_nested: false) ⇒ Object
- #get_client_initialize_response(user, hash_algo, client_sdk_key, include_local_overrides) ⇒ Object
- #get_config(user, config_name, end_result, user_persisted_values: nil, ignore_local_overrides: false) ⇒ Object
- #get_layer(user, layer_name, end_result) ⇒ Object
-
#initialize(store, options, persistent_storage_utils) ⇒ Evaluator
constructor
A new instance of Evaluator.
- #list_autotunes ⇒ Object
- #list_configs ⇒ Object
- #list_experiments ⇒ Object
- #list_gates ⇒ Object
- #list_layers ⇒ Object
- #lookup_config_override(config_name) ⇒ Object
- #lookup_gate_override(gate_name) ⇒ Object
- #maybe_restart_background_threads ⇒ Object
- #override_config(config, value) ⇒ Object
- #override_gate(gate, value) ⇒ Object
- #remove_config_override(config) ⇒ Object
- #remove_gate_override(gate) ⇒ Object
- #shutdown ⇒ Object
- #unsupported_or_unrecognized(config_name, end_result) ⇒ Object
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, , persistent_storage_utils) UAParser.initialize_async CountryLookup.initialize_async @spec_store = store @gate_overrides = {} @config_overrides = {} @options = @persistent_storage_utils = persistent_storage_utils end |
Instance Attribute Details
#config_overrides ⇒ Object
Returns the value of attribute config_overrides.
21 22 23 |
# File 'lib/evaluator.rb', line 21 def config_overrides @config_overrides end |
#gate_overrides ⇒ Object
Returns the value of attribute gate_overrides.
19 20 21 |
# File 'lib/evaluator.rb', line 19 def gate_overrides @gate_overrides end |
#options ⇒ Object
Returns the value of attribute options.
23 24 25 |
# File 'lib/evaluator.rb', line 23 def @options end |
#persistent_storage_utils ⇒ Object
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_store ⇒ Object
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, is_nested: false) ⇒ Object
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 |
# File 'lib/evaluator.rb', line 76 def check_gate(user, gate_name, end_result, ignore_local_overrides: false, is_nested: 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(gate_name, user, @spec_store.get_gate(gate_name), end_result, is_nested: is_nested) end |
#clear_config_overrides ⇒ Object
299 300 301 |
# File 'lib/evaluator.rb', line 299 def clear_config_overrides @config_overrides.clear end |
#clear_gate_overrides ⇒ Object
287 288 289 |
# File 'lib/evaluator.rb', line 287 def clear_gate_overrides @gate_overrides.clear end |
#eval_spec(config_name, user, config, end_result, is_nested: false) ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/evaluator.rb', line 303 def eval_spec(config_name, user, config, end_result, is_nested: false) 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) finalize_secondary_exposures(end_result) return end pass = eval_pass_percent(user, rule, config[:salt]) finalize_eval_result(config, end_result, did_pass: pass, rule: rule, is_nested: is_nested) return end end finalize_eval_result(config, end_result, did_pass: false, rule: nil, is_nested: is_nested) end |
#get_client_initialize_response(user, hash_algo, client_sdk_key, include_local_overrides) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/evaluator.rb', line 219 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 if @spec_store.last_config_sync_time == 0 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 = Statsig. { 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: @spec_store.last_config_sync_time, hash_used: hash_algo, user: user.serialize(true), sdkInfo: {sdkType: ["sdkType"], sdkVersion: ["sdkVersion"]}, } end |
#get_config(user, config_name, end_result, user_persisted_values: nil, ignore_local_overrides: false) ⇒ Object
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 |
# File 'lib/evaluator.rb', line 104 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[:isActive] == 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(config_name, 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[:idType], user_persisted_values) end # Otherwise, remove from persisted storage else @persistent_storage_utils.remove_experiment_from_storage(user, config[:idType], config_name) eval_spec(config_name, user, config, end_result) end end |
#get_layer(user, layer_name, end_result) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/evaluator.rb', line 166 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(layer_name, user, @spec_store.get_layer(layer_name), end_result) end |
#list_autotunes ⇒ Object
206 207 208 209 210 211 212 213 |
# File 'lib/evaluator.rb', line 206 def list_autotunes keys = [] @spec_store.configs.each do |key, value| if value[:entity] == Const::TYPE_AUTOTUNE keys << key.to_s end end keys end |
#list_configs ⇒ Object
186 187 188 189 190 191 192 193 194 |
# File 'lib/evaluator.rb', line 186 def list_configs keys = [] @spec_store.configs.each do |key, value| if value[:entity] == Const::TYPE_DYNAMIC_CONFIG keys << key.to_s end end keys end |
#list_experiments ⇒ Object
196 197 198 199 200 201 202 203 204 |
# File 'lib/evaluator.rb', line 196 def list_experiments keys = [] @spec_store.configs.each do |key, value| if value[:entity] == Const::TYPE_EXPERIMENT keys << key.to_s end end keys end |
#list_gates ⇒ Object
182 183 184 |
# File 'lib/evaluator.rb', line 182 def list_gates @spec_store.gates.keys.map(&:to_s) end |
#list_layers ⇒ Object
215 216 217 |
# File 'lib/evaluator.rb', line 215 def list_layers @spec_store.layers.keys.map(&:to_s) end |
#lookup_config_override(config_name) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/evaluator.rb', line 59 def lookup_config_override(config_name) config_name_sym = config_name.to_sym if @config_overrides.key?(config_name_sym) return ConfigResult.new( name: config_name, json_value: @config_overrides[config_name_sym], rule_id: Const::OVERRIDE, id_type: @spec_store.has_config?(config_name) ? @spec_store.get_config(config_name)[:idType] : 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 57 |
# File 'lib/evaluator.rb', line 42 def lookup_gate_override(gate_name) gate_name_sym = gate_name.to_sym if @gate_overrides.key?(gate_name_sym) return ConfigResult.new( name: gate_name, gate_value: @gate_overrides[gate_name_sym], rule_id: Const::OVERRIDE, id_type: @spec_store.has_gate?(gate_name) ? @spec_store.get_gate(gate_name)[:idType] : 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_threads ⇒ Object
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
291 292 293 |
# File 'lib/evaluator.rb', line 291 def override_config(config, value) @config_overrides[config.to_sym] = value end |
#override_gate(gate, value) ⇒ Object
279 280 281 |
# File 'lib/evaluator.rb', line 279 def override_gate(gate, value) @gate_overrides[gate.to_sym] = value end |
#remove_config_override(config) ⇒ Object
295 296 297 |
# File 'lib/evaluator.rb', line 295 def remove_config_override(config) @config_overrides.delete(config.to_sym) end |
#remove_gate_override(gate) ⇒ Object
283 284 285 |
# File 'lib/evaluator.rb', line 283 def remove_gate_override(gate) @gate_overrides.delete(gate.to_sym) end |
#shutdown ⇒ Object
254 255 256 |
# File 'lib/evaluator.rb', line 254 def shutdown @spec_store.shutdown end |
#unsupported_or_unrecognized(config_name, end_result) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/evaluator.rb', line 258 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 |