Class: Statsig::Evaluator

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/evaluator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(network, options, error_callback, diagnostics, error_boundary, logger, persistent_storage_utils) ⇒ Evaluator

Returns a new instance of Evaluator.



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

def initialize(network, options, error_callback, diagnostics, error_boundary, logger, persistent_storage_utils)
  @spec_store = Statsig::SpecStore.new(network, options, error_callback, diagnostics, error_boundary, logger)
  UAParser.initialize_async
  CountryLookup.initialize_async

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

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



27
28
29
# File 'lib/evaluator.rb', line 27

def options
  @options
end

#persistent_storage_utilsObject

Returns the value of attribute persistent_storage_utils.



30
31
32
# File 'lib/evaluator.rb', line 30

def persistent_storage_utils
  @persistent_storage_utils
end

#spec_storeObject

Returns the value of attribute spec_store.



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

def spec_store
  @spec_store
end

Instance Method Details

#check_gate(user, gate_name) ⇒ Object



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

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



176
177
178
179
180
181
182
183
184
# File 'lib/evaluator.rb', line 176

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



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

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 $fetch_from_server if result.to_s == $fetch_from_server
      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']
        )
      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']
  )
end

#get_client_initialize_response(user, hash, client_sdk_key) ⇒ Object



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

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



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

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



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/evaluator.rb', line 134

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

#maybe_restart_background_threadsObject



54
55
56
# File 'lib/evaluator.rb', line 54

def maybe_restart_background_threads
  @spec_store.maybe_restart_background_threads
end

#override_config(config, value) ⇒ Object



194
195
196
# File 'lib/evaluator.rb', line 194

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

#override_gate(gate, value) ⇒ Object



190
191
192
# File 'lib/evaluator.rb', line 190

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

#shutdownObject



186
187
188
# File 'lib/evaluator.rb', line 186

def shutdown
  @spec_store.shutdown
end