GrowthBook.pm

filter: Statementstmt 66.8% 359 / 537 Branchbran 50.0% 93 / 186 Conditioncond 48.9% 48 / 98 MC/DCmcdc 24.5% 14 / 57 Subroutinesub 81.4% 35 / 43 Podpod 63.6% 7 / 11 totaltotal 59.6% 556 / 932 CC 153 SCAR 44.6
CC
153
Cov
61%
_run
251.3
_get_sticky_bucket_variation
132.0
_get_sticky_bucket_assignments
72.0
CRAP 86.1
↑ Summary GrowthBook/Eval.pm »
1 package WebService::GrowthBook;
2 # ABSTRACT: ...
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶no indirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use feature qw(state);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use JSON::MaybeUTF8 qw(decode_json_text);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Data::Compare qw(Compare);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use WebService::GrowthBook::FeatureRepository;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use WebService::GrowthBook::Feature;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use WebService::GrowthBook::FeatureResult;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use WebService::GrowthBook::InMemoryFeatureCache;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use WebService::GrowthBook::Eval qw(eval_condition);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use WebService::GrowthBook::Util qw(gbhash in_range get_query_string_override get_bucket_ranges choose_variation in_namespace adjust_args_camel_to_snake);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use WebService::GrowthBook::Experiment;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use WebService::GrowthBook::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 3 our $VERSION = '0.003';
23  
24 =head1 NAME
25  
26 WebService::GrowthBook - sdk of growthbook
27  
28 =head1 SYNOPSIS
29  
30 use WebService::GrowthBook;
31 my $instance = WebService::GrowthBook->new(client_key => 'my key');
32 $instance->load_features;
33 if($instance->is_on('feature_name')){
34 # do something
35 }
36 else {
37 # do something else
38 }
39 my $string_feature = $instance->get_feature_value('string_feature');
40 my $number_feature = $instance->get_feature_value('number_feature');
41 # get decoded json
42 my $json_feature = $instance->get_feature_value('json_feature');
43  
44 =head1 DESCRIPTION
45  
46 This module is a sdk of growthbook, it provides a simple way to use growthbook features.
47  
48 =cut
49  
50 # singletons
51  
52 class WebService::GrowthBook {
53 field $enabled :param //= 1;
54 3 field $url :param //= 'https://cdn.growthbook.io';
55 3 field $client_key :param //= "";
56 3 field $features :param //= {};
57 3 field $attributes :param :reader :writer //= {};
58 0 ▶ field $cache_ttl :param //= 60;
Subroutine attributes: called CC=1 SCAR=0.6
Subroutine set_attributes: not called CC=1 SCAR=0.6
Pod undocumented
Pod documented
59 3 field $user :param //= {};
60 3 field $forced_variations :param //= {};
61 3 field $overrides :param //= {};
62 3 field $sticky_bucket_service :param //= undef;
63 3 field $groups :param //= {};
64 3 field $qa_mode :param //= 0;
65 3 field $on_experiment_viewed :param //= undef;
66 3 field $tracking_callback :param //= undef;
67  
68 3 field $cache //= WebService::GrowthBook::InMemoryFeatureCache->singleton;
69 3 field $sticky_bucket_assignment_docs //= {};
70 3 field $tracked = {};
71 3 field $assigned = {};
72 3 field $subscriptions = [];
73  
74 3 ▶ sub BUILDARGS{
Subroutine BUILDARGS: called CC=1 SCAR=0.0
Pod undocumented
75 103 my ($class, %args) = @_;
76 103 adjust_args_camel_to_snake(\%args);
77 103 return %args;
78 }
79  
80 ADJUST {
81 $tracking_callback //= $on_experiment_viewed;
82 if($features){
83 $self->set_features($features);
84 }
85 }
86 3 ▶ method load_features {
Subroutine load_features: called CC=2 SCAR=7.1
Pod documented
87 2 my $feature_repository = WebService::GrowthBook::FeatureRepository->new(cache => $cache);
88 2 my $loaded_features = $feature_repository->load_features($url, $client_key, $cache_ttl);
89 2 ▶ if($loaded_features){
Branch: 1 branch
BranchTrueFalseTotal
if ($loaded_features) 2 0 2
90 2 $self->set_features($loaded_features);
91 2 return 1;
92 }
93 0 return undef;
94 3 }
95 105 ▶ method set_features($features_set) {
Subroutine set_features: called CC=4 SCAR=14.6
Pod documented
96 105 $features = {};
97 105 for my $feature_id (keys $features_set->%*) {
98 39 my $feature = $features_set->{$feature_id};
99 39 ▶ if(blessed($feature) && $feature->isa('WebService::GrowthBook::Feature')){
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($feature) and $feature->isa('WebService::GrowthBook::Feature')) { } 0 39 39
Condition33%1/3
builtin::blessed($feature) and $feature->isa("WebService::GrowthBook::Feature")
!ll&&!rl&&r
3900
MC/DC0%0/2
builtin::blessed($feature) and $feature->isa("WebService::GrowthBook::Feature")
builtin::blessed($feature) $feature->isa("WebService::GrowthBook::Feature")
Truth table33%1/3
A and B
A= builtin::blessed($feature)B= $feature->isa("WebService::GrowthBook::Feature")
ABresult
0X0
100
111
100 0 $features->{$feature->id} = $feature;
101 }
102 else {
103 39 $features->{$feature_id} = WebService::GrowthBook::Feature->new(id => $feature_id, default_value => $feature->{defaultValue}, rules => $feature->{rules});
104 }
105 }
106 3 }
107  
108 3 ▶ method is_on($feature_name) {
Subroutine is_on: called CC=2 SCAR=7.0
Pod documented
109 3 my $result = $self->eval_feature($feature_name);
110 3 ▶ return undef unless defined($result);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 3 3
111 3 return $result->on;
112 3 }
113  
114 3 ▶ method is_off($feature_name) {
Subroutine is_off: called CC=2 SCAR=7.0
Pod documented
115 3 my $result = $self->eval_feature($feature_name);
116 3 ▶ return undef unless defined($result);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 3 3
117 3 return $result->off;
118 3 }
119  
120 # I don't know why it is called stack in python version SDK. In fact it is a hash/dict
121 44 ▶ method _eval_feature($feature_name, $stack){
Subroutine _eval_feature: called CC=19 SCAR=31.0
122 44 $log->debug("Evaluating feature $feature_name");
123 44 ▶ if(!exists($features->{$feature_name})){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $features->{$feature_name}) 5 39 44
124 5 $log->debugf("No such feature: %s", $feature_name);
125 5 return WebService::GrowthBook::FeatureResult->new(feature_id => $feature_name, value => undef, source => "unknownFeature");
126 }
127  
128 39 ▶ if ($stack->{$feature_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stack->{$feature_name}) 0 39 39
129 0 $log->warnf("Cyclic prerequisite detected, stack: %s", $stack);
130 0 return WebService::GrowthBook::FeatureResult->new(id => $feature_name, value => undef, source => "cyclicPrerequisite");
131 }
132  
133 39 $stack->{$feature_name} = 1;
134  
135 39 my $feature = $features->{$feature_name};
136 39 for my $rule (@{$feature->rules}){
137 37 $log->debugf("Evaluating feature %s, rule %s", $feature_name, $rule->to_hash());
138 37 ▶ if ($rule->parent_conditions){
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->parent_conditions) 0 37 37
139 0 my $prereq_res = $self->eval_prereqs($rule->parent_conditions, $stack);
140 0 ▶ if ($prereq_res eq "gate") {
Branch: 3 branches
BranchTrueFalseTotal
if ($prereq_res eq 'gate') { } 0 0 0
elsif ($prereq_res eq 'cyclic') { } 0 0 0
elsif ($prereq_res eq 'fail') { } 0 0 0
141 0 $log->debugf("Top-lavel prerequisite failed, return undef, feature %s", $feature_name);
142 0 return WebService::GrowthBook::FeatureResult->new(id => $feature_name, value => undef, source => "prerequisite");
143 }
144 elsif ($prereq_res eq "cyclic") {
145 0 return WebService::GrowthBook::FeatureResult->new(id => $feature_name, value => undef, source => "cyclicPrerequisite");
146 }
147 elsif ($prereq_res eq "fail") {
148 0 $log->debugf("Skip rule becasue of failing prerequisite, feature %s", $feature_name);
149 0 next;
150 }
151 }
152  
153 37 ▶ if ($rule->condition){
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->condition) 11 26 37
154 11 ▶ if (!eval_condition($attributes, $rule->condition)){
Branch: 1 branch
BranchTrueFalseTotal
unless (eval_condition($attributes, $rule->condition)) 7 4 11
155 7 $log->debugf("Skip rule because of failed condition, feature %s", $feature_name);
156 7 next;
157 }
158 }
159  
160 30 ▶ if ($rule->filters) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->filters) 2 28 30
161 2 ▶ if ($self->_is_filtered_out($rule->filters)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_filtered_out($rule->filters)) 1 1 2
162 1 $log->debugf(
163 "Skip rule because of filters/namespaces, feature %s", $feature_name
164 );
165 1 next;
166 }
167 }
168  
169 29 ▶ if (defined($rule->force)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rule->force) 16 13 29
170 16 ▶ if(!$self->_is_included_in_rollout($rule->seed || $feature_name,
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_included_in_rollout($rule->seed || $feature_name, $rule->hash_attribute, $rule->fallback_attribute, $rule->range, $rule->coverage, $rule->hash_version)) 4 12 16
Condition66%2/3
$rule->seed || $feature_name
l!l&&r!l&&!r
1150
MC/DC0%0/2
$rule->seed || $feature_name
$rule->seed $feature_name
Truth table66%2/3
A || B
A= $rule->seedB= $feature_name
ABresult
1X1
011
000
171 $rule->hash_attribute,
172 $rule->fallback_attribute,
173 $rule->range,
174 $rule->coverage,
175 $rule->hash_version
176 )){
177 4 $log->debugf(
178 "Skip rule because user not included in percentage rollout, feature %s",
179 $feature_name,
180 );
181 4 next;
182 }
183 12 $log->debugf("Force value from rule, feature %s", $feature_name);
184 12 return WebService::GrowthBook::FeatureResult->new(
185 value => $rule->force,
186 source => "force",
187 rule_id => $rule->id,
188 feature_id => $feature_name,
189 );
190 }
191  
192 13 ▶ if(!defined($rule->variations)){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rule->variations) 1 12 13
193 1 $log->warnf("Skip invalid rule, feature %s", $feature_name);
194 1 next;
195 }
196 12 ▶ my $exp = WebService::GrowthBook::Experiment->new(
Condition66%2/3
$rule->key || $feature_name
l!l&&r!l&&!r
480
MC/DC0%0/2
$rule->key || $feature_name
$rule->key $feature_name
Truth table66%2/3
A || B
A= $rule->keyB= $feature_name
ABresult
1X1
011
000
197 # TODO change $feature_name to $key
198 key => $rule->key || $feature_name,
199 variations => $rule->variations,
200 coverage => $rule->coverage,
201 weights => $rule->weights,
202 hash_attribute => $rule->hash_attribute,
203 fallback_attribute => $rule->fallback_attribute,
204 namespace => $rule->namespace,
205 hash_version => $rule->hash_version,
206 meta => $rule->meta,
207 ranges => $rule->ranges,
208 name => $rule->name,
209 phase => $rule->phase,
210 seed => $rule->seed,
211 filters => $rule->filters,
212 # skip condition, since it will break test 246 and there is no condition in go version
213 #condition => $rule->condition,
214 disable_sticky_bucketing => $rule->disable_sticky_bucketing,
215 bucket_version => $rule->bucket_version,
216 min_bucket_version => $rule->min_bucket_version,
217 );
218 12 my $result = $self->_run($exp, $feature_name);
219 12 $self->_fire_subscriptions($exp, $result);
220 12 ▶ if (!$result->in_experiment) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result->in_experiment) 3 9 12
221 3 $log->debugf(
222 "Skip rule because user not included in experiment, feature %s", $feature_name
223 );
224 3 next;
225 }
226 9 ▶ if ($result->passthrough) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->passthrough) 1 8 9
227 1 $log->debugf("Continue to next rule, feature %s", $feature_name);
228  
229 1 next;
230 }
231  
232 8 $log->debugf("Assign value from experiment, feature %s", $feature_name);
233 8 return WebService::GrowthBook::FeatureResult->new(
234 value => $result->value,
235 source => "experiment",
236 experiment => $exp,
237 experiment_result => $result,
238 rule_id => $rule->id,
239 feature_id => $feature_name,
240 );
241 }
242 19 my $default_value = $feature->default_value;
243  
244 19 return WebService::GrowthBook::FeatureResult->new(
245 feature_id => $feature_name,
246 value => $default_value,
247 source => "defaultValue",
248 );
249 3 }
250  
251 82 ▶ method _fire_subscriptions($experiment, $result) {
Subroutine _fire_subscriptions: called CC=6 SCAR=21.4
252 82 my $prev = $assigned->{$experiment->key};
253 82 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $prev or $prev->{"result"}->in_experiment != $result->in_experiment or $prev->{"result"}->variation_id != $result->variation_id) 82 0 82
Condition33%2/6
not $prev or $prev->{"result"}->in_experiment != $result->in_experiment or $prev->{"result"}->variation_id != $result->variation_id
l!l&&r!l&&!r
8200
not $prev or $prev->{"result"}->in_experiment != $result->in_experiment
l!l&&r!l&&!r
8200
MC/DC0%0/3
not $prev or $prev->{"result"}->in_experiment != $result->in_experiment or $prev->{"result"}->variation_id != $result->variation_id
not $prev $prev->{"result"}->in_experiment != $result->in_experiment $prev->{"result"}->variation_id != $result->variation_id
Truth table25%1/4
not(A) or B or C
A= not $prevB= $prev->{"result"}->in_experiment != $result->in_experimentC= $prev->{"result"}->variation_id != $result->variation_id
ABCresult
1XX1
01X1
0011
0000
254 !$prev
255 || $prev->{result}->in_experiment != $result->in_experiment
256 || $prev->{result}->variation_id != $result->variation_id
257 ) {
258 82 $assigned->{$experiment->key} = {
259 experiment => $experiment,
260 result => $result,
261 };
262 82 foreach my $cb (@{$subscriptions}) {
263 eval {
264 0 $cb->($experiment, $result);
265 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &$cb($experiment, $result) } } or do { () } 0 0 0
266 # Handle exception silently
267 };
268 }
269 }
270 3 }
271  
272 82 ▶ method _run($experiment, $feature_id = undef){
Subroutine _run: called CC=50 SCAR=55.3
273 # 1. If experiment has less than 2 variations, return immediately
274 82 ▶ if (scalar @{$experiment->variations} < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$experiment->variations;} < 2) 1 81 82
275 1 $log->warnf(
276 "Experiment %s has less than 2 variations, skip", $experiment->key
277 );
278 1 return $self->_get_experiment_result($experiment, feature_id => $feature_id);
279 }
280  
281 # 2. If growthbook is disabled, return immediately
282 81 ▶ if (!$enabled) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($enabled) 1 80 81
283 1 $log->debugf(
284 "Skip experiment %s because GrowthBook is disabled", $experiment->key
285 );
286 1 return $self->_get_experiment_result($experiment, feature_id => $feature_id);
287 }
288 # 2.5. If the experiment props have been overridden, merge them in
289 80 ▶ if (exists $overrides->{$experiment->key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$overrides{$experiment->key}) 0 80 80
290 0 $experiment->update($overrides->{$experiment->{key}});
291 }
292  
293 # 3. If experiment is forced via a querystring in the URL
294 my $qs = get_query_string_override(
295 80 $experiment->key, $url, scalar @{$experiment->variations}
296 );
297 80 ▶ if (defined $qs) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $qs) 2 78 80
298 2 $log->debugf(
299 "Force variation %d from URL querystring, experiment %s",
300 $qs,
301 $experiment->key,
302 );
303 2 return $self->_get_experiment_result($experiment, variation_id => $qs, feature_id => $feature_id);
304 }
305  
306 # 4. If variation is forced in the context
307 78 ▶ if (exists $forced_variations->{$experiment->key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$forced_variations{$experiment->key}) 3 75 78
308 $log->debugf(
309 "Force variation %d from GrowthBook context, experiment %s",
310 3 $forced_variations->{$experiment->key},
311 $experiment->key,
312 );
313 return $self->_get_experiment_result(
314 3 $experiment, variation_id => $forced_variations->{$experiment->key}, feature_id => $feature_id
315 );
316 }
317  
318 # 5. If experiment is a draft or not active, return immediately
319 75 ▶ if ($experiment->status eq "draft" or not $experiment->active) {
Branch: 1 branch
BranchTrueFalseTotal
if ($experiment->status eq "draft" or not $experiment->active) 1 74 75
Condition66%2/3
$experiment->status eq "draft" or not $experiment->active
l!l&&r!l&&!r
0174
MC/DC50%1/2
$experiment->status eq "draft" or not $experiment->active
$experiment->status eq "draft" not $experiment->active
Truth table66%2/3
A or not(B)
A= $experiment->status eq "draft"B= not $experiment->active
ABresult
1X1
011
000
320 1 $log->debugf("Experiment %s is not active, skip", $experiment->key);
321 1 return $self->_get_experiment_result($experiment, feature_id => $feature_id);
322 }
323  
324 # 6. Get the user hash attribute and value
325 74 my ($hash_attribute, $hash_value) = $self->_get_hash_value($experiment->hash_attribute, $experiment->fallback_attribute);
326 74 ▶ if (!$hash_value) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hash_value) 5 69 74
327 5 $log->debugf(
328 "Skip experiment %s because user's hashAttribute value is empty",
329 $experiment->key,
330 );
331 5 return $self->_get_experiment_result($experiment, feature_id => $feature_id);
332 }
333  
334 69 my $assigned = -1;
335  
336 69 my $found_sticky_bucket = 0;
337 69 my $sticky_bucket_version_is_blocked = 0;
338 69 ▶ if ($sticky_bucket_service && !$experiment->disableStickyBucketing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sticky_bucket_service and not $experiment->disableStickyBucketing) 0 69 69
Condition33%1/3
$sticky_bucket_service and not $experiment->disableStickyBucketing
!ll&&!rl&&r
6900
MC/DC0%0/2
$sticky_bucket_service and not $experiment->disableStickyBucketing
$sticky_bucket_service not $experiment->disableStickyBucketing
Truth table33%1/3
A and not(B)
A= $sticky_bucket_serviceB= not $experiment->disableStickyBucketing
ABresult
0X0
100
111
339 0 my $sticky_bucket = $self->_get_sticky_bucket_variation(
340 experiment_key => $experiment->key,
341 bucket_version => $experiment->bucketVersion,
342 min_bucket_version => $experiment->minBucketVersion,
343 meta => $experiment->meta,
344 hash_attribute => $experiment->hashAttribute,
345 fallback_attribute => $experiment->fallbackAttribute,
346 );
347 0 $found_sticky_bucket = $sticky_bucket->{variation} >= 0;
348 0 $assigned = $sticky_bucket->{variation};
349 0 $sticky_bucket_version_is_blocked = $sticky_bucket->{versionIsBlocked};
350 }
351  
352  
353 69 ▶ if ($found_sticky_bucket) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found_sticky_bucket) { } 0 69 69
354 0 $log->debugf(
355 "Found sticky bucket for experiment %s, assigning sticky variation %s",
356 $experiment->key, $assigned
357 );
358 }
359  
360 # Some checks are not needed if we already have a sticky bucket
361 else {
362 69 ▶ if ($experiment->filters){
Branch: 2 branches
BranchTrueFalseTotal
if ($experiment->filters) { } 4 65 69
elsif ($experiment->namespace and not in_namespace($hash_value, $experiment->namespace)) { } 2 125 127
363  
364 # 7. Filtered out / not in namespace
365 4 ▶ if ($self->_is_filtered_out($experiment->filters)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_filtered_out($experiment->filters)) 1 3 4
366 1 $log->debugf(
367 "Skip experiment %s because of filters/namespaces", $experiment->key
368 );
369 1 return $self->_get_experiment_result($experiment, feature_id => $feature_id);
370 }
371 }
372 ▶ elsif ($experiment->namespace && !in_namespace($hash_value, $experiment->namespace)) {
Condition100%3/3
$experiment->namespace and not in_namespace($hash_value, $experiment->namespace)
!ll&&!rl&&r
6212
MC/DC100%2/2
$experiment->namespace and not in_namespace($hash_value, $experiment->namespace)
$experiment->namespace not in_namespace($hash_value, $experiment->namespace)
Truth table100%3/3
A and not(B)
A= $experiment->namespaceB= not in_namespace($hash_value, $experiment->namespace)
ABresult
0X0
100
111
373 2 $log->debugf("Skip experiment %s because of namespace", $experiment->key);
374 2 return $self->_get_experiment_result($experiment, feature_id => $feature_id);
375 }
376  
377 # 7.5. If experiment has an include property
378 66 ▶ if ($experiment->include) {
Branch: 1 branch
BranchTrueFalseTotal
if ($experiment->include) 0 66 66
379 eval {
380 0 ▶ unless ($experiment->include->()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($experiment->include->()) 0 0 0
381 0 $log->debugf(
382 "Skip experiment %s because include() returned false",
383 $experiment->key,
384 );
385 0 return $self->_get_experiment_result($experiment, feature_id => $feature_id);
386 }
387 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { unless ($experiment->include->()) { $WebService::GrowthBook::log->debugf("Skip experiment %s because include() returned false", $experiment->key); return $self->_get_experiment_result($experiment, "feature_id", $feature_id); } } } or do { $WebService::GrowthBook::log->warnf("Skip experiment %s because include() raised an Exception", $experiment->key); return $self->_get_experiment_result($experiment, "feature_id", $feature_id) } 0 0 0
388 0 $log->warnf(
389 "Skip experiment %s because include() raised an Exception",
390 $experiment->key,
391 );
392 0 return $self->_get_experiment_result($experiment, feature_id => $feature_id);
393 };
394 }
395  
396 # 8. Exclude if condition is false
397 66 ▶ if ($experiment->condition && !eval_condition($self->attributes, $experiment->condition)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($experiment->condition and not eval_condition($self->attributes, $experiment->condition)) 1 65 66
Condition100%3/3
$experiment->condition and not eval_condition($self->attributes, $experiment->condition)
!ll&&!rl&&r
6411
MC/DC100%2/2
$experiment->condition and not eval_condition($self->attributes, $experiment->condition)
$experiment->condition not eval_condition($self->attributes, $experiment->condition)
Truth table100%3/3
A and not(B)
A= $experiment->conditionB= not eval_condition($self->attributes, $experiment->condition)
ABresult
0X0
100
111
398 1 $log->debugf(
399 "Skip experiment %s because user failed the condition", $experiment->key
400 );
401 1 return $self->_get_experiment_result($experiment, feature_id => $feature_id);
402 }
403  
404 # 8.05 Exclude if parent conditions are not met
405 65 ▶ if ($experiment->parent_conditions) {
Branch: 1 branch
BranchTrueFalseTotal
if ($experiment->parent_conditions) 0 65 65
406 0 my $prereq_res = $self->eval_prereqs($experiment->parent_conditions, {});
407 0 ▶ if ($prereq_res eq "gate" || $prereq_res eq "fail") {
Branch: 1 branch
BranchTrueFalseTotal
if ($prereq_res eq "gate" or $prereq_res eq "fail") 0 0 0
Condition0%0/3
$prereq_res eq "gate" or $prereq_res eq "fail"
l!l&&r!l&&!r
000
MC/DC0%0/2
$prereq_res eq "gate" or $prereq_res eq "fail"
$prereq_res eq "gate" $prereq_res eq "fail"
Truth table0%0/3
A or B
A= $prereq_res eq "gate"B= $prereq_res eq "fail"
ABresult
1X1
011
000
408 0 $log->debugf(
409 "Skip experiment %s because of failing prerequisite", $experiment->key
410 );
411 0 return $self->_get_experiment_result($experiment, feature_id => $feature_id);
412 }
413 0 ▶ if ($prereq_res eq "cyclic") {
Branch: 1 branch
BranchTrueFalseTotal
if ($prereq_res eq "cyclic") 0 0 0
414 0 $log->debugf(
415 "Skip experiment %s because of cyclic prerequisite", $experiment->key
416 );
417 0 return $self->_get_experiment_result($experiment, feature_id => $feature_id);
418 }
419 }
420  
421 # 8.1. Make sure user is in a matching group
422 0 ▶ if ($experiment->groups && @{$experiment->groups}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($experiment->groups and @{$experiment->groups;}) 0 65 65
Condition33%1/3
$experiment->groups and @{$experiment->groups;}
!ll&&!rl&&r
6500
MC/DC0%0/2
$experiment->groups and @{$experiment->groups;}
$experiment->groups @{$experiment->groups;}
Truth table33%1/3
A and B
A= $experiment->groupsB= @{$experiment->groups;}
ABresult
0X0
100
111
423 0 ▶ my $exp_groups = $groups || {};
Condition0%0/2
$groups || {}
l!l
00
MC/DC0%0/1
$groups || {}
$groups
Truth table0%0/2
A
A= $groups
Aresult
11
00
424 0 my $matched = 0;
425 0 foreach my $group (@{$experiment->groups}) {
426 0 ▶ if ($exp_groups->{$group}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp_groups->{$group}) 0 0 0
427 0 $matched = 1;
428 0 last;
429 }
430 }
431 0 ▶ if (!$matched) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched) 0 0 0
432 0 $log->debugf(
433 "Skip experiment %s because user not in required group",
434 $experiment->key,
435 );
436 0 return $self->_get_experiment_result($experiment, feature_id => $feature_id);
437 }
438 }
439  
440 }
441  
442 # The following apply even when in a sticky bucket
443  
444 # 8.2. If experiment.url is set, see if it's valid
445 65 ▶ if ($experiment->url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($experiment->url) 0 65 65
446 0 ▶ unless ($self->_url_is_valid($experiment->url)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_url_is_valid($experiment->url)) 0 0 0
447 0 $log->debugf(
448 "Skip experiment %s because current URL is not targeted",
449 $experiment->key,
450 );
451 0 return $self->_get_experiment_result($experiment, feature_id => $feature_id);
452 }
453 }
454  
455 # 9. Get bucket ranges and choose variation
456 65 ▶ my $n = gbhash(
Condition60%3/5
$experiment->seed // $experiment->key
l!l&&r!l&&!r
4610
$experiment->hash_version // 1
l!l
650
MC/DC0%0/2
$experiment->seed // $experiment->key
$experiment->seed $experiment->key
MC/DC0%0/1
$experiment->hash_version // 1
$experiment->hash_version
Truth table66%2/3
A // B
A= $experiment->seedB= $experiment->key
ABresult
1X1
011
000
Truth table50%1/2
A
A= $experiment->hash_version
Aresult
11
00
457 $experiment->seed // $experiment->key, $hash_value, $experiment->hash_version // 1
458 );
459 65 ▶ if (!defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 0 65 65
460 0 $log->warnf(
461 "Skip experiment %s because of invalid hashVersion", $experiment->key
462 );
463 0 return $self->_get_experiment_result($experiment, feature_id => $feature_id);
464 }
465  
466 65 ▶ if (!$found_sticky_bucket) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found_sticky_bucket) 65 0 65
467 65 my $c = $experiment->coverage;
468 my $ranges = $experiment->ranges || get_bucket_ranges(
469 65 ▶ scalar @{$experiment->variations}, defined $c ? $c : 1, $experiment->weights
Branch: 1 branch
BranchTrueFalseTotal
defined $c ? : 13 45 58
Condition66%2/3
$experiment->ranges || get_bucket_ranges(scalar @{$experiment->variations;}, defined $c ? $c : 1, $experiment->weights)
l!l&&r!l&&!r
7580
MC/DC0%0/2
$experiment->ranges || get_bucket_ranges(scalar @{$experiment->variations;}, defined $c ? $c : 1, $experiment->weights)
$experiment->ranges get_bucket_ranges(scalar @{$experiment->variations;}, defined $c ? $c : 1, $experiment->weights)
Truth table66%2/3
A || B
A= $experiment->rangesB= get_bucket_ranges(scalar @{$experiment->variations;}, defined $c ? $c : 1, $experiment->weights)
ABresult
1X1
011
000
470 );
471 65 $assigned = choose_variation($n, $ranges);
472  
473 }
474  
475 # Unenroll if any prior sticky buckets are blocked by version
476 65 ▶ if ($sticky_bucket_version_is_blocked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sticky_bucket_version_is_blocked) 0 65 65
477 0 $log->debugf(
478 "Skip experiment %s because sticky bucket version is blocked",
479 $experiment->key
480 );
481 0 return $self->_get_experiment_result(
482 $experiment, feature_id => $feature_id, sticky_bucket_used => 1
483 );
484 }
485  
486 # 10. Return if not in experiment
487 65 ▶ if ($assigned < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($assigned < 0) 8 57 65
488 8 $log->debugf(
489 "Skip experiment %s because user is not included in the rollout",
490 $experiment->key,
491 );
492 8 return $self->_get_experiment_result($experiment, feature_id => $feature_id);
493 }
494  
495 # 11. If experiment is forced, return immediately
496 57 ▶ if (defined $experiment->force) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $experiment->force) 3 54 57
497 3 $log->debugf(
498 "Force variation %d in experiment %s", $experiment->force, $experiment->key
499 );
500 3 return $self->_get_experiment_result(
501 $experiment, feature_id => $feature_id, variation_id => $experiment->force
502 );
503 }
504  
505 # 12. Exclude if in QA mode
506 54 ▶ if ($qa_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qa_mode) 1 53 54
507 1 $log->debugf("Skip experiment %s because of QA Mode", $experiment->key);
508 1 return $self->_get_experiment_result($experiment, feature_id => $feature_id);
509 }
510  
511 # 12.5. If experiment is stopped, return immediately
512 53 ▶ if ($experiment->status eq "stopped") {
Branch: 1 branch
BranchTrueFalseTotal
if ($experiment->status eq "stopped") 0 53 53
513 0 $log->debugf("Skip experiment %s because it is stopped", $experiment->key);
514 0 return $self->_get_experiment_result($experiment, feature_id => $feature_id);
515 }
516  
517 # 13. Build the result object
518 53 my $result = $self->_get_experiment_result(
519 $experiment,
520 variation_id => $assigned,
521 hash_used => 1,
522 feature_id => $feature_id,
523 bucket => $n,
524 sticky_bucket_used => $found_sticky_bucket
525 );
526  
527 # 13.5 Persist sticky bucket
528 53 ▶ if ($sticky_bucket_service && !$experiment->disable_sticky_bucketing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sticky_bucket_service and not $experiment->disable_sticky_bucketing) 0 53 53
Condition33%1/3
$sticky_bucket_service and not $experiment->disable_sticky_bucketing
!ll&&!rl&&r
5300
MC/DC0%0/2
$sticky_bucket_service and not $experiment->disable_sticky_bucketing
$sticky_bucket_service not $experiment->disable_sticky_bucketing
Truth table33%1/3
A and not(B)
A= $sticky_bucket_serviceB= not $experiment->disable_sticky_bucketing
ABresult
0X0
100
111
529 0 my %assignment;
530 0 $assignment{$self->_get_sticky_bucket_experiment_key(
531 $experiment->key,
532 $experiment->bucketVersion
533 )} = $result->key;
534 0 my $data = $self->_generate_sticky_bucket_assignment_doc(
535 $hash_attribute,
536 $hash_value,
537 \%assignment
538 );
539 0 my $doc = $data->{doc};
540 0 ▶ if ($doc && $data->{changed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc and $data->{"changed"}) 0 0 0
Condition0%0/3
$doc and $data->{"changed"}
!ll&&!rl&&r
000
MC/DC0%0/2
$doc and $data->{"changed"}
$doc $data->{"changed"}
Truth table0%0/3
A and B
A= $docB= $data->{"changed"}
ABresult
0X0
100
111
541 0 ▶ $sticky_bucket_assignment_docs //= {};
Condition0%0/2
$sticky_bucket_assignment_docs //= {}
l!l
00
MC/DC0%0/1
$sticky_bucket_assignment_docs //= {}
$sticky_bucket_assignment_docs
Truth table0%0/2
A
A= $sticky_bucket_assignment_docs
Aresult
11
00
542 0 $sticky_bucket_assignment_docs->{$data->{key}} = $doc;
543 0 $sticky_bucket_service->save_assignments($doc);
544 }
545 }
546 # 14. Fire the tracking callback if set
547 53 $self->_track($experiment, $result);
548  
549 # 15. Return the result
550 53 $log->debugf("Assigned variation %d in experiment %s", $assigned, $experiment->key);
551 53 return $result;
552 3 }
553  
554 53 ▶ method _track($experiment, $result) {
Subroutine _track: called CC=4 SCAR=19.8
555  
556 53 ▶ return unless $tracking_callback;
Branch: 1 branch
BranchTrueFalseTotal
unless $tracking_callback 53 0 53
557  
558 0 my $key = $result->hash_attribute
559 . $result->hash_value
560 . $experiment->key
561 . $result->variation_id;
562  
563 0 ▶ unless ($tracked->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tracked->{$key}) 0 0 0
564 eval {
565 0 $tracking_callback->($experiment, $result);
566 0 $tracked->{$key} = 1;
567 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &$tracking_callback($experiment, $result); $tracked->{$key} = 1 } } or do { () } 0 0 0
568 # Handle exception silently
569 };
570 }
571 3 }
572  
573 0 ▶ method _generate_sticky_bucket_assignment_doc($attribute_name, $attribute_value, $assignments){
Subroutine _generate_sticky_bucket_assignment_doc: not called CC=2 SCAR=17.9
574 0 my $key = $attribute_name . "||" . $attribute_value;
575 0 ▶ my $existing_assignments = $sticky_bucket_assignment_docs->{$key}{assignments} // {};
Condition0%0/2
$sticky_bucket_assignment_docs->{$key}{"assignments"} // {}
l!l
00
MC/DC0%0/1
$sticky_bucket_assignment_docs->{$key}{"assignments"} // {}
$sticky_bucket_assignment_docs->{$key}{"assignments"}
Truth table0%0/2
A
A= $sticky_bucket_assignment_docs->{$key}{"assignments"}
Aresult
11
00
576  
577 0 my %new_assignments = (%$existing_assignments, %$assignments);
578  
579 0 my $changed = !Compare($existing_assignments, \%new_assignments);
580  
581 return {
582 0 key => $key,
583 doc => {
584 attribute_name => $attribute_name,
585 attribute_value => $attribute_value,
586 assignments => \%new_assignments
587 },
588 changed => $changed
589 };
590 3 }
591  
592 0 ▶ method _url_is_valid($pattern) {
Subroutine _url_is_valid: not called CC=5 SCAR=34.0
593  
594 0 ▶ return 0 unless $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 0 0
595  
596 eval {
597 0 my $r = qr/$pattern/;
598 0 ▶ if ($self->{_url} =~ $r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_url"} =~ /$r/) 0 0 0
599 0 return 1;
600 }
601  
602 0 my $path_only = $url;
603 0 $path_only =~ s/^[^\/]*\//\//;
604 0 $path_only =~ s/^https?:\/\///;
605  
606 0 ▶ if ($path_only =~ $r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path_only =~ /$r/) 0 0 0
607 0 return 1;
608 }
609 0 return 0;
610 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $r = qr/$pattern/; if ($self->{"_url"} =~ /$r/) { return 1; } ; my $path_only = $url; $path_only =~ s[^[^/]*/][/]; $path_only =~ s[^https?://][]; if ($path_only =~ /$r/) { return 1; } ; return 0 } } or do { return 1 } 0 0 0
611 0 return 1;
612 };
613 3 }
614  
615 6 ▶ method _is_filtered_out($filters) {
Subroutine _is_filtered_out: called CC=10 SCAR=23.6
616  
617 6 foreach my $filter (@$filters) {
618 8 ▶ my ($dummy, $hash_value) = $self->_get_hash_value($filter->{attribute} // "id");
Condition100%2/2
$filter->{"attribute"} // "id"
l!l
44
MC/DC100%1/1
$filter->{"attribute"} // "id"
$filter->{"attribute"}
Truth table100%2/2
A
A= $filter->{"attribute"}
Aresult
11
00
619 8 ▶ if ($hash_value eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash_value eq "") 0 8 8
620 0 return 0;
621 }
622  
623 8 ▶ my $n = gbhash($filter->{seed} // "", $hash_value, $filter->{hashVersion} // 2);
Condition50%2/4
$filter->{"seed"} // ""
l!l
80
$filter->{"hashVersion"} // 2
l!l
08
MC/DC0%0/1
$filter->{"seed"} // ""
$filter->{"seed"}
MC/DC0%0/1
$filter->{"hashVersion"} // 2
$filter->{"hashVersion"}
Truth table50%1/2
A
A= $filter->{"seed"}
Aresult
11
00
Truth table50%1/2
A
A= $filter->{"hashVersion"}
Aresult
11
00
624 8 ▶ if (!defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 0 8 8
625 0 return 0;
626 }
627  
628 8 my $filtered = 0;
629 8 foreach my $range (@{$filter->{ranges}}) {
630 11 ▶ if (in_range($n, $range)) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_range($n, $range)) 6 5 11
631 6 $filtered = 1;
632 6 last;
633 }
634 }
635 8 ▶ if (!$filtered) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filtered) 2 6 8
636 2 return 1;
637 }
638 }
639 4 return 0;
640 3 }
641  
642 0 ▶ method _get_sticky_bucket_assignments($attr = '', $fallback = ''){
Subroutine _get_sticky_bucket_assignments: not called CC=8 SCAR=42.8
643 0 my %merged;
644  
645 0 my ($dummy, $hash_value) = $self->_get_hash_value($attr);
646 0 my $key = "$attr||$hash_value";
647 0 ▶ if (exists $sticky_bucket_assignment_docs->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sticky_bucket_assignment_docs->{$key}) 0 0 0
648 0 %merged = %{ $sticky_bucket_assignment_docs->{$key}{assignments} };
649 }
650  
651 0 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 0 0 0
652 0 ($dummy, $hash_value) = $self->_get_hash_value($fallback);
653 0 $key = "$fallback||$hash_value";
654 0 ▶ if (exists $self->{_sticky_bucket_assignment_docs}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_sticky_bucket_assignment_docs"}{$key}) 0 0 0
655 # Merge the fallback assignments, but don't overwrite existing ones
656 0 for my $k (keys %{ $sticky_bucket_assignment_docs->{$key}{assignments} }) {
657 0 ▶ $merged{$k} //= $sticky_bucket_assignment_docs->{$key}{assignments}{$k};
Condition0%0/3
$merged{$k} //= $sticky_bucket_assignment_docs->{$key}{"assignments"}{$k}
l!l&&r!l&&!r
000
MC/DC0%0/2
$merged{$k} //= $sticky_bucket_assignment_docs->{$key}{"assignments"}{$k}
$merged{$k} $sticky_bucket_assignment_docs->{$key}{"assignments"}{$k}
Truth table0%0/3
A //= B
A= $merged{$k}B= $sticky_bucket_assignment_docs->{$key}{"assignments"}{$k}
ABresult
1X1
011
000
658 }
659 }
660 }
661  
662 0 return \%merged;
663 3 }
664  
665 0 ▶ method _get_sticky_bucket_variation($experiment_key, $bucket_version = 0, $min_bucket_version = 0, $meta = {}, $hash_attribute = undef, $fallback_attribute = undef){
Subroutine _get_sticky_bucket_variation: not called CC=11 SCAR=48.8
666 0 my $id = $self->_get_sticky_bucket_experiment_key($experiment_key, $bucket_version);
667  
668  
669 0 my $assignments = $self->_get_sticky_bucket_assignments($hash_attribute, $fallback_attribute);
670 0 ▶ if ($self->_is_blocked($assignments, $experiment_key, $min_bucket_version)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_blocked($assignments, $experiment_key, $min_bucket_version)) 0 0 0
671 return {
672 0 variation => -1,
673 versionIsBlocked => 1
674 };
675 }
676  
677 0 my $variation_key = $assignments->{$id};
678 0 ▶ if (!$variation_key) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($variation_key) 0 0 0
679 return {
680 0 variation => -1
681 };
682 }
683  
684 # Find the key in meta
685 0 my $variation = -1;
686 0 ▶ for (my $i = 0; $i < @$meta; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$meta) 0 0 0
687 0 ▶ if ($meta->[$i]->{key} eq $variation_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->[$i]{"key"} eq $variation_key) 0 0 0
688 0 $variation = $i;
689 0 last;
690 }
691 }
692  
693 0 ▶ if ($variation < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($variation < 0) 0 0 0
694 return {
695 0 variation => -1
696 };
697 }
698  
699 0 return { variation => $variation };
700 3 }
701  
702 0 ▶ method _is_blocked($assignments, $experiment_key, $min_bucket_version = 0){
Subroutine _is_blocked: not called CC=5 SCAR=34.0
703 0 ▶ if ($min_bucket_version > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min_bucket_version > 0) 0 0 0
704 0 for my $i (0 .. $min_bucket_version - 1) {
705 0 my $blocked_key = $self->_get_sticky_bucket_experiment_key($experiment_key, $i);
706 0 ▶ if (exists $assignments->{$blocked_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $assignments->{$blocked_key}) 0 0 0
707 0 return 1;
708 }
709 }
710 }
711 0 return 0;
712 3 }
713  
714 0 ▶ method _get_sticky_bucket_experiment_key($experiment_key, $bucket_version = 0){
Subroutine _get_sticky_bucket_experiment_key: not called CC=2 SCAR=17.9
715 0 return $experiment_key . "__" . $bucket_version;
716 3 }
717  
718 82 ▶ method _get_experiment_result($experiment, %args){
Subroutine _get_experiment_result: called CC=7 SCAR=19.5
719 82 ▶ my $variation_id = $args{variation_id} // -1;
Condition100%2/2
$args{"variation_id"} // -1
l!l
6121
MC/DC100%1/1
$args{"variation_id"} // -1
$args{"variation_id"}
Truth table100%2/2
A
A= $args{"variation_id"}
Aresult
11
00
720 82 ▶ my $hash_used = $args{hash_used} // 0;
Condition100%2/2
$args{"hash_used"} // 0
l!l
5329
MC/DC100%1/1
$args{"hash_used"} // 0
$args{"hash_used"}
Truth table100%2/2
A
A= $args{"hash_used"}
Aresult
11
00
721 82 my $feature_id = $args{feature_id};
722 82 my $bucket = $args{bucket};
723 82 ▶ my $sticky_bucket_used = $args{sticky_bucket_used} // 0;
Condition100%2/2
$args{"sticky_bucket_used"} // 0
l!l
5329
MC/DC100%1/1
$args{"sticky_bucket_used"} // 0
$args{"sticky_bucket_used"}
Truth table100%2/2
A
A= $args{"sticky_bucket_used"}
Aresult
11
00
724 82 my $in_experiment = 1;
725 82 ▶ if ($variation_id < 0 || $variation_id > @{$experiment->variations} - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($variation_id < 0 or $variation_id > @{$experiment->variations;} - 1) 23 59 82
Condition100%3/3
$variation_id < 0 or $variation_id > @{$experiment->variations;} - 1
l!l&&r!l&&!r
22159
MC/DC100%2/2
$variation_id < 0 or $variation_id > @{$experiment->variations;} - 1
$variation_id < 0 $variation_id > @{$experiment->variations;} - 1
Truth table100%3/3
A or B
A= $variation_id < 0B= $variation_id > @{$experiment->variations;} - 1
ABresult
1X1
011
000
726 23 $variation_id = 0;
727 23 $in_experiment = 0;
728 }
729  
730 82 my $meta;
731 82 ▶ if ($experiment->meta) {
Branch: 1 branch
BranchTrueFalseTotal
if ($experiment->meta) 3 79 82
732 3 $meta = $experiment->meta->[$variation_id];
733 }
734  
735 82 my ($hash_attribute, $hash_value) = $self->_get_orig_hash_value($experiment->hash_attribute, $experiment->fallback_attribute);
736 82 return WebService::GrowthBook::Result->new(
737 feature_id => $feature_id,
738 in_experiment => $in_experiment,
739 variation_id => $variation_id,
740 value => $experiment->variations->[$variation_id],
741 hash_used => $hash_used,
742 hash_attribute => $hash_attribute,
743 hash_value => $hash_value,
744 meta => $meta,
745 bucket => $bucket,
746 sticky_bucket_used => $sticky_bucket_used
747 );
748 3 }
749  
750 16 ▶ method _is_included_in_rollout($seed, $hash_attribute, $fallback_attribute, $range, $coverage, $hash_version){
Subroutine _is_included_in_rollout: called CC=8 SCAR=20.8
751 16 ▶ if (!defined($coverage) && !defined($range)){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $coverage or defined $range) 8 8 16
Condition100%3/3
defined $coverage or defined $range
l!l&&r!l&&!r
538
MC/DC100%2/2
defined $coverage or defined $range
defined $coverage defined $range
Truth table100%3/3
A or B
A= defined $coverageB= defined $range
ABresult
1X1
011
000
752 8 return 1;
753 }
754 8 my $hash_value;
755 8 (undef, $hash_value) = $self->_get_hash_value($hash_attribute, $fallback_attribute);
756 8 ▶ if($hash_value eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash_value eq "") 1 7 8
757  
758 1 return 0;
759 }
760  
761 7 ▶ my $n = gbhash($seed, $hash_value, $hash_version || 1);
Condition50%1/2
$hash_version || 1
l!l
70
MC/DC0%0/1
$hash_version || 1
$hash_version
Truth table50%1/2
A
A= $hash_version
Aresult
11
00
762 7 ▶ if (!defined($n)){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 1 6 7
763  
764 1 return 0;
765 }
766  
767 6 ▶ if($range){
Branch: 2 branches
BranchTrueFalseTotal
if ($range) { } 4 2 6
elsif ($coverage) { } 2 0 2
768  
769 4 return in_range($n, $range);
770 }
771 elsif($coverage){
772 2 return $n < $coverage;
773 }
774  
775 0 return 1;
776 3 }
777  
778 90 ▶ method _get_hash_value($attr, $fallback_attr = undef){
Subroutine _get_hash_value: called CC=2 SCAR=6.9
779 90 my $val;
780 90 ($attr, $val) = $self->_get_orig_hash_value($attr, $fallback_attr);
781 90 return ($attr, "$val");
782 3 }
783  
784 172 ▶ method _get_orig_hash_value($attr, $fallback_attr){
Subroutine _get_orig_hash_value: called CC=16 SCAR=40.7
785 172 ▶ $attr ||= "id";
Condition50%1/2
$attr ||= "id"
l!l
1720
MC/DC0%0/1
$attr ||= "id"
$attr
Truth table50%1/2
A
A= $attr
Aresult
11
00
786 172 my $val = "";
787  
788 172 ▶ if (exists $attributes->{$attr}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $attributes->{$attr}) { } 165 7 172
elsif (exists $user->{$attr}) { } 0 7 7
789 165 ▶ $val = $attributes->{$attr} || "";
Condition100%2/2
$attributes->{$attr} || ''
l!l
1614
MC/DC100%1/1
$attributes->{$attr} || ''
$attributes->{$attr}
Truth table100%2/2
A
A= $attributes->{$attr}
Aresult
11
00
790 } elsif (exists $user->{$attr}) {
791 0 ▶ $val = $user->{$attr} || "";
Condition0%0/2
$user->{$attr} || ''
l!l
00
MC/DC0%0/1
$user->{$attr} || ''
$user->{$attr}
Truth table0%0/2
A
A= $user->{$attr}
Aresult
11
00
792 }
793  
794 # If no match, try fallback
795 172 ▶ if ((!$val || $val eq "") && $fallback_attr && $self->{sticky_bucket_service}) {
Branch: 1 branch
BranchTrueFalseTotal
if (!$val || $val eq "" and $fallback_attr and $self->{"sticky_bucket_service"}) 0 172 172
Condition55%5/9
!$val || $val eq "" and $fallback_attr and $self->{"sticky_bucket_service"}
!ll&&!rl&&r
17200
!$val || $val eq "" and $fallback_attr
!ll&&!rl&&r
161110
not $val or $val eq ""
l!l&&r!l&&!r
110161
MC/DC0%0/4
!$val || $val eq "" and $fallback_attr and $self->{"sticky_bucket_service"}
not $val $val eq "" $fallback_attr $self->{"sticky_bucket_service"}
Truth table28%2/7
not(A) or B and C and D
A= not $valB= $val eq ""C= $fallback_attrD= $self->{"sticky_bucket_service"}
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
796 0 ▶ if (exists $attributes->{$fallback_attr}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $attributes->{$fallback_attr}) { } 0 0 0
elsif (exists $user->{$fallback_attr}) { } 0 0 0
797 0 ▶ $val = $attributes->{$fallback_attr} || "";
Condition0%0/2
$attributes->{$fallback_attr} || ''
l!l
00
MC/DC0%0/1
$attributes->{$fallback_attr} || ''
$attributes->{$fallback_attr}
Truth table0%0/2
A
A= $attributes->{$fallback_attr}
Aresult
11
00
798 } elsif (exists $user->{$fallback_attr}) {
799 0 ▶ $val = $user->{$fallback_attr} || "";
Condition0%0/2
$user->{$fallback_attr} || ''
l!l
00
MC/DC0%0/1
$user->{$fallback_attr} || ''
$user->{$fallback_attr}
Truth table0%0/2
A
A= $user->{$fallback_attr}
Aresult
11
00
800 }
801  
802 0 ▶ if (!$val || $val ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $val or $val ne "") 0 0 0
Condition0%0/3
not $val or $val ne ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not $val or $val ne ""
not $val $val ne ""
Truth table0%0/3
not(A) or B
A= not $valB= $val ne ""
ABresult
1X1
011
000
803 0 $attr = $fallback_attr;
804 }
805 }
806  
807 172 return ($attr, $val);
808 3 }
809  
810 0 ▶ method eval_prereqs($parent_conditions, $stack){
Subroutine eval_prereqs: not called CC=5 SCAR=34.0
Pod undocumented
811 0 foreach my $parent_condition (@$parent_conditions) {
812 0 my $parent_res = $self->_eval_feature($parent_condition->{id}, $stack);
813  
814 0 ▶ if ($parent_res->{source} eq "cyclicPrerequisite") {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_res->{"source"} eq "cyclicPrerequisite") 0 0 0
815 0 return "cyclic";
816 }
817  
818 0 ▶ if (!eval_condition({ value => $parent_res->{value} }, $parent_condition->{condition})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval_condition({"value", $parent_res->{"value"}}, $parent_condition->{"condition"})) 0 0 0
819 0 ▶ if ($parent_condition->{gate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_condition->{"gate"}) 0 0 0
820 0 return "gate";
821 }
822 0 return "fail";
823 }
824 }
825 0 return "pass";
826 3 }
827 44 ▶ method eval_feature($feature_name){
Subroutine eval_feature: called CC=1 SCAR=0.0
Pod documented
828 44 return $self->_eval_feature($feature_name, {});
829 3 }
830  
831 6 ▶ method get_feature_value($feature_name, $fallback = undef){
Subroutine get_feature_value: called CC=3 SCAR=11.0
Pod documented
832 6 my $result = $self->eval_feature($feature_name);
833 6 ▶ return $fallback unless defined($result->value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result->value 2 4 6
834 4 return $result->value;
835 3 }
836  
837 70 ▶ method run($experiment){
Subroutine run: called CC=1 SCAR=0.0
Pod undocumented
838 70 my $result = $self->_run($experiment);
839 70 $self->_fire_subscriptions($experiment, $result);
840 70 return $result;
841 3 }
842 }
843  
844 3 =head1 METHODS
845  
846 =head2 load_features
847  
848 load features from growthbook API
849  
850 $instance->load_features;
851  
852 =head2 is_on
853  
854 check if a feature is on
855  
856 $instance->is_on('feature_name');
857  
858 Please note it will return undef if the feature does not exist.
859  
860 =head2 is_off
861  
862 check if a feature is off
863  
864 $instance->is_off('feature_name');
865  
866 Please note it will return undef if the feature does not exist.
867  
868 =head2 get_feature_value
869  
870 get the value of a feature
871  
872 $instance->get_feature_value('feature_name');
873  
874 Please note it will return undef if the feature does not exist.
875  
876 =head2 set_features
877  
878 set features
879  
880 $instance->set_features($features);
881  
882 =head2 eval_feature
883  
884 evaluate a feature to get the value
885  
886 $instance->eval_feature('feature_name');
887  
888 =head2 set_attributes
889  
890 set attributes (can be set when creating gb object) and evaluate features
891  
892 $instance->set_attributes({attr1 => 'value1', attr2 => 'value2'});
893 $instance->eval_feature('feature_name');
894  
895 =cut
896  
897  
898 3 1;
899  
900  
901 =head1 SEE ALSO
902  
903 =over 4
904  
905 =item * L<https://docs.growthbook.io/>
906  
907 =item * L<PYTHON VERSION|https://github.com/growthbook/growthbook-python>
908  
909 =back
910  
↑ Summary GrowthBook/Eval.pm »