| 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
| |||||||||||||||||||||||||||||
| 18 | 31 | my $ids; | |||||||||||||||||||||||||||
| 19 | 31 | ▶ | if(defined($args{ids})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 16 | ▶ | if(!ref($args{ids})) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 {
}
MC/DC0%0/2
$args{'callback'} || sub {
}
$args{'callback'}
sub {
}
Truth table33%1/3
A || B
A= $args{'callback'}B= sub {
}
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $ids or defined $max_id
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $ids and @$ids
MC/DC100%2/2
defined $ids and @$ids
defined $ids
@$ids
Truth table100%3/3
A and B
A= defined $idsB= @$ids
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 101 | 15 | ▶ | croak 'query argument is mandatory' if not defined($query); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 14 | ▶ | croak 'callback argument is mandatory' if not defined($callback); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 13 | ▶ | if(ref($query) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 104 | ; | ||||||||||||||||||||||||||||
| 105 | ▶ | }elsif(ref($query) eq 'HASH' || !ref($query)) { | |||||||||||||||||||||||||||
|
Condition66%2/3
ref $query eq "HASH" or not ref $query
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 111 | 1 | croak 'query argument must not contain undef'; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 12 | ▶ | if(!@$query) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 120 | 56 | ▶ | defined($id) ? future_of($self, "get_statuses", timeline => $timeline, count => 1, max_id => $id) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 152 | 15 | ▶ | croak 'callback arg is mandatory' if not defined $args{callback}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$$status{'busybird'}{'level'} || 0
$$status{'busybird'}{'level'}
Truth table100%2/2
A
A= $$status{'busybird'}{'level'}
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||