blib/lib/PDL/ML/Gateway.pm

filter: Statementstmt 64.5% 51 / 79 Branchbran 60.0% 6 / 10 Conditioncond 47.3% 18 / 38 MC/DCmcdc 21.0% 4 / 19 Subroutinesub 56.2% 9 / 16 Podpod 0.0% 0 / 6 totaltotal 52.3% 88 / 168 CC 25 SCAR 19.3
CC
25
Cov
59%
__ANON__
12.0
select_node
11.4
add_node
8.6
CRAP 6.9
↑ Summary
1 package PDL::ML::Gateway;
2  
3 2 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use JSON::PP qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = '0.012';
10  
11 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
12 2 my ($class, %args) = @_;
13 return bless {
14 2 ▶ nodes => $args{nodes} || [],
Condition50%1/2
$args{'nodes'} || []
l!l
02
MC/DC0%0/1
$args{'nodes'} || []
$args{'nodes'}
Truth table50%1/2
A
A= $args{'nodes'}
Aresult
11
00
15 }, $class;
16 }
17  
18 4 ▶sub add_node {
Subroutine add_node: called CC=7 SCAR=21.5
Pod undocumented
19 4 my ($self, %node) = @_;
20 4 ▶ croak 'node name and url required' unless $node{name} && $node{url};
Branch: 1 branch
BranchTrueFalseTotal
unless $node{"name"} and $node{"url"} 0 4 4
Condition33%1/3
$node{"name"} and $node{"url"}
!ll&&!rl&&r
004
MC/DC0%0/2
$node{"name"} and $node{"url"}
$node{"name"} $node{"url"}
Truth table33%1/3
A and B
A= $node{"name"}B= $node{"url"}
ABresult
0X0
100
111
21 4 ▶ $node{healthy} //= 1;
Condition50%1/2
$node{"healthy"} //= 1
l!l
04
MC/DC0%0/1
$node{"healthy"} //= 1
$node{"healthy"}
Truth table50%1/2
A
A= $node{"healthy"}
Aresult
11
00
22 4 ▶ $node{priority} //= 10;
Condition50%1/2
$node{"priority"} //= 10
l!l
40
MC/DC0%0/1
$node{"priority"} //= 10
$node{"priority"}
Truth table50%1/2
A
A= $node{"priority"}
Aresult
11
00
23 4 ▶ $node{models} //= [];
Condition50%1/2
$node{"models"} //= []
l!l
40
MC/DC0%0/1
$node{"models"} //= []
$node{"models"}
Truth table50%1/2
A
A= $node{"models"}
Aresult
11
00
24 4 ▶ $node{dtypes} //= [];
Condition50%1/2
$node{"dtypes"} //= []
l!l
40
MC/DC0%0/1
$node{"dtypes"} //= []
$node{"dtypes"}
Truth table50%1/2
A
A= $node{"dtypes"}
Aresult
11
00
25 4 push @{ $self->{nodes} }, \%node;
26 4 return \%node;
27 }
28  
29 0 ▶sub nodes { @{ $_[0]->{nodes} } }
Subroutine nodes: not called CC=1 SCAR=6.9
Pod undocumented
30  
31 1 ▶sub list_models {
Subroutine list_models: called CC=5 SCAR=16.1
Pod undocumented
32 1 my ($self) = @_;
33 1 my %seen;
34 my @out;
35 1 for my $n (@{ $self->{nodes} }) {
36 4 ▶ next unless $n->{healthy};
Branch: 1 branch
BranchTrueFalseTotal
unless $n->{"healthy"} 0 4 4
37 4 for my $m (@{ $n->{models} }) {
38 5 ▶ next if $seen{$m}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$m}++ 2 3 5
39 3 push @out, { id => $m, object => 'model', owned_by => $n->{name} };
40 }
41 }
42 1 return { object => 'list', data => \@out };
43 }
44  
45 4 ▶sub select_node {
Subroutine select_node: called CC=10 SCAR=24.4
Pod undocumented
46 4 my ($self, %req) = @_;
47 4 ▶ my $model = $req{model} // '';
Condition50%1/2
$req{"model"} // ""
l!l
40
MC/DC0%0/1
$req{"model"} // ""
$req{"model"}
Truth table50%1/2
A
A= $req{"model"}
Aresult
11
00
48 4 my $dtype = $req{dtype};
49  
50 my @cands = grep {
51 $_->{healthy}
52 20 ▶ && ( !length($model) || grep { $_ eq $model } @{ $_->{models} } )
Condition100%3/3
not $dtype or grep {$_ eq $dtype;} @{$$_{"dtypes"};}
l!l&&r!l&&!r
221
MC/DC100%2/2
not $dtype or grep {$_ eq $dtype;} @{$$_{"dtypes"};}
not $dtype grep {$_ eq $dtype;} @{$$_{"dtypes"};}
Truth table100%3/3
not(A) or B
A= not $dtypeB= grep {$_ eq $dtype;} @{$$_{"dtypes"};}
ABresult
1X1
011
000
53 16 ▶ && ( !$dtype || grep { $_ eq $dtype } @{ $_->{dtypes} } )
Condition77%7/9
$_->{'healthy'} && (!length($model) || grep({$_ eq $model;} @{$$_{'models'};})) && (!$dtype || grep({$_ eq $dtype;} @{$$_{'dtypes'};}))
!ll&&!rl&&r
1114
$_->{"healthy"} and !length($model) || grep({$_ eq $model;} @{$$_{"models"};})
!ll&&!rl&&r
0115
not length $model or grep {$_ eq $model;} @{$$_{"models"};}
l!l&&r!l&&!r
0511
MC/DC50%2/4
$_->{'healthy'} && (!length($model) || grep({$_ eq $model;} @{$$_{'models'};})) && (!$dtype || grep({$_ eq $dtype;} @{$$_{'dtypes'};}))
$_->{"healthy"} not length $model grep {$_ eq $model;} @{$$_{"models"};} (!$dtype || grep({$_ eq $dtype;} @{$$_{'dtypes'};}))
Truth table50%3/6
A and not(B) or C && D
A= $_->{"healthy"}B= not length $modelC= grep {$_ eq $model;} @{$$_{"models"};}D= (!$dtype || grep({$_ eq $dtype;} @{$$_{'dtypes'};}))
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
54 4 } @{ $self->{nodes} };
55  
56 4 ▶ return undef unless @cands;
Branch: 1 branch
BranchTrueFalseTotal
unless @cands 1 3 4
57 @cands = sort {
58 3 ($a->{priority} <=> $b->{priority})
59 1 ▶ || (($b->{free_vram_gb} || 0) <=> ($a->{free_vram_gb} || 0))
Condition14%1/7
$PDL::ML::Gateway::a->{'priority'} <=> $PDL::ML::Gateway::b->{'priority'} || ($PDL::ML::Gateway::b->{'free_vram_gb'} || 0) <=> ($PDL::ML::Gateway::a->{'free_vram_gb'} || 0)
l!l&&r!l&&!r
100
$PDL::ML::Gateway::b->{'free_vram_gb'} || 0
l!l
00
$PDL::ML::Gateway::a->{'free_vram_gb'} || 0
l!l
00
MC/DC0%0/2
$PDL::ML::Gateway::a->{'priority'} <=> $PDL::ML::Gateway::b->{'priority'} || ($PDL::ML::Gateway::b->{'free_vram_gb'} || 0) <=> ($PDL::ML::Gateway::a->{'free_vram_gb'} || 0)
$PDL::ML::Gateway::a->{'priority'} <=> $PDL::ML::Gateway::b->{'priority'} $PDL::ML::Gateway::b->{'free_vram_gb'}
MC/DC0%0/1
$PDL::ML::Gateway::a->{'free_vram_gb'} || 0
$PDL::ML::Gateway::a->{'free_vram_gb'}
Truth table33%1/3
A || B
A= $PDL::ML::Gateway::a->{'priority'} <=> $PDL::ML::Gateway::b->{'priority'}B= $PDL::ML::Gateway::b->{'free_vram_gb'}
ABresult
1X1
011
000
Truth table0%0/2
A
A= $PDL::ML::Gateway::a->{'free_vram_gb'}
Aresult
11
00
60 } @cands;
61 3 return $cands[0];
62 }
63  
64 0 ▶sub build_mojo_app {
Subroutine build_mojo_app: not called CC=2 SCAR=17.9
Pod undocumented
65 0 my ($self) = @_;
66 0 require Mojolicious;
67 0 my $app = Mojolicious->new;
68 0 my $r = $app->routes;
69 0 my $gw = $self;
70  
71 0 ▶ $r->get('/health' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 0 my ($c) = @_;
73 0 $c->render(json => { status => 'ok', nodes => scalar($gw->nodes) });
74 0 });
75  
76 0 ▶ $r->get('/v1/models' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 0 my ($c) = @_;
78 0 $c->render(json => $gw->list_models);
79 0 });
80  
81 0 for my $ep ('/v1/chat/completions', '/v1/completions', '/v1/embeddings') {
82 0 ▶ $r->post($ep => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
83 0 my ($c) = @_;
84 0 ▶ my $body = $c->req->json || {};
Condition0%0/2
$c->req->json || {}
l!l
00
MC/DC0%0/1
$c->req->json || {}
$c->req->json
Truth table0%0/2
A
A= $c->req->json
Aresult
11
00
85 0 my $node = $gw->select_node(model => $body->{model}, dtype => $body->{dtype});
86 0 ▶ return $c->render(status => 503, json => { error => { message => "No healthy backend for model '$body->{model}'" } })
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 0 0
87 unless $node;
88  
89 0 $c->render_later;
90 0 my $target = $node->{url} . $ep;
91 0 ▶ $c->ua->post_p($target => json => $body)->then(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
92 0 my ($tx) = @_;
93 0 ▶ $c->res->headers->content_type($tx->result->headers->content_type || 'application/json');
Condition0%0/2
$tx->result->headers->content_type || 'application/json'
l!l
00
MC/DC0%0/1
$tx->result->headers->content_type || 'application/json'
$tx->result->headers->content_type
Truth table0%0/2
A
A= $tx->result->headers->content_type
Aresult
11
00
94 0 $c->render(status => $tx->result->code, data => $tx->result->body);
95 0 ▶ })->catch(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 0 my ($err) = @_;
97 0 $c->render(status => 502, json => { error => { message => "Backend $node->{name} error: $err" } });
98 0 });
99 0 });
100 }
101  
102 0 return $app;
103 }
104  
105 2 1;
106  
107 __END__
↑ Summary