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

filter: Statementstmt 79.2% 88 / 111 Branchbran 50.0% 6 / 12 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 92.0% 23 / 25 Podpod 0.0% 0 / 8 totaltotal 73.2% 118 / 161 CC 8 SCAR 8.9
CC
8
Cov
75%
init
7.6
print_usage
3.8
build_dlaggregator
2.0
CRAP 2.4
« blib/lib/App/RecordStream/Operation/help.pm ↑ Summary blib/lib/App/RecordStream/Operation/collate/BaseClumperCallback.pm »
1 package App::RecordStream::Operation::collate;
2  
3 4 our $VERSION = "4.0.23";
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use base qw(App::RecordStream::Operation);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use App::RecordStream::Aggregator;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use App::RecordStream::Clumper::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use App::RecordStream::DomainLanguage::Library;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use App::RecordStream::DomainLanguage::Snippet;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use App::RecordStream::DomainLanguage;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use App::RecordStream::Operation::collate::BaseClumperCallback;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use App::RecordStream::Operation;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 sub init {
19 16 ▶ my $this = shift;
Subroutine init: called CC=5 SCAR=20.3
Pod undocumented
20 16 my $args = shift;
21  
22 16 App::RecordStream::Aggregator->load_implementations();
23  
24 # clumping
25 16 my $clumper_options = $this->{'CLUMPER_OPTIONS'} = App::RecordStream::Clumper::Options->new();
26  
27 # aggregation
28 16 my @aggregators;
29 my %dlaggregators;
30 16 my @mr_aggregators;
31 16 my @ii_aggregators;
32 16 my $incremental = 0;
33 16 my $bucket = 1;
34  
35 # help
36 16 my $list_aggregators = 0;
37 16 my $aggregator = 0;
38  
39 my $spec = {
40 $clumper_options->main_options(),
41  
42 # aggregation
43 17 ▶ "aggregator|a=s" => sub { push @aggregators, $_[1]; },
Subroutine __ANON__: called CC=1 SCAR=0.0
44 1 ▶ "dlaggregator|A=s" => sub { build_dlaggregator(\%dlaggregators, $_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 16 "mr-agg=s{4}" => \@mr_aggregators,
46 "ii-agg=s{4}" => \@ii_aggregators,
47 "incremental|i" => \$incremental,
48 "bucket!" => \$bucket,
49  
50 # help
51 "list-aggregators" => \$list_aggregators,
52 "show-aggregator=s" => \$aggregator,
53 $clumper_options->help_options(),
54 "list" => \$list_aggregators,
55 };
56  
57 16 $this->parse_options($args, $spec);
58  
59 # check help first
60  
61 15 ▶ if ( $list_aggregators ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_aggregators) 0 15 15
62 0 ▶ die sub { print App::RecordStream::Aggregator->list_implementations(); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
63 }
64  
65 15 ▶ if ( $aggregator ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aggregator) 0 15 15
66 0 ▶ die sub { App::RecordStream::Aggregator->show_implementation($aggregator) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 }
68  
69 15 my $aggregator_objects = App::RecordStream::Aggregator->make_aggregators(@aggregators);
70  
71 15 $aggregator_objects = {%$aggregator_objects, %dlaggregators};
72  
73 15 ▶ for(my $i = 0; $i < @mr_aggregators; 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @mr_aggregators) 0 15 15
74 0 my $name = $mr_aggregators[$i++];
75 0 my $map_string = $mr_aggregators[$i++];
76 0 my $reduce_string = $mr_aggregators[$i++];
77 0 my $squish_string = $mr_aggregators[$i++];
78  
79 0 my $map_snippet = App::RecordStream::DomainLanguage::Snippet->new($map_string);
80 0 my $reduce_snippet = App::RecordStream::DomainLanguage::Snippet->new($reduce_string);
81 0 my $squish_snippet = App::RecordStream::DomainLanguage::Snippet->new($squish_string);
82  
83 0 $aggregator_objects->{$name} = App::RecordStream::DomainLanguage::Library::map_reduce_aggregator($map_snippet, $reduce_snippet, $squish_snippet);
84 }
85  
86 15 ▶ for(my $i = 0; $i < @ii_aggregators; 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @ii_aggregators) 0 15 15
87 0 my $name = $ii_aggregators[$i++];
88 0 my $initial_string = $ii_aggregators[$i++];
89 0 my $combine_string = $ii_aggregators[$i++];
90 0 my $squish_string = $ii_aggregators[$i++];
91  
92 0 my $initial_snippet = App::RecordStream::DomainLanguage::Snippet->new($initial_string);
93 0 my $combine_snippet = App::RecordStream::DomainLanguage::Snippet->new($combine_string);
94 0 my $squish_snippet = App::RecordStream::DomainLanguage::Snippet->new($squish_string);
95  
96 0 $aggregator_objects->{$name} = App::RecordStream::DomainLanguage::Library::inject_into_aggregator($initial_snippet, $combine_snippet, $squish_snippet);
97 }
98  
99 15 ▶ $clumper_options->check_options(App::RecordStream::Operation::collate::BaseClumperCallback->new($aggregator_objects, $incremental, $bucket, sub { $this->push_record($_[0]); }));
Subroutine __ANON__: called CC=1 SCAR=0.0
100 }
101  
102 sub build_dlaggregator {
103 1 ▶ my $dlaggregators_ref = shift;
Subroutine build_dlaggregator: called CC=2 SCAR=7.1
Pod undocumented
104 1 my $string = shift;
105  
106 1 my $name;
107 1 ▶ if($string =~ s/^([^=]*)=//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/^([^=]*)=//) { } 1 0 1
108 1 $name = $1;
109 }
110 else {
111 0 die "Bad domain language aggregator option (missing '=' to separate name and code): " . $string;
112 }
113  
114 1 $dlaggregators_ref->{$name} = App::RecordStream::DomainLanguage::Snippet->new($string)->evaluate_as('AGGREGATOR');
115 }
116  
117 sub accept_record {
118 96 ▶ my $this = shift;
Subroutine accept_record: called CC=1 SCAR=0.0
Pod undocumented
119 96 my $record = shift;
120  
121 96 $this->{'CLUMPER_OPTIONS'}->accept_record($record);
122 }
123  
124 sub stream_done {
125 14 ▶ my $this = shift;
Subroutine stream_done: called CC=1 SCAR=0.0
Pod undocumented
126  
127 14 $this->{'CLUMPER_OPTIONS'}->stream_done();
128 }
129  
130 sub print_usage {
131 1 ▶ my $this = shift;
Subroutine print_usage: called CC=3 SCAR=13.5
Pod undocumented
132 1 my $message = shift;
133  
134 1 ▶ if ( $message && UNIVERSAL::isa($message, 'CODE') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message and UNIVERSAL::isa($message, "CODE")) 0 1 1
Condition33%1/3
$message and UNIVERSAL::isa($message, "CODE")
!ll&&!rl&&r
100
MC/DC0%0/2
$message and UNIVERSAL::isa($message, "CODE")
$message UNIVERSAL::isa($message, "CODE")
Truth table33%1/3
A and B
A= $messageB= UNIVERSAL::isa($message, "CODE")
ABresult
0X0
100
111
135 0 $message->();
136 0 exit 1;
137 }
138  
139 1 $this->SUPER::print_usage($message);
140 }
141  
142 sub add_help_types {
143 16 ▶ my $this = shift;
Subroutine add_help_types: called CC=1 SCAR=0.0
Pod undocumented
144 16 $this->use_help_type('keyspecs');
145 16 $this->use_help_type('keygroups');
146 16 $this->use_help_type('keys');
147 16 $this->use_help_type('domainlanguage');
148 16 $this->use_help_type('clumping');
149 $this->add_help_type(
150 'aggregators',
151 1 ▶ sub { print App::RecordStream::Aggregator->list_implementations(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
152 16 'List the aggregators'
153 );
154 $this->add_help_type(
155 'more',
156 1 ▶ sub { $this->more_help() },
Subroutine __ANON__: called CC=1 SCAR=0.0
157 16 'Larger help documentation'
158 );
159 }
160  
161 sub usage {
162 2 ▶ my $this = shift;
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
163  
164 my $options = [
165 [ 'dlaggregator|-A ...', 'Specify a domain language aggregate. See "Domain Language Integration" below.'],
166 [ 'aggregator|-a <aggregators>', 'Colon separated list of aggregate field specifiers. See "Aggregates" section below.'],
167 [ 'mr-agg <name> <map> <reduce> <squish>', 'Specify a map reduce aggregator via 3 snippets, similar to mr_agg() from the domain language.'],
168 [ 'ii-agg <name> <initial> <combine> <squish>', 'Specify an inject into aggregator via 3 snippets, similar to ii_agg() from the domain language.'],
169 [ 'incremental', 'Output a record every time an input record is added to a clump (instead of every time a clump is flushed).'],
170 [ '[no]-bucket', 'With --bucket outputs one record per clump, with --no-bucket outputs one record for each record that went into the clump.'],
171 $this->{'CLUMPER_OPTIONS'}->main_usage(),
172  
173 [ 'list-aggregators|--list', 'Bail and output a list of aggregators' ],
174 [ 'show-aggregator <aggregator>', 'Bail and output this aggregator\'s detailed usage.'],
175 2 $this->{'CLUMPER_OPTIONS'}->help_usage(),
176 ];
177  
178 2 my $args_string = $this->options_string($options);
179  
180 return <<USAGE
181 Usage: recs-collate <args> [<files>]
182 __FORMAT_TEXT__
183 Take records, grouped togther by --keys, and compute statistics (like
184 average, count, sum, concat, etc) within those groups.
185  
186 For starting with collate, try doing single --key collates with some number
187 of aggregators (list available in --list-agrregators)
188 __FORMAT_TEXT__
189  
190 Arguments:
191 $args_string
192  
193 Examples:
194 Count clumps of adjacent lines with matching x fields.
195 recs-collate --adjacent --key x --aggregator count
196 Count number of each x field value in the entire file.
197 recs-collate --key x --aggregator count
198 Finds the maximum latency for each date, hour pair
199 recs-collate --key date,hour --aggregator worst_latency=max,latency
200 Find the median value of x+y in records
201 recs-collate --dlaggregator "m=perc(50,snip(<<{{x}}+{{y}}>>))"
202 USAGE
203 2 }
204  
205 sub more_help {
206 1 ▶ my $this = shift;
Subroutine more_help: called CC=1 SCAR=0.0
Pod undocumented
207 1 my $usage = $this->usage() . <<USAGE;
208  
209 Aggregates:
210 __FORMAT_TEXT__
211 Aggregates are specified as [<fieldname>=]<aggregator>[,<arguments>]. The
212 default field name is aggregator and arguments joined by underscores. See
213 --list-aggregators for a list of available aggregators.
214  
215 Fieldname maybe a key spec. (i.e. foo/bar=sum,field). Additionally, all key
216 name arguments to aggregators maybe be key specs (i.e.
217 foo=max,latency/url), but not key groups
218 __FORMAT_TEXT__
219  
220 Cubing:
221 __FORMAT_TEXT__
222 Instead of added one entry for each input record, we add 2 ** (number of key
223 fields), with every possible combination of fields replaced with the default
224 of "ALL". This is not meant to be used with --adjacent or --size. If our
225 key fields were x and y then we'd get output records for {x = 1, y = 2}, {x
226 = 1, y = ALL}, {x = ALL, y = 2} and {x = ALL, y = ALL}.
227 __FORMAT_TEXT__
228  
229 Domain Lanuage Integration:
230 USAGE
231 1 $usage .= App::RecordStream::DomainLanguage::short_usage() . <<USAGE;
232  
233 __FORMAT_TEXT__
234 Either aggregates or keys may be specified using the recs domain language.
235 Both --dlkey and --dlaggregator require an options of the format
236 '<name>=<domain language code>'. --dlkey requires the code evaluate as a
237 valuation, --dlaggregator requires the code evaluate as an aggregator.
238  
239 See --help-domainlanguage for a more complete description of its workings
240 and a list of available functions.
241  
242 See the examples below for a more gentle introduction.
243 __FORMAT_TEXT__
244  
245 Examples:
246 Count clumps of adjacent lines with matching x fields.
247 recs-collate --adjacent --key x --aggregator count
248 Count number of each x field in the entire file.
249 recs-collate --key x --aggregator count
250 Count number of each x field in the entire file, including an "ALL" line.
251 recs-collate --key x --aggregator count --cube
252 Produce a cummulative sum of field profit up to each date
253 recs-collate --key date --adjacent --incremental --aggregator profit_to_date=sum,profit
254 Produce record count for each date, hour pair
255 recs-collate --key date,hour --aggregator count
256 Finds the maximum latency for each date, hour pair
257 recs-collate --key date,hour --aggregator worst_latency=max,latency
258 Produce a list of hosts in each datacenter.
259 recs-collate --key dc --dlaggregator "hosts=uconcat(', ', 'host')"
260 Sum all time fields
261 recs-collate --key ... --dlaggregator 'times=for_field(qr/^t/, <<sum(\$f)>>)'
262 Find the median value of x+y in records
263 recs-collate --dlaggregator "m=perc(50,snip(<<{{x}}+{{y}}>>))"
264 Count people by first three letters of their name
265 recs-collate --dlkey "tla=<<substr({{name}},0,3)>>" -a ct
266 USAGE
267 1 print $this->format_usage($usage);
268 }
269  
270 4 1;
« blib/lib/App/RecordStream/Operation/help.pm ↑ Summary blib/lib/App/RecordStream/Operation/collate/BaseClumperCallback.pm »