File Coverage

blib/lib/App/RecordStream/Operation/substream.pm
Criterion Covered Total %
statement 34 38 89.4
branch 13 14 92.8
condition 6 6 100.0
subroutine 7 8 87.5
pod 0 5 0.0
total 60 71 84.5


line stmt bran cond sub pod time code
1             package App::RecordStream::Operation::substream;
2              
3 2     2   708 use strict;
  2         3  
  2         53  
4              
5 2     2   8 use base qw(App::RecordStream::Operation);
  2         4  
  2         108  
6              
7 2     2   10 use App::RecordStream::Executor;
  2         4  
  2         547  
8              
9             sub init {
10 5     5 0 8 my $this = shift;
11 5         8 my $args = shift;
12              
13 5         9 my $begin;
14             my $end;
15              
16 5         11 my $spec = {
17             "begin|b=s" => \$begin,
18             "end|e=s" => \$end,
19             };
20              
21 5         19 $this->parse_options($args, $spec);
22              
23 5 100       26 $this->{'BEGIN_EXECUTOR'} = App::RecordStream::Executor->new($begin) if defined $begin;
24 5 100       29 $this->{'END_EXECUTOR'} = App::RecordStream::Executor->new($end) if defined $end;
25             }
26              
27             sub accept_record {
28 33     33 0 46 my $this = shift;
29 33         52 my $record = shift;
30              
31 33 100       58 if ( ! $this->{'IN_SUBSTREAM'} ) {
32 8         11 my $executor = $this->{'BEGIN_EXECUTOR'};
33              
34 8 100 100     24 if ( (! defined $executor) || $executor->execute_code($record) ) {
35 5         10 $this->{'IN_SUBSTREAM'} = 1;
36             }
37             }
38              
39 33 100       60 if ( $this->{'IN_SUBSTREAM'} ) {
40 30         37 my $executor = $this->{'END_EXECUTOR'};
41              
42 30         74 $this->push_record($record);
43 30         42 $this->{'SEEN_RECORD'} = 1;
44              
45 30 100 100     59 if ( (defined $executor) && $executor->execute_code($record) ) {
46 3         6 $this->{'IN_SUBSTREAM'} = 0;
47 3         8 return 0; # terminate early to avoid reading the rest of the stream
48             }
49             }
50              
51 30         88 return 1;
52             }
53              
54             sub stream_done {
55 5     5 0 7 my $this = shift;
56 5 50       45 $this->_set_exit_value(1) unless ( $this->{'SEEN_RECORD'} );
57             }
58              
59             sub add_help_types {
60 5     5 0 8 my $this = shift;
61 5         15 $this->use_help_type('snippet');
62             }
63              
64             sub usage {
65 0     0 0   my $this = shift;
66              
67 0           my $options = [
68             ['begin|b SNIP ', 'Begins outputting records when this snippet becomes true. If omitted, output starts from beginning of the stream.'],
69             ['end|e SNIP', 'Stops outputting records after this snippet becomes true. If omitted, outputs to the end of the stream.'],
70             ];
71              
72 0           my $args_string = $this->options_string($options);
73              
74 0           return <
75             Usage: recs-substream []
76             __FORMAT_TEXT__
77             Filters to a range of records delimited from when the begin snippet becomes true to when the end snippet becomes true, ie. [begin, end]. Compare to Perl's inclusive, bistable ".." range operator.
78              
79             See --help-snippet for details on snippets.
80             __FORMAT_TEXT__
81              
82             Arguments:
83             $args_string
84              
85             Examples:
86             Filter to a specific minute:
87             recs-substream -b '{{EndTime}} =~ /Thu, 07 Nov 2013 22:42/' -e 'not {{EndTime}} =~ /Thu, 07 Nov 2013 22:42/'
88             Truncate past a specific date:
89             recs-substream -e '{{EndTime}} =~ /Thu, 07 Nov/'
90             USAGE
91             }
92              
93             1;