Class: Statsig::Evaluator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(network, options, error_callback, init_diagnostics = nil) ⇒ Evaluator

Returns a new instance of Evaluator.



20
21
22
23
24
25
26
27
# File 'lib/evaluator.rb', line 20

def initialize(network, options, error_callback, init_diagnostics = nil)
  @spec_store = Statsig::SpecStore.new(network, options, error_callback, init_diagnostics)
  UAParser.initialize_async
  CountryLookup.initialize_async

  @gate_overrides = {}
  @config_overrides = {}
end

Instance Attribute Details

#spec_storeObject

Returns the value of attribute spec_store.



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

def spec_store
  @spec_store
end

Instance Method Details

#check_gate(user, gate_name) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/evaluator.rb', line 33

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



129
130
131
132
133
134
135
136
137
# File 'lib/evaluator.rb', line 129

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



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

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) ⇒ Object



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 101

def get_client_initialize_response(user)
  if @spec_store.is_ready_for_checks == false
    return nil
  end

  formatter = ClientInitializeHelpers::ResponseFormatter.new(self, user)

  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,
  }
end

#get_config(user, config_name) ⇒ Object



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

def get_config(user, config_name)
  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

  eval_spec(user, @spec_store.get_config(config_name))
end

#get_layer(user, layer_name) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/evaluator.rb', line 89

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



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

def maybe_restart_background_threads
  @spec_store.maybe_restart_background_threads
end

#override_config(config, value) ⇒ Object



147
148
149
# File 'lib/evaluator.rb', line 147

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

#override_gate(gate, value) ⇒ Object



143
144
145
# File 'lib/evaluator.rb', line 143

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

#shutdownObject



139
140
141
# File 'lib/evaluator.rb', line 139

def shutdown
  @spec_store.shutdown
end