Class: Statsig::Evaluator
- Inherits:
-
Object
- Object
- Statsig::Evaluator
- Extended by:
- T::Sig
- Defined in:
- lib/evaluator.rb
Constant Summary collapse
- UNSUPPORTED_EVALUATION =
:unsupported_eval
Instance Attribute Summary collapse
-
#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) ⇒ Object
- #clean_exposures(exposures) ⇒ Object
- #eval_spec(user, config) ⇒ Object
- #get_client_initialize_response(user, hash, client_sdk_key) ⇒ Object
- #get_config(user, config_name, user_persisted_values: nil) ⇒ Object
- #get_layer(user, layer_name) ⇒ 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
- #maybe_restart_background_threads ⇒ Object
- #override_config(config, value) ⇒ Object
- #override_gate(gate, value) ⇒ Object
- #shutdown ⇒ Object
Constructor Details
#initialize(store, options, persistent_storage_utils) ⇒ Evaluator
Returns a new instance of Evaluator.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/evaluator.rb', line 38 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
#options ⇒ Object
Returns the value of attribute options.
26 27 28 |
# File 'lib/evaluator.rb', line 26 def @options end |
#persistent_storage_utils ⇒ Object
Returns the value of attribute persistent_storage_utils.
29 30 31 |
# File 'lib/evaluator.rb', line 29 def persistent_storage_utils @persistent_storage_utils end |
#spec_store ⇒ Object
Returns the value of attribute spec_store.
23 24 25 |
# File 'lib/evaluator.rb', line 23 def spec_store @spec_store end |
Instance Method Details
#check_gate(user, gate_name) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/evaluator.rb', line 53 def check_gate(user, gate_name) if @gate_overrides.has_key?(gate_name) return Statsig::ConfigResult.new( gate_name, @gate_overrides[gate_name], @gate_overrides[gate_name], 'override', [], evaluation_details: EvaluationDetails.local_override(@spec_store.last_config_sync_time, @spec_store.initial_config_sync_time)) end if @spec_store.init_reason == EvaluationReason::UNINITIALIZED return Statsig::ConfigResult.new(gate_name, evaluation_details: EvaluationDetails.uninitialized) end unless @spec_store.has_gate?(gate_name) return Statsig::ConfigResult.new(gate_name, evaluation_details: EvaluationDetails.unrecognized(@spec_store.last_config_sync_time, @spec_store.initial_config_sync_time)) end eval_spec(user, @spec_store.get_gate(gate_name)) end |
#clean_exposures(exposures) ⇒ Object
191 192 193 194 195 196 197 198 199 |
# File 'lib/evaluator.rb', line 191 def clean_exposures(exposures) seen = {} exposures.reject do |exposure| key = "#{exposure["gate"]}|#{exposure["gateValue"]}|#{exposure["ruleID"]}}" should_reject = seen[key] seen[key] = true should_reject == true end end |
#eval_spec(user, config) ⇒ Object
214 215 216 217 218 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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/evaluator.rb', line 214 def eval_spec(user, config) default_rule_id = 'default' exposures = [] if config['enabled'] i = 0 until i >= config['rules'].length do rule = config['rules'][i] result = eval_rule(user, rule) return Statsig::ConfigResult.new( config['name'], false, config['defaultValue'], '', exposures, evaluation_details: EvaluationDetails.new( @spec_store.last_config_sync_time, @spec_store.initial_config_sync_time, EvaluationReason::UNSUPPORTED, ), group_name: nil, id_type: config['idType'], target_app_ids: config['targetAppIDs'] ) if result == UNSUPPORTED_EVALUATION exposures = exposures + result.secondary_exposures if result.gate_value if (delegated_result = eval_delegate(config['name'], user, rule, exposures)) return delegated_result end pass = eval_pass_percent(user, rule, config['salt']) return Statsig::ConfigResult.new( config['name'], pass, pass ? result.json_value : config['defaultValue'], result.rule_id, exposures, evaluation_details: EvaluationDetails.new( @spec_store.last_config_sync_time, @spec_store.initial_config_sync_time, @spec_store.init_reason ), is_experiment_group: result.is_experiment_group, group_name: result.group_name, id_type: config['idType'], target_app_ids: config['targetAppIDs'] ) end i += 1 end else default_rule_id = 'disabled' end Statsig::ConfigResult.new( config['name'], false, config['defaultValue'], default_rule_id, exposures, evaluation_details: EvaluationDetails.new( @spec_store.last_config_sync_time, @spec_store.initial_config_sync_time, @spec_store.init_reason ), group_name: nil, id_type: config['idType'], target_app_ids: config['targetAppIDs'] ) end |
#get_client_initialize_response(user, hash, client_sdk_key) ⇒ Object
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 |
# File 'lib/evaluator.rb', line 161 def get_client_initialize_response(user, hash, client_sdk_key) if @spec_store.is_ready_for_checks == false return nil end formatter = ClientInitializeHelpers::ResponseFormatter.new(self, user, hash, client_sdk_key) 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" => formatter.get_responses(:gates), "dynamic_configs" => formatter.get_responses(:configs), "layer_configs" => formatter.get_responses(:layers), "sdkParams" => {}, "has_updates" => true, "generator" => "statsig-ruby-sdk", "evaluated_keys" => evaluated_keys, "time" => 0, "hash_used" => hash, "user_hash" => user.to_hash_without_stable_id() } end |
#get_config(user, config_name, user_persisted_values: nil) ⇒ 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 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/evaluator.rb', line 76 def get_config(user, config_name, user_persisted_values: nil) if @config_overrides.key?(config_name) id_type = @spec_store.has_config?(config_name) ? @spec_store.get_config(config_name)['idType'] : '' return Statsig::ConfigResult.new( config_name, false, @config_overrides[config_name], 'override', [], evaluation_details: EvaluationDetails.local_override( @spec_store.last_config_sync_time, @spec_store.initial_config_sync_time ), id_type: id_type ) end if @spec_store.init_reason == EvaluationReason::UNINITIALIZED return Statsig::ConfigResult.new(config_name, evaluation_details: EvaluationDetails.uninitialized) end unless @spec_store.has_config?(config_name) return Statsig::ConfigResult.new( config_name, evaluation_details: EvaluationDetails.unrecognized( @spec_store.last_config_sync_time, @spec_store.initial_config_sync_time ) ) 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_result = Statsig::ConfigResult.from_user_persisted_values(config_name, user_persisted_values) return sticky_result unless sticky_result.nil? # If it doesn't exist, then save to persisted storage if the user was assigned to an experiment group. evaluation = eval_spec(user, config) if evaluation.is_experiment_group @persistent_storage_utils.add_evaluation_to_user_persisted_values(user_persisted_values, config_name, evaluation) @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) evaluation = eval_spec(user, config) end return evaluation end |
#get_layer(user, layer_name) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/evaluator.rb', line 129 def get_layer(user, layer_name) if @spec_store.init_reason == EvaluationReason::UNINITIALIZED return Statsig::ConfigResult.new(layer_name, evaluation_details: EvaluationDetails.uninitialized) end unless @spec_store.has_layer?(layer_name) return Statsig::ConfigResult.new(layer_name, evaluation_details: EvaluationDetails.unrecognized(@spec_store.last_config_sync_time, @spec_store.initial_config_sync_time)) end eval_spec(user, @spec_store.get_layer(layer_name)) end |
#list_autotunes ⇒ Object
153 154 155 |
# File 'lib/evaluator.rb', line 153 def list_autotunes @spec_store.configs.map { |name, config| name if config['entity'] == 'autotune' }.compact end |
#list_configs ⇒ Object
145 146 147 |
# File 'lib/evaluator.rb', line 145 def list_configs @spec_store.configs.map { |name, config| name if config['entity'] == 'dynamic_config' }.compact end |
#list_experiments ⇒ Object
149 150 151 |
# File 'lib/evaluator.rb', line 149 def list_experiments @spec_store.configs.map { |name, config| name if config['entity'] == 'experiment' }.compact end |
#list_gates ⇒ Object
141 142 143 |
# File 'lib/evaluator.rb', line 141 def list_gates @spec_store.gates.map { |name, _| name } end |
#list_layers ⇒ Object
157 158 159 |
# File 'lib/evaluator.rb', line 157 def list_layers @spec_store.layers.map { |name, _| name } end |
#maybe_restart_background_threads ⇒ Object
49 50 51 |
# File 'lib/evaluator.rb', line 49 def maybe_restart_background_threads @spec_store.maybe_restart_background_threads end |
#override_config(config, value) ⇒ Object
209 210 211 |
# File 'lib/evaluator.rb', line 209 def override_config(config, value) @config_overrides[config] = value end |
#override_gate(gate, value) ⇒ Object
205 206 207 |
# File 'lib/evaluator.rb', line 205 def override_gate(gate, value) @gate_overrides[gate] = value end |
#shutdown ⇒ Object
201 202 203 |
# File 'lib/evaluator.rb', line 201 def shutdown @spec_store.shutdown end |