lib/Langertha/Skeid/UsageStore.pm

filter: Statementstmt 97.1% 69 / 71 Branchbran 76.5% 49 / 64 Conditioncond 64.8% 61 / 94 MC/DCmcdc 34.0% 16 / 47 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 71.9% 208 / 289 CC 72 SCAR 40.7
CC
72
Cov
78%
normalize_config
83.1
for_config
28.5
_flush_interval_ms
3.0
CRAP 58.3
« bin/skeid ↑ Summary lib/Langertha/Skeid/Protocol/Anthropic.pm »
1 package Langertha::Skeid::UsageStore;
2 115 our $VERSION = '0.004';
3 # ABSTRACT: Usage event sink - config normalization and backend factory
4 115 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 115 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 115 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 155 ▶sub normalize_config {
Subroutine normalize_config: called CC=50 SCAR=44.2
Pod documented
10 155 my ($class, $cfg, %opts) = @_;
11 155 ▶ $cfg ||= {};
Condition50%1/2
$cfg ||= {}
l!l
1550
MC/DC0%0/1
$cfg ||= {}
$cfg
Truth table50%1/2
A
A= $cfg
Aresult
11
00
12 155 ▶ croak 'usage_store must be a hashref' unless ref($cfg) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cfg eq "HASH" 0 155 155
13  
14 155 ▶ my $backend = lc($cfg->{backend} // '');
Condition100%2/2
$cfg->{"backend"} // ""
l!l
13916
MC/DC100%1/1
$cfg->{"backend"} // ""
$cfg->{"backend"}
Truth table100%2/2
A
A= $cfg->{"backend"}
Aresult
11
00
15 155 ▶ $backend = 'sqlite' if !$backend && (defined($cfg->{sqlite_path}) || defined($cfg->{path}) || defined($cfg->{db_path}));
Branch: 1 branch
BranchTrueFalseTotal
if not $backend and defined $cfg->{"sqlite_path"} || defined $cfg->{"path"} || defined $cfg->{"db_path"} 14 141 155
Condition77%7/9
not $backend and defined $cfg->{"sqlite_path"} || defined $cfg->{"path"} || defined $cfg->{"db_path"}
!ll&&!rl&&r
139214
defined $cfg->{"sqlite_path"} or defined $cfg->{"path"} or defined $cfg->{"db_path"}
l!l&&r!l&&!r
1402
defined $cfg->{"sqlite_path"} or defined $cfg->{"path"}
l!l&&r!l&&!r
1402
MC/DC50%2/4
not $backend and defined $cfg->{"sqlite_path"} || defined $cfg->{"path"} || defined $cfg->{"db_path"}
not $backend defined $cfg->{"sqlite_path"} defined $cfg->{"path"} defined $cfg->{"db_path"}
Truth table60%3/5
not(A) and B or C or D
A= not $backendB= defined $cfg->{"sqlite_path"}C= defined $cfg->{"path"}D= defined $cfg->{"db_path"}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
16 155 ▶ $backend = 'postgresql' if !$backend && defined($cfg->{dsn}) && $cfg->{dsn} =~ /^dbi:Pg:/i;
Branch: 1 branch
BranchTrueFalseTotal
if not $backend and defined $cfg->{"dsn"} and $cfg->{"dsn"} =~ /^dbi:Pg:/i 2 153 155
Condition66%4/6
not $backend and defined $cfg->{"dsn"} and $cfg->{"dsn"} =~ /^dbi:Pg:/i
!ll&&!rl&&r
15302
not $backend and defined $cfg->{"dsn"}
!ll&&!rl&&r
15302
MC/DC33%1/3
not $backend and defined $cfg->{"dsn"} and $cfg->{"dsn"} =~ /^dbi:Pg:/i
not $backend defined $cfg->{"dsn"} $cfg->{"dsn"} =~ /^dbi:Pg:/i
Truth table50%2/4
not(A) and B and C
A= not $backendB= defined $cfg->{"dsn"}C= $cfg->{"dsn"} =~ /^dbi:Pg:/i
ABCresult
0XX0
10X0
1100
1111
17 155 ▶ $backend = 'jsonlog' if !$backend && defined($cfg->{log_path});
Branch: 1 branch
BranchTrueFalseTotal
if not $backend and defined $cfg->{"log_path"} 0 155 155
Condition33%1/3
not $backend and defined $cfg->{"log_path"}
!ll&&!rl&&r
15500
MC/DC0%0/2
not $backend and defined $cfg->{"log_path"}
not $backend defined $cfg->{"log_path"}
Truth table33%1/3
not(A) and B
A= not $backendB= defined $cfg->{"log_path"}
ABresult
0X0
100
111
18 155 ▶ $backend = 'sqlite' unless length $backend;
Branch: 1 branch
BranchTrueFalseTotal
unless length $backend 0 155 155
19 155 ▶ $backend = 'postgresql' if $backend =~ /^postgres/;
Branch: 1 branch
BranchTrueFalseTotal
if $backend =~ /^postgres/ 75 80 155
20 155 ▶ $backend = 'jsonlog' if $backend =~ /^json/;
Branch: 1 branch
BranchTrueFalseTotal
if $backend =~ /^json/ 41 114 155
21  
22 155 ▶ if ($backend eq 'sqlite') {
Branch: 1 branch
BranchTrueFalseTotal
if ($backend eq "sqlite") 39 116 155
23 39 ▶ my $path = $cfg->{sqlite_path} // $cfg->{path} // $cfg->{db_path} // $opts{default_sqlite_path};
Condition44%4/9
(($cfg->{"sqlite_path"} // $cfg->{"path"}) // $cfg->{"db_path"}) // $opts{"default_sqlite_path"}
l!l&&r!l&&!r
3900
($cfg->{"sqlite_path"} // $cfg->{"path"}) // $cfg->{"db_path"}
l!l&&r!l&&!r
3900
$cfg->{"sqlite_path"} // $cfg->{"path"}
l!l&&r!l&&!r
3720
MC/DC0%0/4
(($cfg->{"sqlite_path"} // $cfg->{"path"}) // $cfg->{"db_path"}) // $opts{"default_sqlite_path"}
$cfg->{"sqlite_path"} $cfg->{"path"} $cfg->{"db_path"} $opts{"default_sqlite_path"}
Truth table40%2/5
A // B // C // D
A= $cfg->{"sqlite_path"}B= $cfg->{"path"}C= $cfg->{"db_path"}D= $opts{"default_sqlite_path"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
24 39 ▶ if (!defined($path) || !length($path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path and length $path) 0 39 39
Condition33%1/3
defined $path and length $path
!ll&&!rl&&r
0039
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table33%1/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
25 0 croak 'usage_store.sqlite_path (or path/db_path) is required for sqlite backend';
26 }
27 return {
28 backend => 'sqlite',
29 path => $path,
30 dsn => "dbi:SQLite:dbname=$path",
31 user => '',
32 password => '',
33 schema_file => ($cfg->{schema_file} // ''),
34 39 ▶ auto_migrate => exists($cfg->{auto_migrate}) ? ($cfg->{auto_migrate} ? 1 : 0) : 1,
Branch: 2 branches
BranchTrueFalseTotal
exists $cfg->{'auto_migrate'} ? : 2 37 39
$cfg->{'auto_migrate'} ? : 2 0 2
Condition100%2/2
$cfg->{"schema_file"} // ""
l!l
237
MC/DC100%1/1
$cfg->{"schema_file"} // ""
$cfg->{"schema_file"}
Truth table100%2/2
A
A= $cfg->{"schema_file"}
Aresult
11
00
35 flush_interval_ms => $class->_flush_interval_ms($cfg),
36 spool_dir => $class->_spool_dir($cfg),
37 };
38 }
39  
40 116 ▶ if ($backend eq 'postgresql') {
Branch: 1 branch
BranchTrueFalseTotal
if ($backend eq "postgresql") 75 41 116
41 75 my $dsn = $cfg->{dsn};
42 75 ▶ if (!defined($dsn) || !length($dsn)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dsn and length $dsn) 4 71 75
Condition66%2/3
defined $dsn and length $dsn
!ll&&!rl&&r
4071
MC/DC50%1/2
defined $dsn and length $dsn
defined $dsn length $dsn
Truth table66%2/3
A and B
A= defined $dsnB= length $dsn
ABresult
0X0
100
111
43 4 ▶ my $host = $cfg->{host} // '127.0.0.1';
Condition50%1/2
$cfg->{"host"} // "127.0.0.1"
l!l
40
MC/DC0%0/1
$cfg->{"host"} // "127.0.0.1"
$cfg->{"host"}
Truth table50%1/2
A
A= $cfg->{"host"}
Aresult
11
00
44 4 ▶ my $port = $cfg->{port} // 5432;
Condition100%2/2
$cfg->{"port"} // 5432
l!l
22
MC/DC100%1/1
$cfg->{"port"} // 5432
$cfg->{"port"}
Truth table100%2/2
A
A= $cfg->{"port"}
Aresult
11
00
45 4 ▶ my $name = $cfg->{dbname} // $cfg->{database} // 'skeid';
Condition40%2/5
($cfg->{"dbname"} // $cfg->{"database"}) // "skeid"
l!l
40
$cfg->{"dbname"} // $cfg->{"database"}
l!l&&r!l&&!r
400
MC/DC0%0/2
($cfg->{"dbname"} // $cfg->{"database"}) // "skeid"
$cfg->{"dbname"} $cfg->{"database"}
Truth table33%1/3
A // B
A= $cfg->{"dbname"}B= $cfg->{"database"}
ABresult
1X1
011
000
46 4 $dsn = "dbi:Pg:dbname=$name;host=$host;port=$port";
47 }
48 75 ▶ my $password = defined($cfg->{password}) ? $cfg->{password} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $cfg->{'password'} ? : 2 73 75
49 75 ▶ if (!length($password) && defined($cfg->{password_env}) && length($cfg->{password_env})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $password and defined $cfg->{"password_env"} and length $cfg->{"password_env"}) 3 72 75
Condition83%5/6
not length $password and defined $cfg->{"password_env"} and length $cfg->{"password_env"}
!ll&&!rl&&r
7203
not length $password and defined $cfg->{"password_env"}
!ll&&!rl&&r
2703
MC/DC66%2/3
not length $password and defined $cfg->{"password_env"} and length $cfg->{"password_env"}
not length $password defined $cfg->{"password_env"} length $cfg->{"password_env"}
Truth table75%3/4
not(A) and B and C
A= not length $passwordB= defined $cfg->{"password_env"}C= length $cfg->{"password_env"}
ABCresult
0XX0
10X0
1100
1111
50 3 ▶ $password = $ENV{$cfg->{password_env}} // '';
Condition100%2/2
$ENV{$cfg->{"password_env"}} // ""
l!l
21
MC/DC100%1/1
$ENV{$cfg->{"password_env"}} // ""
$ENV{$cfg->{"password_env"}}
Truth table100%2/2
A
A= $ENV{$cfg->{"password_env"}}
Aresult
11
00
51 }
52 return {
53 backend => 'postgresql',
54 path => '',
55 dsn => $dsn,
56 user => ($cfg->{user} // ''),
57 password => $password,
58 schema_file => ($cfg->{schema_file} // ''),
59 75 ▶ auto_migrate => exists($cfg->{auto_migrate}) ? ($cfg->{auto_migrate} ? 1 : 0) : 1,
Branch: 2 branches
BranchTrueFalseTotal
exists $cfg->{'auto_migrate'} ? : 0 75 75
$cfg->{'auto_migrate'} ? : 0 0 0
Condition75%3/4
$cfg->{"user"} // ""
l!l
2055
$cfg->{"schema_file"} // ""
l!l
075
MC/DC100%1/1
$cfg->{"user"} // ""
$cfg->{"user"}
MC/DC0%0/1
$cfg->{"schema_file"} // ""
$cfg->{"schema_file"}
Truth table100%2/2
A
A= $cfg->{"user"}
Aresult
11
00
Truth table50%1/2
A
A= $cfg->{"schema_file"}
Aresult
11
00
60 flush_interval_ms => $class->_flush_interval_ms($cfg),
61 spool_dir => $class->_spool_dir($cfg),
62 };
63 }
64  
65 41 ▶ if ($backend eq 'jsonlog') {
Branch: 1 branch
BranchTrueFalseTotal
if ($backend eq "jsonlog") 41 0 41
66 41 ▶ my $path = $cfg->{log_path} // $cfg->{path} // '';
Condition60%3/5
($cfg->{"log_path"} // $cfg->{"path"}) // ""
l!l
410
$cfg->{"log_path"} // $cfg->{"path"}
l!l&&r!l&&!r
12290
MC/DC0%0/2
($cfg->{"log_path"} // $cfg->{"path"}) // ""
$cfg->{"log_path"} $cfg->{"path"}
Truth table66%2/3
A // B
A= $cfg->{"log_path"}B= $cfg->{"path"}
ABresult
1X1
011
000
67 41 ▶ croak 'usage_store.log_path (or path) is required for jsonlog backend' unless length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless length $path 0 41 41
68 41 ▶ my $mode = $cfg->{mode} // '';
Condition100%2/2
$cfg->{"mode"} // ""
l!l
2021
MC/DC100%1/1
$cfg->{"mode"} // ""
$cfg->{"mode"}
Truth table100%2/2
A
A= $cfg->{"mode"}
Aresult
11
00
69 41 ▶ if (!length($mode)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $mode) 21 20 41
70 21 ▶ $mode = (-d $path || $path =~ m{/$}) ? 'dir' : 'file';
Branch: 1 branch
BranchTrueFalseTotal
-d $path || $path =~ m[/$] ? : 19 2 21
Condition100%3/3
-d $path or $path =~ m[/$]
l!l&&r!l&&!r
1722
MC/DC100%2/2
-d $path or $path =~ m[/$]
-d $path $path =~ m[/$]
Truth table100%3/3
A or B
A= -d $pathB= $path =~ m[/$]
ABresult
1X1
011
000
71 }
72 return {
73 backend => 'jsonlog',
74 path => $path,
75 mode => $mode,
76 41 ▶ fsync => ($cfg->{fsync} ? 1 : 0),
Branch: 1 branch
BranchTrueFalseTotal
$cfg->{'fsync'} ? : 1 40 41
77 };
78 }
79  
80 0 croak "unsupported usage_store backend '$backend'";
81 }
82  
83 # Write-behind for the DBI backends (skeid k78). 0 -- the default -- is the synchronous write
84 # every deployment had before; anything else widens the loss window, so it has to be asked for
85 # with a number that means what it says.
86 114 ▶sub _flush_interval_ms {
Subroutine _flush_interval_ms: called CC=3 SCAR=11.0
87 114 my ($class, $cfg) = @_;
88 114 ▶ my $ms = $cfg->{flush_interval_ms} // 0;
Condition100%2/2
$cfg->{"flush_interval_ms"} // 0
l!l
12102
MC/DC100%1/1
$cfg->{"flush_interval_ms"} // 0
$cfg->{"flush_interval_ms"}
Truth table100%2/2
A
A= $cfg->{"flush_interval_ms"}
Aresult
11
00
89 114 ▶ croak 'usage_store.flush_interval_ms must be a whole number of milliseconds (0 = write each event at once)'
Branch: 1 branch
BranchTrueFalseTotal
unless $ms =~ /\A\d+\z/ 3 111 114
90 unless $ms =~ /\A\d+\z/;
91 111 return 0 + $ms;
92 }
93  
94 # The usage spool of the DBI backends (skeid k110, ADR 0027). Empty -- the default -- keeps the
95 # behavior from before: what the database cannot take is reported lost.
96 111 ▶sub _spool_dir {
Subroutine _spool_dir: called CC=3 SCAR=11.0
97 111 my ($class, $cfg) = @_;
98 111 ▶ my $dir = $cfg->{spool_dir} // '';
Condition100%2/2
$cfg->{"spool_dir"} // ""
l!l
5106
MC/DC100%1/1
$cfg->{"spool_dir"} // ""
$cfg->{"spool_dir"}
Truth table100%2/2
A
A= $cfg->{"spool_dir"}
Aresult
11
00
99 111 ▶ croak 'usage_store.spool_dir must be a directory path' if ref $dir;
Branch: 1 branch
BranchTrueFalseTotal
if ref $dir 1 110 111
100 110 return "$dir";
101 }
102  
103  
104 71 ▶sub for_config {
Subroutine for_config: called CC=17 SCAR=33.5
Pod documented
105 71 my ($class, $cfg) = @_;
106 71 ▶ return unless ref($cfg) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cfg eq "HASH" 0 71 71
107 71 ▶ my $backend = $cfg->{backend} // '';
Condition50%1/2
$cfg->{"backend"} // ""
l!l
710
MC/DC0%0/1
$cfg->{"backend"} // ""
$cfg->{"backend"}
Truth table50%1/2
A
A= $cfg->{"backend"}
Aresult
11
00
108 71 ▶ return unless length $backend;
Branch: 1 branch
BranchTrueFalseTotal
unless length $backend 0 71 71
109  
110 71 ▶ if ($backend eq 'jsonlog') {
Branch: 1 branch
BranchTrueFalseTotal
if ($backend eq "jsonlog") 38 33 71
111 38 require Langertha::Skeid::UsageStore::JsonLog;
112 return Langertha::Skeid::UsageStore::JsonLog->new(
113 path => ($cfg->{path} // ''),
114 mode => ($cfg->{mode} // 'dir'),
115 38 ▶ fsync => ($cfg->{fsync} ? 1 : 0),
Branch: 1 branch
BranchTrueFalseTotal
$cfg->{'fsync'} ? : 1 37 38
Condition50%2/4
$cfg->{"path"} // ""
l!l
380
$cfg->{"mode"} // "dir"
l!l
380
MC/DC0%0/1
$cfg->{"path"} // ""
$cfg->{"path"}
MC/DC0%0/1
$cfg->{"mode"} // "dir"
$cfg->{"mode"}
Truth table50%1/2
A
A= $cfg->{"path"}
Aresult
11
00
Truth table50%1/2
A
A= $cfg->{"mode"}
Aresult
11
00
116 );
117 }
118  
119 33 require Langertha::Skeid::UsageStore::DBI;
120 return Langertha::Skeid::UsageStore::DBI->new(
121 backend => $backend,
122 dsn => ($cfg->{dsn} // ''),
123 user => ($cfg->{user} // ''),
124 password => ($cfg->{password} // ''),
125 path => ($cfg->{path} // ''),
126 schema_file => ($cfg->{schema_file} // ''),
127 auto_migrate => (exists($cfg->{auto_migrate}) ? ($cfg->{auto_migrate} ? 1 : 0) : 1),
128 flush_interval_ms => ($cfg->{flush_interval_ms} // 0),
129 33 ▶ spool_dir => ($cfg->{spool_dir} // q{}),
Branch: 2 branches
BranchTrueFalseTotal
exists $cfg->{'auto_migrate'} ? : 33 0 33
$cfg->{'auto_migrate'} ? : 33 0 33
Condition50%7/14
$cfg->{"dsn"} // ""
l!l
330
$cfg->{"user"} // ""
l!l
330
$cfg->{"password"} // ""
l!l
330
$cfg->{"path"} // ""
l!l
330
$cfg->{"schema_file"} // ""
l!l
330
$cfg->{"flush_interval_ms"} // 0
l!l
330
$cfg->{"spool_dir"} // ""
l!l
330
MC/DC0%0/1
$cfg->{"dsn"} // ""
$cfg->{"dsn"}
MC/DC0%0/1
$cfg->{"user"} // ""
$cfg->{"user"}
MC/DC0%0/1
$cfg->{"password"} // ""
$cfg->{"password"}
MC/DC0%0/1
$cfg->{"path"} // ""
$cfg->{"path"}
MC/DC0%0/1
$cfg->{"schema_file"} // ""
$cfg->{"schema_file"}
MC/DC0%0/1
$cfg->{"flush_interval_ms"} // 0
$cfg->{"flush_interval_ms"}
MC/DC0%0/1
$cfg->{"spool_dir"} // ""
$cfg->{"spool_dir"}
Truth table50%1/2
A
A= $cfg->{"dsn"}
Aresult
11
00
Truth table50%1/2
A
A= $cfg->{"user"}
Aresult
11
00
Truth table50%1/2
A
A= $cfg->{"password"}
Aresult
11
00
Truth table50%1/2
A
A= $cfg->{"path"}
Aresult
11
00
Truth table50%1/2
A
A= $cfg->{"schema_file"}
Aresult
11
00
Truth table50%1/2
A
A= $cfg->{"flush_interval_ms"}
Aresult
11
00
Truth table50%1/2
A
A= $cfg->{"spool_dir"}
Aresult
11
00
130 );
131 }
132  
133  
134 3644 ▶sub num {
Subroutine num: called CC=2 SCAR=6.9
Pod documented
135 3644 my ($v) = @_;
136 3644 ▶ return 0 unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 721 2923 3644
137 2923 return 0 + $v;
138 }
139  
140  
141 134 ▶sub read_text_file {
Subroutine read_text_file: called CC=2 SCAR=7.0
Pod documented
142 134 my ($path) = @_;
143 134 ▶ open my $fh, '<', $path or die "Cannot open $path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or die "Cannot open ${path}: $!" 134 0 134
144 134 local $/;
145 134 my $text = <$fh>;
146 134 close $fh;
147 134 return $text;
148 }
149  
150  
151 115 1;
152  
153 __END__
« bin/skeid ↑ Summary lib/Langertha/Skeid/Protocol/Anthropic.pm »