lib/InfluxDB/Writer/CompactFiles.pm

filter: Statementstmt 50.7% 36 / 71 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 10 / 12 Podpod 0.0% 0 / 2 totaltotal 43.8% 46 / 105 CC 11 SCAR 39.3
CC
11
Cov
40%
run
110.0
add_tags_to_line
6.0
BEGIN
1.0
CRAP 51.0
« lib/InfluxDB/Writer/SendLines.pm ↑ Summary lib/InfluxDB/Writer/RememberingFileTailer.pm »
1 package InfluxDB::Writer::CompactFiles;
2  
3 # ABSTRACT: Collect and compact files containing InfluxDB lines
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 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Sys::Hostname qw(hostname);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use InfluxDB::LineProtocol qw(line2data data2line);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Time::Moment;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 has 'dir' => ( is => 'ro', isa => 'Str', required => 1 );
19 1 has 'tags' => ( is => 'ro', isa => 'HashRef', predicate => 'has_tags' );
20 1 has 'delete' => ( is => 'ro', isa => 'Bool', default => 1 );
21  
22 0 ▶sub run {
Subroutine run: not called CC=10 SCAR=47.0
Pod undocumented
23 0 my $self = shift;
24  
25 0 ▶ unless ( -d $self->dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->dir) 0 0 0
26 0 croak "Not a directory: " . $self->dir;
27 }
28  
29 0 my $now = Time::Moment->now->to_string;
30 0 my $outfile = join('_',hostname(),'stats',$now) . '.compacted';
31  
32 0 my $target = catfile( $self->dir, $outfile );
33 0 ▶ open( my $out, ">>", $target ) || die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">>", $target or die $! 0 0 0
34 0 $log->infof( "Starting %s of directory %s into %s", __PACKAGE__,
35 $self->dir, $target );
36  
37 0 opendir( my $dh, $self->dir );
38 0 ▶ while ( my $file = readdir($dh) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = readdir $dh)) 0 0 0
39 0 ▶ next unless $file =~ /\.stats$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\.stats$/ 0 0 0
40 0 $file =~ /(\d+)\.stats/;
41 0 my $pid = $1;
42 0 my $is_running = kill 0, $pid;
43 0 ▶ if ($is_running) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_running) { } 0 0 0
44 0 $log->debugf( "Skip file %s because pid %i is still running",
45 $file, $pid );
46 0 next;
47 }
48 else {
49 0 $log->infof( "Append file %s to %s", $file, $outfile );
50 0 my $source = catfile( $self->dir, $file );
51 0 open( my $fh, "<", $source );
52 0 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
53 0 ▶ if ( $self->has_tags ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_tags) 0 0 0
54 0 $line = $self->add_tags_to_line($line);
55 }
56 0 say $out $line;
57 }
58 0 ▶ if ( $self->delete ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->delete) 0 0 0
59 0 ▶ unlink($source) || die "$!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $source or die "$!" 0 0 0
60 }
61 }
62 }
63 0 system('gzip', $target);
64 }
65  
66 0 ▶sub add_tags_to_line {
Subroutine add_tags_to_line: not called CC=2 SCAR=17.9
Pod undocumented
67 0 my ( $self, $line ) = @_;
68  
69 0 my ( $measurement, $values, $tags, $timestamp ) = line2data($line);
70 0 my $combined_tags;
71 0 ▶ if ($tags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tags) { } 0 0 0
72 0 $combined_tags = { %$tags, %{ $self->tags } };
73 }
74 else {
75 0 $combined_tags = $tags;
76 }
77 0 return data2line( $measurement, $values, $combined_tags, $timestamp );
78 }
79  
80 1 __PACKAGE__->meta->make_immutable;
81 1 1;
82  
83 __END__
« lib/InfluxDB/Writer/SendLines.pm ↑ Summary lib/InfluxDB/Writer/RememberingFileTailer.pm »