lib/InfluxDB/Writer/SendLines.pm

filter: Statementstmt 43.7% 35 / 80 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 80.0% 8 / 10 Podpod 0.0% 0 / 2 totaltotal 31.6% 43 / 136 CC 18 SCAR 44.3
CC
18
Cov
30%
send
156.0
run
56.0
BEGIN
1.0
CRAP 84.1
↑ Summary lib/InfluxDB/Writer/CompactFiles.pm »
1 package InfluxDB::Writer::SendLines;
2  
3 # ABSTRACT: Send lines from a file to InfluxDB
4 1 our $VERSION = '1.003'; # VERSION
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Hijk ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 has 'file' => ( is => 'ro', isa => 'Str', required => 1 );
18 1 has 'influx_host' => ( is => 'ro', isa => 'Str', required => 1 );
19 1 has 'influx_port' =>
20 ( is => 'ro', isa => 'Int', default => 8086, required => 1 );
21 1 has 'influx_db' => ( is => 'ro', isa => 'Str', required => 1 );
22 1 has 'buffer_size' => ( is => 'ro', isa => 'Int', default => 1000 );
23  
24 1 with qw(InfluxDB::Writer::AuthHeaderRole);
25  
26 1 $| = 1;
27  
28 1 my @buffer;
29  
30 0 ▶sub run {
Subroutine run: not called CC=7 SCAR=40.3
Pod undocumented
31 0 my $self = shift;
32  
33 0 $log->infof( "Starting %s with file %s", __PACKAGE__, $self->file );
34  
35 0 my $f = $self->file;
36 0 my $lines = `wc -l $f`;
37 0 chomp($lines);
38 0 $lines =~ s/ .*//;
39 0 my $total = $lines;
40 0 my $start = scalar time;
41  
42 0 ▶ open( my $in, "<", $self->file ) || die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<", $self->file or die $! 0 0 0
43  
44 0 my $cnt = 0;
45 0 my $print_cnt = $self->buffer_size * 50;
46 0 ▶ while ( my $line = <$in> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $in)) 0 0 0
47 0 my $elements = push( @buffer, $line );
48 0 ▶ if ( $elements >= $self->buffer_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elements >= $self->buffer_size) 0 0 0
49 0 $self->send;
50 }
51 0 $cnt++;
52 0 ▶ if ( $cnt % $print_cnt == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt % $print_cnt == 0) 0 0 0
53 0 my $now = scalar time;
54 0 ▶ my $diff = $now - $start || 1;
Condition0%0/2
$now - $start || 1
l!l
00
MC/DC0%0/1
$now - $start || 1
$now - $start
Truth table0%0/2
A
A= $now - $start
Aresult
11
00
55 0 my $speed = $cnt / $diff;
56 0 ▶ my $estimate =
Branch: 1 branch
BranchTrueFalseTotal
$speed > 0 ? : 0 0 0
57 $speed > 0 ? ( $total - $cnt ) / $speed : 'Infinity';
58 0 printf( " % 6i/%i (%.2f/s) time left: %i sec\n",
59 $cnt, $total, $speed, $estimate );
60 }
61 }
62 0 $self->send;
63 }
64  
65 0 ▶sub send {
Subroutine send: not called CC=12 SCAR=50.5
Pod undocumented
66 0 my $self = shift;
67 0 my $second_try = shift;
68 0 my $new_buffer = shift;
69  
70 0 ▶ my $to_send = $second_try ? $new_buffer : \@buffer;
Branch: 1 branch
BranchTrueFalseTotal
$second_try ? : 0 0 0
71  
72 0 my %args;
73 0 ▶ if ( $self->_with_auth ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_with_auth) 0 0 0
74 0 $args{head} = [ "Authorization" => $self->_auth_header ];
75 }
76  
77 0 $log->debugf( "Sending %i lines to influx", scalar @$to_send );
78 0 my $res = Hijk::request(
79 { method => "POST",
80 host => $self->influx_host,
81 port => $self->influx_port,
82 path => "/write",
83 query_string => "db=" . $self->influx_db,
84 body => join( '', @$to_send ),
85 %args,
86 }
87 );
88  
89 0 ▶ if ( $res->{status} != 204 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{'status'} != 204) { } 0 0 0
90 0 ▶ if (!$second_try
Branch: 1 branch
BranchTrueFalseTotal
if (not $second_try and exists $res->{'error'} && $res->{'error'} & 3 || $res->{'status'} == 500 && $res->{'body'} =~ /timeout/) { } 0 0 0
Condition0%0/12
not $second_try and exists $res->{"error"} && $res->{"error"} & 3 || $res->{"status"} == 500 && $res->{"body"} =~ /timeout/
!ll&&!rl&&r
000
exists $res->{"error"} and $res->{"error"} & 3 or $res->{"status"} == 500 and $res->{"body"} =~ /timeout/
l!l&&r!l&&!r
000
exists $res->{"error"} and $res->{"error"} & 3
!ll&&!rl&&r
000
$res->{"status"} == 500 and $res->{"body"} =~ /timeout/
!ll&&!rl&&r
000
MC/DC0%0/5
not $second_try and exists $res->{"error"} && $res->{"error"} & 3 || $res->{"status"} == 500 && $res->{"body"} =~ /timeout/
not $second_try exists $res->{"error"} $res->{"error"} & 3 $res->{"status"} == 500 $res->{"body"} =~ /timeout/
Truth table0%0/8
not(A) and B and C or D and E
A= not $second_tryB= exists $res->{"error"}C= $res->{"error"} & 3D= $res->{"status"} == 500E= $res->{"body"} =~ /timeout/
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
91 && (
92 ( exists $res->{error}
93 && $res->{error} & Hijk::Error::TIMEOUT
94 )
95 || ( $res->{status} == 500 && $res->{body} =~ /timeout/ )
96 )
97 ) {
98 # wait a bit and try again with smaller packages
99 0 my @half = splice( @$to_send, 0, int( scalar @$to_send / 2 ) );
100 0 print ':';
101 0 $self->send( 1, \@half );
102 0 $self->send( 1, $to_send );
103 }
104 else {
105 $log->errorf(
106 "Could not send %i lines to influx: %s",
107 scalar @$to_send,
108 $res->{body}
109 0 );
110 0 ▶ open( my $fh, ">>", $self->file . '.err' ) || die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", $self->file . '.err' or die $! 0 0 0
111 0 print $fh join( '', @$to_send );
112 0 close $fh;
113 0 print 'X';
114 }
115 }
116 else { # success
117 0 ▶ print $second_try ? ',' : '.';
Branch: 1 branch
BranchTrueFalseTotal
$second_try ? : 0 0 0
118 }
119  
120 0 ▶ @$to_send = () unless $second_try;
Branch: 1 branch
BranchTrueFalseTotal
unless $second_try 0 0 0
121 }
122  
123 1 __PACKAGE__->meta->make_immutable;
124 1 1;
125  
126 __END__
↑ Summary lib/InfluxDB/Writer/CompactFiles.pm »