blib/lib/Message/Inform.pm

filter: Statementstmt 94.8% 166 / 175 Branchbran 70.0% 42 / 60 Conditioncond 42.8% 9 / 21 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 94.4% 17 / 18 Podpod 100% 7 / 7 totaltotal 82.9% 243 / 293 CC 51 SCAR 26.7
CC
51
Cov
85%
message
23.5
fire_action
22.9
_handle_tick
10.1
CRAP 14.4
↑ Summary
1 package Message::Inform;
2 {
3 7 $Message::Inform::VERSION = '1.132270';
4 }
5  
6 7 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Message::Match qw(mmatch);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Message::Transform qw(mtransform);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Message::Inform - Intelligently distribute messages over time
17  
18 =cut
19  
20 7 our $config = {};
21 7 our $instances = {};
22 7 our $tick = 0;
23 #$instances->{$instance} = {
24 # message => $merged_message,
25 # config => $most_recently_merged_config,
26 # initial_ts => $ts_when_this_appeared
27 #};
28  
29  
30 =head1 SYNOPSIS
31  
32 use Message::Inform;
33  
34 sub a1 {
35 my %args = @_;
36 #$args{message}
37 #$args{action}
38 #$args{inform_instance}
39 #$args{inform_instance_name}
40 #$args{interval_time}
41 }
42 my $inform = Message::Inform->new;
43 $inform->config({
44 informs => [
45 { inform_name => 'i1',
46 match => { x => 'y' },
47 close_match => { x => 'y', level => 'OK' },
48 instance => ' specials/"i1:$message->{a}"',
49 intervals => {
50 '0' => [ #right away
51 { action_type => 'open',
52 action_name => 'a1',
53 },{ action_type => 'close',
54 action_name => 'a1',
55 }
56 ],
57 '2' => [ #2 seconds
58 { action_type => 'open',
59 action_name => 'a1',
60 },{ action_type => 'intermediate',
61 action_name => 'a1',
62 }
63 ],
64 }
65 }
66 ],
67 action_map => {
68 a1 => 'main::a1',
69 },
70 });
71  
72 $inform->message({x => 'y', a => 'b'});
73 #main::a1() calls with this message immediately.
74 for (1..4) {
75 $inform->message();
76 sleep 1;
77 }
78 #main::a1() calls with the previous message as an 'open' in 2
79 #seconds
80 $inform->message({x => 'y', a => 'b', something => 'else'});
81 #main::a1() immediately calls as an 'intermediate'
82 $inform->message({x => 'y', a => 'b', level => 'OK'});
83 #main::a1() immediately calls as a 'close'
84  
85 =head1 DESCRIPTION
86  
87 This module obviously has some 'deep' and 'subtle' behaviour; this
88 0.1 release won't describe that, but future releases certainly will.
89  
90 =head1 SUBROUTINES/METHODS
91  
92 =head2 new(state => $previous_state)
93  
94 Typical constructor. Pass in the output from get_state() to resume
95 operations as they were at that time.
96  
97 =cut
98 5 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
99 5 my $class = shift;
100 5 my $self = {};
101  
102 5 bless ($self, $class);
103 5 return $self;
104 }
105  
106 =head2 config($config)
107  
108 Set initial config or update running config at any time.
109
110 =cut
111 5 ▶sub config {
Subroutine config: called CC=1 SCAR=0.0
Pod documented
112 5 my $self = shift;
113 5 my $new_config = shift;
114 5 $config = $new_config;
115 5 return $config;
116 }
117  
118 #select the fastest possible way to clone (and be portable too)
119 #For now, Storable is what I know
120 60 ▶sub _fast_clone {
Subroutine _fast_clone: called CC=1 SCAR=0.0
121 7 ▶ use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
122 60 my $thing = shift;
123 60 return Storable::dclone $thing;
124 }
125  
126 =head2 get_message_configs($message)
127  
128 Returns all of the merged configs that will apply to the passed in
129 message.
130  
131 =cut
132 18 ▶sub get_message_configs {
Subroutine get_message_configs: called CC=4 SCAR=13.9
Pod documented
133 18 my $self = shift;
134 18 my $message = shift;
135 18 my $my_configs = {
136 match => {},
137 close_match => {},
138 };
139  
140 18 my $relevant_informs = $self->get_relevant_informs($message);
141 18 foreach my $type ('match','close_match') {
142 36 foreach my $inform (@{$relevant_informs->{$type}}) {
143 18 mtransform($message, { inform_instance => $inform->{instance}});
144 18 my $instance = $message->{inform_instance};
145 $my_configs->{$type}->{$instance} = _fast_clone($message)
146 18 ▶ unless $my_configs->{$type}->{$instance};
Branch: 1 branch
BranchTrueFalseTotal
unless $my_configs->{$type}{$instance} 14 4 18
147 18 mtransform($my_configs->{$type}->{$instance}, _fast_clone($inform));
148 }
149 }
150 18 return $my_configs;
151 }
152  
153 =head2 get_relevant_informs($message)
154  
155 Returns all of the informs that would apply to the passed in message.
156  
157 =cut
158 18 ▶sub get_relevant_informs {
Subroutine get_relevant_informs: called CC=6 SCAR=18.0
Pod documented
159 18 my $self = shift;
160 18 my $message = shift;
161 18 my $informs = {
162 match => [],
163 close_match => [],
164 };
165 18 foreach my $inform (@{$config->{informs}}) {
166 12 push @{$informs->{match}}, $inform
167 if $inform->{match} and
168 26 ▶ mmatch $message, $inform->{match};
Branch: 1 branch
BranchTrueFalseTotal
if $inform->{"match"} and mmatch($message, $inform->{"match"}) 12 14 26
Condition66%2/3
$inform->{"match"} and mmatch($message, $inform->{"match"})
!ll&&!rl&&r
01412
MC/DC50%1/2
$inform->{"match"} and mmatch($message, $inform->{"match"})
$inform->{"match"} mmatch($message, $inform->{"match"})
Truth table66%2/3
A and B
A= $inform->{"match"}B= mmatch($message, $inform->{"match"})
ABresult
0X0
100
111
169 6 push @{$informs->{close_match}}, $inform
170 if $inform->{close_match} and
171 26 ▶ mmatch $message, $inform->{close_match};
Branch: 1 branch
BranchTrueFalseTotal
if $inform->{"close_match"} and mmatch($message, $inform->{"close_match"}) 6 20 26
Condition66%2/3
$inform->{"close_match"} and mmatch($message, $inform->{"close_match"})
!ll&&!rl&&r
0206
MC/DC50%1/2
$inform->{"close_match"} and mmatch($message, $inform->{"close_match"})
$inform->{"close_match"} mmatch($message, $inform->{"close_match"})
Truth table66%2/3
A and B
A= $inform->{"close_match"}B= mmatch($message, $inform->{"close_match"})
ABresult
0X0
100
111
172 }
173 18 return $informs;
174 }
175  
176 #housekeeping for timing
177 {
178 7 my $last_tick_ts;
179 14 ▶sub _handle_tick {
Subroutine _handle_tick: called CC=10 SCAR=23.1
180 14 my $self = shift;
181 14 ▶ $last_tick_ts = time unless $last_tick_ts;
Branch: 1 branch
BranchTrueFalseTotal
unless $last_tick_ts 2 12 14
182 14 my $tick_delta = time - $last_tick_ts;
183 14 $tick+=$tick_delta;
184 #now we need to handle all of the intervals scheduled from
185 #$tick-$tick_delta until now ($tick)
186  
187 #for now, we do it dumb: full-blown iteration
188 14 foreach my $instance_name (keys %{$instances}) {
189 10 my $instance = $instances->{$instance_name};
190 10 my $previous_instance_tick_offset = $instance->{last_update_tick} - $instance->{initial_tick};
191 10 my $new_instance_tick_offset = $previous_instance_tick_offset + $tick_delta;
192 #so we need to fire all of the opens between
193 #$previous_instance_tick_offset and $new_instance_tick_offset
194 #exclusive to inclusive
195 #meaning we do NOT fire anything at $previous_instance_tick_offset
196 #and we DO fire anything at $new_instance_tick_offset
197
198 10 my $local_config = _fast_clone($instance->{config});
199 10 foreach my $interval_time (sort {$a <=> $b} keys %{$local_config->{intervals}}) {
200 18 ▶ next if $interval_time <= $previous_instance_tick_offset;
Branch: 1 branch
BranchTrueFalseTotal
if $interval_time <= $previous_instance_tick_offset 14 4 18
201 4 ▶ next if $interval_time > $new_instance_tick_offset;
Branch: 1 branch
BranchTrueFalseTotal
if $interval_time > $new_instance_tick_offset 3 1 4
202 1 my $interval = $local_config->{intervals}->{$interval_time};
203 1 $instance->{last_update_tick} = $new_instance_tick_offset;
204 1 foreach my $action (@{$interval}) {
205 2 ▶ next unless $action->{action_type};
Branch: 1 branch
BranchTrueFalseTotal
unless $action->{"action_type"} 0 2 2
206 2 ▶ next if $action->{action_type} ne 'open';
Branch: 1 branch
BranchTrueFalseTotal
if $action->{"action_type"} ne "open" 1 1 2
207 1 eval {
208 my $ret = $self->fire_action($instance->{message}, $action,
209 1 inform_instance => $instances->{$instance_name},
210 inform_instance_name => $instance_name,
211 interval_time => $interval_time,
212 );
213 };
214 1 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
215 0 print "Exception: $@\n";
216 }
217 }
218 }
219 }
220 }
221 }
222  
223 =head2 message($message)
224  
225 Send a message into Inform. This can be called with no arguments
226 to trigger timed Inform fires if there's no other messages to be
227 sent.
228  
229 =cut
230 7 ▶sub message {
Subroutine message: called CC=22 SCAR=31.6
Pod documented
231 14 my $self = shift;
232 14 my $message = shift;
233 14 ▶ if(not $message) { #special case; just keep the pump primed
Branch: 1 branch
BranchTrueFalseTotal
unless ($message) 0 14 14
234 0 $self->_handle_tick();
235 0 return 1;
236 }
237 14 my $message_configs = $self->get_message_configs($message);
238  
239 14 my $closed_instances = {}; #so we can skip these if we see any opens
240 14 foreach my $instance_name (keys %{$message_configs->{close_match}}) {
241 2 $closed_instances->{$instance_name} = $message_configs->{close_match}->{$instance_name};
242 2 my $local_config = _fast_clone($message_configs->{close_match}->{$instance_name});
243 #now we iterate on the intervals backwards
244 #TODO: start on our current interval
245 2 foreach my $interval_time (sort {$a <=> $b} keys %{$local_config->{intervals}}) {
246 3 my $interval = $local_config->{intervals}->{$interval_time};
247 3 foreach my $action (@{$interval}) {
248 6 ▶ next unless $action->{action_type};
Branch: 1 branch
BranchTrueFalseTotal
unless $action->{"action_type"} 0 6 6
249 6 ▶ next if $action->{action_type} ne 'close';
Branch: 1 branch
BranchTrueFalseTotal
if $action->{"action_type"} ne "close" 4 2 6
250 2 eval {
251 my $ret = $self->fire_action($message, $action,
252 2 inform_instance => $instances->{$instance_name},
253 inform_instance_name => $instance_name,
254 interval_time => $interval_time,
255 );
256 };
257 2 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
258 0 print "exception: $@";
259 }
260 }
261 3 delete $instances->{$instance_name};
262 }
263 }
264  
265 14 foreach my $instance_name (keys %{$message_configs->{match}}) {
266 6 ▶ next if $closed_instances->{$instance_name}; #this was closed before; that trumps
Branch: 1 branch
BranchTrueFalseTotal
if $closed_instances->{$instance_name} 2 4 6
267 4 my $local_config = _fast_clone($message_configs->{match}->{$instance_name});
268 4 ▶ if(not $instances->{$instance_name}) { #this is an 'open'
Branch: 1 branch
BranchTrueFalseTotal
if (not $Message::Inform::instances->{$instance_name}) { } 3 1 4
269 3 $instances->{$instance_name} = {
270 message => _fast_clone($message),
271 config => $local_config,
272 initial_ts => time,
273 initial_tick => $tick,
274 last_message_ts => time,
275 last_message_tick => $tick,
276 last_update_ts => time,
277 last_update_tick => $tick,
278 };
279 #Here we look for any intervals at '0', and fire them
280 3 ▶ if($local_config->{intervals}->{'0'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local_config->{"intervals"}{0}) 3 0 3
281 3 foreach my $action (@{$local_config->{intervals}->{'0'}}) {
282 6 ▶ next unless $action->{action_type};
Branch: 1 branch
BranchTrueFalseTotal
unless $action->{"action_type"} 0 6 6
283 6 ▶ next if $action->{action_type} ne 'open';
Branch: 1 branch
BranchTrueFalseTotal
if $action->{"action_type"} ne "open" 3 3 6
284 3 eval {
285 my $ret = $self->fire_action($message, $action,
286 3 inform_instance => $instances->{$instance_name},
287 inform_instance_name => $instance_name,
288 interval_time => 0,
289 );
290 };
291 3 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
292 0 print "exception: $@";
293 }
294 }
295 }
296 } else { #this is an 'intermediate'
297 1 my $instance = $instances->{$instance_name};
298 1 $instance->{last_message_ts} = time;
299 1 $instance->{last_message_tick} = $tick;
300 1 $instance->{last_update_ts} = time;
301 1 $instance->{last_update_tick} = $tick;
302 1 $instance->{config} = $local_config;
303 1 mtransform($instance->{message}, $message);
304 #Here we fire any defined 'intermediates' at the current interval
305 1 my $instance_tick_offset = $instance->{last_update_tick} - $instance->{initial_tick};
306 1 my $local_config = _fast_clone($instance->{config});
307 1 my $interval;
308 my $run_interval_time;
309 1 foreach my $interval_time (sort {$a <=> $b} keys %{$local_config->{intervals}}) {
310 2 ▶ next if $interval_time > $instance_tick_offset;
Branch: 1 branch
BranchTrueFalseTotal
if $interval_time > $instance_tick_offset 0 2 2
311 2 $interval = $local_config->{intervals}->{$interval_time};
312 2 $run_interval_time = $interval_time;
313 }
314 1 $instance->{last_update_tick} = $tick; ##????
315 1 foreach my $action (@{$interval}) {
316 2 ▶ next unless $action->{action_type};
Branch: 1 branch
BranchTrueFalseTotal
unless $action->{"action_type"} 0 2 2
317 2 ▶ next if $action->{action_type} ne 'intermediate';
Branch: 1 branch
BranchTrueFalseTotal
if $action->{"action_type"} ne "intermediate" 1 1 2
318 1 eval {
319 my $ret = $self->fire_action($instance->{message}, $action,
320 1 inform_instance => $instances->{$instance_name},
321 inform_instance_name => $instance_name,
322 interval_time => $run_interval_time,
323 );
324 };
325 1 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
326 0 print "Exception: $@\n";
327 }
328 }
329 }
330 }
331 14 $self->_handle_tick();
332 14 return $message;
333 }
334  
335 =head2 get_state()
336  
337 Called with no argument, this returns the necessary state to be passed
338 into a future constructor. The module will then continue to function
339 in exactly the same state as it was when get_state() was called.
340 =cut
341 0 ▶sub get_state {
Subroutine get_state: not called CC=1 SCAR=6.9
Pod documented
342 return {
343 0 tick => $tick,
344 config => $config,
345 instances => $instances,
346 };
347 }
348  
349 =head2 fire_action($message, $action)
350  
351 This might not want to be a public method, but it is for now.
352  
353 =cut
354 8 ▶sub fire_action {
Subroutine fire_action: called CC=13 SCAR=31.3
Pod documented
355 8 my ($self, $message, $action, @args) = @_;
356 8 ▶ croak 'first argument must be message, a HASH reference'
Branch: 1 branch
BranchTrueFalseTotal
if not $message && ref $message or ref $message ne "HASH" 0 8 8
Condition33%2/6
not $message && ref $message or ref $message ne "HASH"
l!l&&r!l&&!r
008
$message && ref $message
!ll&&!rl&&r
008
MC/DC0%0/3
not $message && ref $message or ref $message ne "HASH"
$message ref $message ref $message ne "HASH"
Truth table25%1/4
not(A && B) or C
A= $messageB= ref $messageC= ref $message ne "HASH"
ABCresult
0XX1
10X1
1111
1100
357 if not $message or
358 not ref $message or
359 ref $message ne 'HASH';
360 8 ▶ croak 'even number of arguments required'
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args % 2 0 8 8
361 if scalar @args % 2;
362 8 my %args = @args;
363 8 ▶ croak 'passed action must be a HASH reference'
Branch: 1 branch
BranchTrueFalseTotal
if not $action && ref $action or ref $action ne "HASH" 0 8 8
Condition33%2/6
not $action && ref $action or ref $action ne "HASH"
l!l&&r!l&&!r
008
$action && ref $action
!ll&&!rl&&r
008
MC/DC0%0/3
not $action && ref $action or ref $action ne "HASH"
$action ref $action ref $action ne "HASH"
Truth table25%1/4
not(A && B) or C
A= $actionB= ref $actionC= ref $action ne "HASH"
ABCresult
0XX1
10X1
1111
1100
364 if not $action or
365 not ref $action or
366 ref $action ne 'HASH';
367 croak 'passed action must have "action_name" as a defined scalar'
368 if not $action->{action_name} or
369 8 ▶ ref $action->{action_name};
Branch: 1 branch
BranchTrueFalseTotal
if not $action->{"action_name"} or ref $action->{"action_name"} 0 8 8
Condition33%1/3
not $action->{"action_name"} or ref $action->{"action_name"}
l!l&&r!l&&!r
008
MC/DC0%0/2
not $action->{"action_name"} or ref $action->{"action_name"}
not $action->{"action_name"} ref $action->{"action_name"}
Truth table33%1/3
not(A) or B
A= not $action->{"action_name"}B= ref $action->{"action_name"}
ABresult
1X1
011
000
370 croak "passed action had 'action_name' $action->{action_name} did not have defined action_map"
371 8 ▶ unless $config->{action_map}->{$action->{action_name}};
Branch: 1 branch
BranchTrueFalseTotal
unless $Message::Inform::config->{"action_map"}{$action->{"action_name"}} 0 8 8
372 8 my $action_message = _fast_clone($message);
373 8 ▶ if($action->{transform}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action->{"transform"}) 0 8 8
374 0 mtransform($action_message, $action->{transform});
375 }
376 8 my $ret;
377 8 eval {
378 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
379 8 $ret = &{$config->{action_map}->{$action->{action_name}}}(
380 message => $action_message,
381 action => $action,
382 %args
383 );
384 8 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
385 0 croak "action '$action->{action_name}' failed: $@";
386 }
387 };
388 8 return $ret;
389 }
390  
391 =head1 AUTHOR
392  
393 Dana M. Diederich, C<diederich@gmail.com>
394  
395 =head1 BUGS
396  
397 Please report any bugs or feature requests through L<https://github.com/dana/perl-Message-Inform/issues>. I will be notified, and then you'll
398 automatically be notified of progress on your bug as I make changes.
399  
400 =head1 SUPPORT
401  
402 You can find documentation for this module with the perldoc command.
403  
404 perldoc Message::Inform
405  
406 You can also look for information at:
407  
408 =over 4
409  
410 =item * Report bugs here:
411  
412 L<https://github.com/dana/perl-Message-Inform/issues>
413  
414 =item * AnnoCPAN: Annotated CPAN documentation
415  
416 L<http://annocpan.org/dist/Message-Inform>
417  
418 =item * CPAN Ratings
419  
420 L<http://cpanratings.perl.org/d/Message-Inform>
421  
422 =item * Search CPAN
423  
424 L<https://metacpan.org/module/Message::Inform>
425  
426 =back
427  
428  
429 =head1 LICENSE AND COPYRIGHT
430  
431 Copyright 2013 Dana M. Diederich.
432  
433 This program is free software; you can redistribute it and/or modify it
434 under the terms of the the Artistic License (2.0). You may obtain a
435 copy of the full license at:
436  
437 L<http://www.perlfoundation.org/artistic_license_2_0>
438  
439 Any use, modification, and distribution of the Standard or Modified
440 Versions is governed by this Artistic License. By using, modifying or
441 distributing the Package, you accept this license. Do not use, modify,
442 or distribute the Package, if you do not accept this license.
443  
444 If your Modified Version has been derived from a Modified Version made
445 by someone other than you, you are nevertheless required to ensure that
446 your Modified Version complies with the requirements of this license.
447  
448 This license does not grant you the right to use any trademark, service
449 mark, tradename, or logo of the Copyright Holder.
450  
451 This license includes the non-exclusive, worldwide, free-of-charge
452 patent license to make, have made, use, offer to sell, sell, import and
453 otherwise transfer the Package with respect to any patent claims
454 licensable by the Copyright Holder that are necessarily infringed by the
455 Package. If you institute patent litigation (including a cross-claim or
456 counterclaim) against any party alleging that the Package constitutes
457 direct or contributory patent infringement, then this Artistic License
458 to you shall terminate on the date that such litigation is filed.
459  
460 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
461 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
462 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
463 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
464 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
465 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
466 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
467 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
468  
469  
470 =cut
471  
472 7 1; # End of Message::Inform
473  
474  
↑ Summary