blib/lib/Quote/LineProtocol.pm

filter: Statementstmt 87.1% 54 / 62 Branchbran 52.6% 20 / 38 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 90.0% 9 / 10 Podpod 100% 4 / 4 totaltotal 66.6% 92 / 138 CC 26 SCAR 31.2
CC
26
Cov
69%
timestamp
56.0
fields
22.3
tags
4.1
CRAP 22.7
↑ Summary
1 package Quote::LineProtocol;
2  
3 2 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Time::Moment;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Syntax::Keyword::Match;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 our @EXPORT_OK = qw(measurement tags fields timestamp);
11  
12 2 our $VERSION = "0.1.5";
13 2 ▶our $TELEGRAF = $ENV{QUOTE_TELEGRAF} // 0; # Telegraf requires special quoting
Condition50%1/2
$ENV{"QUOTE_TELEGRAF"} // 0
l!l
02
MC/DC0%0/1
$ENV{"QUOTE_TELEGRAF"} // 0
$ENV{"QUOTE_TELEGRAF"}
Truth table50%1/2
A
A= $ENV{"QUOTE_TELEGRAF"}
Aresult
11
00
14  
15 2 my $qr = qr{([,=\s])}; # Match tag and field keys, and tag values
16 2 ▶my $qs = $TELEGRAF ? qr{(["\\,=])} : qr{(["\\,=\s])}; # Match field string values
Branch: 1 branch
BranchTrueFalseTotal
$Quote::LineProtocol::TELEGRAF ? : 0 2 2
17  
18 4 ▶sub measurement {
Subroutine measurement: called CC=1 SCAR=0.0
Pod documented
19 4 my $str = shift;
20 4 return $str =~ s{([,\s])}{\Q$1\E}gr;
21 }
22  
23 7 ▶sub tags {
Subroutine tags: called CC=4 SCAR=14.0
Pod documented
24 7 my (%tags) = @_;
25 7 my @r;
26  
27 7 for my $key (keys %tags) {
28 7 my $val = $tags{$key};
29 7 ▶ $val = "$val" if blessed($val);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($val) 0 7 7
30 7 ▶ next if $val eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "" 0 7 7
31 7 push @r, sprintf(qq(%s=%s), $key =~ s{$qr}{\Q$1\E}gr, $val =~ s{$qr}{\Q$1\E}gr);
32 }
33  
34 7 return join ',', @r;
35 }
36  
37 10 ▶sub fields {
Subroutine fields: called CC=17 SCAR=31.0
Pod documented
38 10 my (%fields) = @_;
39 10 my @r;
40  
41 10 for my $key (sort keys %fields) {
42 10 my $val = $fields{$key};
43 10 ▶ $val = "$val" if blessed($val);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($val) 0 10 10
44 10 ▶ next if $val eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "" 0 10 10
45 10 my $type;
46  
47 10 ▶ if(ref $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) { } 5 5 10
48 5 ▶ die "Type missing or invalid for field '$key'" unless exists $val->{type} && $val->{type} =~ /^[fisbu]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $val->{"type"} and $val->{"type"} =~ /^[fisbu]\z/u 0 5 5
Condition33%1/3
exists $val->{"type"} and $val->{"type"} =~ /^[fisbu]\z/u
!ll&&!rl&&r
005
MC/DC0%0/2
exists $val->{"type"} and $val->{"type"} =~ /^[fisbu]\z/u
exists $val->{"type"} $val->{"type"} =~ /^[fisbu]\z/u
Truth table33%1/3
A and B
A= exists $val->{"type"}B= $val->{"type"} =~ /^[fisbu]\z/u
ABresult
0X0
100
111
49 5 $type = $val->{type};
50 5 $val = $val->{value};
51 5 ▶ die "Type is UInt but value is negative, '$key' = '$val'" if $type eq 'u' && $val < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "u" and $val < 0 0 5 5
Condition66%2/3
$type eq "u" and $val < 0
!ll&&!rl&&r
410
MC/DC0%0/2
$type eq "u" and $val < 0
$type eq "u" $val < 0
Truth table66%2/3
A and B
A= $type eq "u"B= $val < 0
ABresult
0X0
100
111
52 }
53 else {
54 match($val : =~) {
55 case(/^-?[0-9]+\.[0-9]+$/) {
56 0 ▶ $type = 'f';
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) =~ /^-?[0-9]+$/u ? : 0 5 5
57 }
58 case(/^-?[0-9]+$/) {
59 0 ▶ $type = 'i';
Condition33%1/3
$(Syntax::Keyword::Match/topic) =~ /\w+/u and do { $type = "s" }
!ll&&!rl&&r
050
MC/DC0%0/2
$(Syntax::Keyword::Match/topic) =~ /\w+/u and do { $type = "s" }
$(Syntax::Keyword::Match/topic) =~ /\w+/u do { $type = "s" }
Truth table33%1/3
A and B
A= $(Syntax::Keyword::Match/topic) =~ /\w+/uB= do { $type = "s" }
ABresult
0X0
100
111
60 }
61 5 ▶ case(/\w+/) {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) =~ /^-?[0-9]+\.[0-9]+$/u ? : 0 5 5
62 5 $type = 's';
63 }
64 }
65 }
66  
67 match($type : eq) {
68 case('f') {
69 1 ▶ push @r, sprintf(qq(%s=%s), $key =~ s{$qr}{\Q$1\E}gr, $val);
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'i' ? : 1 8 9
70 }
71 case('i') {
72 1 ▶ push @r, sprintf(qq(%s=%di), $key =~ s{$qr}{\Q$1\E}gr, $val);
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'u' ? : 1 7 8
73 }
74 case('u') {
75 1 ▶ push @r, sprintf(qq(%s=%du), $key =~ s{$qr}{\Q$1\E}gr, $val);
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 's' ? : 6 1 7
76 }
77 case('s') {
78 6 ▶ push @r, sprintf(qq)%s="%s"), $key =~ s{$qr}{\Q$1\E}gr, $val =~ s{$qs}{\Q$1\E}gr);
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'b' ? : 1 0 1
79 }
80 case('b') {
81 1 push @r, sprintf(qq(%s=%s), $key =~ s{$qr}{\Q$1\E}gr, $val);
82 }
83 10 ▶ default {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'f' ? : 1 9 10
84 0 push @r, sprintf(qq(%s="%s"), $key =~ s{$qr}{\Q$1\E}gr, $val =~ s{$qs}{\Q$1\E}gr);
85 }
86 }
87 }
88  
89 10 return join ',', @r;
90 }
91  
92 0 ▶sub timestamp {
Subroutine timestamp: not called CC=7 SCAR=40.3
Pod documented
93 0 my ($unit, $utc) = @_;
94 0 ▶ $utc //= 0;
Condition0%0/2
$utc //= 0
l!l
00
MC/DC0%0/1
$utc //= 0
$utc
Truth table0%0/2
A
A= $utc
Aresult
11
00
95 0 ▶ $unit //= '';
Condition0%0/2
$unit //= ""
l!l
00
MC/DC0%0/1
$unit //= ""
$unit
Truth table0%0/2
A
A= $unit
Aresult
11
00
96 0 ▶ my $now = $utc ? Time::Moment->now_utc : Time::Moment->now;
Branch: 1 branch
BranchTrueFalseTotal
$utc ? : 0 0 0
97 0 ▶ return sprintf("%d", sprintf("%d%d", $now->epoch, $unit eq 'ns' ? $now->nanosecond
Branch: 3 branches
BranchTrueFalseTotal
$unit eq 'ns' ? : 0 0 0
$unit eq 'us' ? : 0 0 0
$unit eq 'ms' ? : 0 0 0
98 : $unit eq 'us' ? $now->microsecond
99 : $unit eq 'ms' ? $now->millisecond
100 : $now->nanosecond));
101 }
102  
103 2 1;
104 __END__
↑ Summary