Module: Shipeasy::SDK::Eval
- Defined in:
- lib/shipeasy/sdk/eval.rb
Defined Under Namespace
Classes: ExperimentResult
Class Method Summary collapse
- .enabled?(v) ⇒ Boolean
-
.eval_experiment(exp, flags_blob, exps_blob, user, exp_name: nil, sticky_store: nil) ⇒ Object
exp_name + sticky_store are optional so existing callers stay deterministic.
- .eval_gate(gate, user) ⇒ Object
- .match_rule(rule, user) ⇒ Object
- .murmur3(key) ⇒ Object
-
.pick_identifier(user, bucket_by) ⇒ Object
Pick the bucketing identifier.
- .to_num(v) ⇒ Object
Class Method Details
.enabled?(v) ⇒ Boolean
10 11 12 |
# File 'lib/shipeasy/sdk/eval.rb', line 10 def self.enabled?(v) v == 1 || v == true end |
.eval_experiment(exp, flags_blob, exps_blob, user, exp_name: nil, sticky_store: nil) ⇒ Object
exp_name + sticky_store are optional so existing callers stay deterministic. When a sticky_store is passed, an enrolled unit whose stored salt prefix still matches skips the allocation gate (so a shrinking allocation keeps it in) and returns the stored group without re-running the pick. A fresh pick is persisted via store.set; a salt mismatch / missing stored group falls through to re-bucket + overwrite. Mirrors the TS reference (doc 20 §2). exp_name is the key under which the entry is stored.
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/shipeasy/sdk/eval.rb', line 102 def self.eval_experiment(exp, flags_blob, exps_blob, user, exp_name: nil, sticky_store: nil) not_in = ExperimentResult.new(in_experiment: false, group: "control", params: nil) return not_in unless exp && exp["status"] == "running" targeting_gate = exp["targetingGate"] if targeting_gate && !targeting_gate.empty? gate = flags_blob&.dig("gates", targeting_gate) return not_in unless gate && eval_gate(gate, user) end bucket_by = exp["bucketBy"] || exp[:bucketBy] uid = pick_identifier(user, bucket_by) return not_in unless uid universe_name = exp["universe"] universe = exps_blob&.dig("universes", universe_name) holdout = universe&.dig("holdout_range") if holdout seg = murmur3("#{universe_name}:#{uid}") % 10000 return not_in if seg >= holdout[0] && seg <= holdout[1] end salt = exp["salt"] allocation_pct = exp["allocationPct"] || 0 groups = exp["groups"] || [] salt8 = (salt || "")[0, 8] # Sticky short-circuit: an enrolled unit whose stored salt prefix still # matches skips allocation and returns the stored group. If the stored # group no longer exists, fall through to re-bucket + overwrite. if sticky_store && exp_name entry = (sticky_store.get(uid) || {})[exp_name] if entry && entry["s"] == salt8 g = groups.find { |x| x["name"] == entry["g"] } return ExperimentResult.new(in_experiment: true, group: g["name"], params: g["params"]) if g end end return not_in if murmur3("#{salt}:alloc:#{uid}") % 10000 >= allocation_pct group_hash = murmur3("#{salt}:group:#{uid}") % 10000 cumulative = 0 groups.each_with_index do |g, i| cumulative += g["weight"] if group_hash < cumulative || i == groups.length - 1 sticky_store.set(uid, exp_name, { "g" => g["name"], "s" => salt8 }) if sticky_store && exp_name return ExperimentResult.new(in_experiment: true, group: g["name"], params: g["params"]) end end not_in end |
.eval_gate(gate, user) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/shipeasy/sdk/eval.rb', line 73 def self.eval_gate(gate, user) return false if enabled?(gate["killswitch"]) return false unless enabled?(gate["enabled"]) (gate["rules"] || []).each do |rule| return false unless match_rule(rule, user) end uid = user["user_id"] || user[:user_id] || user["anonymous_id"] || user[:anonymous_id] # No unit id (an unidentified request before any anon id is minted): a # fully-rolled gate is on for everyone, so it can be answered without # bucketing; a fractional rollout genuinely needs a stable unit, so deny # until one exists. Rules above are still checked, so targeting wins. # See experiment-platform/18-identity-bucketing.md. return (gate["rolloutPct"] || gate[:rolloutPct] || 0) >= 10000 unless uid salt = gate["salt"] || gate[:salt] murmur3("#{salt}:#{uid}") % 10000 < (gate["rolloutPct"] || gate[:rolloutPct] || 0) end |
.match_rule(rule, user) ⇒ Object
23 24 25 26 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 |
# File 'lib/shipeasy/sdk/eval.rb', line 23 def self.match_rule(rule, user) attr = rule["attr"] || rule[:attr] op = rule["op"] || rule[:op] value = rule["value"] || rule[:value] actual = user[attr] || user[attr.to_sym] case op when "eq" then actual == value when "neq" then actual != value when "in" then Array(value).include?(actual) when "not_in" then !Array(value).include?(actual) when "contains" if actual.is_a?(String) && value.is_a?(String) actual.include?(value) elsif actual.is_a?(Array) actual.include?(value) else false end when "regex" actual.is_a?(String) && value.is_a?(String) && Regexp.new(value).match?(actual) rescue false when "gt", "gte", "lt", "lte" a = to_num(actual) b = to_num(value) return false if a.nil? || b.nil? case op when "gt" then a > b when "gte" then a >= b when "lt" then a < b when "lte" then a <= b end else false end end |
.murmur3(key) ⇒ Object
6 7 8 |
# File 'lib/shipeasy/sdk/eval.rb', line 6 def self.murmur3(key) Murmur3.hash32(key, 0) end |
.pick_identifier(user, bucket_by) ⇒ Object
Pick the bucketing identifier. When bucket_by is set and the user carries that attribute as a non-empty string (or any number, stringified), bucket on it — so a whole company/org lands on one variant. Otherwise fall back to user_id, then anonymous_id. Mirrors core’s pickIdentifier.
64 65 66 67 68 69 70 71 |
# File 'lib/shipeasy/sdk/eval.rb', line 64 def self.pick_identifier(user, bucket_by) if bucket_by && !bucket_by.to_s.empty? v = user[bucket_by] || user[bucket_by.to_sym] return v if v.is_a?(String) && !v.empty? return v.to_s if v.is_a?(Numeric) end user["user_id"] || user[:user_id] || user["anonymous_id"] || user[:anonymous_id] end |
.to_num(v) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/shipeasy/sdk/eval.rb', line 14 def self.to_num(v) case v when Numeric then v.finite? ? v : nil when String n = Float(v) rescue nil n&.finite? ? n : nil end end |