bin/skeid

filter: Statementstmt 75.5% 130 / 172 Branchbran 52.9% 54 / 102 Conditioncond 41.5% 54 / 130 MC/DCmcdc 4.1% 3 / 72 Subroutinesub 78.9% 15 / 19 Podpod n/a totaltotal 51.7% 256 / 495 CC 95 SCAR 56.5
CC
95
Cov
59%
_run_usage
402.6
_run_keyid
56.0
_config_file
5.0
CRAP 285.5
↑ Summary lib/Langertha/Skeid/UsageStore.pm »
1 #!/usr/bin/env perl
2 # PODNAME: skeid
3 # ABSTRACT: Skeid control-plane CLI and proxy launcher
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Getopt::Long qw(GetOptions GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use lib "$FindBin::Bin/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Mojo::Server::Daemon;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Mojo::Server::Prefork;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Langertha::Skeid;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use Langertha::Skeid::Proxy;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 15 my $cmd = 'serve';
16 15 ▶if (@ARGV && defined($ARGV[0]) && $ARGV[0] !~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV and defined $ARGV[0] and not $ARGV[0] =~ /^-/) 15 0 15
Condition33%2/6
@ARGV and defined $ARGV[0] and not $ARGV[0] =~ /^-/
!ll&&!rl&&r
0015
@ARGV and defined $ARGV[0]
!ll&&!rl&&r
0015
MC/DC0%0/3
@ARGV and defined $ARGV[0] and not $ARGV[0] =~ /^-/
@ARGV defined $ARGV[0] not $ARGV[0] =~ /^-/
Truth table25%1/4
A and B and not(C)
A= @ARGVB= defined $ARGV[0]C= not $ARGV[0] =~ /^-/
ABCresult
0XX0
10X0
1100
1111
17 15 $cmd = shift @ARGV;
18 }
19  
20 15 ▶if ($cmd eq 'usage') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "usage") 12 3 15
21 12 exit _run_usage(@ARGV);
22 }
23 3 ▶if ($cmd eq 'keyid') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "keyid") 0 3 3
24 0 exit _run_keyid(@ARGV);
25 }
26 3 ▶if ($cmd ne 'serve') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd ne "serve") 0 3 3
27 0 die _usage_text();
28 }
29  
30 3 my $listen = '127.0.0.1:8090';
31 3 my $config;
32 3 my $admin_api_key = '';
33 3 my $workers = 1;
34  
35 3 ▶GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("listen|l=s", \$listen, "config|c=s", \$config, "admin-api-key=s", \$admin_api_key, "workers|w=i", \$workers) or die _usage_text() 3 0 3
36 'listen|l=s' => \$listen,
37 'config|c=s' => \$config,
38 'admin-api-key=s' => \$admin_api_key,
39 'workers|w=i' => \$workers,
40 ) or die _usage_text();
41  
42 3 ▶$workers = 1 if !$workers || $workers < 1;
Branch: 1 branch
BranchTrueFalseTotal
if not $workers or $workers < 1 0 3 3
Condition33%1/3
not $workers or $workers < 1
l!l&&r!l&&!r
003
MC/DC0%0/2
not $workers or $workers < 1
not $workers $workers < 1
Truth table33%1/3
not(A) or B
A= not $workersB= $workers < 1
ABresult
1X1
011
000
43  
44 3 my %opts;
45 3 my $config_file = _config_file($config);
46 1 ▶$opts{config_file} = $config_file if defined $config_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $config_file 1 0 1
47 1 ▶$opts{admin_api_key} = $admin_api_key if defined($admin_api_key) && length($admin_api_key);
Branch: 1 branch
BranchTrueFalseTotal
if defined $admin_api_key and length $admin_api_key 0 1 1
Condition33%1/3
defined $admin_api_key and length $admin_api_key
!ll&&!rl&&r
010
MC/DC0%0/2
defined $admin_api_key and length $admin_api_key
defined $admin_api_key length $admin_api_key
Truth table33%1/3
A and B
A= defined $admin_api_keyB= length $admin_api_key
ABresult
0X0
100
111
48 # Set before build_app so the first probe interval and the first admission decision already
49 # know how many processes are sharing the node (ADR 0010).
50 1 $opts{worker_count} = $workers;
51  
52 1 my $app = Langertha::Skeid::Proxy->build_app(%opts);
53  
54 # A write-behind usage store (usage_store.flush_interval_ms) holds events in memory until its
55 # timer fires. The loop stops before that on SIGINT/SIGTERM to a single process, and a prefork
56 # worker leaves through exit after a graceful stop (SIGQUIT) -- both run END, the last point the
57 # database handle is still alive. Without it those events, billed requests, would be gone
58 # (skeid k78). A worker the prefork manager SIGKILLs runs nothing.
59 15 ▶END {
Subroutine END: called CC=2 SCAR=7.2
60 15 local $?;
61 15 ▶ $app->skeid->flush_usage if $app;
Branch: 1 branch
BranchTrueFalseTotal
if $app 0 15 15
62 }
63  
64 0 print "Starting Skeid proxy on http://$listen\n";
65 0 ▶print "Config: ", ($opts{config_file} // '(none)'), "\n";
Condition0%0/2
$opts{"config_file"} // "(none)"
l!l
00
MC/DC0%0/1
$opts{"config_file"} // "(none)"
$opts{"config_file"}
Truth table0%0/2
A
A= $opts{"config_file"}
Aresult
11
00
66 0 ▶print "Workers: $workers\n" if $workers > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $workers > 1 0 0 0
67  
68 # An operator who wrote max_conns meant it for the node, not for each process. When the split
69 # cannot honour that, say so -- silently admitting more than was configured is how a GPU ends
70 # up queueing.
71 0 warn "WARNING: $_\n" for @{ $app->skeid->worker_share_warnings };
72  
73 # Customer keys from a database relation (ADR 0026): one bounded fetch before listening, so the
74 # first request already sees the source's customers. A failed fetch does not stop the start --
75 # that would take down every customer the file declares too: Skeid starts degraded and
76 # fail-closed, the file's config in force and no source rows, and keeps retrying. Before the
77 # prefork manager forks, the connection this fetch opened is closed: each worker opens its own.
78 0 ▶if (my $source = $app->skeid->customer_source_status) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $source = $app->skeid->customer_source_status) 0 0 0
79 0 print "Customer source: $source->{relation}\n";
80 0 ▶ if ($app->skeid->customer_source_startup_fetch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app->skeid->customer_source_startup_fetch) { } 0 0 0
81 0 my $rows = $app->skeid->customer_source_status->{rows};
82 0 print "Customer source: $rows->{accepted} accepted, $rows->{rejected} rejected, "
83 . "$rows->{shadowed} shadowed\n";
84 } else {
85 0 warn "WARNING: customer_source: the first fetch failed; starting degraded and fail-closed "
86 . "(no source customer is let in until a fetch succeeds)\n";
87 }
88 0 ▶ $app->skeid->customer_source_release if $workers > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $workers > 1 0 0 0
89 }
90  
91 0 ▶if ($workers > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($workers > 1) { } 0 0 0
92 0 my $prefork = Mojo::Server::Prefork->new(
93 app => $app,
94 listen => ["http://$listen"],
95 workers => $workers,
96 );
97 0 $prefork->run;
98 } else {
99 0 my $daemon = Mojo::Server::Daemon->new(
100 app => $app,
101 listen => ["http://$listen"],
102 );
103 # Daemon->run handles INT and TERM only; SIGQUIT (the image's STOPSIGNAL) would otherwise
104 # kill the process with its default action, before END could flush a write-behind queue.
105 # The loop's own `finish` event goes first, as it does in a prefork worker on SIGQUIT: open
106 # realtime sessions end (1001) and write their usage events (ADR 0029). Their close frames get
107 # a moment to go out before the loop stops.
108 0 ▶ $SIG{QUIT} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 0 my $loop = $daemon->ioloop;
110 0 $loop->emit('finish');
111 0 ▶ $loop->timer(0.2 => sub { $loop->stop });
Subroutine __ANON__: not called CC=1 SCAR=6.9
112 0 };
113 0 $daemon->run;
114 }
115  
116 12 ▶sub _run_usage {
Subroutine _run_usage: called CC=79 SCAR=60.0
117 12 my (@argv) = @_;
118  
119 12 my $config;
120 12 my $since = '';
121 12 my $limit = 20;
122 12 my $as_json = 0;
123 12 my $backend = '';
124 12 my $db_path = '';
125 12 my $log_path = '';
126 12 my $dsn = '';
127 12 my $db_user = '';
128 12 my $db_pass = '';
129 12 my $db_pass_env = '';
130 12 my $api_key_id = '';
131 12 my $model = '';
132  
133 12 ▶ GetOptionsFromArray(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptionsFromArray(\@argv, "config|c=s", \$config, "since=s", \$since, "limit=i", \$limit, "json!", \$as_json, "backend=s", \$backend, "db|sqlite=s", \$db_path, "log-path|jsonlog=s", \$log_path, "dsn=s", \$dsn, "db-user=s", \$db_user, "db-pass=s", \$db_pass, "db-pass-env=s", \$db_pass_env, "api-key-id=s", \$api_key_id, "model=s", \$model) or die _usage_text() 12 0 12
134 \@argv,
135 'config|c=s' => \$config,
136 'since=s' => \$since,
137 'limit=i' => \$limit,
138 'json!' => \$as_json,
139 'backend=s' => \$backend,
140 'db|sqlite=s' => \$db_path,
141 'log-path|jsonlog=s' => \$log_path,
142 'dsn=s' => \$dsn,
143 'db-user=s' => \$db_user,
144 'db-pass=s' => \$db_pass,
145 'db-pass-env=s' => \$db_pass_env,
146 'api-key-id=s' => \$api_key_id,
147 'model=s' => \$model,
148 ) or die _usage_text();
149  
150 12 my %skeid_opts;
151 12 my $config_file = _config_file($config);
152 11 ▶ $skeid_opts{config_file} = $config_file if defined $config_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $config_file 1 10 11
153  
154 11 my $skeid = Langertha::Skeid->new(%skeid_opts);
155  
156 11 my %usage_store;
157 11 ▶ if (defined $backend && length $backend) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $backend and length $backend) 1 10 11
Condition66%2/3
defined $backend and length $backend
!ll&&!rl&&r
0101
MC/DC50%1/2
defined $backend and length $backend
defined $backend length $backend
Truth table66%2/3
A and B
A= defined $backendB= length $backend
ABresult
0X0
100
111
158 1 $usage_store{backend} = $backend;
159 }
160 11 ▶ if (defined $db_path && length $db_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $db_path and length $db_path) 0 11 11
Condition33%1/3
defined $db_path and length $db_path
!ll&&!rl&&r
0110
MC/DC0%0/2
defined $db_path and length $db_path
defined $db_path length $db_path
Truth table33%1/3
A and B
A= defined $db_pathB= length $db_path
ABresult
0X0
100
111
161 0 ▶ $usage_store{backend} ||= 'sqlite';
Condition0%0/2
$usage_store{"backend"} ||= "sqlite"
l!l
00
MC/DC0%0/1
$usage_store{"backend"} ||= "sqlite"
$usage_store{"backend"}
Truth table0%0/2
A
A= $usage_store{"backend"}
Aresult
11
00
162 0 $usage_store{sqlite_path} = $db_path;
163 }
164 11 ▶ if (defined $dsn && length $dsn) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dsn and length $dsn) 0 11 11
Condition33%1/3
defined $dsn and length $dsn
!ll&&!rl&&r
0110
MC/DC0%0/2
defined $dsn and length $dsn
defined $dsn length $dsn
Truth table33%1/3
A and B
A= defined $dsnB= length $dsn
ABresult
0X0
100
111
165 0 ▶ $usage_store{backend} ||= 'postgresql';
Condition0%0/2
$usage_store{"backend"} ||= "postgresql"
l!l
00
MC/DC0%0/1
$usage_store{"backend"} ||= "postgresql"
$usage_store{"backend"}
Truth table0%0/2
A
A= $usage_store{"backend"}
Aresult
11
00
166 0 $usage_store{dsn} = $dsn;
167 }
168 11 ▶ if (defined $log_path && length $log_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $log_path and length $log_path) 10 1 11
Condition66%2/3
defined $log_path and length $log_path
!ll&&!rl&&r
0110
MC/DC50%1/2
defined $log_path and length $log_path
defined $log_path length $log_path
Truth table66%2/3
A and B
A= defined $log_pathB= length $log_path
ABresult
0X0
100
111
169 10 ▶ $usage_store{backend} ||= 'jsonlog';
Condition100%2/2
$usage_store{"backend"} ||= "jsonlog"
l!l
19
MC/DC100%1/1
$usage_store{"backend"} ||= "jsonlog"
$usage_store{"backend"}
Truth table100%2/2
A
A= $usage_store{"backend"}
Aresult
11
00
170 10 $usage_store{log_path} = $log_path;
171 }
172 11 ▶ $usage_store{user} = $db_user if defined($db_user) && length($db_user);
Branch: 1 branch
BranchTrueFalseTotal
if defined $db_user and length $db_user 0 11 11
Condition33%1/3
defined $db_user and length $db_user
!ll&&!rl&&r
0110
MC/DC0%0/2
defined $db_user and length $db_user
defined $db_user length $db_user
Truth table33%1/3
A and B
A= defined $db_userB= length $db_user
ABresult
0X0
100
111
173 11 ▶ $usage_store{password} = $db_pass if defined($db_pass) && length($db_pass);
Branch: 1 branch
BranchTrueFalseTotal
if defined $db_pass and length $db_pass 0 11 11
Condition33%1/3
defined $db_pass and length $db_pass
!ll&&!rl&&r
0110
MC/DC0%0/2
defined $db_pass and length $db_pass
defined $db_pass length $db_pass
Truth table33%1/3
A and B
A= defined $db_passB= length $db_pass
ABresult
0X0
100
111
174 11 ▶ $usage_store{password_env} = $db_pass_env if defined($db_pass_env) && length($db_pass_env);
Branch: 1 branch
BranchTrueFalseTotal
if defined $db_pass_env and length $db_pass_env 0 11 11
Condition33%1/3
defined $db_pass_env and length $db_pass_env
!ll&&!rl&&r
0110
MC/DC0%0/2
defined $db_pass_env and length $db_pass_env
defined $db_pass_env length $db_pass_env
Truth table33%1/3
A and B
A= defined $db_pass_envB= length $db_pass_env
ABresult
0X0
100
111
175  
176 11 ▶ if (%usage_store) {
Branch: 1 branch
BranchTrueFalseTotal
if (%usage_store) 10 1 11
177 10 $skeid->configure_usage_store(\%usage_store);
178 }
179  
180 11 ▶ my $report = $skeid->call_function('usage.report', {
Branch: 3 branches
BranchTrueFalseTotal
defined $since && length $since ? : 0 11 11
defined $api_key_id && length $api_key_id ? : 0 11 11
defined $model && length $model ? : 0 11 11
Condition33%3/9
defined $since and length $since
!ll&&!rl&&r
0110
defined $api_key_id and length $api_key_id
!ll&&!rl&&r
0110
defined $model and length $model
!ll&&!rl&&r
0110
MC/DC0%0/2
defined $since and length $since
defined $since length $since
MC/DC0%0/2
defined $api_key_id and length $api_key_id
defined $api_key_id length $api_key_id
MC/DC0%0/2
defined $model and length $model
defined $model length $model
Truth table33%1/3
A and B
A= defined $sinceB= length $since
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= defined $api_key_idB= length $api_key_id
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= defined $modelB= length $model
ABresult
0X0
100
111
181 limit => $limit,
182 (defined($since) && length($since) ? (since => $since) : ()),
183 (defined($api_key_id) && length($api_key_id) ? (api_key_id => $api_key_id) : ()),
184 (defined($model) && length($model) ? (model => $model) : ()),
185 });
186  
187 11 ▶ unless ($report->{ok}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($report->{"ok"}) 0 11 11
188 0 ▶ my $err = $report->{error} // 'unknown usage report error';
Condition0%0/2
$report->{"error"} // "unknown usage report error"
l!l
00
MC/DC0%0/1
$report->{"error"} // "unknown usage report error"
$report->{"error"}
Truth table0%0/2
A
A= $report->{"error"}
Aresult
11
00
189 0 print "ERROR: $err\n";
190 0 return 2;
191 }
192  
193 11 ▶ if ($as_json) {
Branch: 1 branch
BranchTrueFalseTotal
if ($as_json) 2 9 11
194 2 my $json = JSON::MaybeXS->new(utf8 => 1, canonical => 1, pretty => 1);
195 2 print $json->encode($report);
196 2 return 0;
197 }
198  
199 9 ▶ my $backend_name = $report->{backend} // '';
Condition50%1/2
$report->{"backend"} // ""
l!l
90
MC/DC0%0/1
$report->{"backend"} // ""
$report->{"backend"}
Truth table50%1/2
A
A= $report->{"backend"}
Aresult
11
00
200 my $db_label = $backend_name eq 'sqlite' ? ($report->{db_path} // '')
201 9 ▶ : $backend_name eq 'jsonlog' ? ($report->{log_path} // '')
Branch: 3 branches
BranchTrueFalseTotal
$backend_name eq 'sqlite' ? : 0 9 9
$backend_name eq 'jsonlog' ? : 9 0 9
$backend_name eq 'postgresql' ? : 0 0 0
Condition25%1/4
$report->{"db_path"} // ""
l!l
00
$report->{"log_path"} // ""
l!l
90
MC/DC0%0/1
$report->{"db_path"} // ""
$report->{"db_path"}
MC/DC0%0/1
$report->{"log_path"} // ""
$report->{"log_path"}
Truth table0%0/2
A
A= $report->{"db_path"}
Aresult
11
00
Truth table50%1/2
A
A= $report->{"log_path"}
Aresult
11
00
202 : $backend_name eq 'postgresql' ? 'configured DSN'
203 : '(none)';
204 9 print "Usage backend: $backend_name\n";
205 9 print "Store: $db_label\n";
206 9 ▶ print "Since: ", ($report->{since} && length($report->{since}) ? $report->{since} : '(all)'), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$report->{'since'} && length $report->{'since'} ? : 0 9 9
Condition33%1/3
$report->{"since"} and length $report->{"since"}
!ll&&!rl&&r
900
MC/DC0%0/2
$report->{"since"} and length $report->{"since"}
$report->{"since"} length $report->{"since"}
Truth table33%1/3
A and B
A= $report->{"since"}B= length $report->{"since"}
ABresult
0X0
100
111
207 9 print "\n";
208  
209 9 ▶ my $tot = $report->{totals} || {};
Condition50%1/2
$report->{'totals'} || {}
l!l
90
MC/DC0%0/1
$report->{'totals'} || {}
$report->{'totals'}
Truth table50%1/2
A
A= $report->{'totals'}
Aresult
11
00
210 printf "Totals: requests=%d input=%d output=%d total=%d cached=%d cache_write=%d tools=%d cost=\$%.8f\n",
211 ($tot->{requests} // 0),
212 ($tot->{input_tokens} // 0),
213 ($tot->{output_tokens} // 0),
214 ($tot->{total_tokens} // 0),
215 ($tot->{cached_tokens} // 0),
216 ($tot->{cache_write_tokens} // 0),
217 ($tot->{tool_calls} // 0),
218 9 ▶ ($tot->{total_cost_usd} // 0);
Condition50%8/16
$tot->{"requests"} // 0
l!l
90
$tot->{"input_tokens"} // 0
l!l
90
$tot->{"output_tokens"} // 0
l!l
90
$tot->{"total_tokens"} // 0
l!l
90
$tot->{"cached_tokens"} // 0
l!l
90
$tot->{"cache_write_tokens"} // 0
l!l
90
$tot->{"tool_calls"} // 0
l!l
90
$tot->{"total_cost_usd"} // 0
l!l
90
MC/DC0%0/1
$tot->{"requests"} // 0
$tot->{"requests"}
MC/DC0%0/1
$tot->{"input_tokens"} // 0
$tot->{"input_tokens"}
MC/DC0%0/1
$tot->{"output_tokens"} // 0
$tot->{"output_tokens"}
MC/DC0%0/1
$tot->{"total_tokens"} // 0
$tot->{"total_tokens"}
MC/DC0%0/1
$tot->{"cached_tokens"} // 0
$tot->{"cached_tokens"}
MC/DC0%0/1
$tot->{"cache_write_tokens"} // 0
$tot->{"cache_write_tokens"}
MC/DC0%0/1
$tot->{"tool_calls"} // 0
$tot->{"tool_calls"}
MC/DC0%0/1
$tot->{"total_cost_usd"} // 0
$tot->{"total_cost_usd"}
Truth table50%1/2
A
A= $tot->{"requests"}
Aresult
11
00
Truth table50%1/2
A
A= $tot->{"input_tokens"}
Aresult
11
00
Truth table50%1/2
A
A= $tot->{"output_tokens"}
Aresult
11
00
Truth table50%1/2
A
A= $tot->{"total_tokens"}
Aresult
11
00
Truth table50%1/2
A
A= $tot->{"cached_tokens"}
Aresult
11
00
Truth table50%1/2
A
A= $tot->{"cache_write_tokens"}
Aresult
11
00
Truth table50%1/2
A
A= $tot->{"tool_calls"}
Aresult
11
00
Truth table50%1/2
A
A= $tot->{"total_cost_usd"}
Aresult
11
00
219 # Only where an audio route recorded some (skeid k91): a store without audio events prints
220 # what it always printed.
221 9 ▶ printf "Audio: seconds=%s\n", _seconds($tot->{audio_seconds}) if $tot->{audio_seconds};
Branch: 1 branch
BranchTrueFalseTotal
if $tot->{"audio_seconds"} 2 7 9
222 # Likewise the documents of the rerank route (skeid k92).
223 9 ▶ printf "Rerank: documents=%d\n", $tot->{documents} if $tot->{documents};
Branch: 1 branch
BranchTrueFalseTotal
if $tot->{"documents"} 1 8 9
224 # And the pages of the documents routes (skeid k94): one line, only where there are pages.
225 9 ▶ printf "Pages: pages=%d\n", $tot->{pages} if $tot->{pages};
Branch: 1 branch
BranchTrueFalseTotal
if $tot->{"pages"} 0 9 9
226  
227 9 print "\nBy API key:\n";
228 9 ▶ print " (none)\n" unless @{$report->{by_key} || []};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$report->{"by_key"} or [];} 0 9 9
Condition50%1/2
$report->{'by_key'} || []
l!l
90
MC/DC0%0/1
$report->{'by_key'} || []
$report->{'by_key'}
Truth table50%1/2
A
A= $report->{'by_key'}
Aresult
11
00
229 9 ▶ for my $row (@{$report->{by_key} || []}) {
Condition50%1/2
$report->{'by_key'} || []
l!l
90
MC/DC0%0/1
$report->{'by_key'} || []
$report->{'by_key'}
Truth table50%1/2
A
A= $report->{'by_key'}
Aresult
11
00
230 printf " %-16s requests=%d tokens=%d cost=\$%.8f%s\n",
231 ($row->{api_key_id} || '(none)'),
232 ($row->{requests} // 0),
233 ($row->{total_tokens} // 0),
234 16 ▶ ($row->{total_cost_usd} // 0),
Condition50%4/8
$row->{'api_key_id'} || '(none)'
l!l
160
$row->{"requests"} // 0
l!l
160
$row->{"total_tokens"} // 0
l!l
160
$row->{"total_cost_usd"} // 0
l!l
160
MC/DC0%0/1
$row->{'api_key_id'} || '(none)'
$row->{'api_key_id'}
MC/DC0%0/1
$row->{"requests"} // 0
$row->{"requests"}
MC/DC0%0/1
$row->{"total_tokens"} // 0
$row->{"total_tokens"}
MC/DC0%0/1
$row->{"total_cost_usd"} // 0
$row->{"total_cost_usd"}
Truth table50%1/2
A
A= $row->{'api_key_id'}
Aresult
11
00
Truth table50%1/2
A
A= $row->{"requests"}
Aresult
11
00
Truth table50%1/2
A
A= $row->{"total_tokens"}
Aresult
11
00
Truth table50%1/2
A
A= $row->{"total_cost_usd"}
Aresult
11
00
235 _row_units($row);
236 }
237  
238 9 print "\nBy model:\n";
239 9 ▶ print " (none)\n" unless @{$report->{by_model} || []};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$report->{"by_model"} or [];} 0 9 9
Condition50%1/2
$report->{'by_model'} || []
l!l
90
MC/DC0%0/1
$report->{'by_model'} || []
$report->{'by_model'}
Truth table50%1/2
A
A= $report->{'by_model'}
Aresult
11
00
240 9 ▶ for my $row (@{$report->{by_model} || []}) {
Condition50%1/2
$report->{'by_model'} || []
l!l
90
MC/DC0%0/1
$report->{'by_model'} || []
$report->{'by_model'}
Truth table50%1/2
A
A= $report->{'by_model'}
Aresult
11
00
241 printf " %-24s requests=%d tokens=%d cost=\$%.8f%s\n",
242 ($row->{model} || '(none)'),
243 ($row->{requests} // 0),
244 ($row->{total_tokens} // 0),
245 12 ▶ ($row->{total_cost_usd} // 0),
Condition50%4/8
$row->{'model'} || '(none)'
l!l
120
$row->{"requests"} // 0
l!l
120
$row->{"total_tokens"} // 0
l!l
120
$row->{"total_cost_usd"} // 0
l!l
120
MC/DC0%0/1
$row->{'model'} || '(none)'
$row->{'model'}
MC/DC0%0/1
$row->{"requests"} // 0
$row->{"requests"}
MC/DC0%0/1
$row->{"total_tokens"} // 0
$row->{"total_tokens"}
MC/DC0%0/1
$row->{"total_cost_usd"} // 0
$row->{"total_cost_usd"}
Truth table50%1/2
A
A= $row->{'model'}
Aresult
11
00
Truth table50%1/2
A
A= $row->{"requests"}
Aresult
11
00
Truth table50%1/2
A
A= $row->{"total_tokens"}
Aresult
11
00
Truth table50%1/2
A
A= $row->{"total_cost_usd"}
Aresult
11
00
246 _row_units($row);
247 }
248  
249 9 print "\nRecent:\n";
250 9 ▶ print " (none)\n" unless @{$report->{recent} || []};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$report->{"recent"} or [];} 0 9 9
Condition50%1/2
$report->{'recent'} || []
l!l
90
MC/DC0%0/1
$report->{'recent'} || []
$report->{'recent'}
Truth table50%1/2
A
A= $report->{'recent'}
Aresult
11
00
251 9 ▶ for my $row (@{$report->{recent} || []}) {
Condition50%1/2
$report->{'recent'} || []
l!l
90
MC/DC0%0/1
$report->{'recent'} || []
$report->{'recent'}
Truth table50%1/2
A
A= $report->{'recent'}
Aresult
11
00
252 # %s, not %d: a jsonlog event id is a string (time, pid, nonce, sequence), a DBI one a row id.
253 printf " #%s %s %-9s %-20s model=%s status=%d tokens=%d cached=%d cost=\$%.8f\n",
254 ($row->{id} // ''),
255 ($row->{created_at} || ''),
256 ($row->{api_format} || ''),
257 ($row->{api_key_id} || 'anonymous'),
258 ($row->{model} || ''),
259 ($row->{status_code} // 0),
260 ($row->{total_tokens} // 0),
261 ($row->{cached_tokens} // 0),
262 23 ▶ ($row->{cost_total_usd} // 0);
Condition50%9/18
$row->{"id"} // ""
l!l
230
$row->{'created_at'} || ''
l!l
230
$row->{'api_format'} || ''
l!l
230
$row->{'api_key_id'} || 'anonymous'
l!l
230
$row->{'model'} || ''
l!l
230
$row->{"status_code"} // 0
l!l
230
$row->{"total_tokens"} // 0
l!l
230
$row->{"cached_tokens"} // 0
l!l
230
$row->{"cost_total_usd"} // 0
l!l
230
MC/DC0%0/1
$row->{"id"} // ""
$row->{"id"}
MC/DC0%0/1
$row->{'created_at'} || ''
$row->{'created_at'}
MC/DC0%0/1
$row->{'api_format'} || ''
$row->{'api_format'}
MC/DC0%0/1
$row->{'api_key_id'} || 'anonymous'
$row->{'api_key_id'}
MC/DC0%0/1
$row->{'model'} || ''
$row->{'model'}
MC/DC0%0/1
$row->{"status_code"} // 0
$row->{"status_code"}
MC/DC0%0/1
$row->{"total_tokens"} // 0
$row->{"total_tokens"}
MC/DC0%0/1
$row->{"cached_tokens"} // 0
$row->{"cached_tokens"}
MC/DC0%0/1
$row->{"cost_total_usd"} // 0
$row->{"cost_total_usd"}
Truth table50%1/2
A
A= $row->{"id"}
Aresult
11
00
Truth table50%1/2
A
A= $row->{'created_at'}
Aresult
11
00
Truth table50%1/2
A
A= $row->{'api_format'}
Aresult
11
00
Truth table50%1/2
A
A= $row->{'api_key_id'}
Aresult
11
00
Truth table50%1/2
A
A= $row->{'model'}
Aresult
11
00
Truth table50%1/2
A
A= $row->{"status_code"}
Aresult
11
00
Truth table50%1/2
A
A= $row->{"total_tokens"}
Aresult
11
00
Truth table50%1/2
A
A= $row->{"cached_tokens"}
Aresult
11
00
Truth table50%1/2
A
A= $row->{"cost_total_usd"}
Aresult
11
00
263 }
264  
265 9 return 0;
266 }
267  
268 # What a by-key or by-model row ends in: the units of the relayed routes, each only where the
269 # row has some, so a row without any is printed as it always was.
270 28 ▶sub _row_units {
Subroutine _row_units: called CC=4 SCAR=13.9
271 28 my ($row) = @_;
272 return join '',
273 ($row->{audio_seconds} ? ' audio_seconds=' . _seconds($row->{audio_seconds}) : ()),
274 ($row->{documents} ? ' documents=' . (0 + $row->{documents}) : ()),
275 28 ▶ ($row->{pages} ? ' pages=' . (0 + $row->{pages}) : ());
Branch: 3 branches
BranchTrueFalseTotal
$row->{'audio_seconds'} ? : 4 24 28
$row->{'documents'} ? : 2 26 28
$row->{'pages'} ? : 0 28 28
276 }
277  
278 # Seconds of audio for the report: a float sum, printed without the noise binary fractions
279 # leave behind (12.300000000000001) and without trailing zeros.
280 6 ▶sub _seconds {
Subroutine _seconds: called CC=3 SCAR=11.4
281 6 my ($seconds) = @_;
282 6 ▶ my $text = sprintf('%.3f', $seconds // 0);
Condition50%1/2
$seconds // 0
l!l
60
MC/DC0%0/1
$seconds // 0
$seconds
Truth table50%1/2
A
A= $seconds
Aresult
11
00
283 6 ▶ $text =~ s/\.?0+\z// if $text =~ /\./;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\./ 6 0 6
284 6 return $text;
285 }
286  
287 # The config file to load. Without --config, ./skeid.yaml is used when it exists and Skeid runs
288 # without a config when it does not. A --config that names no file ends the program: the
289 # operator asked for that config, and starting without it would serve no nodes (skeid k68).
290 15 ▶sub _config_file {
Subroutine _config_file: called CC=5 SCAR=16.1
291 15 my ($config) = @_;
292 15 ▶ return (-f 'skeid.yaml' ? 'skeid.yaml' : undef) unless defined $config;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $config 10 5 15
-f 'skeid.yaml' ? : 0 10 10
293 5 ▶ return $config if -f $config;
Branch: 1 branch
BranchTrueFalseTotal
if -f $config 2 3 5
294 3 ▶ print STDERR 'ERROR: config file not found: ' . $config
Branch: 1 branch
BranchTrueFalseTotal
-e $config ? : 1 2 3
295 . (-e $config ? ' (not a regular file)' : '') . "\n";
296 3 exit 2;
297 }
298  
299 # Prints the key id a customer key routes and bills under. Needed because a config names
300 # customers in keys:, and must be able to do that without holding their keys.
301 0 ▶sub _run_keyid {
Subroutine _run_keyid: not called CC=7 SCAR=40.3
302 0 my (@argv) = @_;
303  
304 0 ▶ my @keys = grep { defined && length } @argv;
Condition0%0/3
defined $_ && length $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table0%0/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
305 0 ▶ if (!@keys) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@keys) 0 0 0
306 # Reading from stdin keeps the key off the command line, and out of the shell history.
307 0 ▶ while (my $line = <STDIN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline STDIN)) 0 0 0
308 0 chomp $line;
309 0 ▶ push @keys, $line if length $line;
Branch: 1 branch
BranchTrueFalseTotal
if length $line 0 0 0
310 }
311 }
312 0 ▶ die _usage_text() unless @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 0 0 0
313  
314 0 print Langertha::Skeid->key_id_for_key($_), "\n" for @keys;
315 0 return 0;
316 }
317  
318 0 ▶sub _usage_text {
Subroutine _usage_text: not called CC=1 SCAR=6.9
319 0 return <<'USAGE';
320 Usage:
321 skeid serve [--listen host:port] [--config skeid.yaml] [--admin-api-key KEY]
322 [--workers N] N > 1 runs prefork; each worker takes max_conns/N
323 skeid usage [--config skeid.yaml] [--since ISO8601] [--limit N] [--json]
324 [--backend jsonlog|sqlite|postgresql] [--log-path /path/to/events/]
325 [--db /path/to.sqlite] [--dsn dbi:Pg:...]
326 [--db-user USER] [--db-pass PASS|--db-pass-env ENV]
327 [--api-key-id ID] [--model NAME]
328 skeid keyid [KEY ...] the key id to use in the config's keys: section
329 (reads stdin when given no argument)
330 USAGE
331 }
332  
333 __END__
↑ Summary lib/Langertha/Skeid/UsageStore.pm »