| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::RecordStream::Operation::substream; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 780 | use strict; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 44 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 2 |  |  | 2 |  | 6 | use base qw(App::RecordStream::Operation); | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 125 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 2 |  |  | 2 |  | 9 | use App::RecordStream::Executor; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 597 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | sub init { | 
| 10 | 5 |  |  | 5 | 0 | 6 | my $this = shift; | 
| 11 | 5 |  |  |  |  | 6 | my $args = shift; | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 5 |  |  |  |  | 6 | 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 |  |  |  |  | 15 | $this->parse_options($args, $spec); | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 5 | 100 |  |  |  | 19 | $this->{'BEGIN_EXECUTOR'} = App::RecordStream::Executor->new($begin) if defined $begin; | 
| 24 | 5 | 100 |  |  |  | 26 | $this->{'END_EXECUTOR'}   = App::RecordStream::Executor->new($end)   if defined $end; | 
| 25 |  |  |  |  |  |  | } | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | sub accept_record { | 
| 28 | 33 |  |  | 33 | 0 | 33 | my $this   = shift; | 
| 29 | 33 |  |  |  |  | 31 | my $record = shift; | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 33 | 100 |  |  |  | 47 | if ( ! $this->{'IN_SUBSTREAM'} ) { | 
| 32 | 8 |  |  |  |  | 9 | my $executor = $this->{'BEGIN_EXECUTOR'}; | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 8 | 100 | 100 |  |  | 20 | if ( (! defined $executor) || $executor->execute_code($record) ) { | 
| 35 | 5 |  |  |  |  | 8 | $this->{'IN_SUBSTREAM'} = 1; | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 33 | 100 |  |  |  | 43 | if ( $this->{'IN_SUBSTREAM'} ) { | 
| 40 | 30 |  |  |  |  | 30 | my $executor = $this->{'END_EXECUTOR'}; | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 30 |  |  |  |  | 62 | $this->push_record($record); | 
| 43 | 30 |  |  |  |  | 32 | $this->{'SEEN_RECORD'} = 1; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 30 | 100 | 100 |  |  | 50 | if ( (defined $executor) && $executor->execute_code($record) ) { | 
| 46 | 3 |  |  |  |  | 3 | $this->{'IN_SUBSTREAM'} = 0; | 
| 47 | 3 |  |  |  |  | 8 | return 0; # terminate early to avoid reading the rest of the stream | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 30 |  |  |  |  | 75 | return 1; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | sub stream_done { | 
| 55 | 5 |  |  | 5 | 0 | 6 | my $this = shift; | 
| 56 | 5 | 50 |  |  |  | 10 | $this->_set_exit_value(1) unless ( $this->{'SEEN_RECORD'} ); | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | sub add_help_types { | 
| 60 | 5 |  |  | 5 | 0 | 7 | my $this = shift; | 
| 61 | 5 |  |  |  |  | 10 | $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; |