Backbone/SendPolicy/MinimumRepeatInterval.pm

filter: Statementstmt 81.6% 40 / 49 Branchbran 50.0% 7 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 73.0% 57 / 78 CC 10 SCAR 21.3
CC
10
Cov
71%
allow_send
16.0
purge_send_cache
3.0
BEGIN
1.0
CRAP 8.4
« Backbone/Message.pm ↑ Summary Backbone/Service/ConsoleChat.pm »
1 package Bot::Backbone::SendPolicy::MinimumRepeatInterval;
2 1 $Bot::Backbone::SendPolicy::MinimumRepeatInterval::VERSION = '0.161950';
3 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 with 'Bot::Backbone::SendPolicy';
7  
8 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # ABSTRACT: Prevent any message from being repeated too often
11  
12  
13 1 has interval => (
14 is => 'ro',
15 isa => 'Num',
16 required => 1,
17 );
18  
19  
20 1 has queue_length => (
21 is => 'ro',
22 isa => 'Int',
23 predicate => 'has_queue',
24 );
25  
26  
27 1 has discard => (
28 is => 'ro',
29 isa => 'Bool',
30 required => 1,
31 default => 0,
32 );
33  
34  
35 1 has lingering_interval => (
36 is => 'ro',
37 isa => 'Num',
38 predicate => 'has_lingering_interval',
39 );
40  
41  
42 has cache_key => (
43 is => 'ro',
44 isa => 'CodeRef',
45 required => 1,
46 1 ▶ default => sub { sub { $_[0]->{text} } },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
47 1 traits => [ 'Code' ],
48 handles => {
49 'get_cache_key' => 'execute',
50 },
51 );
52  
53  
54 has send_cache => (
55 is => 'ro',
56 isa => 'HashRef[ArrayRef[Num]]',
57 required => 1,
58 1 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 1 traits => [ 'Hash' ],
60 handles => {
61 'list_cache_keys' => 'keys',
62 'delete_cache_key' => 'delete',
63 'last_send_times' => 'get',
64 'set_last_send_times' => 'set',
65 'has_cache_key' => 'defined',
66 },
67 );
68  
69  
70 299 ▶sub purge_send_cache {
Subroutine purge_send_cache: called CC=3 SCAR=11.0
Pod documented
71 299 my $self = shift;
72  
73 299 my $now = AnyEvent->now;
74 299 for my $key ($self->list_cache_keys) {
75 591 my ($last_send, $orig_send) = @{ $self->last_send_times($key) };
76  
77 # Delete if it's been longer than interval since last send
78 591 ▶ $self->delete_cache_key($key)
Branch: 1 branch
BranchTrueFalseTotal
if $last_send + $self->interval < $now 8 583 591
79 if $last_send + $self->interval < $now;
80 }
81 }
82  
83  
84 299 ▶sub allow_send {
Subroutine allow_send: called CC=8 SCAR=27.7
Pod documented
85 299 my ($self, $options) = @_;
86  
87 299 $self->purge_send_cache;
88  
89 299 my %send = ( allow => 1 );
90 299 my $now = AnyEvent->now;
91 299 my $key = $self->get_cache_key($options);
92 299 my $save = 1;
93 299 my $after = 0;
94 299 my ($last_send, $orig_send) = ($now, $now);
95  
96 299 ▶ if ($self->has_cache_key($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_cache_key($key)) 289 10 299
97  
98 # If there's already a cache key in place, don't save
99 289 $save = 0;
100  
101 # Discard immediately if requested
102 289 ▶ if ($self->discard) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->discard) { } 289 0 289
103 289 $send{allow} = 0;
104 }
105  
106 # Otherwise, determine how long to delay sending
107 else {
108 0 ($last_send, $orig_send) = @{ $self->last_send_times($key) };
109  
110 # Wait for whatever is left of the interval since the last send
111 0 $send{after} = $after = ($last_send + $self->interval) - $now;
112  
113 # If we have a lingering interval, we need to modify the send cache
114 0 ▶ if ($self->has_lingering_interval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_lingering_interval) 0 0 0
115 0 $save = 1;
116  
117 # The lingering interval has not been passed, so move the last
118 # send date forward
119 0 ▶ if ($now - $orig_send < $self->lingering_interval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now - $orig_send < $self->lingering_interval) { } 0 0 0
120 0 $last_send = $now + $after;
121 }
122  
123 # The lingering interval has passed, so move it back to the
124 # original, which should guarantee it is purged next cycle
125 else {
126 0 $last_send = $orig_send;
127 }
128 }
129  
130 # If the number of messages queued is too long, nevermind...
131 0 ▶ $send{allow} = 0
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_queue and $after / $self->interval > $self->queue_length 0 0 0
Condition0%0/3
$self->has_queue and $after / $self->interval > $self->queue_length
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_queue and $after / $self->interval > $self->queue_length
$self->has_queue $after / $self->interval > $self->queue_length
Truth table0%0/3
A and B
A= $self->has_queueB= $after / $self->interval > $self->queue_length
ABresult
0X0
100
111
132 if $self->has_queue
133 and $after / $self->interval > $self->queue_length;
134 }
135 }
136  
137 299 ▶ $self->set_last_send_times($key, [ $last_send, $orig_send ]) if $save;
Branch: 1 branch
BranchTrueFalseTotal
if $save 10 289 299
138 299 return \%send;
139 }
140  
141 1 __PACKAGE__->meta->make_immutable;
142  
143 __END__
« Backbone/Message.pm ↑ Summary Backbone/Service/ConsoleChat.pm »