lib/App/teai/Presence.pm

filter: Statementstmt 20.1% 32 / 159 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 69 MC/DCmcdc 0.0% 0 / 38 Subroutinesub 22.5% 7 / 31 Podpod 100% 5 / 5 totaltotal 12.0% 44 / 364 CC 65 SCAR 37.7
CC
65
Cov
11%
_sessions
110.0
_value
90.0
__ANON__
72.0
CRAP 43.5
« lib/App/teai/MCP/Tools.pm ↑ Summary lib/App/teai/Address.pm »
1 package App::teai::Presence;
2 10 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use EV;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Cpanel::JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Cpanel::JSON::XS::Type ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use App::teai::Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use App::teai::Wire ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 our $VERSION = '0.08';
11  
12 10 my $BUCKET = 'teai_peers';
13 10 my $BEAT = 40;
14 10 my $RETRY = 1;
15 10 my $STOP_WAIT = 1;
16 10 my %KINDS = map { $_ => 1 } qw(mcp waiter wait exec);
17 10 my %INTERACTIVE = map { $_ => 1 } qw(mcp waiter wait);
18 10 my $KEY = qr/\A([A-Za-z0-9_-]{1,64})\.([A-Za-z0-9_-]{1,64})\.[1-9]\d*-\d+\z/;
19 10 my $JSON = Cpanel::JSON::XS->new->utf8->canonical;
20  
21 0 ▶sub key ($session) {
Subroutine key: not called CC=2 SCAR=17.9
Pod documented
22 0 require App::teai::Hook;
23 0 ▶ my $start = App::teai::Hook::starttime($$) // 0;
Condition0%0/2
App::teai::Hook::starttime($$) // 0
l!l
00
MC/DC0%0/1
App::teai::Hook::starttime($$) // 0
App::teai::Hook::starttime($$)
Truth table0%0/2
A
A= App::teai::Hook::starttime($$)
Aresult
11
00
24 0 return App::teai::Config::host() . ".$session.$$-$start";
25 }
26  
27 0 ▶sub _beat_secs () {
Subroutine _beat_secs: not called CC=4 SCAR=30.0
28 0 my $v = $ENV{TEAI_TEST_PRESENCE_BEAT};
29 0 ▶ return defined $v && $v =~ /\A\d+(?:\.\d+)?\z/ && $v > 0 ? $v : $BEAT;
Branch: 1 branch
BranchTrueFalseTotal
defined $v && $v =~ /\A\d+(?:\.\d+)?\z/u && $v > 0 ? : 0 0 0
Condition0%0/6
defined $v and $v =~ /\A\d+(?:\.\d+)?\z/u and $v > 0
!ll&&!rl&&r
000
defined $v and $v =~ /\A\d+(?:\.\d+)?\z/u
!ll&&!rl&&r
000
MC/DC0%0/3
defined $v and $v =~ /\A\d+(?:\.\d+)?\z/u and $v > 0
defined $v $v =~ /\A\d+(?:\.\d+)?\z/u $v > 0
Truth table0%0/4
A and B and C
A= defined $vB= $v =~ /\A\d+(?:\.\d+)?\z/uC= $v > 0
ABCresult
0XX0
10X0
1100
1111
30 }
31  
32 0 ▶sub new ($class, %a) {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
33 0 my $self = bless { kind => $a{kind}, session => $a{session}, getter => $a{bus} }, $class;
34 0 local $@;
35 0 eval {
36 0 $self->{key} = key($a{session});
37 0 $self->{agent} = App::teai::Config::agent_of($a{session});
38 0 $self->{project} = eval { App::teai::Config::project() };
39 0 $self->{role} = eval { App::teai::Config::role() };
40 0 $self->{beat} = _beat_secs();
41 0 $self->_arm(0);
42 0 1;
43 };
44 0 return $self;
45 }
46  
47 0 ▶sub _arm ($self, $after) {
Subroutine _arm: not called CC=1 SCAR=6.9
48 0 Scalar::Util::weaken(my $weak = $self);
49 0 EV::now_update();
50 0 ▶ my $t = EV::timer $after, 0, sub { $weak->_beat if $weak };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $weak 0 0 0
51 0 $t->keepalive(0);
52 0 $self->{timer} = $t;
53 }
54  
55 0 ▶sub _retry ($self) { $self->_arm($RETRY) unless $self->{stopped} }
Subroutine _retry: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"stopped"} 0 0 0
56  
57 0 ▶sub _beat ($self) {
Subroutine _beat: not called CC=7 SCAR=40.3
58 0 ▶ return if $self->{stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stopped"} 0 0 0
59 0 local $@;
60 0 Scalar::Util::weaken(my $weak = $self);
61 0 my $sent = eval {
62 0 my $bus = $self->{getter}->();
63 0 ▶ return 0 unless $bus && $bus->nats && $bus->nats->is_connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $bus and $bus->nats and $bus->nats->is_connected 0 0 0
Condition0%0/6
$bus and $bus->nats and $bus->nats->is_connected
!ll&&!rl&&r
000
$bus and $bus->nats
!ll&&!rl&&r
000
MC/DC0%0/3
$bus and $bus->nats and $bus->nats->is_connected
$bus $bus->nats $bus->nats->is_connected
Truth table0%0/4
A and B and C
A= $busB= $bus->natsC= $bus->nats->is_connected
ABCresult
0XX0
10X0
1100
1111
64 0 Scalar::Util::weaken(my $via = $bus);
65 0 $self->_arm($self->{beat});
66 0 ▶ $bus->session_attrs($self->{session}, sub ($attrs, $err = undef, @) {
Subroutine __ANON__: not called CC=8 SCAR=42.8
67 0 local $@;
68 0 eval {
69 0 ▶ return if !$weak || $weak->{stopped};
Branch: 1 branch
BranchTrueFalseTotal
if not $weak or $weak->{"stopped"} 0 0 0
Condition0%0/3
not $weak or $weak->{"stopped"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $weak or $weak->{"stopped"}
not $weak $weak->{"stopped"}
Truth table0%0/3
not(A) or B
A= not $weakB= $weak->{"stopped"}
ABresult
1X1
011
000
70 0 ▶ return $weak->_retry unless $via;
Branch: 1 branch
BranchTrueFalseTotal
unless $via 0 0 0
71 0 ▶ if (!defined $err) { @$weak{qw(read attrs)} = (1, $attrs) }
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $err) { } 0 0 0
elsif (not $weak->{'read'}) { } 0 0 0
72 0 ▶ elsif (!$weak->{read}) { return App::teai::Bus::no_bucket($err) ? () : $weak->_retry }
Branch: 1 branch
BranchTrueFalseTotal
App::teai::Bus::no_bucket($err) ? : 0 0 0
73 0 $weak->_write($via, $weak->_fields);
74 };
75 0 });
76 0 1;
77 };
78 0 ▶ eval { $self->_arm($RETRY) } unless $sent || $self->{stopped};
Branch: 1 branch
BranchTrueFalseTotal
unless $sent or $self->{"stopped"} 0 0 0
Condition0%0/3
$sent or $self->{"stopped"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$sent or $self->{"stopped"}
$sent $self->{"stopped"}
Truth table0%0/3
A or B
A= $sentB= $self->{"stopped"}
ABresult
1X1
011
000
79 }
80  
81 0 ▶sub _fields ($self) {
Subroutine _fields: not called CC=5 SCAR=34.0
82 0 my $attrs = $self->{attrs};
83 0 ▶ my $own = $attrs && defined $attrs->{agent};
Condition0%0/3
$attrs && defined $attrs->{'agent'}
!ll&&!rl&&r
000
MC/DC0%0/2
$attrs && defined $attrs->{'agent'}
$attrs defined $attrs->{'agent'}
Truth table0%0/3
A && B
A= $attrsB= defined $attrs->{'agent'}
ABresult
0X0
100
111
84 return {
85 agent => $own ? $attrs->{agent} : $self->{agent}, kind => $self->{kind}, pid => 0 + $$,
86 project => $own ? $attrs->{project} : $self->{project},
87 role => $own ? $attrs->{role} : $self->{role},
88 0 ▶ };
Branch: 3 branches
BranchTrueFalseTotal
$own ? : 0 0 0
$own ? : 0 0 0
$own ? : 0 0 0
89 }
90  
91 0 ▶sub _write ($self, $bus, $v) {
Subroutine _write: not called CC=1 SCAR=6.9
92 0 Scalar::Util::weaken(my $weak = $self);
93 0 Scalar::Util::weaken(my $via = $bus);
94 0 ▶ $bus->removed($self->{session}, sub ($gone, $err = undef) {
Subroutine __ANON__: not called CC=6 SCAR=37.4
95 0 local $@;
96 0 eval {
97 0 ▶ return if !$weak || $weak->{stopped};
Branch: 1 branch
BranchTrueFalseTotal
if not $weak or $weak->{"stopped"} 0 0 0
Condition0%0/3
not $weak or $weak->{"stopped"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $weak or $weak->{"stopped"}
not $weak $weak->{"stopped"}
Truth table0%0/3
not(A) or B
A= not $weakB= $weak->{"stopped"}
ABresult
1X1
011
000
98 0 ▶ return $weak->_retry unless $via;
Branch: 1 branch
BranchTrueFalseTotal
unless $via 0 0 0
99 0 ▶ return $weak->_gone($via) if $gone;
Branch: 1 branch
BranchTrueFalseTotal
if $gone 0 0 0
100 0 $weak->_put($via, $v);
101 };
102 0 });
103 }
104  
105 0 ▶sub _gone ($self, $bus) {
Subroutine _gone: not called CC=1 SCAR=6.9
106 0 $self->{stopped} = 1;
107 0 delete @$self{qw(timer via)};
108 0 $bus->kv_delete($BUCKET, $self->{key}, sub (@) {});
109 }
110  
111 0 ▶sub _put ($self, $bus, $v) {
Subroutine _put: not called CC=1 SCAR=6.9
112 0 my $bytes = $JSON->encode({ %$v, ts => App::teai::Wire::utc_now() });
113 0 Scalar::Util::weaken(my $weak = $self);
114 0 ▶ $bus->kv_put($BUCKET, $self->{key}, $bytes, sub ($ok = undef, $err = undef, @) {
Subroutine __ANON__: not called CC=6 SCAR=37.4
115 0 local $@;
116 0 ▶ eval { $weak->_retry if $weak && !$ok && !App::teai::Bus::no_bucket($err) };
Branch: 1 branch
BranchTrueFalseTotal
if $weak and not $ok and not App::teai::Bus::no_bucket($err) 0 0 0
Condition0%0/6
$weak and not $ok and not App::teai::Bus::no_bucket($err)
!ll&&!rl&&r
000
$weak and not $ok
!ll&&!rl&&r
000
MC/DC0%0/3
$weak and not $ok and not App::teai::Bus::no_bucket($err)
$weak not $ok not App::teai::Bus::no_bucket($err)
Truth table0%0/4
A and not(B) and not(C)
A= $weakB= not $okC= not App::teai::Bus::no_bucket($err)
ABCresult
0XX0
10X0
1100
1111
117 0 });
118 0 Scalar::Util::weaken($self->{via} = $bus);
119 }
120  
121 0 ▶sub stop ($self) {
Subroutine stop: not called CC=6 SCAR=37.4
Pod documented
122 0 local $@;
123 0 eval {
124 0 ▶ if (!$self->{stopped}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"stopped"}++) 0 0 0
125 0 delete $self->{timer};
126 0 my $bus = delete $self->{via};
127 0 ▶ if ($bus && $bus->nats && $bus->nats->is_connected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bus and $bus->nats and $bus->nats->is_connected) 0 0 0
Condition0%0/6
$bus and $bus->nats and $bus->nats->is_connected
!ll&&!rl&&r
000
$bus and $bus->nats
!ll&&!rl&&r
000
MC/DC0%0/3
$bus and $bus->nats and $bus->nats->is_connected
$bus $bus->nats $bus->nats->is_connected
Truth table0%0/4
A and B and C
A= $busB= $bus->natsC= $bus->nats->is_connected
ABCresult
0XX0
10X0
1100
1111
128 0 my $over;
129 0 EV::now_update();
130 0 ▶ my $guard = EV::timer $STOP_WAIT, 0, sub { $over = 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 0 ▶ $bus->kv_delete($BUCKET, $self->{key}, sub (@) { $over = 1 });
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 0 ▶ EV::run(EV::RUN_ONCE) until $over;
Branch: 1 branch
BranchTrueFalseTotal
$over or do { &EV::run(2) } 0 0 0
133 }
134 }
135 0 1;
136 };
137 0 return;
138 }
139  
140 0 ▶sub interactive ($live) { ref $live eq 'ARRAY' && grep({ defined && $INTERACTIVE{$_} } @$live) ? 1 : 0 }
Subroutine interactive: not called CC=4 SCAR=30.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $live eq 'ARRAY' && grep({defined $_ and $INTERACTIVE{$_};} @$live) ? : 0 0 0
Condition0%0/6
ref $live eq "ARRAY" and grep {defined $_ and $INTERACTIVE{$_};} @$live
!ll&&!rl&&r
000
defined $_ && $INTERACTIVE{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $live eq "ARRAY" and grep {defined $_ and $INTERACTIVE{$_};} @$live
ref $live eq "ARRAY" grep {defined $_ and $INTERACTIVE{$_};} @$live
MC/DC0%0/2
defined $_ && $INTERACTIVE{$_}
defined $_ $INTERACTIVE{$_}
Truth table0%0/3
A and B
A= ref $live eq "ARRAY"B= grep {defined $_ and $INTERACTIVE{$_};} @$live
ABresult
0X0
100
111
Truth table0%0/3
A && B
A= defined $_B= $INTERACTIVE{$_}
ABresult
0X0
100
111
141  
142 0 ▶sub _value ($bytes) {
Subroutine _value: not called CC=9 SCAR=45.0
143 0 my $types;
144 0 my $v = eval { $JSON->decode($bytes, $types) };
145 0 ▶ return undef unless ref $v eq 'HASH' && defined $v->{kind} && !ref $v->{kind} && $KINDS{ $v->{kind} };
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v eq "HASH" and defined $v->{"kind"} and not ref $v->{"kind"} and $KINDS{$v->{"kind"}} 0 0 0
Condition0%0/9
ref $v eq "HASH" and defined $v->{"kind"} and not ref $v->{"kind"} and $KINDS{$v->{"kind"}}
!ll&&!rl&&r
000
ref $v eq "HASH" and defined $v->{"kind"} and not ref $v->{"kind"}
!ll&&!rl&&r
000
ref $v eq "HASH" and defined $v->{"kind"}
!ll&&!rl&&r
000
MC/DC0%0/4
ref $v eq "HASH" and defined $v->{"kind"} and not ref $v->{"kind"} and $KINDS{$v->{"kind"}}
ref $v eq "HASH" defined $v->{"kind"} not ref $v->{"kind"} $KINDS{$v->{"kind"}}
Truth table0%0/5
A and B and not(C) and D
A= ref $v eq "HASH"B= defined $v->{"kind"}C= not ref $v->{"kind"}D= $KINDS{$v->{"kind"}}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
146 0 ▶ my $t = ref $types eq 'HASH' ? $types->{ts} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $types eq 'HASH' ? : 0 0 0
147 0 ▶ return undef unless defined $t && !ref $t && $t == Cpanel::JSON::XS::Type::JSON_TYPE_STRING();
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t and not ref $t and $t == 4 0 0 0
Condition0%0/6
defined $t and not ref $t and $t == 4
!ll&&!rl&&r
000
defined $t and not ref $t
!ll&&!rl&&r
000
MC/DC0%0/3
defined $t and not ref $t and $t == 4
defined $t not ref $t $t == 4
Truth table0%0/4
A and not(B) and C
A= defined $tB= not ref $tC= $t == 4
ABCresult
0XX0
10X0
1100
1111
148 0 return $v;
149 }
150  
151 0 ▶sub _attrs ($session, $v) {
Subroutine _attrs: not called CC=2 SCAR=17.9
152 0 my $a = App::teai::Bus::attrs_of($v);
153 0 ▶ return { agent => $a->{agent} // App::teai::Config::agent_of($session), project => $a->{project}, role => $a->{role} };
Condition0%0/3
$a->{"agent"} // App::teai::Config::agent_of($session)
l!l&&r!l&&!r
000
MC/DC0%0/2
$a->{"agent"} // App::teai::Config::agent_of($session)
$a->{"agent"} App::teai::Config::agent_of($session)
Truth table0%0/3
A // B
A= $a->{"agent"}B= App::teai::Config::agent_of($session)
ABresult
1X1
011
000
154 }
155  
156 0 ▶sub list ($bus, $cb) {
Subroutine list: not called CC=1 SCAR=6.9
Pod documented
157 0 ▶ $bus->kv_entries($BUCKET, sub ($entries, $err = undef, $failed = {}) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
158 0 ▶ return $cb->(undef, $err) if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 0 0 0
159 0 my $list = _sessions($entries, $failed);
160 0 ▶ $bus->removed_ids([ sort keys %$list ], sub ($gone, $e = undef) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
161 0 ▶ return $cb->(undef, $e) if defined $e;
Branch: 1 branch
BranchTrueFalseTotal
if defined $e 0 0 0
162 0 delete @$list{ keys %$gone };
163 0 $cb->($list);
164 0 });
165 0 });
166 }
167  
168 0 ▶sub _sessions ($entries, $failed) {
Subroutine _sessions: not called CC=10 SCAR=47.0
169 0 my %by;
170 0 for my $k (sort keys %$entries) {
171 0 ▶ my ($host, $session) = $k =~ $KEY or next;
Branch: 1 branch
BranchTrueFalseTotal
() = $k =~ /$KEY/u or next 0 0 0
172 0 ▶ my $v = _value($entries->{$k}) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $v = _value($entries->{$k}) or next 0 0 0
173 0 ▶ my $s = $by{$session} //= { kinds => {}, hosts => {} };
Condition0%0/2
$by{$session} //= {"kinds", {}, "hosts", {}}
l!l
00
MC/DC0%0/1
$by{$session} //= {"kinds", {}, "hosts", {}}
$by{$session}
Truth table0%0/2
A
A= $by{$session}
Aresult
11
00
174 0 $s->{kinds}{ $v->{kind} } = 1;
175 0 $s->{hosts}{$host} = 1;
176 0 ▶ next if defined $s->{seen} && $v->{ts} le $s->{seen};
Branch: 1 branch
BranchTrueFalseTotal
if defined $s->{"seen"} and $v->{"ts"} le $s->{"seen"} 0 0 0
Condition0%0/3
defined $s->{"seen"} and $v->{"ts"} le $s->{"seen"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $s->{"seen"} and $v->{"ts"} le $s->{"seen"}
defined $s->{"seen"} $v->{"ts"} le $s->{"seen"}
Truth table0%0/3
A and B
A= defined $s->{"seen"}B= $v->{"ts"} le $s->{"seen"}
ABresult
0X0
100
111
177 0 $s->{seen} = $v->{ts};
178 0 $s->{attrs} = _attrs($session, $v);
179 }
180 my %list = map {
181 0 my $s = $by{$_};
182 0 ($_ => { live => [ sort keys %{ $s->{kinds} } ], hosts => [ sort keys %{ $s->{hosts} } ], seen => $s->{seen}, %{ $s->{attrs} } })
183 } keys %by;
184 0 for my $k (sort keys %$failed) {
185 0 ▶ my (undef, $session) = $k =~ $KEY or next;
Branch: 1 branch
BranchTrueFalseTotal
my(undef, $session) = $k =~ /$KEY/u or next 0 0 0
186 0 ▶ $list{$session} //= { live => undef, seen => undef, error => $failed->{$k} };
Condition0%0/2
$list{$session} //= {"live", undef, "seen", undef, "error", $failed->{$k}}
l!l
00
MC/DC0%0/1
$list{$session} //= {"live", undef, "seen", undef, "error", $failed->{$k}}
$list{$session}
Truth table0%0/2
A
A= $list{$session}
Aresult
11
00
187 }
188 0 return \%list;
189 }
190  
191 10 1;
192 __END__
« lib/App/teai/MCP/Tools.pm ↑ Summary lib/App/teai/Address.pm »