blib/lib/Bot/Cobalt/IRC/FloodChk.pm

filter: Statementstmt 37.5% 18 / 48 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 60.0% 6 / 10 Podpod 100% 3 / 3 totaltotal 30.0% 27 / 90 CC 14 SCAR 32.5
CC
14
Cov
25%
check
42.0
expire
42.0
clear
20.0
CRAP 25.7
« blib/lib/Bot/Cobalt/IRC/Role/AdminCmds.pm ↑ Summary blib/lib/Bot/Cobalt/Plugin/Games/RockPaperScissors.pm »
1 package Bot::Cobalt::IRC::FloodChk;
2 $Bot::Cobalt::IRC::FloodChk::VERSION = '0.021003';
3 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Bot::Cobalt::Common ':types';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use List::Objects::WithUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 ## _fqueue->{$context}->{$key} = array()
14 ## FIXME Should probably be an obj ...
15 has _fqueue => (
16 is => 'rw',
17 lazy => 1,
18 0 ▶ default => sub { +{} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
19 );
20  
21 has count => ( is => 'rw', isa => Num, required => 1 );
22 has in => ( is => 'rw', isa => Num, required => 1 );
23  
24 0 ▶sub check {
Subroutine check: not called CC=6 SCAR=37.4
Pod documented
25 0 my ($self, $context, $key) = @_;
26 0 ▶ return unless defined $context and defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $context and defined $key 0 0 0
Condition0%0/3
defined $context and defined $key
!ll&&!rl&&r
000
MC/DC0%0/2
defined $context and defined $key
defined $context defined $key
Truth table0%0/3
A and B
A= defined $contextB= defined $key
ABresult
0X0
100
111
27
28 0 ▶ my $thisq = $self->_fqueue->{$context}->{$key} //= array;
Condition0%0/3
$self->_fqueue->{$context}{$key} //= array()
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_fqueue->{$context}{$key} //= array()
$self->_fqueue->{$context}{$key} array()
Truth table0%0/3
A //= B
A= $self->_fqueue->{$context}{$key}B= array()
ABresult
1X1
011
000
29
30 0 ▶ if ((my $pending = $thisq->count) >= $self->count) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $pending = $thisq->count) >= $self->count) 0 0 0
31  
32 0 my $oldest_ts = $thisq->head;
33 0 my $ev_c = $self->count;
34 0 my $ev_sec = $self->in;
35  
36 0 my $delayed =
37 ($oldest_ts + ($pending * $ev_sec / $ev_c) )
38 - Time::HiRes::time();
39
40 ## Too many events in this time window:
41 0 ▶ return $delayed if $delayed > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $delayed > 0 0 0 0
42  
43 ## ...otherwise shift and continue:
44 0 $thisq->shift;
45 }
46  
47 ## Safe to push this ev, no delay:
48 0 $thisq->push( Time::HiRes::time() );
49 0 return 0
50 }
51  
52 0 ▶sub clear {
Subroutine clear: not called CC=4 SCAR=30.0
Pod documented
53 0 my ($self, $context, $key) = @_;
54 0 ▶ confess "clear() needs a context specified"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $context 0 0 0
55 unless defined $context;
56
57 0 ▶ return unless exists $self->_fqueue->{$context};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->_fqueue->{$context} 0 0 0
58
59 0 ▶ return delete $self->_fqueue->{$context}->{$key}
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 0 0 0
60 if defined $key;
61
62 0 delete $self->_fqueue->{$context}
63 }
64  
65 0 ▶sub expire {
Subroutine expire: not called CC=6 SCAR=37.4
Pod documented
66 ## Clear keys when recent_event_time - time > $self->in
67 0 my ($self) = @_;
68  
69 0 CONTEXT: for my $context (keys %{ $self->_fqueue } ) {
70  
71 0 KEY: for my $key (keys %{ $self->_fqueue->{$context} } ) {
72  
73 0 my $events = $self->_fqueue->{$context}->{$key};
74 0 ▶ my $latest_time = $events->get(-1) // next KEY;
Condition0%0/2
$events->get(-1) // (next KEY)
l!l
00
MC/DC0%0/1
$events->get(-1) // (next KEY)
$events->get(-1)
Truth table0%0/2
A
A= $events->get(-1)
Aresult
11
00
75
76 0 ▶ if (Time::HiRes::time() - $latest_time > $self->in) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Time::HiRes::time() - $latest_time > $self->in) 0 0 0
77 ## It's been more than ->in seconds since latest event was
78 ## noted. We can clear() this entry.
79 0 $self->clear($context, $key);
80 }
81 } # KEY
82
83 0 ▶ unless (keys %{ $self->_fqueue->{$context} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %{$self->_fqueue->{$context};}) 0 0 0
84 ## Nothing left for this context.
85 0 $self->clear($context);
86 }
87 }
88 }
89  
90 1;
91 __END__
« blib/lib/Bot/Cobalt/IRC/Role/AdminCmds.pm ↑ Summary blib/lib/Bot/Cobalt/Plugin/Games/RockPaperScissors.pm »