lib/InfluxDB/LineProtocol.pm

filter: Statementstmt 97.5% 201 / 206 Branchbran 80.0% 48 / 60 Conditioncond 83.3% 10 / 12 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 23 / 23 Podpod 25.0% 2 / 8 totaltotal 91.1% 289 / 317 CC 45 SCAR 21.7
CC
45
Cov
93%
_data2line_0_9_2
17.4
data2line
12.5
import
9.0
CRAP 8.7
↑ Summary
1 package InfluxDB::LineProtocol;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: Write and read InfluxDB LineProtocol
6  
7 5 our $VERSION = '1.015'; # VERSION
8  
9 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Time::HiRes qw(gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 my %versions = (
13 'v0.9.2' => '_0_9_2',
14 );
15  
16 10 ▶sub import {
Subroutine import: called CC=9 SCAR=22.0
17 10 my $class = shift;
18 10 my $caller = caller();
19  
20 10 my @to_export;
21 my $version;
22 10 my $precision = 'ns';
23 10 foreach my $param (@_) {
24 20 ▶ if ($param eq 'data2line' || $param eq 'line2data') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "data2line" or $param eq "line2data") 13 7 20
Condition100%3/3
$param eq "data2line" or $param eq "line2data"
l!l&&r!l&&!r
1037
MC/DC100%2/2
$param eq "data2line" or $param eq "line2data"
$param eq "data2line" $param eq "line2data"
Truth table100%3/3
A or B
A= $param eq "data2line"B= $param eq "line2data"
ABresult
1X1
011
000
25 13 push(@to_export,$param);
26 }
27 20 ▶ if ($param =~ /^precision=(\w+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^precision=(\w+)$/) 6 14 20
28 6 $precision = $1;
29 }
30 20 ▶ if ($param =~ /^v[\d\.]+$/ && $versions{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^v[\d\.]+$/ and $versions{$param}) 1 19 20
Condition66%2/3
$param =~ /^v[\d\.]+$/ and $versions{$param}
!ll&&!rl&&r
1901
MC/DC50%1/2
$param =~ /^v[\d\.]+$/ and $versions{$param}
$param =~ /^v[\d\.]+$/ $versions{$param}
Truth table66%2/3
A and B
A= $param =~ /^v[\d\.]+$/B= $versions{$param}
ABresult
0X0
100
111
31 1 $version = $versions{$param};
32 }
33 }
34  
35 10 foreach my $function (@to_export) {
36 13 my $target = $function;
37 13 ▶ $function = '_'.$function.$version if $version;
Branch: 1 branch
BranchTrueFalseTotal
if $version 2 11 13
38  
39 {
40 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 13 *{"$caller\::$target"} = \&$function;
42 }
43 }
44  
45 # set up ts_$precision
46 {
47 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 10 my $selected = 'ts_'.$precision;
49 10 *{"$caller\::get_ts"} = \&$selected;
50 }
51  
52 }
53  
54 40 ▶sub _format_key {
Subroutine _format_key: called CC=1 SCAR=0.0
55 40 my $k = shift;
56  
57 40 $k =~ s/([, ])/\\$1/g;
58  
59 40 return $k;
60 }
61  
62 49 ▶sub _format_measurement {
Subroutine _format_measurement: called CC=1 SCAR=0.0
63 49 my $measurement = shift;
64  
65 49 $measurement =~ s/([, ])/\\$1/g;
66 49 return $measurement;
67 }
68  
69 18 ▶sub _format_tag_key {
Subroutine _format_tag_key: called CC=1 SCAR=0.0
70 18 my $key = shift;
71  
72 18 $key =~ s/([, =])/\\$1/g;
73 18 return $key;
74 }
75  
76 18 ▶sub _format_tag_value {
Subroutine _format_tag_value: called CC=1 SCAR=0.0
77 18 my $value = shift;
78  
79 18 $value =~ s/([, =])/\\$1/g;
80 18 return $value;
81 }
82  
83 56 ▶sub _format_field_key {
Subroutine _format_field_key: called CC=1 SCAR=0.0
84 56 my $k = shift;
85  
86 56 $k =~ s/([, =])/\\$1/g;
87  
88 56 return $k;
89 }
90  
91 56 ▶sub _format_field_value {
Subroutine _format_field_value: called CC=5 SCAR=16.1
92 56 my $v = shift;
93  
94 56 ▶ if ( $v =~ /^(-?\d+)(?:i?)$/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($v =~ /^(-?\d+)(?:i?)$/) { } 32 24 56
elsif ($v =~ /^[Ff](?:ALSE|alse)?$/) { } 3 21 24
elsif ($v =~ /^[Tt](?:RUE|rue)?$/) { } 2 19 21
elsif ($v =~ /^-?\d+(?:\.\d+)?(?:e(?:-|\+)?\d+)?$/) { } 6 13 19
95 32 $v = $1 . 'i';
96 }
97 elsif ( $v =~ /^[Ff](?:ALSE|alse)?$/ ) {
98 3 $v = 'FALSE';
99 }
100 elsif ( $v =~ /^[Tt](?:RUE|rue)?$/ ) {
101 2 $v = 'TRUE';
102 }
103 elsif ( $v =~ /^-?\d+(?:\.\d+)?(?:e(?:-|\+)?\d+)?$/ ) {
104 # pass it on, no mod
105 }
106 else {
107 # string actually, but this should be quoted differently?
108 13 $v =~ s/(["\\])/\\$1/g;
109 13 $v = '"' . $v . '"';
110 }
111  
112 56 return $v;
113 }
114  
115 49 ▶sub data2line {
Subroutine data2line: called CC=12 SCAR=25.3
Pod documented
116 49 my ( $measurement, $values, $tags, $timestamp ) = @_;
117 49 my $caller = caller();
118  
119 49 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 0 49 49
120 # no $fields, so assume we already got a line
121 0 return $measurement;
122 }
123  
124 49 my $key = _format_measurement($measurement);
125  
126 # $tags has to be a hashref, if it's not, we don't have tags, so it's the timestamp
127 49 ▶ if ( defined $tags ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tags) 16 33 49
128 16 ▶ if ( ref($tags) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $tags eq 'HASH') { } 13 3 16
elsif (not ref $tags) { } 3 0 3
129 13 my @tags;
130 13 foreach my $k ( sort keys %$tags )
131 { # Influx wants the tags presorted
132 # TODO check if sorting algorithm matches
133 # http://golang.org/pkg/bytes/#Compare
134 18 my $v = $tags->{$k};
135 18 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 18 18
136  
137 18 my $esc_k = _format_tag_key($k);
138 18 my $esc_v = _format_tag_value($v);
139  
140 18 push( @tags, $esc_k . '=' . $esc_v );
141 }
142 13 ▶ $key .= join( ',', '', @tags ) if @tags;
Branch: 1 branch
BranchTrueFalseTotal
if @tags 13 0 13
143 }
144 elsif ( !ref($tags) ) {
145 3 $timestamp = $tags;
146 }
147 }
148  
149 49 ▶ $timestamp ||= $caller->get_ts();
Condition66%2/3
$timestamp ||= $caller->get_ts
l!l&&r!l&&!r
6430
MC/DC0%0/2
$timestamp ||= $caller->get_ts
$timestamp $caller->get_ts
Truth table66%2/3
A ||= B
A= $timestampB= $caller->get_ts
ABresult
1X1
011
000
150 49 ▶ croak("$timestamp does not look like an epoch timestamp")
Branch: 1 branch
BranchTrueFalseTotal
unless $timestamp =~ /^\d+$/ 0 49 49
151 unless $timestamp =~ /^\d+$/;
152  
153 # If values is not a hashref, convert it into one
154 49 ▶ $values = { value => $values } if (not ref($values));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $values 35 14 49
155  
156 49 my @fields;
157 49 foreach my $k ( sort keys %$values ) {
158 56 my $v = $values->{$k};
159  
160 56 my $esc_k = _format_field_key($k);
161 56 my $esc_v = _format_field_value($v);
162  
163 56 push( @fields, $esc_k . '=' . $esc_v );
164 }
165 49 my $fields = join( ',', @fields );
166  
167 49 return sprintf( "%s %s %s", $key, $fields, $timestamp );
168 }
169  
170 2 ▶sub ts_h {
Subroutine ts_h: called CC=1 SCAR=0.0
Pod undocumented
171 2 my $now = time();
172 2 return int $now / 3600;
173 }
174  
175 2 ▶sub ts_m {
Subroutine ts_m: called CC=1 SCAR=0.0
Pod undocumented
176 2 my $now = time();
177 2 return int $now / 60;
178 }
179  
180 2 ▶sub ts_s {
Subroutine ts_s: called CC=1 SCAR=0.0
Pod undocumented
181 2 return scalar time();
182 }
183  
184 2 ▶sub ts_ms {
Subroutine ts_ms: called CC=1 SCAR=0.0
Pod undocumented
185 2 my ($s,$us) = gettimeofday();
186 2 return sprintf("%s%03d", $s,substr($us,0,3));
187 }
188  
189 2 ▶sub ts_us {
Subroutine ts_us: called CC=1 SCAR=0.0
Pod undocumented
190 2 return sprintf("%s%06d", gettimeofday());
191 }
192  
193 39 ▶sub ts_ns {
Subroutine ts_ns: called CC=1 SCAR=0.0
Pod undocumented
194 39 return sprintf("%s%06d000", gettimeofday());
195 }
196  
197 42 ▶sub line2data {
Subroutine line2data: called CC=4 SCAR=13.9
Pod documented
198 42 my $line = shift;
199 42 chomp($line);
200  
201 42 $line =~ s/\\ /ESCAPEDSPACE/g;
202 42 $line =~ s/\\,/ESCAPEDCOMMA/g;
203 42 $line =~ s/\\"/ESCAPEDDBLQUOTE/g;
204 42 $line =~ s/\\\\/ESCAPEDBACKSLASH/g;
205 42 $line =~ s/\\=/ESCAPEDEQUALS/g;
206  
207 42 $line=~/^(.*?) (.*) (.*)$/;
208 42 my ($key, $fields, $timestamp) = ( $1, $2, $3);
209  
210 42 my ( $measurement, @taglist ) = split( /,/, $key );
211 42 $measurement =~ s/ESCAPEDSPACE/ /g;
212 42 $measurement =~ s/ESCAPEDCOMMA/,/g;
213  
214 42 my $tags;
215 42 foreach my $tagset (@taglist) {
216 18 $tagset =~ s/ESCAPEDSPACE/ /g;
217 18 $tagset =~ s/ESCAPEDCOMMA/,/g;
218 18 my ( $k, $v ) = split( /=/, $tagset );
219 18 $k =~ s/ESCAPEDEQUALS/=/g;
220 18 $v =~ s/ESCAPEDEQUALS/=/g;
221 18 $tags->{$k} = $v;
222 }
223  
224 42 my $values;
225 my @strings;
226 42 ▶ if ($fields =~ /"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields =~ /"/) 11 31 42
227 11 my $cnt=0;
228 11 $fields=~s/"(.*?)"/push(@strings, $1); 'ESCAPEDSTRING_'.$cnt++;/ge;
229 }
230 42 foreach my $valset ( split( /,/, $fields ) ) {
231 49 $valset =~ s/ESCAPEDSPACE/ /g;
232 49 $valset =~ s/ESCAPEDCOMMA/,/g;
233 49 my ( $k, $v ) = split( /=/, $valset );
234 49 $v =~ s/ESCAPEDSTRING_(\d+)/$strings[$1]/ge;
235 49 $v =~ s/ESCAPEDDBLQUOTE/"/g;
236 49 $v =~ s/ESCAPEDBACKSLASH/\\/g;
237 49 $v =~ s/^(-?\d+)i$/$1/;
238 49 $k =~ s/ESCAPEDBACKSLASH/\\\\/g;
239 49 $k =~ s/ESCAPEDEQUALS/=/g;
240 49 $values->{$k} = $v;
241 }
242  
243 42 return ( $measurement, $values, $tags, $timestamp );
244 }
245  
246 34 ▶sub _data2line_0_9_2 {
Subroutine _data2line_0_9_2: called CC=16 SCAR=28.6
247 34 my ( $measurement, $values, $tags, $timestamp ) = @_;
248  
249 34 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 0 34 34
250 # no $fields, so assume we already got a line
251 0 return $measurement;
252 }
253  
254 34 my $key = $measurement;
255 34 $key =~ s/([, ])/\\$1/g;
256  
257 # $tags has to be a hashref, if it's not, we don't have tags, so it's the timestamp
258 34 ▶ if ( defined $tags ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tags) 12 22 34
259 12 ▶ if ( ref($tags) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $tags eq 'HASH') { } 9 3 12
elsif (not ref $tags) { } 3 0 3
260 9 my @tags;
261 9 foreach my $k ( sort keys %$tags )
262 { # Influx wants the tags presorted
263 # TODO check if sorting algorithm matches
264 # http://golang.org/pkg/bytes/#Compare
265 13 my $v = $tags->{$k};
266 13 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 13 13
267 13 $k =~ s/([, ])/\\$1/g;
268 13 $v =~ s/([, ])/\\$1/g;
269 13 push( @tags, $k . '=' . $v );
270 }
271 9 ▶ $key .= join( ',', '', @tags ) if @tags;
Branch: 1 branch
BranchTrueFalseTotal
if @tags 9 0 9
272 }
273 elsif ( !ref($tags) ) {
274 3 $timestamp = $tags;
275 }
276 }
277  
278 34 ▶ if ($timestamp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timestamp) { } 6 28 34
279 6 ▶ croak("$timestamp does not look like an epoch timestamp")
Branch: 1 branch
BranchTrueFalseTotal
unless $timestamp =~ /^\d+$/ 0 6 6
280 unless $timestamp =~ /^\d+$/;
281 6 ▶ if ( length($timestamp) < 19 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $timestamp < 19) 0 6 6
282 0 my $missing = 19 - length($timestamp);
283 0 my $zeros = 0 x $missing;
284 0 $timestamp .= $zeros;
285 }
286 }
287 else {
288 28 $timestamp = join( '', gettimeofday(), '000' );
289 28 ▶ $timestamp .= '0' if length($timestamp) < 19;
Branch: 1 branch
BranchTrueFalseTotal
if length $timestamp < 19 0 28 28
290 }
291  
292 # If values is not a hashref, convert it into one
293 34 ▶ $values = { value => $values } if (not ref($values));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $values 25 9 34
294  
295 34 my @fields;
296 34 foreach my $k ( sort keys %$values ) {
297 40 my $v = $values->{$k};
298 40 my $esc_k = _format_key($k);
299  
300 40 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless ($v =~ /^-?\d+(?:\.\d+)?(?:e-?\d+)?$/ or $v =~ /^(?:F(?:ALSE)?|f(?:alse)?|T(?:RUE)?|t(?:rue)?)$/) 13 27 40
Condition100%3/3
$v =~ /^-?\d+(?:\.\d+)?(?:e-?\d+)?$/ or $v =~ /^(?:F(?:ALSE)?|f(?:alse)?|T(?:RUE)?|t(?:rue)?)$/
l!l&&r!l&&!r
24313
MC/DC100%2/2
$v =~ /^-?\d+(?:\.\d+)?(?:e-?\d+)?$/ or $v =~ /^(?:F(?:ALSE)?|f(?:alse)?|T(?:RUE)?|t(?:rue)?)$/
$v =~ /^-?\d+(?:\.\d+)?(?:e-?\d+)?$/ $v =~ /^(?:F(?:ALSE)?|f(?:alse)?|T(?:RUE)?|t(?:rue)?)$/
Truth table100%3/3
A or B
A= $v =~ /^-?\d+(?:\.\d+)?(?:e-?\d+)?$/B= $v =~ /^(?:F(?:ALSE)?|f(?:alse)?|T(?:RUE)?|t(?:rue)?)$/
ABresult
1X1
011
000
301 # positive & negativ ints, exponentials, use Regexp::Common?
302 $v !~ /^-?\d+(?:\.\d+)?(?:e-?\d+)?$/
303 &&
304 # perl 5.12 Regexp::Assemble->new->add(qw(t T true TRUE f F false FALSE))->re;
305 $v !~ /^(?:F(?:ALSE)?|f(?:alse)?|T(?:RUE)?|t(?:rue)?)$/
306 )
307 {
308 13 $v =~ s/"/\\"/g;
309 13 $v = '"' . $v . '"';
310 }
311 40 push( @fields, $esc_k . '=' . $v );
312 }
313 34 my $fields = join( ',', @fields );
314  
315 34 return sprintf( "%s %s %s", $key, $fields, $timestamp );
316 }
317  
318 34 ▶sub _line2data_0_9_2 {
Subroutine _line2data_0_9_2: called CC=4 SCAR=13.9
319 34 my $line = shift;
320 34 chomp($line);
321  
322 34 $line =~ s/\\ /ESCAPEDSPACE/g;
323 34 $line =~ s/\\,/ESCAPEDCOMMA/g;
324 34 $line =~ s/\\"/ESCAPEDDBLQUOTE/g;
325  
326 34 $line=~/^(.*?) (.*) (.*)$/;
327 34 my ($key, $fields, $timestamp) = ( $1, $2, $3);
328  
329 34 my ( $measurement, @taglist ) = split( /,/, $key );
330 34 $measurement =~ s/ESCAPEDSPACE/ /g;
331 34 $measurement =~ s/ESCAPEDCOMMA/,/g;
332  
333 34 my $tags;
334 34 foreach my $tagset (@taglist) {
335 13 $tagset =~ s/ESCAPEDSPACE/ /g;
336 13 $tagset =~ s/ESCAPEDCOMMA/,/g;
337 13 my ( $k, $v ) = split( /=/, $tagset );
338 13 $tags->{$k} = $v;
339 }
340  
341 34 my $values;
342 my @strings;
343 34 ▶ if ($fields =~ /"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields =~ /"/) 11 23 34
344 11 my $cnt=0;
345 11 $fields=~s/"(.*?)"/push(@strings, $1); 'ESCAPEDSTRING_'.$cnt++;/ge;
346 }
347 34 foreach my $valset ( split( /,/, $fields ) ) {
348 40 $valset =~ s/ESCAPEDSPACE/ /g;
349 40 $valset =~ s/ESCAPEDCOMMA/,/g;
350 40 my ( $k, $v ) = split( /=/, $valset );
351 40 $v =~ s/ESCAPEDSTRING_(\d+)/$strings[$1]/ge;
352 40 $v =~ s/ESCAPEDDBLQUOTE/"/g;
353 40 $values->{$k} = $v;
354 }
355  
356 34 return ( $measurement, $values, $tags, $timestamp );
357 }
358  
359 5 1;
360  
361 __END__
↑ Summary