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

filter: Statementstmt 90.4% 160 / 177 Branchbran 81.2% 13 / 16 Conditioncond 66.6% 6 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 78.3% 29 / 37 Podpod 0.0% 0 / 6 totaltotal 83.6% 210 / 251 CC 17 SCAR 12.8
CC
17
Cov
89%
create_recorder
11.6
accept_record
3.0
init
2.0
CRAP 3.6
« blib/lib/App/RecordStream/Aggregator/Internal/ForField2.pm ↑ Summary blib/lib/App/RecordStream/Record.pm »
1 package App::RecordStream::Operation::annotate;
2  
3 2 our $VERSION = "4.0.23";
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use base qw(App::RecordStream::Operation);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use App::RecordStream::Executor::Getopt;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use App::RecordStream::Executor;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use App::RecordStream::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub init {
14 5 ▶ my $this = shift;
Subroutine init: called CC=2 SCAR=7.0
Pod undocumented
15 5 my $args = shift;
16  
17 5 my $key_groups = App::RecordStream::KeyGroups->new();
18  
19 5 my $executor_options = App::RecordStream::Executor::Getopt->new();
20 my $spec = {
21 $executor_options->arguments(),
22 4 ▶ 'keys|k=s' => sub { $key_groups->add_groups($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 5 };
24  
25 5 $this->parse_options($args, $spec, ['bundling']);
26  
27 4 my $expression = $executor_options->get_string($args);
28 4 my $executor = App::RecordStream::Executor->new(<<" CODE");
29 $expression
30 ; # Safe from a trailing comment in \$expression
31 \$r
32 CODE
33  
34 4 ▶ if ( ! $key_groups->has_any_group() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key_groups->has_any_group) 0 4 4
35 0 die "Must specify at least one --key, maybe you want recs-xform instead?\n";
36 }
37  
38 4 $this->{'EXECUTOR'} = $executor;
39 4 $this->{'KEYGROUP'} = $key_groups;
40 4 $this->{'ANNOTATIONS'} = {};
41 }
42  
43 sub accept_record {
44 19 ▶ my $this = shift;
Subroutine accept_record: called CC=3 SCAR=11.0
Pod undocumented
45 19 my $record = shift;
46  
47 19 my $specs = $this->{'KEYGROUP'}->get_keyspecs_for_record($record);
48  
49 19 my @values;
50 19 foreach my $key (sort @$specs) {
51 19 my $value = ${$record->guess_key_from_spec($key)};
52 19 push @values, $value;
53 }
54  
55 # Join keys with the ASCII record separator character (30)
56 19 my $synthetic_key = join(chr(30), @values);
57  
58 19 ▶ if ( exists $this->{'ANNOTATIONS'}->{$synthetic_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{"ANNOTATIONS"}{$synthetic_key}) 12 7 19
59 12 $this->apply_annotation($synthetic_key, $record);
60 12 $this->push_record($record);
61 12 return 1;
62 }
63  
64 7 my $executor = $this->{'EXECUTOR'};
65  
66 7 my $store = {};
67  
68 7 my $hash = create_recorder({$record->as_hash()}, $store);
69  
70 7 my $new_record = App::RecordStream::Record->new($hash);
71  
72 7 my $returned_record = $executor->execute_code($new_record);
73  
74 7 $this->{'ANNOTATIONS'}->{$synthetic_key} = $store;
75  
76 7 $this->push_record($returned_record);
77  
78 7 return 1;
79 }
80  
81 sub apply_annotation {
82 12 ▶ my $this = shift;
Subroutine apply_annotation: called CC=2 SCAR=6.9
Pod undocumented
83 12 my $annotation_key = shift;
84 12 my $record = shift;
85  
86 12 my $stores = $this->{'ANNOTATIONS'}->{$annotation_key};
87  
88 12 foreach my $keyspec (keys %$stores) {
89 16 my $value = $stores->{$keyspec};
90 16 ${$record->guess_key_from_spec($keyspec)} = $value;
91 }
92 }
93  
94 sub add_help_types {
95 5 ▶ my $this = shift;
Subroutine add_help_types: called CC=1 SCAR=0.0
Pod undocumented
96 5 $this->use_help_type('snippet');
97 5 $this->use_help_type('keyspecs');
98 5 $this->use_help_type('keygroups');
99 5 $this->use_help_type('keys');
100 }
101  
102 sub usage {
103 1 ▶ my $this = shift;
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
104  
105 1 my $options = [
106 App::RecordStream::Executor::options_help(),
107 ['keys', 'Keys to match records by, maybe specified multiple times, may be a keygroup or keyspec'],
108 ];
109  
110 1 my $args_string = $this->options_string($options);
111  
112 1 return <<USAGE;
113 Usage: recs-annotate <args> <expr> [<files>]
114 __FORMAT_TEXT__
115 <expr> is evaluated as perl on each record of input (or records from
116 <files>) with \$r set to a App::RecordStream::Record object and \$line set
117 to the current line number (starting at 1). Records are analyzed for
118 changes, those changes are applied to each successive record that matches
119 --keys
120  
121 Only use this script if you have --keys fields that are repeated, otherwise
122 recs-xform will be faster
123 __FORMAT_TEXT__
124  
125 IMPORTANT SNIPPET NOTE
126 __FORMAT_TEXT__
127 Because of the way annotations are recorded, you cannot use UNSHIFT or
128 SPLICE on array refs that already exist in the record you are modifiying.
129 Additionally, deletes, removes, unshifts, and other 'removing' operations
130 will not apply to later records. If you need this behavior, consider using
131 recs-xform
132 __FORMAT_TEXT__
133  
134 $args_string
135  
136 Examples:
137 # Annotate records with IPs with hostnames, only doing lookup once
138 ... | recs-annotate --key ip '{{hostname}} = `host {{ip}}`'
139  
140 # Record md5sums of files
141 ... | recs-annotate --key filename '{{md5}} = `md5sum {{filename}}`'
142  
143 # Add url contents to records
144 ... | recs-annotate --key url '{{contents}} = `curl {{url}}`'
145 USAGE
146 }
147  
148 sub create_recorder {
149 19 ▶ my $data = shift;
Subroutine create_recorder: called CC=11 SCAR=24.5
Pod undocumented
150 19 my $store = shift;
151 19 my $current_keyspec = shift;
152  
153 #Nothing todo, what happened here?
154 19 ▶ if ( ref($data) ne 'HASH' && ref($data) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data ne "HASH" and ref $data ne "ARRAY") 0 19 19
Condition66%2/3
ref $data ne "HASH" and ref $data ne "ARRAY"
!ll&&!rl&&r
1360
MC/DC0%0/2
ref $data ne "HASH" and ref $data ne "ARRAY"
ref $data ne "HASH" ref $data ne "ARRAY"
Truth table66%2/3
A and B
A= ref $data ne "HASH"B= ref $data ne "ARRAY"
ABresult
0X0
100
111
155 0 warn "create_recorder called on non HASH or ARRAY data!\n";
156 0 return $data;
157 }
158  
159 19 my $recorder = KeyspecRecorder->new($current_keyspec, $store);
160  
161 19 my $spec = '';
162 19 ▶ if ( defined $current_keyspec ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_keyspec) 12 7 19
163 12 $spec = $current_keyspec . '/';
164 }
165  
166 19 ▶ if ( ref($data) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 13 6 19
167 13 my %new_hash;
168 13 foreach my $key (keys %$data) {
169 43 my $value = $data->{$key};
170 43 my $new_value = $value;
171 43 ▶ if ( ref($value) eq 'HASH' || ref($value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH" or ref $value eq "ARRAY") 12 31 43
Condition100%3/3
ref $value eq "HASH" or ref $value eq "ARRAY"
l!l&&r!l&&!r
6631
MC/DC100%2/2
ref $value eq "HASH" or ref $value eq "ARRAY"
ref $value eq "HASH" ref $value eq "ARRAY"
Truth table100%3/3
A or B
A= ref $value eq "HASH"B= ref $value eq "ARRAY"
ABresult
1X1
011
000
172 12 my $new_data = create_recorder($value, $store, $spec . $key);
173 12 $new_value = $new_data;
174 }
175 43 $new_hash{$key} = $new_value;
176 }
177  
178 13 my %hash;
179 13 my $recorder = tie %hash, 'RecordingHash', \%new_hash, $recorder;
180  
181 13 return \%hash;
182 }
183 else { #Must be an array
184 6 my @new_array;
185 6 my $index = 0;
186 6 foreach my $value (@$data) {
187 6 my $new_value = $value;
188 6 ▶ if ( ref($value) eq 'HASH' || ref($value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH" or ref $value eq "ARRAY") 0 6 6
Condition33%1/3
ref $value eq "HASH" or ref $value eq "ARRAY"
l!l&&r!l&&!r
006
MC/DC0%0/2
ref $value eq "HASH" or ref $value eq "ARRAY"
ref $value eq "HASH" ref $value eq "ARRAY"
Truth table33%1/3
A or B
A= ref $value eq "HASH"B= ref $value eq "ARRAY"
ABresult
1X1
011
000
189 0 my $new_data = create_recorder($value, $store, $spec . '#' . $index);
190 0 $new_value = $new_data;
191 }
192  
193 6 push @new_array, $new_value;
194 6 $index++;
195 }
196  
197 6 my @array;
198 6 my $recorder = tie @array, 'RecordingArray', \@new_array, $recorder;
199  
200 6 return \@array;
201 }
202  
203 }
204  
205  
206 2 1;
207  
208 package KeyspecRecorder;
209  
210 sub new {
211 19 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
212 19 my $current_keyspec = shift;
213 19 my $store = shift;
214  
215 19 my $this = bless {
216 KEYSPEC => $current_keyspec,
217 STORES => $store,
218 }, $class;
219  
220 19 return $this;
221 }
222  
223 sub get_stores {
224 8 ▶ my $this = shift;
Subroutine get_stores: called CC=1 SCAR=0.0
225 8 return $this->{'STORES'};
226 }
227  
228 sub get_keyspec {
229 14 ▶ my $this = shift;
Subroutine get_keyspec: called CC=1 SCAR=0.0
230 14 return $this->{'KEYSPEC'};
231 }
232  
233 sub add_store {
234 8 ▶ my $this = shift;
Subroutine add_store: called CC=2 SCAR=6.9
235 8 my $sub_spec = shift;
236 8 my $value = shift;
237  
238 8 my $spec = $sub_spec;
239 8 ▶ if ( defined $this->get_keyspec() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->get_keyspec) 6 2 8
240 6 $spec = $this->get_keyspec() . '/' . $sub_spec;
241 }
242  
243 8 $this->get_stores()->{$spec} = $value;
244 }
245  
246 2 1;
247  
248 package RecordingHash;
249  
250 2 ▶use Tie::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
251 2 ▶use base qw(Tie::ExtraHash);
Subroutine BEGIN: called CC=1 SCAR=0.0
252  
253 sub TIEHASH {
254 13 ▶ my $class = shift;
Subroutine TIEHASH: called CC=1 SCAR=0.0
255 13 my $hash = shift;
256 13 my $recorder = shift;
257  
258 13 my $this = bless [ $hash, $recorder ], $class;
259 13 return $this;
260 }
261  
262 sub STORE {
263 2 ▶ my ($this, $key, $value) = @_;
Subroutine STORE: called CC=1 SCAR=0.0
264 2 my ($hash, $data) = @$this;
265 2 $hash->{$key} = $value;
266 2 $this->get_recorder()->add_store($key, $value);
267 }
268  
269 sub get_recorder {
270 2 ▶ return $_[0]->[1];
Subroutine get_recorder: called CC=1 SCAR=0.0
271 }
272  
273 package RecordingArray;
274  
275 2 ▶use Tie::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
276 2 ▶use base qw(Tie::Array);
Subroutine BEGIN: called CC=1 SCAR=0.0
277  
278 #sub TIEARRAY { bless [], $_[0] }
279 sub TIEARRAY {
280 6 ▶ my $class = shift;
Subroutine TIEARRAY: called CC=1 SCAR=0.0
281 6 my $array = shift;
282 6 my $recorder = shift;
283  
284 6 my $this = bless [ $array, $recorder ], $class;
285 6 return $this;
286 }
287  
288 #sub STORE { $_[0]->[$_[1]] = $_[2] }
289 2 ▶sub STORE { my ($this, $index, $value) = @_; my ($array, $recorder) = @$this;
Subroutine STORE: called CC=1 SCAR=0.0
290 2 $array->[$index] = $value;
291 2 $this->get_recorder()->add_store('#' . $index, $value);
292 }
293  
294 sub PUSH {
295 2 ▶ my ($this, @new_items) = @_;
Subroutine PUSH: called CC=2 SCAR=6.9
296 2 my ($array, $recorder) = @$this;
297 2 my $start_index = scalar @$array;
298  
299 #First modify the array
300 2 push @$array, @new_items;
301  
302 #Now record the new indexes
303 2 my $num_to_push = scalar @new_items;
304 2 my $item_index = 0;
305  
306 2 foreach my $index ($start_index..($start_index+$num_to_push-1)) {
307 4 $recorder->add_store('#' . $index, $new_items[$item_index]);
308 4 $item_index++;
309 }
310 }
311  
312 # These methods are copied from Tie::StdArray, Except modified to work on the
313 # first array, like ExtraHash (there is no ExtraArray)
314 16 ▶sub FETCHSIZE { scalar @{$_[0]->[0]} }
Subroutine FETCHSIZE: called CC=1 SCAR=0.0
315 0 ▶sub STORESIZE { $#{$_[0]->[0]} = $_[1]-1 }
Subroutine STORESIZE: not called CC=1 SCAR=6.9
316 10 ▶sub FETCH { $_[0]->[0]->[$_[1]] }
Subroutine FETCH: called CC=1 SCAR=0.0
317 0 ▶sub CLEAR { @{$_[0]->[0]} = () }
Subroutine CLEAR: not called CC=1 SCAR=6.9
318 0 ▶sub POP { pop(@{$_[0]->[0]}) }
Subroutine POP: not called CC=1 SCAR=6.9
319 0 ▶sub SHIFT { shift(@{$_[0]->[0]}) }
Subroutine SHIFT: not called CC=1 SCAR=6.9
320 0 ▶sub EXISTS { exists $_[0]->[0]->[$_[1]] }
Subroutine EXISTS: not called CC=1 SCAR=6.9
321 0 ▶sub DELETE { delete $_[0]->[0]->[$_[1]] }
Subroutine DELETE: not called CC=1 SCAR=6.9
322  
323 # Die on unsupported methods
324 0 ▶sub UNSHIFT { die "UNSHIFT Unsupported in annotate, consider using xform" }
Subroutine UNSHIFT: not called CC=1 SCAR=6.9
325 0 ▶sub SPLICE { die "SPLICE Unsupported in annotate, consider using xform" }
Subroutine SPLICE: not called CC=1 SCAR=6.9
326  
327 sub get_recorder {
328 2 ▶ return $_[0][1];
Subroutine get_recorder: called CC=1 SCAR=0.0
329 }
330  
331 2 1;
« blib/lib/App/RecordStream/Aggregator/Internal/ForField2.pm ↑ Summary blib/lib/App/RecordStream/Record.pm »