bin/skeid

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