BusyBird/StatusStorage/Common.pm

filter: Statementstmt 91.7% 123 / 134 Branchbran 91.3% 42 / 46 Conditioncond 78.5% 11 / 14 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 88.0% 22 / 25 Podpod 100% 3 / 3 totaltotal 89.1% 206 / 231 CC 32 SCAR 16.4
CC
32
Cov
91%
contains
11.2
ack_statuses
9.2
__ANON__
4.1
CRAP 5.2
« BusyBird/Test/StatusStorage.pm ↑ Summary BusyBird/Main.pm »
1 package BusyBird::StatusStorage::Common;
2 2 ▶use v5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Exporter 5.57 qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use BusyBird::Util qw(future_of);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use BusyBird::DateTime::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Future::Q;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our @EXPORT_OK = qw(contains ack_statuses get_unacked_counts);
14  
15 32 ▶sub ack_statuses {
Subroutine ack_statuses: called CC=9 SCAR=22.2
Pod documented
16 32 my ($self, %args) = @_;
17 32 ▶ croak 'timeline arg is mandatory' if not defined $args{timeline};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"timeline"} 1 31 32
18 31 my $ids;
19 31 ▶ if(defined($args{ids})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"ids"}) 16 15 31
20 16 ▶ if(!ref($args{ids})) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $args{'ids'}) { } 6 10 16
elsif (ref $args{'ids'} eq 'ARRAY') { } 10 0 10
21 6 $ids = [$args{ids}];
22 }elsif(ref($args{ids}) eq 'ARRAY') {
23 10 $ids = $args{ids};
24 10 ▶ croak "ids arg array must not contain undef" if grep { !defined($_) } @$ids;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} @$ids 1 9 10
25 }else {
26 0 croak "ids arg must be either undef, status ID or array-ref of IDs";
27 }
28 }
29 30 my $max_id = $args{max_id};
30 30 my $timeline = $args{timeline};
31 30 ▶ my $callback = $args{callback} || sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$args{'callback'} || sub { }
l!l&&r!l&&!r
3000
MC/DC0%0/2
$args{'callback'} || sub { }
$args{'callback'} sub { }
Truth table33%1/3
A || B
A= $args{'callback'}B= sub { }
ABresult
1X1
011
000
32 30 my $ack_str = BusyBird::DateTime::Format->format_datetime(
33 DateTime->now(time_zone => 'UTC')
34 );
35 30 my @subfutures = (_get_unacked_statuses_by_ids_future($self, $timeline, $ids));
36 30 ▶ if(!defined($ids) || defined($max_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ids or defined $max_id) 21 9 30
Condition100%3/3
not defined $ids or defined $max_id
l!l&&r!l&&!r
1569
MC/DC100%2/2
not defined $ids or defined $max_id
not defined $ids defined $max_id
Truth table100%3/3
not(A) or B
A= not defined $idsB= defined $max_id
ABresult
1X1
011
000
37 21 push @subfutures, future_of(
38 $self, 'get_statuses',
39 timeline => $timeline,
40 max_id => $max_id, count => 'all',
41 ack_state => 'unacked'
42 );
43 }
44 30 ▶ Future::Q->needs_all(@subfutures)->then(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
45 30 my @statuses_list = @_;
46 30 my @target_statuses = _uniq_statuses(map { @$_ } @statuses_list);
47 30 ▶ if(!@target_statuses) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@target_statuses) 6 24 30
48 6 return 0;
49 }
50 24 $_->{busybird}{acked_at} = $ack_str foreach @target_statuses;
51 24 return future_of(
52 $self, 'put_statuses',
53 timeline => $timeline, mode => 'update',
54 statuses => \@target_statuses,
55 );
56 30 ▶ })->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 ## invocations of $callback should be at the same level of
58 ## then() chain, because $callback might throw exception and
59 ## we should not catch that exception.
60
61 30 my ($changed) = @_;
62 30 @_ = (undef, $changed);
63 30 goto $callback;
64 0 ▶ }, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 0 my ($error) = @_;
66 0 @_ = ($error);
67 0 goto $callback;
68 30 });
69 }
70  
71 30 ▶sub _get_unacked_statuses_by_ids_future {
Subroutine _get_unacked_statuses_by_ids_future: called CC=3 SCAR=11.0
72 30 my ($self, $timeline, $ids) = @_;
73 30 ▶ if(!defined($ids) || !@$ids) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ids and @$ids) 17 13 30
Condition100%3/3
defined $ids and @$ids
!ll&&!rl&&r
15213
MC/DC100%2/2
defined $ids and @$ids
defined $ids @$ids
Truth table100%3/3
A and B
A= defined $idsB= @$ids
ABresult
0X0
100
111
74 17 return Future::Q->new->fulfill([]);
75 }
76 my @status_futures = map {
77 13 my $id = $_;
78 36 future_of(
79 $self, 'get_statuses',
80 timeline => $timeline, max_id => $id, ack_state => 'unacked', count => 1
81 );
82 } @$ids;
83 13 ▶ return Future::Q->needs_all(@status_futures)->then(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
84 13 my @statuses_list = @_;
85 13 ▶ return [ map { defined($_->[0]) ? ($_->[0]) : () } @statuses_list ];
Branch: 1 branch
BranchTrueFalseTotal
defined $_->[0] ? : 24 12 36
86 13 });
87 }
88  
89 30 ▶sub _uniq_statuses {
Subroutine _uniq_statuses: called CC=1 SCAR=0.0
90 30 my (@statuses) = @_;
91 30 my %id_to_s = map { $_->{id} => $_ } @statuses;
92 30 return values %id_to_s;
93 }
94  
95 16 ▶sub contains {
Subroutine contains: called CC=11 SCAR=24.2
Pod documented
96 16 my ($self, %args) = @_;
97 16 my $timeline = $args{timeline};
98 16 my $query = $args{query};
99 16 my $callback = $args{callback};
100 16 ▶ croak 'timeline argument is mandatory' if not defined($timeline);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeline 1 15 16
101 15 ▶ croak 'query argument is mandatory' if not defined($query);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query 1 14 15
102 14 ▶ croak 'callback argument is mandatory' if not defined($callback);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $callback 1 13 14
103 13 ▶ if(ref($query) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $query eq 'ARRAY') { } 8 5 13
elsif (ref $query eq 'HASH' or not ref $query) { } 5 0 5
104 ;
105 ▶ }elsif(ref($query) eq 'HASH' || !ref($query)) {
Condition66%2/3
ref $query eq "HASH" or not ref $query
l!l&&r!l&&!r
320
MC/DC0%0/2
ref $query eq "HASH" or not ref $query
ref $query eq "HASH" not ref $query
Truth table66%2/3
A or not(B)
A= ref $query eq "HASH"B= not ref $query
ABresult
1X1
011
000
106 5 $query = [$query];
107 }else {
108 0 croak 'query argument must be either STATUS, ID or ARRAYREF_OF_STATUSES_OR_IDS';
109 }
110 13 ▶ if(grep { !defined($_) } @$query) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not defined $_;} @$query) 1 12 13
111 1 croak 'query argument must not contain undef';
112 }
113 12 ▶ if(!@$query) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$query) 1 11 12
114 1 @_ = (undef, [], []);
115 1 goto $callback;
116 }
117 my @subfutures = map {
118 11 my $query_elem = $_;
119 56 ▶ my $id = ref($query_elem) ? $query_elem->{id} : $query_elem;
Branch: 1 branch
BranchTrueFalseTotal
ref $query_elem ? : 13 43 56
120 56 ▶ defined($id) ? future_of($self, "get_statuses", timeline => $timeline, count => 1, max_id => $id)
Branch: 1 branch
BranchTrueFalseTotal
defined $id ? : 53 3 56
121 : Future::Q->new->fulfill([]); ## ID-less status is always 'not contained'.
122 } @$query;
123 11 ▶ Future::Q->needs_all(@subfutures)->then(sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
124 11 my (@statuses_list) = @_;
125 11 ▶ if(@statuses_list != @$query) {
Branch: 1 branch
BranchTrueFalseTotal
if (@statuses_list != @$query) 0 11 11
126 0 confess("fatal error: number of statuses_list does not match the number of query");
127 }
128 11 my @contained = ();
129 11 my @not_contained = ();
130 11 foreach my $i (0 .. $#statuses_list) {
131 56 ▶ if(@{$statuses_list[$i]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$statuses_list[$i];}) { } 20 36 56
132 20 push @contained, $query->[$i];
133 }else {
134 36 push @not_contained, $query->[$i];
135 }
136 }
137 11 return (\@contained, \@not_contained);
138 11 ▶ })->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
139 11 my ($contained, $not_contained) = @_;
140 11 @_ = (undef, $contained, $not_contained);
141 11 goto $callback;
142 0 ▶ }, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 0 my ($error) = @_;
144 0 @_ = ($error);
145 0 goto $callback;
146 11 });
147 }
148  
149 16 ▶sub get_unacked_counts {
Subroutine get_unacked_counts: called CC=3 SCAR=11.0
Pod documented
150 16 my ($self, %args) = @_;
151 16 ▶ croak 'timeline arg is mandatory' if not defined $args{timeline};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"timeline"} 1 15 16
152 15 ▶ croak 'callback arg is mandatory' if not defined $args{callback};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"callback"} 1 14 15
153 14 my $timeline = $args{timeline};
154 14 my $callback = $args{callback};
155
156 ## get_statuses() called plainly. its exception propagates to the caller.
157 $self->get_statuses(
158 timeline => $timeline, ack_state => "unacked", count => "all",
159 14 ▶ callback => sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
160 14 my ($error, $statuses) = @_;
161 14 ▶ if(defined($error)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 0 14 14
162 0 @_ = ("get error: $error");
163 0 goto $callback;
164 }
165 14 my %count = (total => int(@$statuses));
166 14 foreach my $status (@$statuses) {
167 39 my $level = do {
168 2 ▶ no autovivification;
Subroutine BEGIN: called CC=1 SCAR=0.0
169 39 ▶ $status->{busybird}{level} || 0;
Condition100%2/2
$$status{'busybird'}{'level'} || 0
l!l
1326
MC/DC100%1/1
$$status{'busybird'}{'level'} || 0
$$status{'busybird'}{'level'}
Truth table100%2/2
A
A= $$status{'busybird'}{'level'}
Aresult
11
00
170 };
171 39 $count{$level}++;
172 }
173 14 @_ = (undef, \%count);
174 14 goto $callback;
175 }
176 14 );
177 }
178  
179  
180 2 1;
181 __END__
« BusyBird/Test/StatusStorage.pm ↑ Summary BusyBird/Main.pm »