File Coverage

blib/lib/App/RecordStream/Operation/topn.pm
Criterion Covered Total %
statement 41 45 91.1
branch 5 6 83.3
condition n/a
subroutine 8 9 88.8
pod 0 5 0.0
total 54 65 83.0


line stmt bran cond sub pod time code
1             package App::RecordStream::Operation::topn;
2              
3             our $VERSION = "4.0.25";
4              
5 2     2   849 use strict;
  2         4  
  2         41  
6 2     2   6 use warnings;
  2         3  
  2         49  
7              
8 2     2   8 use base qw(App::RecordStream::Operation);
  2         4  
  2         739  
9              
10             sub init {
11 3     3 0 4 my $this = shift;
12 3         4 my $args = shift;
13              
14              
15 3         4 my $top = 10;
16 3         4 my $value_delimiter = "9t%7Oz%]";
17 3         13 my $key_groups = App::RecordStream::KeyGroups->new();
18              
19             my $spec = {
20 3     3   1518 "key|k=s" => sub { $key_groups->add_groups($_[1]); },
21 3         13 "topn|n=i" => \$top,
22             "delimiter=s" => \$value_delimiter,
23             };
24              
25 3         12 $this->parse_options($args, $spec);
26              
27 3 50       6 die "Must at least specify --topn " unless $top;
28              
29 3         5 $this->{'KEY_GROUPS'} = $key_groups;
30 3         4 $this->{'NUM'} = $top;
31             $this->{'DELIM'} = $value_delimiter,
32              
33 3         23 $this->{'PRIOR_KEY_VALUES'} = "";
34             }
35              
36             sub init_keys {
37 3     3 0 4 my $this = shift;
38 3         4 my $record = shift;
39              
40 3         8 $this->{'KEYS'} = $this->{'KEY_GROUPS'}->get_keyspecs($record);
41             }
42              
43             sub accept_record {
44 15     15 0 18 my $this = shift;
45 15         12 my $record = shift;
46              
47 15 100       24 if ( ! $this->{'KEYS'} ) {
48 3         4 $this->init_keys($record);
49             }
50              
51 15         16 my $current_key_values = "";
52 15         16 foreach my $k ( @{$this->{'KEYS'}} ) {
  15         20  
53 15         16 $current_key_values .= ${$record->guess_key_from_spec( $k )} . $this->{'DELIM'};
  15         24  
54             }
55              
56 15         22 $this->{'NUM_SEEN'}->{$current_key_values}++;
57 15 100       26 if( $this->{'NUM_SEEN'}->{$current_key_values} <= $this->{'NUM'} ) {
58 13         27 $this->push_record($record);
59             }
60              
61 15         38 return 1;
62             }
63              
64             sub add_help_types {
65 3     3 0 4 my $this = shift;
66 3         10 $this->use_help_type('keyspecs');
67 3         7 $this->use_help_type('keygroups');
68 3         6 $this->use_help_type('keys');
69             }
70              
71             sub usage
72             {
73 0     0 0   my $this = shift;
74              
75 0           my $options = [
76             ['key ', '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 of records to output. Default is 10.'],
78             ['delimiter ', '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 0           my $args_string = $this->options_string($options);
82              
83 0           return <
84             Usage: recs-topn []
85             __FORMAT_TEXT__
86             Outputs the top n records from input stream or from . 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             1;