| 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
MC/DC0%0/1
$ENV{"QUOTE_TELEGRAF"} // 0
$ENV{"QUOTE_TELEGRAF"}
Truth table50%1/2
A
A= $ENV{"QUOTE_TELEGRAF"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 30 | 7 | ▶ | next if $val eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 44 | 10 | ▶ | next if $val eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 10 | my $type; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 10 | ▶ | if(ref $val) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 5 | ▶ | die "Type missing or invalid for field '$key'" unless exists $val->{type} && $val->{type} =~ /^[fisbu]\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $val->{"type"} and $val->{"type"} =~ /^[fisbu]\z/u
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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$type eq "u" and $val < 0
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
| |||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | else { | ||||||||||||||||||||||||||||
| 54 | match($val : =~) { | ||||||||||||||||||||||||||||
| 55 | case(/^-?[0-9]+\.[0-9]+$/) { | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | $type = 'f'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | case(/^-?[0-9]+$/) { | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | $type = 'i'; | ||||||||||||||||||||||||||
|
Condition33%1/3
$(Syntax::Keyword::Match/topic) =~ /\w+/u and do {
$type = "s"
}
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"
}
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 5 | ▶ | case(/\w+/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | case('i') { | ||||||||||||||||||||||||||||
| 72 | 1 | ▶ | push @r, sprintf(qq(%s=%di), $key =~ s{$qr}{\Q$1\E}gr, $val); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | case('u') { | ||||||||||||||||||||||||||||
| 75 | 1 | ▶ | push @r, sprintf(qq(%s=%du), $key =~ s{$qr}{\Q$1\E}gr, $val); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$utc //= 0
$utc
Truth table0%0/2
A
A= $utc
| |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | $unit //= ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$unit //= ""
MC/DC0%0/1
$unit //= ""
$unit
Truth table0%0/2
A
A= $unit
| |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | my $now = $utc ? Time::Moment->now_utc : Time::Moment->now; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | return sprintf("%d", sprintf("%d%d", $now->epoch, $unit eq 'ns' ? $now->nanosecond | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 98 | : $unit eq 'us' ? $now->microsecond | ||||||||||||||||||||||||||||
| 99 | : $unit eq 'ms' ? $now->millisecond | ||||||||||||||||||||||||||||
| 100 | : $now->nanosecond)); | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 2 | 1; | |||||||||||||||||||||||||||
| 104 | __END__ | ||||||||||||||||||||||||||||