growthbook 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/growthbook/conditions.rb +13 -3
- data/lib/growthbook/context.rb +70 -1
- data/lib/growthbook/redis_resolver.rb +76 -0
- data/lib/growthbook/remote_saved_groups.rb +94 -0
- data/lib/growthbook/version.rb +6 -0
- data/lib/growthbook.rb +2 -0
- metadata +5 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5eb34725d5148fbbc06e76a4d50d03adab9c1aa4f0680de6c76250d9532d6314
|
|
4
|
+
data.tar.gz: abfe0006395e6530c2118055a9d63c343f5abbb422b774da3aacb25e5bd128e7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 232022e2b2de32ecc090cf66aad9a24406d9c5269db779d5d25ecb2b3bcd840d09e997fd1f74e36fabd9e983b1cba3185dcad3ece4584bd8bd19d7978fdf6b9c
|
|
7
|
+
data.tar.gz: c03a85dc1f4360fca5033dc7e0045f86a45b8bc5625c84a3b8426e999364d2ffec425419708cdea6a3c9fa7639f4d93cef32710638874d63e0ba14066fff18cb
|
|
@@ -120,9 +120,10 @@ module Growthbook
|
|
|
120
120
|
end
|
|
121
121
|
|
|
122
122
|
# Resolves a `$savedGroup` reference. Anything unrecognized matches nobody.
|
|
123
|
-
# With prune, an unknown attribute leaves a list group undecided (nil),
|
|
124
|
-
#
|
|
125
|
-
#
|
|
123
|
+
# With prune, an unknown attribute leaves a list group undecided (nil), unknown
|
|
124
|
+
# remote group ids leave a remote group undecided, and a condition group is
|
|
125
|
+
# pruned rather than evaluated. With keep_malformed, a condition group that
|
|
126
|
+
# raises while pruning is returned as written.
|
|
126
127
|
def self.eval_saved_group(attributes, reference, ctx, prune: false, keep_malformed: false)
|
|
127
128
|
return false unless reference.is_a?(Hash)
|
|
128
129
|
|
|
@@ -146,6 +147,15 @@ module Growthbook
|
|
|
146
147
|
return nil if prune && !known?(attributes, key)
|
|
147
148
|
|
|
148
149
|
in?(get_path(attributes, key), entry['values'])
|
|
150
|
+
when 'remote'
|
|
151
|
+
# The ids live outside the payload: the user's groups arrive resolved, in
|
|
152
|
+
# the __gb_remoteGroupIds attribute. Another attribute needs a derived
|
|
153
|
+
# group, which isn't supported yet.
|
|
154
|
+
return false if reference.key?('attributeKey') && attribute_key != entry['attributeKey']
|
|
155
|
+
return nil if prune && !known?(attributes, RemoteSavedGroups::ATTRIBUTE)
|
|
156
|
+
|
|
157
|
+
group_ids = get_path(attributes, RemoteSavedGroups::ATTRIBUTE)
|
|
158
|
+
group_ids.is_a?(Array) && group_ids.include?(id)
|
|
149
159
|
when 'condition'
|
|
150
160
|
# A condition group has no single attribute, so any override is ignored
|
|
151
161
|
return false unless entry['condition'].is_a?(Hash)
|
data/lib/growthbook/context.rb
CHANGED
|
@@ -38,6 +38,11 @@ module Growthbook
|
|
|
38
38
|
# @return [Hash[String, Array]] Saved groups, used by the $inGroup/$notInGroup operators
|
|
39
39
|
attr_reader :saved_groups
|
|
40
40
|
|
|
41
|
+
# @return [Array<String>, nil] The remote saved groups the user is in, resolved by the app.
|
|
42
|
+
# nil means unresolved, which is not the same as no groups: rules using remote
|
|
43
|
+
# groups then fail, "not in group" included. See #add_remote_saved_groups.
|
|
44
|
+
attr_reader :remote_group_ids
|
|
45
|
+
|
|
41
46
|
# @return [Growthbook::StickyBucketService] Sticky bucket service for sticky bucketing
|
|
42
47
|
attr_reader :sticky_bucket_service
|
|
43
48
|
|
|
@@ -63,9 +68,11 @@ module Growthbook
|
|
|
63
68
|
@impressions = {}
|
|
64
69
|
@sticky_bucket_assignment_docs = {}
|
|
65
70
|
@saved_groups = {}
|
|
71
|
+
@remote_group_ids = nil
|
|
66
72
|
|
|
67
73
|
features = {}
|
|
68
74
|
attributes = {}
|
|
75
|
+
remote_group_ids = nil
|
|
69
76
|
|
|
70
77
|
options.transform_keys(&:to_sym).each do |key, value|
|
|
71
78
|
case key
|
|
@@ -98,6 +105,8 @@ module Growthbook
|
|
|
98
105
|
@sticky_bucket_identifier_attributes = value
|
|
99
106
|
when :saved_groups, :savedGroups
|
|
100
107
|
@saved_groups = value || {}
|
|
108
|
+
when :remote_group_ids, :remoteGroupIds
|
|
109
|
+
remote_group_ids = value
|
|
101
110
|
else
|
|
102
111
|
warn("Unknown context option: #{key}")
|
|
103
112
|
end
|
|
@@ -105,6 +114,7 @@ module Growthbook
|
|
|
105
114
|
|
|
106
115
|
@using_derived_sticky_bucket_attributes = !@sticky_bucket_identifier_attributes
|
|
107
116
|
self.attributes = attributes
|
|
117
|
+
self.remote_group_ids = remote_group_ids
|
|
108
118
|
self.features = features
|
|
109
119
|
end
|
|
110
120
|
|
|
@@ -125,10 +135,67 @@ module Growthbook
|
|
|
125
135
|
|
|
126
136
|
def attributes=(attrs)
|
|
127
137
|
@attributes = stringify_keys(attrs || {})
|
|
138
|
+
@condition_attributes = RemoteSavedGroups.with_ids(@attributes, @remote_group_ids)
|
|
128
139
|
|
|
129
140
|
refresh_sticky_buckets
|
|
130
141
|
end
|
|
131
142
|
|
|
143
|
+
# Sets the remote saved groups the user is in. A remote saved group keeps its
|
|
144
|
+
# members outside the payload, so the app resolves the user's groups and passes
|
|
145
|
+
# their ids here, or uses #add_remote_saved_groups. Conditions read them as the
|
|
146
|
+
# `__gb_remoteGroupIds` attribute, over any value the attributes set for it.
|
|
147
|
+
#
|
|
148
|
+
# Pass nil while the groups are unresolved: rules that use remote groups then
|
|
149
|
+
# fail, including "not in group". Don't pass [] for that: it means "in no
|
|
150
|
+
# group", so "not in group" rules match.
|
|
151
|
+
#
|
|
152
|
+
# @param ids [Array<String>, nil]
|
|
153
|
+
def remote_group_ids=(ids)
|
|
154
|
+
@remote_group_ids = ids&.map(&:to_s)
|
|
155
|
+
@condition_attributes = RemoteSavedGroups.with_ids(@attributes, @remote_group_ids)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def saved_groups=(saved_groups)
|
|
159
|
+
@saved_groups = saved_groups || {}
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# Looks up the user's remote saved groups with resolver and sets them as
|
|
163
|
+
# #remote_group_ids. Leaves the context unchanged when the payload has no
|
|
164
|
+
# remote groups or the resolver raises.
|
|
165
|
+
#
|
|
166
|
+
# Requires a saved group references v2 payload for the group metadata. With
|
|
167
|
+
# inline/v1 payloads, resolve membership in the app and set #remote_group_ids
|
|
168
|
+
# directly instead.
|
|
169
|
+
#
|
|
170
|
+
# @param resolver [#call] Called with the attributes the payload's remote groups
|
|
171
|
+
# use (keyed by attributeKey) and the groups (hashes with 'id', 'attributeKey'
|
|
172
|
+
# and, for a derived group, 'valuesFrom'). Returns the ids of the groups the
|
|
173
|
+
# user is in. See Growthbook::RedisResolver.
|
|
174
|
+
# @return [Growthbook::Context] self
|
|
175
|
+
def add_remote_saved_groups(resolver)
|
|
176
|
+
saved_groups = @saved_groups
|
|
177
|
+
# No remote entries doesn't mean no remote rules: payloads for SDKs without
|
|
178
|
+
# remote support check __gb_remoteGroupIds directly. Leaving it unset makes
|
|
179
|
+
# those rules fail closed; [] would let every "not in group" rule match.
|
|
180
|
+
return self if RemoteSavedGroups.groups(saved_groups).empty?
|
|
181
|
+
|
|
182
|
+
ids = RemoteSavedGroups.resolve(@attributes, saved_groups, resolver)
|
|
183
|
+
# A new payload set during the lookup can add groups the result doesn't
|
|
184
|
+
# cover, and a missing group would pass "not in group" rules. Look up once
|
|
185
|
+
# more for the new payload.
|
|
186
|
+
unless @saved_groups.equal?(saved_groups)
|
|
187
|
+
return self if RemoteSavedGroups.groups(@saved_groups).empty?
|
|
188
|
+
|
|
189
|
+
ids = RemoteSavedGroups.resolve(@attributes, @saved_groups, resolver)
|
|
190
|
+
end
|
|
191
|
+
self.remote_group_ids = ids
|
|
192
|
+
self
|
|
193
|
+
rescue StandardError => e
|
|
194
|
+
# The class only: messages can hold attribute values, such as a Redis key
|
|
195
|
+
warn("GrowthBook: could not resolve remote saved groups (#{e.class})")
|
|
196
|
+
self
|
|
197
|
+
end
|
|
198
|
+
|
|
132
199
|
def forced_variations=(forced_variations)
|
|
133
200
|
@forced_variations = stringify_keys(forced_variations || {})
|
|
134
201
|
end
|
|
@@ -138,10 +205,12 @@ module Growthbook
|
|
|
138
205
|
end
|
|
139
206
|
|
|
140
207
|
def eval_feature(key)
|
|
208
|
+
RemoteSavedGroups.warn_if_missing_ids(@attributes, @remote_group_ids, @saved_groups)
|
|
141
209
|
_eval_feature(key, Set.new)
|
|
142
210
|
end
|
|
143
211
|
|
|
144
212
|
def run(exp)
|
|
213
|
+
RemoteSavedGroups.warn_if_missing_ids(@attributes, @remote_group_ids, @saved_groups)
|
|
145
214
|
_run(exp)
|
|
146
215
|
end
|
|
147
216
|
|
|
@@ -390,7 +459,7 @@ module Growthbook
|
|
|
390
459
|
def condition_passes?(condition)
|
|
391
460
|
return false if condition.nil?
|
|
392
461
|
|
|
393
|
-
Growthbook::Conditions.eval_condition(@
|
|
462
|
+
Growthbook::Conditions.eval_condition(@condition_attributes, condition, @saved_groups)
|
|
394
463
|
rescue StandardError
|
|
395
464
|
false
|
|
396
465
|
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'growthbook/util'
|
|
4
|
+
|
|
5
|
+
module Growthbook
|
|
6
|
+
# A resolver for Context#add_remote_saved_groups, for remote saved groups loaded
|
|
7
|
+
# into Redis by GrowthBook's loader: one `SMEMBERS gb:member:<attribute>:<value>`
|
|
8
|
+
# per attribute value, sent in one pipeline when the client has `pipelined`.
|
|
9
|
+
# Takes any client with `smembers(key)`, such as the redis
|
|
10
|
+
# gem's `Redis`; the SDK doesn't depend on one. Not loaded by `require 'growthbook'`:
|
|
11
|
+
#
|
|
12
|
+
# require 'growthbook/redis_resolver'
|
|
13
|
+
# context.add_remote_saved_groups(Growthbook::RedisResolver.new(Redis.new))
|
|
14
|
+
#
|
|
15
|
+
# A derived group checks its own attribute's value in its source group's sets:
|
|
16
|
+
# `grp_vip:parent_id` looks up `gb:member:<grp_vip's attribute>:<parent_id>` and
|
|
17
|
+
# matches when `grp_vip` comes back.
|
|
18
|
+
class RedisResolver
|
|
19
|
+
# @param redis [#smembers] A Redis client
|
|
20
|
+
def initialize(redis)
|
|
21
|
+
@redis = redis
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# @param attributes [Hash] The attributes the groups use, keyed by attributeKey
|
|
25
|
+
# @param groups [Array<Hash>] The payload's remote groups
|
|
26
|
+
# @return [Array<String>] The ids of the groups the user is in
|
|
27
|
+
def call(attributes, groups)
|
|
28
|
+
# For each group and value: the Redis key, and the id that means a match
|
|
29
|
+
checks = []
|
|
30
|
+
groups.each do |group|
|
|
31
|
+
source = group['valuesFrom'] ? groups.find { |g| g['id'] == group['valuesFrom'] } : group
|
|
32
|
+
next if source.nil?
|
|
33
|
+
|
|
34
|
+
lookup_values(attributes[group['attributeKey']]).each do |value|
|
|
35
|
+
checks << [group['id'], "gb:member:#{source['attributeKey']}:#{value}", source['id']]
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
keys = checks.map { |check| check[1] }.uniq
|
|
39
|
+
members = keys.zip(smembers(keys)).to_h
|
|
40
|
+
# Checking for the expected id, not any id in the set, also keeps keys that
|
|
41
|
+
# collide when attributes or values contain ":" (`a:b` + `c` and `a` + `b:c`)
|
|
42
|
+
# from mixing up groups.
|
|
43
|
+
checks.select { |_id, key, match_id| members[key].map(&:to_s).include?(match_id) }.map(&:first).uniq
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
# One pipeline when the client can pipeline these keys, so the lookups cost
|
|
49
|
+
# one round trip; otherwise one call per key
|
|
50
|
+
def smembers(keys)
|
|
51
|
+
return [] if keys.empty?
|
|
52
|
+
|
|
53
|
+
if @redis.respond_to?(:pipelined)
|
|
54
|
+
begin
|
|
55
|
+
return @redis.pipelined { |pipe| keys.each { |key| pipe.smembers(key) } }
|
|
56
|
+
rescue StandardError
|
|
57
|
+
# Redis::Distributed can't pipeline, and redis 4.x cluster mode can't
|
|
58
|
+
# pipeline keys on different nodes
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
keys.map { |key| @redis.smembers(key) }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Each item of an array is looked up. Numbers use JS `String(value)`, the same
|
|
65
|
+
# form the loader stores them in.
|
|
66
|
+
def lookup_values(value)
|
|
67
|
+
(value.is_a?(Array) ? value : [value]).each_with_object([]) do |v, out|
|
|
68
|
+
if v.is_a?(String)
|
|
69
|
+
out << v
|
|
70
|
+
elsif (v.is_a?(Integer) || v.is_a?(Float)) && v.to_f.finite?
|
|
71
|
+
out << Growthbook::Util.js_number_to_s(v)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Growthbook
|
|
4
|
+
# internal use only
|
|
5
|
+
# Remote saved groups (growthbook/growthbook#7277). A remote saved group keeps
|
|
6
|
+
# its members outside the payload, which only says which attribute the group is
|
|
7
|
+
# about: `{ "type" => "remote", "attributeKey" => ... }`. The app resolves which
|
|
8
|
+
# of them a user is in, and conditions read the ids from the reserved
|
|
9
|
+
# `__gb_remoteGroupIds` attribute.
|
|
10
|
+
module RemoteSavedGroups
|
|
11
|
+
# The attribute conditions read a user's remote saved groups from. Internal
|
|
12
|
+
# attributes start with `__gb_`, matching GrowthBook's back end.
|
|
13
|
+
ATTRIBUTE = '__gb_remoteGroupIds'
|
|
14
|
+
|
|
15
|
+
NO_GROUPS = [].freeze
|
|
16
|
+
|
|
17
|
+
# Evaluation checks for remote groups often, so each payload is scanned once.
|
|
18
|
+
# One entry is enough: a context holds one saved groups hash at a time.
|
|
19
|
+
@last_scan = nil
|
|
20
|
+
|
|
21
|
+
# Warned once per process, since apps usually make a context per request
|
|
22
|
+
@warned_missing_ids = false
|
|
23
|
+
|
|
24
|
+
# The payload's remote saved groups, as a resolver gets them: hashes with
|
|
25
|
+
# 'id', 'attributeKey' and, for a derived group, 'valuesFrom'. Only v2
|
|
26
|
+
# payloads carry this metadata.
|
|
27
|
+
def self.groups(saved_groups)
|
|
28
|
+
return NO_GROUPS if saved_groups.nil? || saved_groups.empty?
|
|
29
|
+
|
|
30
|
+
last = @last_scan
|
|
31
|
+
return last[1] if last && last[0].equal?(saved_groups)
|
|
32
|
+
|
|
33
|
+
groups = saved_groups.each_with_object([]) do |(id, entry), found|
|
|
34
|
+
# A remote entry without an attribute gives a resolver nothing to look up,
|
|
35
|
+
# so its rules fail closed
|
|
36
|
+
next unless entry.is_a?(Hash) && entry['type'] == 'remote' && entry['attributeKey'].is_a?(String)
|
|
37
|
+
|
|
38
|
+
group = { 'id' => id.to_s, 'attributeKey' => entry['attributeKey'] }
|
|
39
|
+
group['valuesFrom'] = entry['valuesFrom'] if entry['valuesFrom']
|
|
40
|
+
found << group.freeze
|
|
41
|
+
end.freeze
|
|
42
|
+
@last_scan = [saved_groups, groups]
|
|
43
|
+
groups
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Asks resolver which of the payload's remote groups the user is in. Only the
|
|
47
|
+
# attributes those groups use are passed. Raises what the resolver raises.
|
|
48
|
+
#
|
|
49
|
+
# Returns [] when the payload lists no remote groups. Don't set that as the
|
|
50
|
+
# user's remote group ids: the payload may still check `__gb_remoteGroupIds`
|
|
51
|
+
# directly, and [] makes "not in group" rules match. Check groups first.
|
|
52
|
+
def self.resolve(attributes, saved_groups, resolver)
|
|
53
|
+
groups = groups(saved_groups)
|
|
54
|
+
return [] if groups.empty?
|
|
55
|
+
|
|
56
|
+
used = groups.each_with_object({}) do |group, out|
|
|
57
|
+
# Keyed by attributeKey, which can be a dot-separated path
|
|
58
|
+
value = Growthbook::Conditions.get_path(attributes, group['attributeKey'])
|
|
59
|
+
out[group['attributeKey']] = value unless value.nil?
|
|
60
|
+
end
|
|
61
|
+
ids = resolver.call(used, groups)
|
|
62
|
+
raise TypeError, 'a saved group resolver must return an array of group ids' unless ids.is_a?(Array)
|
|
63
|
+
|
|
64
|
+
ids.map(&:to_s)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Whether the payload has remote groups but the user has no remote group ids,
|
|
68
|
+
# so every rule using them will fail
|
|
69
|
+
def self.missing_ids?(attributes, remote_group_ids, saved_groups)
|
|
70
|
+
remote_group_ids.nil? && !attributes[ATTRIBUTE].is_a?(Array) && !groups(saved_groups).empty?
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Logs once when the user has no remote group ids for the payload's remote
|
|
74
|
+
# groups. Logs no attribute values.
|
|
75
|
+
def self.warn_if_missing_ids(attributes, remote_group_ids, saved_groups)
|
|
76
|
+
return if @warned_missing_ids || !missing_ids?(attributes, remote_group_ids, saved_groups)
|
|
77
|
+
|
|
78
|
+
@warned_missing_ids = true
|
|
79
|
+
warn(
|
|
80
|
+
'GrowthBook: the payload has remote saved groups, but the user has no remote group ids. ' \
|
|
81
|
+
"Rules using them won't match. See Context#add_remote_saved_groups."
|
|
82
|
+
)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# The attributes conditions see: attributes, with the resolved remote group
|
|
86
|
+
# ids (when set) as `__gb_remoteGroupIds`, over any value the attributes set
|
|
87
|
+
# for it. A copy, so the caller's hash is left as it was.
|
|
88
|
+
def self.with_ids(attributes, remote_group_ids)
|
|
89
|
+
return attributes if remote_group_ids.nil?
|
|
90
|
+
|
|
91
|
+
attributes.merge(ATTRIBUTE => remote_group_ids)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
data/lib/growthbook.rb
CHANGED
|
@@ -61,7 +61,9 @@ require 'growthbook/fnv'
|
|
|
61
61
|
require 'growthbook/inline_experiment'
|
|
62
62
|
require 'growthbook/inline_experiment_result'
|
|
63
63
|
require 'growthbook/payload_reducer'
|
|
64
|
+
require 'growthbook/remote_saved_groups'
|
|
64
65
|
require 'growthbook/tracking_callback'
|
|
65
66
|
require 'growthbook/util'
|
|
67
|
+
require 'growthbook/version'
|
|
66
68
|
require 'growthbook/feature_usage_callback'
|
|
67
69
|
require 'growthbook/sticky_bucket_service'
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: growthbook
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.
|
|
4
|
+
version: 2.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- GrowthBook
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-30 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: base64
|
|
@@ -127,9 +127,12 @@ files:
|
|
|
127
127
|
- lib/growthbook/inline_experiment.rb
|
|
128
128
|
- lib/growthbook/inline_experiment_result.rb
|
|
129
129
|
- lib/growthbook/payload_reducer.rb
|
|
130
|
+
- lib/growthbook/redis_resolver.rb
|
|
131
|
+
- lib/growthbook/remote_saved_groups.rb
|
|
130
132
|
- lib/growthbook/sticky_bucket_service.rb
|
|
131
133
|
- lib/growthbook/tracking_callback.rb
|
|
132
134
|
- lib/growthbook/util.rb
|
|
135
|
+
- lib/growthbook/version.rb
|
|
133
136
|
homepage: https://github.com/growthbook/growthbook-ruby
|
|
134
137
|
licenses:
|
|
135
138
|
- MIT
|