Class: Statsig::ResponseFormatter
- Inherits:
-
Object
- Object
- Statsig::ResponseFormatter
- Defined in:
- lib/client_initialize_helpers.rb
Class Method Summary collapse
- .get_responses(entities, evaluator, user, client_sdk_key, hash_algo, include_exposures: true, include_local_overrides: false) ⇒ Object
- .hash_name(name, hash_algo) ⇒ Object
- .populate_experiment_fields(config_name, config_spec, eval_result, result, evaluator) ⇒ Object
- .populate_layer_fields(config_spec, eval_result, result, evaluator, hash_algo, include_exposures) ⇒ Object
- .to_response(config_name, config_spec, evaluator, user, client_sdk_key, hash_algo, include_exposures, include_local_overrides) ⇒ Object
Class Method Details
.get_responses(entities, evaluator, user, client_sdk_key, hash_algo, include_exposures: true, include_local_overrides: false) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/client_initialize_helpers.rb', line 7 def self.get_responses( entities, evaluator, user, client_sdk_key, hash_algo, include_exposures: true, include_local_overrides: false ) result = {} entities.each do |name, spec| 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_name(name, hash_algo) ⇒ Object
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/client_initialize_helpers.rb', line 140 def self.hash_name(name, hash_algo) case hash_algo when Statsig::Const::NONE return name when Statsig::Const::DJB2 return Statsig::HashUtils.djb2(name) else return Statsig::HashUtils.sha256(name) end end |
.populate_experiment_fields(config_name, config_spec, eval_result, result, evaluator) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/client_initialize_helpers.rb', line 102 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.is_active == true if config_spec.has_shared_params != true return end result[:is_in_layer] = true result[:explicit_parameters] = config_spec.explicit_parameters || [] layer_name = evaluator.spec_store.experiment_to_layer[config_name] if layer_name.nil? || evaluator.spec_store.layers[layer_name].nil? return end layer = evaluator.spec_store.layers[layer_name] result[:value] = layer[:defaultValue].merge(result[:value]) end |
.populate_layer_fields(config_spec, eval_result, result, evaluator, hash_algo, include_exposures) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/client_initialize_helpers.rb', line 122 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.explicit_parameters || [] if delegate.nil? == false && delegate.empty? == false delegate_spec = evaluator.spec_store.configs[delegate] 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.is_active == true result[:explicit_parameters] = delegate_spec.explicit_parameters || [] end if include_exposures result[:undelegated_secondary_exposures] = eval_result.undelegated_sec_exps || [] end end |
.to_response(config_name, config_spec, evaluator, user, client_sdk_key, hash_algo, include_exposures, include_local_overrides) ⇒ Object
27 28 29 30 31 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 27 def self.to_response(config_name, config_spec, evaluator, user, client_sdk_key, hash_algo, include_exposures, include_local_overrides) target_app_id = evaluator.spec_store.get_app_id_for_sdk_key(client_sdk_key) config_target_apps = config_spec.target_app_ids unless target_app_id.nil? || (!config_target_apps.nil? && config_target_apps.include?(target_app_id)) return nil end category = config_spec.type entity_type = config_spec.entity if entity_type == :segment || entity_type == :holdout return nil end if include_local_overrides case category when :feature_gate local_override = evaluator.lookup_gate_override(config_name) when :dynamic_config local_override = evaluator.lookup_config_override(config_name) end end if local_override.nil? eval_result = ConfigResult.new( name: config_name, disable_evaluation_details: true, disable_exposures: !include_exposures ) evaluator.eval_spec(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 :feature_gate result[:value] = eval_result.gate_value when :dynamic_config id_type = config_spec.id_type 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 else return nil end if entity_type == :experiment populate_experiment_fields(name, config_spec, eval_result, result, evaluator) end if entity_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, hash_algo) result[:name] = hashed_name result[:rule_id] = eval_result.rule_id if include_exposures result[:secondary_exposures] = eval_result.secondary_exposures end [hashed_name, result] end |