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

Returns a new instance of Evaluator.



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

def initialize(network, options, error_callback)
  @spec_store = Statsig::SpecStore.new(network, options, error_callback)
  @ua_parser = UserAgentParser::Parser.new
  CountryLookup.initialize

  @gate_overrides = {}
  @config_overrides = {}
end

Instance Attribute Details

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



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

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



116
117
118
119
120
121
122
123
124
# File 'lib/evaluator.rb', line 116

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



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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/evaluator.rb', line 138

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

#get_client_initialize_response(user) ⇒ Object



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

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



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

def get_config(user, config_name)
  if @config_overrides.has_key?(config_name)
    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))
  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



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/evaluator.rb', line 76

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



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

def maybe_restart_background_threads
  @spec_store.maybe_restart_background_threads
end

#override_config(config, value) ⇒ Object



134
135
136
# File 'lib/evaluator.rb', line 134

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

#override_gate(gate, value) ⇒ Object



130
131
132
# File 'lib/evaluator.rb', line 130

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

#shutdownObject



126
127
128
# File 'lib/evaluator.rb', line 126

def shutdown
  @spec_store.shutdown
end