lib/Langertha/Skeid/CustomerSource.pm

filter: Statementstmt 98.0% 306 / 312 Branchbran 84.6% 149 / 176 Conditioncond 72.7% 80 / 110 MC/DCmcdc 54.1% 33 / 61 Subroutinesub 92.3% 48 / 52 Podpod 91.6% 11 / 12 totaltotal 86.7% 627 / 723 CC 134 SCAR 23.5
CC
134
Cov
89%
normalize_config
24.3
merge_into
19.1
_done
16.5
CRAP 10.5
« lib/Langertha/Skeid/Protocol/Realtime.pm ↑ Summary lib/Langertha/Skeid/Protocol/RawBody.pm »
1 package Langertha::Skeid::CustomerSource;
2 114 our $VERSION = '0.004';
3 # ABSTRACT: Customer keys from a database relation - the section, the row contract, the fetch loop
4 114 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 114 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 114 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 114 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 114 ▶use Digest::SHA qw(sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 114 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 114 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 114 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 114 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 114 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 114 ▶use Langertha::Skeid::UsageStore;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 114 my @COLUMNS = qw( key_id name policy models );
18  
19 # An SQL identifier Skeid quotes itself: letters, digits and underscores, not starting with a
20 # digit, at most 63 bytes (PostgreSQL's NAMEDATALEN). Nothing a quote could break out of.
21 114 my $IDENT = qr/[A-Za-z_][A-Za-z0-9_]{0,62}/;
22  
23 114 my %KNOWN_KEYS = map { $_ => 1 } qw(
24 backend dsn host port dbname database user password_env from_usage_store
25 relation refresh_interval_s listen max_stale_s connect_timeout_s
26 );
27  
28 # The connection keys from_usage_store takes from the usage store instead.
29 114 my @OWN_CONNECTION = qw( dsn host port dbname database );
30  
31 # Longest wait between two retries of a failing fetch, as for a failing config reload.
32 114 my $BACKOFF_MAX = 60;
33  
34  
35 82 ▶sub normalize_config {
Subroutine normalize_config: called CC=24 SCAR=31.9
Pod documented
36 82 my ($class, $section, %opts) = @_;
37 82 ▶ croak 'customer_source must be a hash: backend, dsn (or host/port/dbname), user, '
Branch: 1 branch
BranchTrueFalseTotal
unless ref $section eq "HASH" 1 81 82
38 . 'password_env, relation, ...' unless ref($section) eq 'HASH';
39 81 for my $key (sort keys %$section) {
40 297 ▶ croak 'customer_source.password is not allowed: name the variable that holds it in '
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "password" 1 296 297
41 . 'password_env (ADR 0003)' if $key eq 'password';
42 croak "customer_source: unknown key '$key' (known: " . join(', ', sort keys %KNOWN_KEYS) . ')'
43 296 ▶ unless $KNOWN_KEYS{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $KNOWN_KEYS{$key} 1 295 296
44 }
45  
46 79 ▶ my $backend = lc($section->{backend} // 'postgresql');
Condition100%2/2
$section->{"backend"} // "postgresql"
l!l
178
MC/DC100%1/1
$section->{"backend"} // "postgresql"
$section->{"backend"}
Truth table100%2/2
A
A= $section->{"backend"}
Aresult
11
00
47 79 ▶ croak "customer_source.backend '$backend' is not supported (only postgresql)"
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $backend and $backend =~ /\Apostgres/ 1 78 79
Condition66%2/3
not ref $backend and $backend =~ /\Apostgres/
!ll&&!rl&&r
0178
MC/DC50%1/2
not ref $backend and $backend =~ /\Apostgres/
not ref $backend $backend =~ /\Apostgres/
Truth table66%2/3
not(A) and B
A= not ref $backendB= $backend =~ /\Apostgres/
ABresult
0X0
100
111
48 unless !ref($backend) && $backend =~ /\Apostgres/;
49  
50 78 my $relation = $section->{relation};
51 78 ▶ croak 'customer_source.relation is required: the table or view to read, schema-qualified '
Branch: 1 branch
BranchTrueFalseTotal
unless defined $relation and not ref $relation and length $relation 1 77 78
Condition66%4/6
defined $relation and not ref $relation and length $relation
!ll&&!rl&&r
1077
defined $relation and not ref $relation
!ll&&!rl&&r
1077
MC/DC33%1/3
defined $relation and not ref $relation and length $relation
defined $relation not ref $relation length $relation
Truth table50%2/4
A and not(B) and C
A= defined $relationB= not ref $relationC= length $relation
ABCresult
0XX0
10X0
1100
1111
52 . 'allowed' unless defined($relation) && !ref($relation) && length($relation);
53 77 $class->statement($relation);
54  
55 76 my %conn;
56 76 ▶ if (_true($section->{from_usage_store})) {
Branch: 1 branch
BranchTrueFalseTotal
if (_true($section->{'from_usage_store'})) { } 5 71 76
57 5 my @own = grep { exists $section->{$_} } @OWN_CONNECTION;
58 5 ▶ croak 'customer_source.from_usage_store takes the usage store\'s connection; leave out '
Branch: 1 branch
BranchTrueFalseTotal
if @own 1 4 5
59 . join(', ', @own) . ' (only user and password_env may override it)' if @own;
60 4 my $usage = $opts{usage_store};
61 4 ▶ croak 'customer_source.from_usage_store needs a usage_store section with backend postgresql'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $usage eq "HASH" 1 3 4
62 unless ref($usage) eq 'HASH';
63 3 my $store = Langertha::Skeid::UsageStore->normalize_config($usage);
64 croak 'customer_source.from_usage_store needs the usage store to be postgresql, not '
65 3 ▶ . $store->{backend} unless $store->{backend} eq 'postgresql';
Branch: 1 branch
BranchTrueFalseTotal
unless $store->{"backend"} eq "postgresql" 1 2 3
66 2 %conn = (dsn => $store->{dsn}, user => $store->{user}, password => $store->{password});
67 2 ▶ $conn{user} = $section->{user} if defined $section->{user};
Branch: 1 branch
BranchTrueFalseTotal
if defined $section->{"user"} 1 1 2
68 $conn{password} = $ENV{$section->{password_env}} // ''
69 2 ▶ if defined($section->{password_env}) && length($section->{password_env});
Branch: 1 branch
BranchTrueFalseTotal
if defined $section->{"password_env"} and length $section->{"password_env"} 1 1 2
Condition60%3/5
defined $section->{"password_env"} and length $section->{"password_env"}
!ll&&!rl&&r
101
$ENV{$section->{"password_env"}} // ""
l!l
10
MC/DC50%1/2
defined $section->{"password_env"} and length $section->{"password_env"}
defined $section->{"password_env"} length $section->{"password_env"}
MC/DC0%0/1
$ENV{$section->{"password_env"}} // ""
$ENV{$section->{"password_env"}}
Truth table66%2/3
A and B
A= defined $section->{"password_env"}B= length $section->{"password_env"}
ABresult
0X0
100
111
Truth table50%1/2
A
A= $ENV{$section->{"password_env"}}
Aresult
11
00
70 } else {
71 my $store = Langertha::Skeid::UsageStore->normalize_config({
72 backend => 'postgresql',
73 71 ▶ map { exists($section->{$_}) ? ($_ => $section->{$_}) : () }
Branch: 1 branch
BranchTrueFalseTotal
exists $section->{$_} ? : 96 401 497
74 qw( dsn host port dbname database user password_env ),
75 });
76 71 %conn = (dsn => $store->{dsn}, user => $store->{user}, password => $store->{password});
77 }
78 73 ▶ croak 'customer_source.dsn must be a dbi:Pg: DSN' unless $conn{dsn} =~ /\Adbi:Pg:/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $conn{"dsn"} =~ /\Adbi:Pg:/i 1 72 73
79  
80 72 my $listen = $section->{listen};
81 72 ▶ if (defined $listen) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $listen) 9 63 72
82 9 ▶ croak 'customer_source.listen must be a channel name: letters, digits and underscores'
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $listen and $listen =~ /\A$IDENT\z/ 1 8 9
Condition66%2/3
not ref $listen and $listen =~ /\A$IDENT\z/
!ll&&!rl&&r
018
MC/DC50%1/2
not ref $listen and $listen =~ /\A$IDENT\z/
not ref $listen $listen =~ /\A$IDENT\z/
Truth table66%2/3
not(A) and B
A= not ref $listenB= $listen =~ /\A$IDENT\z/
ABresult
0X0
100
111
83 unless !ref($listen) && $listen =~ /\A$IDENT\z/;
84 }
85  
86 return {
87 71 backend => 'postgresql',
88 %conn,
89 relation => "$relation",
90 refresh_interval_s => _seconds($section, 'refresh_interval_s', 30, 0),
91 listen => $listen,
92 max_stale_s => _seconds($section, 'max_stale_s', 0, 1),
93 connect_timeout_s => _seconds($section, 'connect_timeout_s', 5, 0),
94 };
95 }
96  
97 76 ▶sub _true {
Subroutine _true: called CC=5 SCAR=17.8
98 76 my ($value) = @_;
99 76 ▶ return 0 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 71 5 76
100 5 ▶ return $value ? 1 : 0 if ref $value;
Branch: 2 branches
BranchTrueFalseTotal
if ref $value 0 5 5
$value ? : 0 0 0
101 5 ▶ return "$value" =~ /\A(1|true|yes|on)\z/i ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
"$value" =~ /\A(1|true|yes|on)\z/i ? : 5 0 5
102 }
103  
104 # A number of seconds; fractions allowed. $zero_ok: 0 is a value (max_stale_s), else it must be
105 # above 0.
106 202 ▶sub _seconds {
Subroutine _seconds: called CC=8 SCAR=20.8
107 202 my ($section, $key, $default, $zero_ok) = @_;
108 202 ▶ return $default unless exists $section->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $section->{$key} 105 97 202
109 97 my $value = $section->{$key};
110 97 ▶ my $ok = defined($value) && !ref($value) && $value =~ /\A[0-9]+(?:\.[0-9]+)?\z/
Branch: 1 branch
BranchTrueFalseTotal
$zero_ok ? : 5 83 88
Condition88%8/9
defined $value && !ref($value) && $value =~ /\A[0-9]+(?:\.[0-9]+)?\z/ && ($zero_ok ? $value >= 0 : $value > 0)
!ll&&!rl&&r
9286
defined $value and not ref $value and $value =~ /\A[0-9]+(?:\.[0-9]+)?\z/
!ll&&!rl&&r
2788
defined $value and not ref $value
!ll&&!rl&&r
0295
MC/DC75%3/4
defined $value && !ref($value) && $value =~ /\A[0-9]+(?:\.[0-9]+)?\z/ && ($zero_ok ? $value >= 0 : $value > 0)
defined $value not ref $value $value =~ /\A[0-9]+(?:\.[0-9]+)?\z/ ($zero_ok ? $value >= 0 : $value > 0)
Truth table80%4/5
A and not(B) and C && D
A= defined $valueB= not ref $valueC= $value =~ /\A[0-9]+(?:\.[0-9]+)?\z/D= ($zero_ok ? $value >= 0 : $value > 0)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
111 && ($zero_ok ? $value >= 0 : $value > 0);
112 97 ▶ croak "customer_source.$key must be a number of seconds " . ($zero_ok ? 'of at least 0' : 'above 0')
Branch: 2 branches
BranchTrueFalseTotal
unless $ok 11 86 97
$zero_ok ? : 1 10 11
113 unless $ok;
114 86 return 0 + $value;
115 }
116  
117  
118 89 ▶sub statement {
Subroutine statement: called CC=4 SCAR=14.3
Pod documented
119 89 my ($class, $relation) = @_;
120 89 ▶ croak 'customer_source.relation must be a table or view name, schema-qualified allowed '
Branch: 1 branch
BranchTrueFalseTotal
unless defined $relation and not ref $relation and $relation =~ /\A$IDENT(?:\.$IDENT)?\z/ 10 79 89
Condition50%3/6
defined $relation and not ref $relation and $relation =~ /\A$IDENT(?:\.$IDENT)?\z/
!ll&&!rl&&r
01079
defined $relation and not ref $relation
!ll&&!rl&&r
0089
MC/DC33%1/3
defined $relation and not ref $relation and $relation =~ /\A$IDENT(?:\.$IDENT)?\z/
defined $relation not ref $relation $relation =~ /\A$IDENT(?:\.$IDENT)?\z/
Truth table50%2/4
A and not(B) and C
A= defined $relationB= not ref $relationC= $relation =~ /\A$IDENT(?:\.$IDENT)?\z/
ABCresult
0XX0
10X0
1100
1111
121 . '(letters, digits and underscores: schema.relation)'
122 unless defined($relation) && !ref($relation) && $relation =~ /\A$IDENT(?:\.$IDENT)?\z/;
123 79 my $quoted = join '.', map { qq{"$_"} } split /\./, $relation;
124 79 return 'SELECT ' . join(', ', @COLUMNS) . " FROM $quoted";
125 }
126  
127  
128 30 ▶sub check_rows {
Subroutine check_rows: called CC=12 SCAR=25.0
Pod documented
129 30 my ($class, $rows, %opts) = @_;
130 30 ▶ my $policies = ref($opts{policies}) eq 'HASH' ? $opts{policies} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{'policies'} eq 'HASH' ? : 30 0 30
131 30 ▶ $rows = [] unless ref($rows) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rows eq "ARRAY" 0 30 30
132  
133 # Duplicates are counted over every row that has a key_id string at all: a key_id shared with
134 # a row that is wrong for another reason is still not one row per key id.
135 30 my %seen;
136 30 for my $row (@$rows) {
137 61 ▶ next unless ref($row) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "HASH" 5 56 61
138 56 my $id = $row->{key_id};
139 56 ▶ $seen{$id}++ if defined($id) && !ref($id);
Branch: 1 branch
BranchTrueFalseTotal
if defined $id and not ref $id 54 2 56
Condition66%2/3
defined $id and not ref $id
!ll&&!rl&&r
0254
MC/DC50%1/2
defined $id and not ref $id
defined $id not ref $id
Truth table66%2/3
A and not(B)
A= defined $idB= not ref $id
ABresult
0X0
100
111
140 }
141  
142 30 my (@accepted, @rejected);
143 30 my $position = 0;
144 30 for my $row (@$rows) {
145 61 $position++;
146 61 my $label = _label($row, $position);
147 61 my $reason = _row_problem($row, $policies, \%seen);
148 61 ▶ if (defined $reason) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reason) 23 38 61
149 23 push @rejected, { row => $label, reason => $reason };
150 23 next;
151 }
152 push @accepted, {
153 key_id => $row->{key_id},
154 name => (defined($row->{name}) && !ref($row->{name}) ? "$row->{name}" : undef),
155 policy => $row->{policy},
156 38 ▶ models => (defined($row->{models}) ? [ map { "$_" } @{$row->{models}} ] : undef),
Branch: 2 branches
BranchTrueFalseTotal
defined $row->{'name'} && !ref($row->{'name'}) ? : 16 22 38
defined $row->{'models'} ? : 3 35 38
Condition66%2/3
defined $row->{"name"} and not ref $row->{"name"}
!ll&&!rl&&r
22016
MC/DC50%1/2
defined $row->{"name"} and not ref $row->{"name"}
defined $row->{"name"} not ref $row->{"name"}
Truth table66%2/3
A and not(B)
A= defined $row->{"name"}B= not ref $row->{"name"}
ABresult
0X0
100
111
157 label => $label,
158 };
159 }
160 30 return { accepted => \@accepted, rejected => \@rejected };
161 }
162  
163 # What is wrong with a row, without any of its values, or undef.
164 61 ▶sub _row_problem {
Subroutine _row_problem: called CC=16 SCAR=27.8
165 61 my ($row, $policies, $seen) = @_;
166 61 ▶ return 'the row is not a row of key_id, name, policy, models' unless ref($row) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "HASH" 5 56 61
167 56 my $id = $row->{key_id};
168 56 ▶ return 'key_id is not a full key id (k_ + 40 lowercase hex digits, from `skeid keyid`)'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and not ref $id and $id =~ /\Ak_[0-9a-f]{40}\z/ 7 49 56
Condition83%5/6
defined $id and not ref $id and $id =~ /\Ak_[0-9a-f]{40}\z/
!ll&&!rl&&r
2549
defined $id and not ref $id
!ll&&!rl&&r
0254
MC/DC66%2/3
defined $id and not ref $id and $id =~ /\Ak_[0-9a-f]{40}\z/
defined $id not ref $id $id =~ /\Ak_[0-9a-f]{40}\z/
Truth table75%3/4
A and not(B) and C
A= defined $idB= not ref $idC= $id =~ /\Ak_[0-9a-f]{40}\z/
ABCresult
0XX0
10X0
1100
1111
169 unless defined($id) && !ref($id) && $id =~ /\Ak_[0-9a-f]{40}\z/;
170 49 ▶ return 'key_id appears in more than one row' if ($seen->{$id} // 0) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if ($seen->{$id} // 0) > 1 4 45 49
Condition50%1/2
$seen->{$id} // 0
l!l
490
MC/DC0%0/1
$seen->{$id} // 0
$seen->{$id}
Truth table50%1/2
A
A= $seen->{$id}
Aresult
11
00
171 45 my $policy = $row->{policy};
172 45 ▶ if (defined $policy) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $policy) 12 33 45
173 return 'policy names no profile in the file\'s policies:'
174 12 ▶ unless !ref($policy) && length($policy) && exists $policies->{$policy};
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $policy and length $policy and exists $policies->{$policy} 4 8 12
Condition100%6/6
not ref $policy and length $policy and exists $policies->{$policy}
!ll&&!rl&&r
228
not ref $policy and length $policy
!ll&&!rl&&r
1110
MC/DC100%3/3
not ref $policy and length $policy and exists $policies->{$policy}
not ref $policy length $policy exists $policies->{$policy}
Truth table100%4/4
not(A) and B and C
A= not ref $policyB= length $policyC= exists $policies->{$policy}
ABCresult
0XX0
10X0
1100
1111
175 }
176 41 my $models = $row->{models};
177 41 ▶ if (defined $models) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $models) 6 35 41
178 6 ▶ return 'models is not a list of model names' unless ref($models) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $models eq "ARRAY" 1 5 6
179 return 'models is not a list of model names'
180 5 ▶ if grep { !defined($_) || ref($_) || !length($_) } @$models;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_ or ref $_ or not length $_;} @$models 2 3 5
Condition83%5/6
!defined($_) || ref $_ || !length($_)
l!l&&r!l&&!r
112
not defined $_ or ref $_
l!l&&r!l&&!r
013
MC/DC66%2/3
!defined($_) || ref $_ || !length($_)
not defined $_ ref $_ !length($_)
Truth table75%3/4
not(A) or B || not(C)
A= not defined $_B= ref $_C= !length($_)
ABCresult
1XX1
01X1
0011
0000
181 }
182 38 return undef;
183 }
184  
185 # A row's label in a report: its name, else its position. A name is a display label the owning
186 # application chose; it is printed with control characters replaced and cut to a sane length,
187 # so one row cannot forge log lines.
188 61 ▶sub _label {
Subroutine _label: called CC=6 SCAR=18.0
189 61 my ($row, $position) = @_;
190 61 ▶ my $name = ref($row) eq 'HASH' ? $row->{name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $row eq 'HASH' ? : 56 5 61
191 61 ▶ return "row $position" unless defined($name) && !ref($name) && length($name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and not ref $name and length $name 36 25 61
Condition83%5/6
defined $name and not ref $name and length $name
!ll&&!rl&&r
36025
defined $name and not ref $name
!ll&&!rl&&r
35125
MC/DC66%2/3
defined $name and not ref $name and length $name
defined $name not ref $name length $name
Truth table75%3/4
A and not(B) and C
A= defined $nameB= not ref $nameC= length $name
ABCresult
0XX0
10X0
1100
1111
192 25 (my $clean = "$name") =~ s/[^\x20-\x7e]/?/g;
193 25 ▶ $clean = substr($clean, 0, 80) . '...' if length($clean) > 80;
Branch: 1 branch
BranchTrueFalseTotal
if length $clean > 80 0 25 25
194 25 $clean =~ s/'/"/g;
195 25 return "'$clean'";
196 }
197  
198  
199 114 my $JSON = JSON::MaybeXS->new(canonical => 1, allow_nonref => 1, allow_blessed => 1, utf8 => 1);
200  
201 33 ▶sub snapshot_digest {
Subroutine snapshot_digest: called CC=4 SCAR=14.8
Pod documented
202 33 my ($class, $rows) = @_;
203 my @encoded = map {
204 56 my $row = $_;
205 56 my $text = eval { $JSON->encode($row) };
206 56 ▶ defined($text) ? $text : 'unencodable:' . (ref($row) || 'scalar');
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 56 0 56
Condition0%0/2
ref $row || 'scalar'
l!l
00
MC/DC0%0/1
ref $row || 'scalar'
ref $row
Truth table0%0/2
A
A= ref $row
Aresult
11
00
207 33 ▶ } @{ ref($rows) eq 'ARRAY' ? $rows : [] };
Branch: 1 branch
BranchTrueFalseTotal
ref $rows eq 'ARRAY' ? : 33 0 33
208 33 return sha256_hex(join "\n", sort @encoded);
209 }
210  
211  
212 29 ▶sub merge_into {
Subroutine merge_into: called CC=19 SCAR=29.5
Pod documented
213 29 my ($class, $cfg, $rows) = @_;
214 my $checked = $class->check_rows($rows,
215 29 ▶ policies => (ref($cfg->{policies}) eq 'HASH' ? $cfg->{policies} : {}));
Branch: 1 branch
BranchTrueFalseTotal
ref $cfg->{'policies'} eq 'HASH' ? : 29 0 29
216  
217 29 ▶ my %names = ref($cfg->{names}) eq 'HASH' ? %{$cfg->{names}} : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $cfg->{'names'} eq 'HASH' ? : 6 23 29
218 29 my %file_ids;
219 29 ▶ if (ref($cfg->{keys}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cfg->{"keys"} eq "HASH") 7 22 29
220 7 for my $label (keys %{$cfg->{keys}}) {
221 13 my $mapped = $names{$label};
222 13 ▶ $file_ids{ (defined($mapped) && !ref($mapped)) ? $mapped : $label } = 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $mapped && !ref($mapped) ? : 6 7 13
Condition66%2/3
defined $mapped and not ref $mapped
!ll&&!rl&&r
706
MC/DC50%1/2
defined $mapped and not ref $mapped
defined $mapped not ref $mapped
Truth table66%2/3
A and not(B)
A= defined $mappedB= not ref $mapped
ABresult
0X0
100
111
223 }
224 }
225  
226 29 my (@allow, @shadowed, %entries, %display);
227 29 for my $row (@{$checked->{accepted}}) {
228 35 my $id = $row->{key_id};
229 35 push @allow, $id;
230 # The row's display name, as its sanitized label: a report shows who a key id belongs to.
231 35 ▶ if (defined($row->{name}) && length($row->{name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->{"name"} and length $row->{"name"}) 14 21 35
Condition66%2/3
defined $row->{"name"} and length $row->{"name"}
!ll&&!rl&&r
21014
MC/DC50%1/2
defined $row->{"name"} and length $row->{"name"}
defined $row->{"name"} length $row->{"name"}
Truth table66%2/3
A and B
A= defined $row->{"name"}B= length $row->{"name"}
ABresult
0X0
100
111
232 14 (my $shown = $row->{label}) =~ s/\A'(.*)'\z/$1/s;
233 14 $display{$id} = $shown;
234 }
235 35 ▶ if ($file_ids{$id} || $file_ids{substr($id, 0, 14)} || exists $names{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_ids{$id} or $file_ids{substr($id, 0, 14)} or exists $names{$id}) 3 32 35
Condition83%5/6
$file_ids{$id} or $file_ids{substr($id, 0, 14)} or exists $names{$id}
l!l&&r!l&&!r
3032
$file_ids{$id} or $file_ids{substr($id, 0, 14)}
l!l&&r!l&&!r
2132
MC/DC66%2/3
$file_ids{$id} or $file_ids{substr($id, 0, 14)} or exists $names{$id}
$file_ids{$id} $file_ids{substr($id, 0, 14)} exists $names{$id}
Truth table75%3/4
A or B or C
A= $file_ids{$id}B= $file_ids{substr($id, 0, 14)}C= exists $names{$id}
ABCresult
1XX1
01X1
0011
0000
236 3 push @shadowed, $row->{label};
237 3 next;
238 }
239 32 ▶ next unless defined($row->{policy}) || defined($row->{models});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row->{"policy"} or defined $row->{"models"} 27 5 32
Condition100%3/3
defined $row->{"policy"} or defined $row->{"models"}
l!l&&r!l&&!r
4127
MC/DC100%2/2
defined $row->{"policy"} or defined $row->{"models"}
defined $row->{"policy"} defined $row->{"models"}
Truth table100%3/3
A or B
A= defined $row->{"policy"}B= defined $row->{"models"}
ABresult
1X1
011
000
240 $entries{$id} = {
241 (defined($row->{policy}) ? (policy => $row->{policy}) : ()),
242 5 ▶ (defined($row->{models}) ? (models => $row->{models}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $row->{'policy'} ? : 4 1 5
defined $row->{'models'} ? : 1 4 5
243 };
244 }
245  
246 29 my %merged = %$cfg;
247 29 ▶ $merged{keys} = { (ref($cfg->{keys}) eq 'HASH' ? %{$cfg->{keys}} : ()), %entries } if %entries;
Branch: 2 branches
BranchTrueFalseTotal
if %entries 4 25 29
ref $cfg->{'keys'} eq 'HASH' ? : 3 1 4
248 return {
249 config => \%merged,
250 allow => \@allow,
251 29 accepted => scalar(@{$checked->{accepted}}),
252 rejected => $checked->{rejected},
253 29 shadowed => \@shadowed,
254 names => \%display,
255 };
256 }
257  
258  
259 114 has config => (is => 'ro', required => 1);
260 114 has fetcher => (is => 'ro', required => 1);
261 0 ▶has on_rows => (is => 'ro', default => sub { sub { } });
Subroutine __ANON__: not called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
262 0 ▶has on_failure => (is => 'ro', default => sub { sub { } });
Subroutine __ANON__: not called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
263 22 ▶has notify_debounce_s => (is => 'ro', default => sub { 0.25 });
Subroutine __ANON__: called CC=1 SCAR=0.0
264  
265 22 ▶has last_success_at => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
266 22 ▶has last_failure_at => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
267 22 ▶has failures => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
268 22 ▶has last_error => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
269 22 ▶has completed => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
270  
271 114 has _timer => (is => 'rw');
272 114 has _timeout => (is => 'rw');
273 114 has _debounce => (is => 'rw');
274 22 ▶has _in_flight => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
275 22 ▶has _pending => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
276 22 ▶has _serial => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
277 22 ▶has _started => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
278 22 ▶has _stopped => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
279  
280  
281 22 ▶sub start {
Subroutine start: called CC=3 SCAR=11.0
Pod documented
282 22 my ($self) = @_;
283 22 ▶ return $self if $self->_started;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_started 0 22 22
284 22 $self->_started(1);
285 22 my $weak = $self;
286 22 weaken($weak);
287 22 ▶ if ($self->fetcher->can('on_notify')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fetcher->can("on_notify")) 12 10 22
288 12 ▶ $self->fetcher->on_notify(sub { $weak->_notified if $weak });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if $weak 21 0 21
289 }
290 22 $self->_schedule(0);
291 22 return $self;
292 }
293  
294  
295 44 ▶sub stop {
Subroutine stop: called CC=4 SCAR=13.9
Pod documented
296 44 my ($self) = @_;
297 44 ▶ return if $self->_stopped;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_stopped 22 22 44
298 22 $self->_stopped(1);
299 22 $self->_in_flight(0);
300 22 for my $timer (qw(_timer _timeout _debounce)) {
301 66 my $id = $self->$timer;
302 66 ▶ Mojo::IOLoop->remove($id) if defined $id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 21 45 66
303 66 $self->$timer(undef);
304 }
305 22 $self->release;
306 22 return;
307 }
308  
309  
310 23 ▶sub release {
Subroutine release: called CC=2 SCAR=6.9
Pod documented
311 23 my ($self) = @_;
312 23 my $fetcher = $self->fetcher;
313 23 ▶ eval { $fetcher->release; 1 } if $fetcher->can('release');
Branch: 1 branch
BranchTrueFalseTotal
if $fetcher->can("release") 13 10 23
314 23 return;
315 }
316  
317  
318 50 ▶sub fetch_now {
Subroutine fetch_now: called CC=6 SCAR=18.6
Pod documented
319 50 my ($self) = @_;
320 50 ▶ return if $self->_stopped;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_stopped 0 50 50
321 50 ▶ if ($self->_in_flight) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_in_flight) 0 50 50
322 0 $self->_pending(1);
323 0 return;
324 }
325 50 my $timer = $self->_timer;
326 50 ▶ Mojo::IOLoop->remove($timer) if defined $timer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timer 9 41 50
327 50 $self->_timer(undef);
328  
329 50 $self->_in_flight(1);
330 50 my $serial = $self->_serial + 1;
331 50 $self->_serial($serial);
332 50 my $weak = $self;
333 50 weaken($weak);
334 50 my $limit = $self->config->{connect_timeout_s};
335 2 ▶ $self->_timeout(Mojo::IOLoop->timer($limit => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
336 2 ▶ $weak->_done($serial, "fetch timed out after ${limit}s") if $weak;
Branch: 1 branch
BranchTrueFalseTotal
if $weak 2 0 2
337 50 }));
338 50 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->fetcher->fetch(sub { my($err, $rows) = @_; $weak->_done($serial, $err, $rows) if $weak; } ); 1 } } or $self->_done($serial, $@ || "fetch failed") 50 0 50
339 46 ▶ $self->fetcher->fetch(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
340 46 my ($err, $rows) = @_;
341 46 ▶ $weak->_done($serial, $err, $rows) if $weak;
Branch: 1 branch
BranchTrueFalseTotal
if $weak 45 1 46
342 50 });
343 50 ▶ 1;
Condition0%0/2
$@ || 'fetch failed'
l!l
00
MC/DC0%0/1
$@ || 'fetch failed'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
344 } or $self->_done($serial, $@ || 'fetch failed');
345 50 return;
346 }
347  
348 47 ▶sub _done {
Subroutine _done: called CC=15 SCAR=28.0
349 47 my ($self, $serial, $err, $rows) = @_;
350 # A late answer -- after its timeout, or after a stop -- belongs to nobody any more.
351 47 ▶ return unless $self->_in_flight && $serial == $self->_serial && !$self->_stopped;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_in_flight and $serial == $self->_serial and not $self->_stopped 1 46 47
Condition66%4/6
$self->_in_flight and $serial == $self->_serial and not $self->_stopped
!ll&&!rl&&r
1046
$self->_in_flight and $serial == $self->_serial
!ll&&!rl&&r
1046
MC/DC33%1/3
$self->_in_flight and $serial == $self->_serial and not $self->_stopped
$self->_in_flight $serial == $self->_serial not $self->_stopped
Truth table50%2/4
A and B and not(C)
A= $self->_in_flightB= $serial == $self->_serialC= not $self->_stopped
ABCresult
0XX0
10X0
1100
1111
352 46 $self->_in_flight(0);
353 46 my $timeout = $self->_timeout;
354 46 ▶ Mojo::IOLoop->remove($timeout) if defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timeout 46 0 46
355 46 $self->_timeout(undef);
356 46 $self->completed($self->completed + 1);
357  
358 46 ▶ $err = 'the fetch answered no row list' if !defined($err) && ref($rows) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $err and ref $rows ne "ARRAY" 0 46 46
Condition66%2/3
not defined $err and ref $rows ne "ARRAY"
!ll&&!rl&&r
13330
MC/DC0%0/2
not defined $err and ref $rows ne "ARRAY"
not defined $err ref $rows ne "ARRAY"
Truth table66%2/3
not(A) and B
A= not defined $errB= ref $rows ne "ARRAY"
ABresult
0X0
100
111
359 46 ▶ if (defined $err) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) { } 13 33 46
360 13 $err = _clean_error($err);
361 13 my $previous = $self->last_error;
362 13 $self->failures($self->failures + 1);
363 13 $self->last_failure_at(Time::HiRes::time());
364 13 $self->last_error($err);
365 13 ▶ warn "skeid: customer_source fetch failed, keeping the last good snapshot: $err\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $previous or $previous ne $err 7 6 13
Condition100%3/3
not defined $previous or $previous ne $err
l!l&&r!l&&!r
616
MC/DC100%2/2
not defined $previous or $previous ne $err
not defined $previous $previous ne $err
Truth table100%3/3
not(A) or B
A= not defined $previousB= $previous ne $err
ABresult
1X1
011
000
366 if !defined($previous) || $previous ne $err;
367 13 ▶ eval { $self->on_failure->($err); 1 } or warn "skeid: customer_source: $@";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->on_failure->($err); 1 } } or warn "skeid: customer_source: $@" 13 0 13
368 13 $self->_schedule($self->retry_delay);
369 } else {
370 33 ▶ warn "skeid: customer_source fetch succeeded again\n" if $self->failures;
Branch: 1 branch
BranchTrueFalseTotal
if $self->failures 3 30 33
371 33 $self->failures(0);
372 33 $self->last_error(undef);
373 33 $self->last_success_at(Time::HiRes::time());
374 33 ▶ eval { $self->on_rows->($rows); 1 } or warn "skeid: customer_source: $@";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->on_rows->($rows); 1 } } or warn "skeid: customer_source: $@" 33 0 33
375 33 $self->_schedule($self->config->{refresh_interval_s});
376 }
377  
378 46 ▶ if ($self->_pending && !$self->_stopped) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_pending and not $self->_stopped) 0 46 46
Condition33%1/3
$self->_pending and not $self->_stopped
!ll&&!rl&&r
4600
MC/DC0%0/2
$self->_pending and not $self->_stopped
$self->_pending not $self->_stopped
Truth table33%1/3
A and not(B)
A= $self->_pendingB= not $self->_stopped
ABresult
0X0
100
111
379 0 $self->_pending(0);
380 0 $self->_schedule(0);
381 }
382 46 return;
383 }
384  
385  
386 13 ▶sub retry_delay {
Subroutine retry_delay: called CC=3 SCAR=11.4
Pod documented
387 13 my ($self) = @_;
388 13 ▶ my $failures = $self->failures || 1;
Condition50%1/2
$self->failures || 1
l!l
130
MC/DC0%0/1
$self->failures || 1
$self->failures
Truth table50%1/2
A
A= $self->failures
Aresult
11
00
389 13 my $delay = $self->config->{refresh_interval_s} * 2 ** ($failures - 1);
390 13 ▶ return $delay > $BACKOFF_MAX ? $BACKOFF_MAX : $delay;
Branch: 1 branch
BranchTrueFalseTotal
$delay > $BACKOFF_MAX ? : 0 13 13
391 }
392  
393 68 ▶sub _schedule {
Subroutine _schedule: called CC=3 SCAR=11.0
394 68 my ($self, $delay) = @_;
395 68 ▶ return if $self->_stopped;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_stopped 1 67 68
396 67 my $timer = $self->_timer;
397 67 ▶ Mojo::IOLoop->remove($timer) if defined $timer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timer 0 67 67
398 67 my $weak = $self;
399 67 weaken($weak);
400 41 ▶ $self->_timer(Mojo::IOLoop->timer($delay => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
401 41 ▶ return unless $weak;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak 0 41 41
402 41 $weak->_timer(undef);
403 41 $weak->fetch_now;
404 67 }));
405 67 return;
406 }
407  
408 # A notification: one fetch after the debounce, however many more arrive before it runs.
409 21 ▶sub _notified {
Subroutine _notified: called CC=3 SCAR=11.0
410 21 my ($self) = @_;
411 21 ▶ return if $self->_stopped || defined $self->_debounce;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_stopped or defined $self->_debounce 19 2 21
Condition66%2/3
$self->_stopped or defined $self->_debounce
l!l&&r!l&&!r
0192
MC/DC50%1/2
$self->_stopped or defined $self->_debounce
$self->_stopped defined $self->_debounce
Truth table66%2/3
A or B
A= $self->_stoppedB= defined $self->_debounce
ABresult
1X1
011
000
412 2 my $weak = $self;
413 2 weaken($weak);
414 2 ▶ $self->_debounce(Mojo::IOLoop->timer($self->notify_debounce_s => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
415 2 ▶ return unless $weak;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak 0 2 2
416 2 $weak->_debounce(undef);
417 2 $weak->fetch_now;
418 2 }));
419 2 return;
420 }
421  
422 # A driver error names the DSN it failed on, and a DSN may carry a password.
423 13 ▶sub _clean_error {
Subroutine _clean_error: called CC=2 SCAR=7.0
424 13 my ($err) = @_;
425 13 $err = "$err";
426 13 $err =~ s/\b(password|pwd)=[^;'"\s)]*/$1=***/gi;
427 13 $err =~ s/\s+\z//;
428 13 ▶ return length($err) ? $err : 'fetch failed';
Branch: 1 branch
BranchTrueFalseTotal
length $err ? : 13 0 13
429 }
430  
431  
432 231 ▶sub status {
Subroutine status: called CC=6 SCAR=17.9
Pod documented
433 231 my ($self) = @_;
434 231 ▶ my $iso = sub { defined($_[0]) ? strftime('%Y-%m-%dT%H:%M:%SZ', gmtime(int $_[0])) : undef };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 236 3 239
435 231 ▶ my $ok = defined($self->last_success_at) && !$self->failures ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->last_success_at && !$self->failures ? : 222 9 231
Condition100%3/3
defined $self->last_success_at and not $self->failures
!ll&&!rl&&r
36222
MC/DC100%2/2
defined $self->last_success_at and not $self->failures
defined $self->last_success_at not $self->failures
Truth table100%3/3
A and not(B)
A= defined $self->last_success_atB= not $self->failures
ABresult
0X0
100
111
436 return {
437 231 ▶ ok => $ok,
Branch: 2 branches
BranchTrueFalseTotal
$self->failures ? : 8 223 231
$ok ? : 222 9 231
Condition100%2/2
$self->last_error // "no fetch has finished yet"
l!l
81
MC/DC100%1/1
$self->last_error // "no fetch has finished yet"
$self->last_error
Truth table100%2/2
A
A= $self->last_error
Aresult
11
00
438 last_success_at => $iso->($self->last_success_at),
439 failed_at => ($self->failures ? $iso->($self->last_failure_at) : undef),
440 failures => 0 + $self->failures,
441 error => ($ok ? undef : ($self->last_error // 'no fetch has finished yet')),
442 };
443 }
444  
445 22 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=7.1
Pod undocumented
446 22 my ($self, $global) = @_;
447 22 ▶ return if $global;
Branch: 1 branch
BranchTrueFalseTotal
if $global 0 22 22
448 22 $self->stop;
449 }
450  
451  
452 114 1;
453  
454 __END__
« lib/Langertha/Skeid/Protocol/Realtime.pm ↑ Summary lib/Langertha/Skeid/Protocol/RawBody.pm »