Class: Statsig::ResponseFormatter

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

Class Method Summary collapse

Class Method Details

.get_responses(entities, evaluator, user, client_sdk_key, hash_algo, include_exposures: true, include_local_overrides: false) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/client_initialize_helpers.rb', line 6

def self.get_responses(
  entities,
  evaluator,
  user,
  client_sdk_key,
  hash_algo,
  include_exposures: true,
  include_local_overrides: false
)
  result = {}
  target_app_id = evaluator.spec_store.get_app_id_for_sdk_key(client_sdk_key)
  entities.each do |name, spec|
    config_target_apps = spec[:targetAppIDs]

    unless target_app_id.nil? || (!config_target_apps.nil? && config_target_apps.include?(target_app_id))
      next
    end
    hashed_name, value = to_response(name, spec, evaluator, user, client_sdk_key, hash_algo, include_exposures, include_local_overrides)
    if !hashed_name.nil? && !value.nil?
      result[hashed_name] = value
    end
  end

  result
end

.hash_exposures(exposures, hash_algo) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/client_initialize_helpers.rb', line 102

def self.hash_exposures(exposures, hash_algo)
  return nil if exposures.nil?
  hashed_exposures = []
  exposures.each do |exp|
    hashed_exposures << {
      gate: hash_name(exp[:gate], hash_algo),
      gateValue: exp[:gateValue],
      ruleID: exp[:ruleID]
    }
  end

  hashed_exposures
end

.hash_name(name, hash_algo) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/client_initialize_helpers.rb', line 146

def self.hash_name(name, hash_algo)
  Statsig::Memo.for_global(:hash_name, "#{hash_algo}|#{name}") do
    case hash_algo
    when Statsig::Const::NONE
      name
    when Statsig::Const::DJB2
      Statsig::HashUtils.djb2(name)
    else
      Statsig::HashUtils.sha256(name)
    end
  end
end

.populate_experiment_fields(config_name, config_spec, eval_result, result, evaluator) ⇒ Object



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

def self.populate_experiment_fields(config_name, config_spec, eval_result, result, evaluator)
  result[:is_user_in_experiment] = eval_result.is_experiment_group
  result[:is_experiment_active] = config_spec[:isActive] == true

  if config_spec[:hasSharedParams] != true
    return
  end

  result[:is_in_layer] = true
  result[:explicit_parameters] = config_spec[:explicitParameters] || []
end

.populate_layer_fields(config_spec, eval_result, result, evaluator, hash_algo, include_exposures) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/client_initialize_helpers.rb', line 128

def self.populate_layer_fields(config_spec, eval_result, result, evaluator, hash_algo, include_exposures)
  delegate = eval_result.config_delegate
  result[:explicit_parameters] = config_spec[:explicitParameters] || []

  if delegate.nil? == false && delegate.empty? == false
    delegate_spec = evaluator.spec_store.configs[delegate.to_sym]

    result[:allocated_experiment_name] = hash_name(delegate, hash_algo)
    result[:is_user_in_experiment] = eval_result.is_experiment_group
    result[:is_experiment_active] = delegate_spec[:isActive] == true
    result[:explicit_parameters] = delegate_spec[:explicitParameters] || []
  end

  if include_exposures
    result[:undelegated_secondary_exposures] = hash_exposures(eval_result.undelegated_sec_exps || [], hash_algo)
  end
end

.to_response(config_name, config_spec, evaluator, user, client_sdk_key, hash_algo, include_exposures, include_local_overrides) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/client_initialize_helpers.rb', line 32

def self.to_response(config_name, config_spec, evaluator, user, client_sdk_key, hash_algo, include_exposures, include_local_overrides)
  category = config_spec[:type]
  entity_type = config_spec[:entity]
  if entity_type == Const::TYPE_SEGMENT || entity_type == Const::TYPE_HOLDOUT
    return nil
  end

  if include_local_overrides
    case category
    when Const::TYPE_FEATURE_GATE
      local_override = evaluator.lookup_gate_override(config_name)
    when Const::TYPE_DYNAMIC_CONFIG
      local_override = evaluator.lookup_config_override(config_name)
    end
  end

  config_name_str = config_name.to_s
  if local_override.nil?
    eval_result = ConfigResult.new(
      name: config_name,
      disable_evaluation_details: true,
      disable_exposures: !include_exposures,
      include_local_overrides: include_local_overrides
    )
    evaluator.eval_spec(config_name_str, user, config_spec, eval_result)
  else
    eval_result = local_override
  end

  result = {}

  result[:id_type] = eval_result.id_type
  unless eval_result.group_name.nil?
    result[:group_name] = eval_result.group_name
  end

  case category
  when Const::TYPE_FEATURE_GATE
    result[:value] = eval_result.gate_value
  when Const::TYPE_DYNAMIC_CONFIG
    id_type = config_spec[:idType]
    result[:value] = eval_result.json_value
    result[:group] = eval_result.rule_id
    result[:is_device_based] = id_type.is_a?(String) && id_type.downcase == Statsig::Const::STABLEID
    result[:passed] = eval_result.gate_value
  else
    return nil
  end

  if entity_type == Const::TYPE_EXPERIMENT
    populate_experiment_fields(name, config_spec, eval_result, result, evaluator)
  end

  if entity_type == Const::TYPE_LAYER
    populate_layer_fields(config_spec, eval_result, result, evaluator, hash_algo, include_exposures)
    result.delete(:id_type) # not exposed for layer configs in /initialize
  end

  hashed_name = hash_name(config_name_str, hash_algo)

  result[:name] = hashed_name
  result[:rule_id] = eval_result.rule_id

  if include_exposures
    result[:secondary_exposures] = hash_exposures(eval_result.secondary_exposures, hash_algo)
  end

  [hashed_name, result]
end