| 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
| |||||||||||||||||||||||||||||
| 30 | 24 | my $n = fnv1a32(fnv1a32($seed . $value)); | |||||||||||||||||||||||||||
| 31 | 24 | return ($n % 10000) / 10000; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 90 | ▶ | if ($version == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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]
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]
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $variation and $variation =~ /^\d+$/
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+$/
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$var_id < 0 or $var_id >= $num_variations
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$coverage //= 1
$coverage
Truth table50%1/2
A
A= $coverage
| |||||||||||||||||||||||||||||
| 81 | 71 | ▶ | $weights //= get_equal_weights($num_variations); | ||||||||||||||||||||||||||
|
Condition66%2/3
$weights //= get_equal_weights($num_variations)
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)
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 71 | ▶ | if ($coverage < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 1 | $coverage = 0; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 71 | ▶ | if ($coverage > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 1 | $coverage = 1; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 71 | ▶ | if (@$weights != $num_variations) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 49 | $weights = get_equal_weights($num_variations); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 71 | ▶ | if (sum(@$weights) < 0.99 || sum(@$weights) > 1.01) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
&sum(@$weights) < "0.99" or &sum(@$weights) > "1.01"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 110 | 142 | ▶ | if (in_range($n, $ranges->[$i])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 132 | 19 | ▶ | return $namespace->[1] <= $n && $n < $namespace->[2]; | ||||||||||||||||||||||||||
|
Condition100%3/3
$namespace->[1] <= $n && $n < $namespace->[2]
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]
| |||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 9 | 1; | |||||||||||||||||||||||||||