GrowthBook/Util.pm

filter: Statementstmt 100% 86 / 86 Branchbran 96.6% 29 / 30 Conditioncond 80.0% 16 / 20 MC/DCmcdc 61.5% 8 / 13 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 9 totaltotal 89.0% 154 / 173 CC 26 SCAR 15.3
CC
26
Cov
96%
get_bucket_ranges
9.0
get_query_string_override
7.0
in_namespace
3.0
CRAP 4.6
« GrowthBook/Experiment.pm ↑ Summary GrowthBook/FeatureRepository.pm »
1 package WebService::GrowthBook::Util;
2 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use String::CamelCase qw(decamelize);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 our $VERSION = '0.003'; ## VERSION
10  
11 9 our @EXPORT_OK = qw(gbhash in_range get_query_string_override get_equal_weights get_bucket_ranges adjust_args_camel_to_snake choose_variation in_namespace);
12  
13 136 ▶sub fnv1a32 {
Subroutine fnv1a32: called CC=2 SCAR=6.9
Pod undocumented
14 136 my ($str) = @_;
15 136 my $hval = 0x811C9DC5;
16 136 my $prime = 0x01000193;
17 136 my $uint32_max = 2 ** 32;
18  
19 136 foreach my $s (split //, $str) {
20 1260 $hval = $hval ^ ord($s);
21 1260 $hval = ($hval * $prime) % $uint32_max;
22 }
23  
24 136 return $hval;
25 }
26 114 ▶sub gbhash {
Subroutine gbhash: called CC=3 SCAR=11.0
Pod undocumented
27 114 my ($seed, $value, $version) = @_;
28  
29 114 ▶ if ($version == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version == 2) 24 90 114
30 24 my $n = fnv1a32(fnv1a32($seed . $value));
31 24 return ($n % 10000) / 10000;
32 }
33 90 ▶ if ($version == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version == 1) 88 2 90
34 88 my $n = fnv1a32($value . $seed);
35 88 return ($n % 1000) / 1000;
36 }
37 2 return undef;
38 }
39  
40 157 ▶sub in_range {
Subroutine in_range: called CC=2 SCAR=6.9
Pod undocumented
41 157 my ($n, $range) = @_;
42 157 ▶ return $range->[0] <= $n && $n < $range->[1];
Condition100%3/3
$range->[0] <= $n && $n < $range->[1]
!ll&&!rl&&r
57676
MC/DC100%2/2
$range->[0] <= $n && $n < $range->[1]
$range->[0] <= $n $n < $range->[1]
Truth table100%3/3
A && B
A= $range->[0] <= $nB= $n < $range->[1]
ABresult
0X0
100
111
43 }
44  
45  
46 96 ▶sub get_query_string_override {
Subroutine get_query_string_override: called CC=7 SCAR=19.5
Pod undocumented
47 96 my ($id, $url, $num_variations) = @_;
48 96 my $uri = URI->new($url);
49  
50 # Return undef if there is no query string
51 96 ▶ return undef unless $uri->query;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->query 81 15 96
52  
53 15 my %qs = $uri->query_form;
54  
55 # Return undef if the id is not in the query string
56 15 ▶ return undef unless exists $qs{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $qs{$id} 2 13 15
57  
58 13 my $variation = $qs{$id};
59  
60 # Return undef if the variation is not defined or not a digit
61 13 ▶ return undef unless defined $variation && $variation =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $variation and $variation =~ /^\d+$/ 3 10 13
Condition66%2/3
defined $variation and $variation =~ /^\d+$/
!ll&&!rl&&r
0310
MC/DC50%1/2
defined $variation and $variation =~ /^\d+$/
defined $variation $variation =~ /^\d+$/
Truth table66%2/3
A and B
A= defined $variationB= $variation =~ /^\d+$/
ABresult
0X0
100
111
62  
63 10 my $var_id = int($variation);
64  
65 # Return undef if the variation id is out of range
66 10 ▶ return undef if $var_id < 0 || $var_id >= $num_variations;
Branch: 1 branch
BranchTrueFalseTotal
if $var_id < 0 or $var_id >= $num_variations 2 8 10
Condition66%2/3
$var_id < 0 or $var_id >= $num_variations
l!l&&r!l&&!r
028
MC/DC50%1/2
$var_id < 0 or $var_id >= $num_variations
$var_id < 0 $var_id >= $num_variations
Truth table66%2/3
A or B
A= $var_id < 0B= $var_id >= $num_variations
ABresult
1X1
011
000
67  
68 8 return $var_id;
69 }
70  
71 63 ▶sub get_equal_weights {
Subroutine get_equal_weights: called CC=2 SCAR=6.9
Pod undocumented
72 63 my ($num_variations) = @_;
73 63 ▶ return [] if $num_variations < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $num_variations < 1 2 61 63
74 61 my $weight = 1 / $num_variations;
75 61 return [($weight) x $num_variations];
76 }
77  
78 71 ▶sub get_bucket_ranges {
Subroutine get_bucket_ranges: called CC=9 SCAR=22.0
Pod undocumented
79 71 my ($num_variations, $coverage, $weights) = @_;
80 71 ▶ $coverage //= 1;
Condition50%1/2
$coverage //= 1
l!l
710
MC/DC0%0/1
$coverage //= 1
$coverage
Truth table50%1/2
A
A= $coverage
Aresult
11
00
81 71 ▶ $weights //= get_equal_weights($num_variations);
Condition66%2/3
$weights //= get_equal_weights($num_variations)
l!l&&r!l&&!r
6560
MC/DC0%0/2
$weights //= get_equal_weights($num_variations)
$weights get_equal_weights($num_variations)
Truth table66%2/3
A //= B
A= $weightsB= get_equal_weights($num_variations)
ABresult
1X1
011
000
82  
83 71 ▶ if ($coverage < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coverage < 0) 1 70 71
84 1 $coverage = 0;
85 }
86 71 ▶ if ($coverage > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coverage > 1) 1 70 71
87 1 $coverage = 1;
88 }
89 71 ▶ if (@$weights != $num_variations) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$weights != $num_variations) 49 22 71
90 49 $weights = get_equal_weights($num_variations);
91 }
92 71 ▶ if (sum(@$weights) < 0.99 || sum(@$weights) > 1.01) {
Branch: 1 branch
BranchTrueFalseTotal
if (&sum(@$weights) < "0.99" or &sum(@$weights) > "1.01") 2 69 71
Condition100%3/3
&sum(@$weights) < "0.99" or &sum(@$weights) > "1.01"
l!l&&r!l&&!r
1169
MC/DC100%2/2
&sum(@$weights) < "0.99" or &sum(@$weights) > "1.01"
&sum(@$weights) < "0.99" &sum(@$weights) > "1.01"
Truth table100%3/3
A or B
A= &sum(@$weights) < "0.99"B= &sum(@$weights) > "1.01"
ABresult
1X1
011
000
93 2 $weights = get_equal_weights($num_variations);
94 }
95  
96 71 my $cumulative = 0;
97 71 my @ranges;
98 71 foreach my $w (@$weights) {
99 162 my $start = $cumulative;
100 162 $cumulative += $w;
101 162 push @ranges, [$start, $start + $coverage * $w];
102 }
103  
104 71 return \@ranges;
105 }
106  
107 78 ▶sub choose_variation {
Subroutine choose_variation: called CC=3 SCAR=11.0
Pod undocumented
108 78 my ($n, $ranges) = @_;
109 78 ▶ for (my $i = 0; $i < @$ranges; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$ranges) 142 11 153
110 142 ▶ if (in_range($n, $ranges->[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_range($n, $ranges->[$i])) 67 75 142
111 67 return $i;
112 }
113 }
114 11 return -1;
115 }
116  
117 238 ▶sub adjust_args_camel_to_snake {
Subroutine adjust_args_camel_to_snake: called CC=3 SCAR=11.0
Pod undocumented
118 238 my ($args) = @_;
119 238 for my $key (keys %$args) {
120 680 my $snake_key = decamelize($key);
121 680 ▶ if ($key eq $snake_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq $snake_key) 656 24 680
122 656 next;
123 }
124 24 $args->{$snake_key} = delete $args->{$key};
125 }
126 }
127  
128 19 ▶sub in_namespace {
Subroutine in_namespace: called CC=3 SCAR=11.0
Pod undocumented
129 19 my ($user_id, $namespace) = @_;
130 19 my $n = gbhash("__" . $namespace->[0], $user_id, 1);
131 19 ▶ return 0 unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 19 19
132 19 ▶ return $namespace->[1] <= $n && $n < $namespace->[2];
Condition100%3/3
$namespace->[1] <= $n && $n < $namespace->[2]
!ll&&!rl&&r
289
MC/DC100%2/2
$namespace->[1] <= $n && $n < $namespace->[2]
$namespace->[1] <= $n $n < $namespace->[2]
Truth table100%3/3
A && B
A= $namespace->[1] <= $nB= $n < $namespace->[2]
ABresult
0X0
100
111
133 }
134 9 1;
« GrowthBook/Experiment.pm ↑ Summary GrowthBook/FeatureRepository.pm »