BusyBird/Timeline.pm

filter: Statementstmt 97.7% 170 / 174 Branchbran 85.7% 36 / 42 Conditioncond 77.7% 21 / 27 MC/DCmcdc 56.2% 9 / 16 Subroutinesub 100% 42 / 42 Podpod 100% 15 / 15 totaltotal 92.7% 293 / 316 CC 36 SCAR 14.5
CC
36
Cov
93%
watch_unacked_counts
11.0
__ANON__
8.3
__ANON__
7.3
CRAP 4.3
« BusyBird/Main.pm ↑ Summary BusyBird/Filter/Twitter.pm »
1 package BusyBird::Timeline;
2 11 ▶use v5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use BusyBird::Util qw(set_param);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use BusyBird::Log qw(bblog);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use BusyBird::Flow;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use BusyBird::Watcher::Aggregator;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use BusyBird::DateTime::Format 0.04;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use BusyBird::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Async::Selector 1.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use Scalar::Util qw(weaken looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 11 our @CARP_NOT = qw(BusyBird::Config);
19  
20 229 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
21 229 my ($class, %args) = @_;
22 229 my $self = bless {
23 filter_flow => BusyBird::Flow->new,
24 selector => Async::Selector->new,
25 unacked_counts => {total => 0},
26 config => BusyBird::Config->new(type => "timeline", with_default => 0),
27 id_generator => Data::UUID->new,
28 }, $class;
29 229 $self->set_param(\%args, 'name', undef, 1);
30 229 $self->set_param(\%args, 'storage', undef, 1);
31 229 $self->set_param(\%args, 'watcher_max', 512);
32 229 ▶ croak 'name must not be empty' if $self->{name} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"name"} eq "" 0 229 229
33 229 $self->_init_selector();
34 229 $self->_update_unacked_counts();
35 229 return $self;
36 }
37  
38 11 ▶sub _log {
Subroutine _log: called CC=1 SCAR=0.0
39 11 my ($self, $level, $msg) = @_;
40 11 bblog($level, $self->name . ": $msg");
41 }
42  
43 546 ▶sub _update_unacked_counts {
Subroutine _update_unacked_counts: called CC=1 SCAR=0.0
44 546 my ($self) = @_;
45 546 ▶ $self->get_unacked_counts(callback => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
46 546 my ($error, $unacked_counts) = @_;
47 546 ▶ if(defined($error)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 11 535 546
48 11 $self->_log('error', "error while updating unacked count: $error");
49 11 return;
50 }
51 535 $self->{unacked_counts} = $unacked_counts;
52 535 $self->{selector}->trigger('unacked_counts');
53 546 });
54 }
55  
56 229 ▶sub _init_selector {
Subroutine _init_selector: called CC=1 SCAR=0.0
57 229 my ($self) = @_;
58 229 weaken $self;
59 219 ▶ $self->{selector}->register(unacked_counts => sub {
Subroutine __ANON__: called CC=8 SCAR=21.2
60 219 my ($exp_unacked_counts) = @_;
61 219 ▶ if(!defined($exp_unacked_counts) || ref($exp_unacked_counts) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $exp_unacked_counts or ref $exp_unacked_counts ne "HASH") 0 219 219
Condition33%1/3
not defined $exp_unacked_counts or ref $exp_unacked_counts ne "HASH"
l!l&&r!l&&!r
00219
MC/DC0%0/2
not defined $exp_unacked_counts or ref $exp_unacked_counts ne "HASH"
not defined $exp_unacked_counts ref $exp_unacked_counts ne "HASH"
Truth table33%1/3
not(A) or B
A= not defined $exp_unacked_countsB= ref $exp_unacked_counts ne "HASH"
ABresult
1X1
011
000
62 0 croak "unacked_counts watcher: condition input must be a hash-ref";
63 }
64 219 ▶ return { %{$self->{unacked_counts}} } if !%$exp_unacked_counts;
Branch: 1 branch
BranchTrueFalseTotal
unless %$exp_unacked_counts 10 209 219
65 209 foreach my $key (keys %$exp_unacked_counts) {
66 238 ▶ my $exp_val = $exp_unacked_counts->{$key} || 0;
Condition100%2/2
$$exp_unacked_counts{$key} || 0
l!l
125113
MC/DC100%1/1
$$exp_unacked_counts{$key} || 0
$$exp_unacked_counts{$key}
Truth table100%2/2
A
A= $$exp_unacked_counts{$key}
Aresult
11
00
67 238 ▶ my $got_val = $self->{unacked_counts}{$key} || 0;
Condition100%2/2
$$self{'unacked_counts'}{$key} || 0
l!l
133105
MC/DC100%1/1
$$self{'unacked_counts'}{$key} || 0
$$self{'unacked_counts'}{$key}
Truth table100%2/2
A
A= $$self{'unacked_counts'}{$key}
Aresult
11
00
68 238 ▶ return { %{$self->{unacked_counts}} } if $exp_val != $got_val;
Branch: 1 branch
BranchTrueFalseTotal
if $exp_val != $got_val 89 149 238
69 }
70 120 return undef;
71 229 });
72 316 ▶ $self->{selector}->register(watcher_quota => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
73 316 my ($in) = @_;
74 316 my @watchers = $self->{selector}->watchers('watcher_quota');
75 316 ▶ if(int(@watchers) <= $self->{watcher_max}) {
Branch: 1 branch
BranchTrueFalseTotal
if (int @watchers <= $$self{"watcher_max"}) 300 16 316
76 300 return undef;
77 }
78 16 ▶ my $watcher_age = $in->{age} || 0;
Condition100%2/2
$$in{'age'} || 0
l!l
106
MC/DC100%1/1
$$in{'age'} || 0
$$in{'age'}
Truth table100%2/2
A
A= $$in{'age'}
Aresult
11
00
79 16 ▶ return $watcher_age > $self->{watcher_max} ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$watcher_age > $$self{'watcher_max'} ? : 4 12 16
80 229 });
81 }
82  
83 1857 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
84 1857 return shift->{name};
85 }
86  
87 917 ▶sub _get_from_storage {
Subroutine _get_from_storage: called CC=1 SCAR=0.0
88 917 my ($self, $method, $args_ref) = @_;
89 917 $args_ref->{timeline} = $self->name;
90 917 local @CARP_NOT = (ref($self->{storage}));
91 917 $self->{storage}->$method(%$args_ref);
92 }
93  
94 310 ▶sub get_statuses {
Subroutine get_statuses: called CC=1 SCAR=0.0
Pod documented
95 310 my ($self, %args) = @_;
96 310 $self->_get_from_storage("get_statuses", \%args);
97 }
98  
99 590 ▶sub get_unacked_counts {
Subroutine get_unacked_counts: called CC=1 SCAR=0.0
Pod documented
100 590 my ($self, %args) = @_;
101 590 $self->_get_from_storage("get_unacked_counts", \%args);
102 }
103  
104 319 ▶sub _write_statuses {
Subroutine _write_statuses: called CC=1 SCAR=0.0
105 319 my ($self, $method, $args_ref) = @_;
106 319 $args_ref->{timeline} = $self->name;
107 319 local @CARP_NOT = (ref($self->{storage}));
108 319 my $orig_callback = $args_ref->{callback};
109 317 ▶ $self->{storage}->$method(%$args_ref, callback => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
110 317 $self->_update_unacked_counts();
111 317 ▶ goto $orig_callback if defined($orig_callback);
Branch: 1 branch
BranchTrueFalseTotal
if defined $orig_callback 304 13 317
112 319 });
113 }
114  
115 32 ▶sub put_statuses {
Subroutine put_statuses: called CC=1 SCAR=0.0
Pod documented
116 32 my ($self, %args) = @_;
117 32 $self->_write_statuses('put_statuses', \%args);
118 }
119  
120 19 ▶sub delete_statuses {
Subroutine delete_statuses: called CC=1 SCAR=0.0
Pod documented
121 19 my ($self, %args) = @_;
122 19 $self->_write_statuses('delete_statuses', \%args);
123 }
124  
125 85 ▶sub ack_statuses {
Subroutine ack_statuses: called CC=1 SCAR=0.0
Pod documented
126 85 my ($self, %args) = @_;
127 85 $self->_write_statuses('ack_statuses', \%args);
128 }
129  
130 183 ▶sub add_statuses {
Subroutine add_statuses: called CC=4 SCAR=14.2
Pod documented
131 183 my ($self, %args) = @_;
132 183 ▶ if(!defined($args{statuses})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"statuses"}) 0 183 183
133 0 croak "statuses argument is mandatory";
134 }
135 183 my $ref = ref($args{statuses});
136 183 ▶ if($ref eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 9 174 183
elsif ($ref ne 'ARRAY') { } 0 174 174
137 9 $args{statuses} = [ $args{statuses} ];
138 }elsif($ref ne "ARRAY") {
139 0 croak "statuses argument must be a status or an array-ref of statuses";
140 }
141 183 my $statuses = dclone($args{statuses});
142 183 my $final_callback = $args{callback};
143 183 ▶ $self->{filter_flow}->execute($statuses, sub {
Subroutine __ANON__: called CC=7 SCAR=19.9
144 183 my $filter_result = shift;
145 183 my $cur_time;
146 183 foreach my $status (@$filter_result) {
147 1521 ▶ next if !defined($status) || ref($status) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $status or ref $status ne "HASH" 0 1521 1521
Condition33%1/3
not defined $status or ref $status ne "HASH"
l!l&&r!l&&!r
001521
MC/DC0%0/2
not defined $status or ref $status ne "HASH"
not defined $status ref $status ne "HASH"
Truth table33%1/3
not(A) or B
A= not defined $statusB= ref $status ne "HASH"
ABresult
1X1
011
000
148 1521 ▶ if(!defined($status->{id})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$status{"id"}) 19 1502 1521
149 19 $status->{id} = sprintf('busybird://%s/%s', $self->name, $self->{id_generator}->create_str);
150 }
151 1521 ▶ if(!defined($status->{created_at})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$status{"created_at"}) 16 1505 1521
152 16 ▶ $cur_time ||= DateTime->now;
Condition66%2/3
$cur_time ||= "DateTime"->now
l!l&&r!l&&!r
5110
MC/DC0%0/2
$cur_time ||= "DateTime"->now
$cur_time "DateTime"->now
Truth table66%2/3
A ||= B
A= $cur_timeB= "DateTime"->now
ABresult
1X1
011
000
153 16 $status->{created_at} = BusyBird::DateTime::Format->format_datetime($cur_time);
154 }
155 }
156 183 $self->_write_statuses('put_statuses', {
157 mode => 'insert', statuses => $filter_result,
158 callback => $final_callback
159 });
160 183 });
161 }
162  
163 29 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod documented
164 29 my ($self, $statuses, $callback) = @_;
165 29 $self->add_statuses(statuses => $statuses, callback => $callback);
166 }
167  
168 17 ▶sub contains {
Subroutine contains: called CC=1 SCAR=0.0
Pod documented
169 17 my ($self, %args) = @_;
170 17 $self->_get_from_storage("contains", \%args);
171 }
172  
173 57 ▶sub add_filter {
Subroutine add_filter: called CC=2 SCAR=6.9
Pod documented
174 57 my ($self, $filter, $is_async) = @_;
175 57 ▶ if(!$is_async) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_async) 33 24 57
176 33 my $sync_filter = $filter;
177 50 ▶ $filter = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
178 50 my ($statuses, $done) = @_;
179 50 @_ = $sync_filter->($statuses);
180 48 goto $done;
181 33 };
182 }
183 57 $self->{filter_flow}->add($filter);
184 }
185  
186 8 ▶sub add_filter_async {
Subroutine add_filter_async: called CC=1 SCAR=0.0
Pod documented
187 8 my ($self, $filter) = @_;
188 8 $self->add_filter($filter, 1);
189 }
190  
191 12 ▶sub set_config {
Subroutine set_config: called CC=1 SCAR=0.0
Pod documented
192 12 shift()->{config}->set_config(@_);
193 }
194  
195 310 ▶sub get_config {
Subroutine get_config: called CC=1 SCAR=0.0
Pod documented
196 310 shift()->{config}->get_config(@_);
197 }
198  
199 177 ▶sub watch_unacked_counts {
Subroutine watch_unacked_counts: called CC=11 SCAR=24.0
Pod documented
200 177 my ($self, %watch_args) = @_;
201 177 my $callback = $watch_args{callback};
202 177 my $assumed = $watch_args{assumed};
203 177 ▶ if(!defined($callback) || ref($callback) ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $callback or ref $callback ne "CODE") 10 167 177
Condition100%3/3
not defined $callback or ref $callback ne "CODE"
l!l&&r!l&&!r
46167
MC/DC100%2/2
not defined $callback or ref $callback ne "CODE"
not defined $callback ref $callback ne "CODE"
Truth table100%3/3
not(A) or B
A= not defined $callbackB= ref $callback ne "CODE"
ABresult
1X1
011
000
204 10 croak "watch_unacked_counts: callback must be a code-ref";
205 }
206 167 ▶ if(!defined($assumed) || ref($assumed) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $assumed or ref $assumed ne "HASH") 10 157 167
Condition100%3/3
not defined $assumed or ref $assumed ne "HASH"
l!l&&r!l&&!r
46157
MC/DC100%2/2
not defined $assumed or ref $assumed ne "HASH"
not defined $assumed ref $assumed ne "HASH"
Truth table100%3/3
not(A) or B
A= not defined $assumedB= ref $assumed ne "HASH"
ABresult
1X1
011
000
207 10 croak "watch_unacked_counts: assumed must be a hash-ref";
208 }
209 157 $assumed = +{ %$assumed };
210 157 foreach my $key (keys %$assumed) {
211 199 ▶ next if $key eq 'total' || (looks_like_number($key) && int($key) == $key);
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "total" or &looks_like_number($key) and int $key == $key 184 15 199
Condition83%5/6
$key eq "total" or &looks_like_number($key) and int $key == $key
l!l&&r!l&&!r
8210215
&looks_like_number($key) and int $key == $key
!ll&&!rl&&r
150102
MC/DC66%2/3
$key eq "total" or &looks_like_number($key) and int $key == $key
$key eq "total" &looks_like_number($key) int $key == $key
Truth table75%3/4
A or B and C
A= $key eq "total"B= &looks_like_number($key)C= int $key == $key
ABCresult
1XX1
0111
00X0
0100
212 15 delete $assumed->{$key};
213 }
214 157 my $watcher = BusyBird::Watcher::Aggregator->new();
215 my $orig_watcher = $self->{selector}->watch(
216 103 ▶ unacked_counts => $assumed, watcher_quota => { age => 0 }, sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
217 103 my ($orig_w, %res) = @_;
218 103 ▶ if($res{watcher_quota}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res{"watcher_quota"}) 4 99 103
219 4 $watcher->cancel();
220 4 $callback->("watcher cancelled because it is too old", $watcher);
221 4 return;
222 }
223 99 ▶ if($res{unacked_counts}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res{"unacked_counts"}) 99 0 99
224 99 $callback->(undef, $watcher, $res{unacked_counts});
225 99 return;
226 }
227 0 confess("Something terrible happened.");
228 }
229 157 );
230 157 $watcher->add($orig_watcher);
231 157 ▶ if($watcher->active) {
Branch: 1 branch
BranchTrueFalseTotal
if ($watcher->active) 119 38 157
232 119 my @quota_watchers = $self->{selector}->watchers('watcher_quota');
233 119 foreach my $w (@quota_watchers) {
234 159 my %cond = $w->conditions;
235 159 $cond{watcher_quota}{age}++;
236 }
237 119 $self->{selector}->trigger('watcher_quota');
238 }
239 157 return $watcher;
240 }
241  
242  
243 11 1;
244  
245 __END__
« BusyBird/Main.pm ↑ Summary BusyBird/Filter/Twitter.pm »