blib/lib/App/RecordStream/Operation/topn.pm

filter: Statementstmt 100% 47 / 47 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 5 totaltotal 91.0% 61 / 67 CC 5 SCAR 7.3
CC
5
Cov
98%
accept_record
4.0
init
2.0
BEGIN
1.0
CRAP 2.1
« blib/lib/App/RecordStream/Aggregator/RecordForMinimum.pm ↑ Summary blib/lib/App/RecordStream/Aggregator/Internal/ForField.pm »
1 package App::RecordStream::Operation::topn;
2  
3 3 our $VERSION = "4.0.25";
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use base qw(App::RecordStream::Operation);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶sub init {
Subroutine init: called CC=2 SCAR=6.9
Pod undocumented
11 4 my $this = shift;
12 4 my $args = shift;
13  
14  
15 4 my $top = 10;
16 4 my $value_delimiter = "9t%7Oz%]";
17 4 my $key_groups = App::RecordStream::KeyGroups->new();
18  
19 my $spec = {
20 3 ▶ "key|k=s" => sub { $key_groups->add_groups($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 4 "topn|n=i" => \$top,
22 "delimiter=s" => \$value_delimiter,
23 };
24  
25 4 $this->parse_options($args, $spec);
26  
27 3 ▶ die "Must at least specify --topn <value>" unless $top;
Branch: 1 branch
BranchTrueFalseTotal
unless $top 0 3 3
28  
29 3 $this->{'KEY_GROUPS'} = $key_groups;
30 3 $this->{'NUM'} = $top;
31 $this->{'DELIM'} = $value_delimiter,
32  
33 3 $this->{'PRIOR_KEY_VALUES'} = "";
34 }
35  
36 3 ▶sub init_keys {
Subroutine init_keys: called CC=1 SCAR=0.0
Pod undocumented
37 3 my $this = shift;
38 3 my $record = shift;
39  
40 3 $this->{'KEYS'} = $this->{'KEY_GROUPS'}->get_keyspecs($record);
41 }
42  
43 15 ▶sub accept_record {
Subroutine accept_record: called CC=4 SCAR=13.9
Pod undocumented
44 15 my $this = shift;
45 15 my $record = shift;
46  
47 15 ▶ if ( ! $this->{'KEYS'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"KEYS"}) 3 12 15
48 3 $this->init_keys($record);
49 }
50  
51 15 my $current_key_values = "";
52 15 foreach my $k ( @{$this->{'KEYS'}} ) {
53 15 $current_key_values .= ${$record->guess_key_from_spec( $k )} . $this->{'DELIM'};
54 }
55  
56 15 $this->{'NUM_SEEN'}->{$current_key_values}++;
57 15 ▶ if( $this->{'NUM_SEEN'}->{$current_key_values} <= $this->{'NUM'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"NUM_SEEN"}{$current_key_values} <= $this->{"NUM"}) 13 2 15
58 13 $this->push_record($record);
59 }
60  
61 15 return 1;
62 }
63  
64 4 ▶sub add_help_types {
Subroutine add_help_types: called CC=1 SCAR=0.0
Pod undocumented
65 4 my $this = shift;
66 4 $this->use_help_type('keyspecs');
67 4 $this->use_help_type('keygroups');
68 4 $this->use_help_type('keys');
69 }
70  
71 sub usage
72 1 ▶{
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
73 1 my $this = shift;
74  
75 1 my $options = [
76 ['key <keyspec>', 'Comma separated list of fields. May be specified multiple times. May be a keyspec or keygroup, see \'--help-keys\' for more'],
77 ['topn | -n <number>', 'Number of records to output. Default is 10.'],
78 ['delimiter <string>', 'String used internally to delimit values when performing a topn on a keyspec that inlcudeds multiple keys. This value defaults to "9t%7Oz%]" which may - under unusual and bizarre corner cases - cause false positive key matches if your values contain this value. You can set this to any string.'],
79 ];
80  
81 1 my $args_string = $this->options_string($options);
82  
83 1 return <<USAGE;
84 Usage: recs-topn <args> [<files>]
85 __FORMAT_TEXT__
86 Outputs the top n records from input stream or from <files>. You may
87 segment the input stream based on a list of keys such that unique values
88 of keys are treated as distinct input streams. This enables
89 top n listings per value groupings. The key values need not be contiguous
90 in the input record stream.
91 __FORMAT_TEXT__
92  
93 $args_string
94  
95 Examples:
96 Output just the top 5 records
97 cat records | recs-topn -n=5
98 (this is equivalent to executing "cat records | recs-grep '\$line < 5'")
99  
100 Output just 10 records for each area
101 cat records | recs-sort --key area | recs-topn -n=10 --key area
102  
103 Output the top 10 longest running queries per area and priority level
104 cat records | recs-sort --key area,priority,runtime=-n | recs-topn -n=10 --key area,priority
105 USAGE
106 }
107  
108 3 1;
« blib/lib/App/RecordStream/Aggregator/RecordForMinimum.pm ↑ Summary blib/lib/App/RecordStream/Aggregator/Internal/ForField.pm »