/home/cpancover/.cpan/build/Langertha-Skeid-0.003-0/bin/skeid

filter: Statementstmt 74.5% 114 / 153 Branchbran 48.7% 40 / 82 Conditioncond 40.6% 52 / 128 MC/DCmcdc 4.2% 3 / 71 Subroutinesub 81.2% 13 / 16 Podpod n/a totaltotal 49.3% 222 / 450 CC 87 SCAR 58.2
CC
87
Cov
57%
_run_usage
448.1
_run_keyid
56.0
_config_file
5.0
CRAP 338.2
↑ Summary blib/lib/Langertha/Skeid/UsageStore.pm »
1 #!/usr/bin/env perl
2 # PODNAME: skeid
3 # ABSTRACT: Skeid control-plane CLI and proxy launcher
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Getopt::Long qw(GetOptions GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use lib "$FindBin::Bin/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Mojo::Server::Daemon;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Mojo::Server::Prefork;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Langertha::Skeid;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Langertha::Skeid::Proxy;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 8 my $cmd = 'serve';
16 8 ▶if (@ARGV && defined($ARGV[0]) && $ARGV[0] !~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV and defined $ARGV[0] and not $ARGV[0] =~ /^-/) 8 0 8
Condition33%2/6
@ARGV and defined $ARGV[0] and not $ARGV[0] =~ /^-/
!ll&&!rl&&r
008
@ARGV and defined $ARGV[0]
!ll&&!rl&&r
008
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 8 $cmd = shift @ARGV;
18 }
19  
20 8 ▶if ($cmd eq 'usage') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "usage") 6 2 8
21 6 exit _run_usage(@ARGV);
22 }
23 2 ▶if ($cmd eq 'keyid') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "keyid") 0 2 2
24 0 exit _run_keyid(@ARGV);
25 }
26 2 ▶if ($cmd ne 'serve') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd ne "serve") 0 2 2
27 0 die _usage_text();
28 }
29  
30 2 my $listen = '127.0.0.1:8090';
31 2 my $config;
32 2 my $admin_api_key = '';
33 2 my $workers = 1;
34  
35 2 ▶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() 2 0 2
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 2 ▶$workers = 1 if !$workers || $workers < 1;
Branch: 1 branch
BranchTrueFalseTotal
if not $workers or $workers < 1 0 2 2
Condition33%1/3
not $workers or $workers < 1
l!l&&r!l&&!r
002
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 2 my %opts;
45 2 my $config_file = _config_file($config);
46 0 ▶$opts{config_file} = $config_file if defined $config_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $config_file 0 0 0
47 0 ▶$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 0 0
Condition0%0/3
defined $admin_api_key and length $admin_api_key
!ll&&!rl&&r
000
MC/DC0%0/2
defined $admin_api_key and length $admin_api_key
defined $admin_api_key length $admin_api_key
Truth table0%0/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 0 $opts{worker_count} = $workers;
51  
52 0 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 8 ▶END {
Subroutine END: called CC=2 SCAR=7.2
60 8 local $?;
61 8 ▶ $app->skeid->flush_usage if $app;
Branch: 1 branch
BranchTrueFalseTotal
if $app 0 8 8
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 0 ▶if ($workers > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($workers > 1) { } 0 0 0
74 0 my $prefork = Mojo::Server::Prefork->new(
75 app => $app,
76 listen => ["http://$listen"],
77 workers => $workers,
78 );
79 0 $prefork->run;
80 } else {
81 0 my $daemon = Mojo::Server::Daemon->new(
82 app => $app,
83 listen => ["http://$listen"],
84 );
85 # Daemon->run handles INT and TERM only; SIGQUIT (the image's STOPSIGNAL) would otherwise
86 # kill the process with its default action, before END could flush a write-behind queue.
87 0 ▶ $SIG{QUIT} = sub { $daemon->ioloop->stop };
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 0 $daemon->run;
89 }
90  
91 6 ▶sub _run_usage {
Subroutine _run_usage: called CC=76 SCAR=61.1
92 6 my (@argv) = @_;
93  
94 6 my $config;
95 6 my $since = '';
96 6 my $limit = 20;
97 6 my $as_json = 0;
98 6 my $backend = '';
99 6 my $db_path = '';
100 6 my $log_path = '';
101 6 my $dsn = '';
102 6 my $db_user = '';
103 6 my $db_pass = '';
104 6 my $db_pass_env = '';
105 6 my $api_key_id = '';
106 6 my $model = '';
107  
108 6 ▶ 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() 6 0 6
109 \@argv,
110 'config|c=s' => \$config,
111 'since=s' => \$since,
112 'limit=i' => \$limit,
113 'json!' => \$as_json,
114 'backend=s' => \$backend,
115 'db|sqlite=s' => \$db_path,
116 'log-path|jsonlog=s' => \$log_path,
117 'dsn=s' => \$dsn,
118 'db-user=s' => \$db_user,
119 'db-pass=s' => \$db_pass,
120 'db-pass-env=s' => \$db_pass_env,
121 'api-key-id=s' => \$api_key_id,
122 'model=s' => \$model,
123 ) or die _usage_text();
124  
125 6 my %skeid_opts;
126 6 my $config_file = _config_file($config);
127 5 ▶ $skeid_opts{config_file} = $config_file if defined $config_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $config_file 1 4 5
128  
129 5 my $skeid = Langertha::Skeid->new(%skeid_opts);
130  
131 5 my %usage_store;
132 5 ▶ if (defined $backend && length $backend) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $backend and length $backend) 1 4 5
Condition66%2/3
defined $backend and length $backend
!ll&&!rl&&r
041
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
133 1 $usage_store{backend} = $backend;
134 }
135 5 ▶ if (defined $db_path && length $db_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $db_path and length $db_path) 0 5 5
Condition33%1/3
defined $db_path and length $db_path
!ll&&!rl&&r
050
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
136 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
137 0 $usage_store{sqlite_path} = $db_path;
138 }
139 5 ▶ if (defined $dsn && length $dsn) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dsn and length $dsn) 0 5 5
Condition33%1/3
defined $dsn and length $dsn
!ll&&!rl&&r
050
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
140 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
141 0 $usage_store{dsn} = $dsn;
142 }
143 5 ▶ if (defined $log_path && length $log_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $log_path and length $log_path) 4 1 5
Condition66%2/3
defined $log_path and length $log_path
!ll&&!rl&&r
014
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
144 4 ▶ $usage_store{backend} ||= 'jsonlog';
Condition100%2/2
$usage_store{"backend"} ||= "jsonlog"
l!l
13
MC/DC100%1/1
$usage_store{"backend"} ||= "jsonlog"
$usage_store{"backend"}
Truth table100%2/2
A
A= $usage_store{"backend"}
Aresult
11
00
145 4 $usage_store{log_path} = $log_path;
146 }
147 5 ▶ $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 5 5
Condition33%1/3
defined $db_user and length $db_user
!ll&&!rl&&r
050
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
148 5 ▶ $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 5 5
Condition33%1/3
defined $db_pass and length $db_pass
!ll&&!rl&&r
050
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
149 5 ▶ $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 5 5
Condition33%1/3
defined $db_pass_env and length $db_pass_env
!ll&&!rl&&r
050
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
150  
151 5 ▶ if (%usage_store) {
Branch: 1 branch
BranchTrueFalseTotal
if (%usage_store) 4 1 5
152 4 $skeid->configure_usage_store(\%usage_store);
153 }
154  
155 5 ▶ my $report = $skeid->call_function('usage.report', {
Branch: 3 branches
BranchTrueFalseTotal
defined $since && length $since ? : 0 5 5
defined $api_key_id && length $api_key_id ? : 0 5 5
defined $model && length $model ? : 0 5 5
Condition33%3/9
defined $since and length $since
!ll&&!rl&&r
050
defined $api_key_id and length $api_key_id
!ll&&!rl&&r
050
defined $model and length $model
!ll&&!rl&&r
050
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
156 limit => $limit,
157 (defined($since) && length($since) ? (since => $since) : ()),
158 (defined($api_key_id) && length($api_key_id) ? (api_key_id => $api_key_id) : ()),
159 (defined($model) && length($model) ? (model => $model) : ()),
160 });
161  
162 5 ▶ unless ($report->{ok}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($report->{"ok"}) 0 5 5
163 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
164 0 print "ERROR: $err\n";
165 0 return 2;
166 }
167  
168 5 ▶ if ($as_json) {
Branch: 1 branch
BranchTrueFalseTotal
if ($as_json) 0 5 5
169 0 my $json = JSON::MaybeXS->new(utf8 => 1, canonical => 1, pretty => 1);
170 0 print $json->encode($report);
171 0 return 0;
172 }
173  
174 5 ▶ my $backend_name = $report->{backend} // '';
Condition50%1/2
$report->{"backend"} // ""
l!l
50
MC/DC0%0/1
$report->{"backend"} // ""
$report->{"backend"}
Truth table50%1/2
A
A= $report->{"backend"}
Aresult
11
00
175 my $db_label = $backend_name eq 'sqlite' ? ($report->{db_path} // '')
176 5 ▶ : $backend_name eq 'jsonlog' ? ($report->{log_path} // '')
Branch: 3 branches
BranchTrueFalseTotal
$backend_name eq 'sqlite' ? : 0 5 5
$backend_name eq 'jsonlog' ? : 5 0 5
$backend_name eq 'postgresql' ? : 0 0 0
Condition25%1/4
$report->{"db_path"} // ""
l!l
00
$report->{"log_path"} // ""
l!l
50
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
177 : $backend_name eq 'postgresql' ? 'configured DSN'
178 : '(none)';
179 5 print "Usage backend: $backend_name\n";
180 5 print "Store: $db_label\n";
181 5 ▶ print "Since: ", ($report->{since} && length($report->{since}) ? $report->{since} : '(all)'), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$report->{'since'} && length $report->{'since'} ? : 0 5 5
Condition33%1/3
$report->{"since"} and length $report->{"since"}
!ll&&!rl&&r
500
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
182 5 print "\n";
183  
184 5 ▶ my $tot = $report->{totals} || {};
Condition50%1/2
$report->{'totals'} || {}
l!l
50
MC/DC0%0/1
$report->{'totals'} || {}
$report->{'totals'}
Truth table50%1/2
A
A= $report->{'totals'}
Aresult
11
00
185 printf "Totals: requests=%d input=%d output=%d total=%d cached=%d cache_write=%d tools=%d cost=\$%.8f\n",
186 ($tot->{requests} // 0),
187 ($tot->{input_tokens} // 0),
188 ($tot->{output_tokens} // 0),
189 ($tot->{total_tokens} // 0),
190 ($tot->{cached_tokens} // 0),
191 ($tot->{cache_write_tokens} // 0),
192 ($tot->{tool_calls} // 0),
193 5 ▶ ($tot->{total_cost_usd} // 0);
Condition50%8/16
$tot->{"requests"} // 0
l!l
50
$tot->{"input_tokens"} // 0
l!l
50
$tot->{"output_tokens"} // 0
l!l
50
$tot->{"total_tokens"} // 0
l!l
50
$tot->{"cached_tokens"} // 0
l!l
50
$tot->{"cache_write_tokens"} // 0
l!l
50
$tot->{"tool_calls"} // 0
l!l
50
$tot->{"total_cost_usd"} // 0
l!l
50
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
194  
195 5 print "\nBy API key:\n";
196 5 ▶ print " (none)\n" unless @{$report->{by_key} || []};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$report->{"by_key"} or [];} 0 5 5
Condition50%1/2
$report->{'by_key'} || []
l!l
50
MC/DC0%0/1
$report->{'by_key'} || []
$report->{'by_key'}
Truth table50%1/2
A
A= $report->{'by_key'}
Aresult
11
00
197 5 ▶ for my $row (@{$report->{by_key} || []}) {
Condition50%1/2
$report->{'by_key'} || []
l!l
50
MC/DC0%0/1
$report->{'by_key'} || []
$report->{'by_key'}
Truth table50%1/2
A
A= $report->{'by_key'}
Aresult
11
00
198 printf " %-16s requests=%d tokens=%d cost=\$%.8f\n",
199 ($row->{api_key_id} || '(none)'),
200 ($row->{requests} // 0),
201 ($row->{total_tokens} // 0),
202 10 ▶ ($row->{total_cost_usd} // 0);
Condition50%4/8
$row->{'api_key_id'} || '(none)'
l!l
100
$row->{"requests"} // 0
l!l
100
$row->{"total_tokens"} // 0
l!l
100
$row->{"total_cost_usd"} // 0
l!l
100
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
203 }
204  
205 5 print "\nBy model:\n";
206 5 ▶ print " (none)\n" unless @{$report->{by_model} || []};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$report->{"by_model"} or [];} 0 5 5
Condition50%1/2
$report->{'by_model'} || []
l!l
50
MC/DC0%0/1
$report->{'by_model'} || []
$report->{'by_model'}
Truth table50%1/2
A
A= $report->{'by_model'}
Aresult
11
00
207 5 ▶ for my $row (@{$report->{by_model} || []}) {
Condition50%1/2
$report->{'by_model'} || []
l!l
50
MC/DC0%0/1
$report->{'by_model'} || []
$report->{'by_model'}
Truth table50%1/2
A
A= $report->{'by_model'}
Aresult
11
00
208 printf " %-24s requests=%d tokens=%d cost=\$%.8f\n",
209 ($row->{model} || '(none)'),
210 ($row->{requests} // 0),
211 ($row->{total_tokens} // 0),
212 5 ▶ ($row->{total_cost_usd} // 0);
Condition50%4/8
$row->{'model'} || '(none)'
l!l
50
$row->{"requests"} // 0
l!l
50
$row->{"total_tokens"} // 0
l!l
50
$row->{"total_cost_usd"} // 0
l!l
50
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
213 }
214  
215 5 print "\nRecent:\n";
216 5 ▶ print " (none)\n" unless @{$report->{recent} || []};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$report->{"recent"} or [];} 0 5 5
Condition50%1/2
$report->{'recent'} || []
l!l
50
MC/DC0%0/1
$report->{'recent'} || []
$report->{'recent'}
Truth table50%1/2
A
A= $report->{'recent'}
Aresult
11
00
217 5 ▶ for my $row (@{$report->{recent} || []}) {
Condition50%1/2
$report->{'recent'} || []
l!l
50
MC/DC0%0/1
$report->{'recent'} || []
$report->{'recent'}
Truth table50%1/2
A
A= $report->{'recent'}
Aresult
11
00
218 # %s, not %d: a jsonlog event id is a string (time, pid, nonce, sequence), a DBI one a row id.
219 printf " #%s %s %-9s %-20s model=%s status=%d tokens=%d cached=%d cost=\$%.8f\n",
220 ($row->{id} // ''),
221 ($row->{created_at} || ''),
222 ($row->{api_format} || ''),
223 ($row->{api_key_id} || 'anonymous'),
224 ($row->{model} || ''),
225 ($row->{status_code} // 0),
226 ($row->{total_tokens} // 0),
227 ($row->{cached_tokens} // 0),
228 10 ▶ ($row->{cost_total_usd} // 0);
Condition50%9/18
$row->{"id"} // ""
l!l
100
$row->{'created_at'} || ''
l!l
100
$row->{'api_format'} || ''
l!l
100
$row->{'api_key_id'} || 'anonymous'
l!l
100
$row->{'model'} || ''
l!l
100
$row->{"status_code"} // 0
l!l
100
$row->{"total_tokens"} // 0
l!l
100
$row->{"cached_tokens"} // 0
l!l
100
$row->{"cost_total_usd"} // 0
l!l
100
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
229 }
230  
231 5 return 0;
232 }
233  
234 # The config file to load. Without --config, ./skeid.yaml is used when it exists and Skeid runs
235 # without a config when it does not. A --config that names no file ends the program: the
236 # operator asked for that config, and starting without it would serve no nodes (skeid k68).
237 8 ▶sub _config_file {
Subroutine _config_file: called CC=5 SCAR=16.1
238 8 my ($config) = @_;
239 8 ▶ return (-f 'skeid.yaml' ? 'skeid.yaml' : undef) unless defined $config;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $config 4 4 8
-f 'skeid.yaml' ? : 0 4 4
240 4 ▶ return $config if -f $config;
Branch: 1 branch
BranchTrueFalseTotal
if -f $config 1 3 4
241 3 ▶ print STDERR 'ERROR: config file not found: ' . $config
Branch: 1 branch
BranchTrueFalseTotal
-e $config ? : 1 2 3
242 . (-e $config ? ' (not a regular file)' : '') . "\n";
243 3 exit 2;
244 }
245  
246 # Prints the key id a customer key routes and bills under. Needed because a config names
247 # customers in keys:, and must be able to do that without holding their keys.
248 0 ▶sub _run_keyid {
Subroutine _run_keyid: not called CC=7 SCAR=40.3
249 0 my (@argv) = @_;
250  
251 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
252 0 ▶ if (!@keys) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@keys) 0 0 0
253 # Reading from stdin keeps the key off the command line, and out of the shell history.
254 0 ▶ while (my $line = <STDIN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline STDIN)) 0 0 0
255 0 chomp $line;
256 0 ▶ push @keys, $line if length $line;
Branch: 1 branch
BranchTrueFalseTotal
if length $line 0 0 0
257 }
258 }
259 0 ▶ die _usage_text() unless @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 0 0 0
260  
261 0 print Langertha::Skeid->key_id_for_key($_), "\n" for @keys;
262 0 return 0;
263 }
264  
265 0 ▶sub _usage_text {
Subroutine _usage_text: not called CC=1 SCAR=6.9
266 0 return <<'USAGE';
267 Usage:
268 skeid serve [--listen host:port] [--config skeid.yaml] [--admin-api-key KEY]
269 [--workers N] N > 1 runs prefork; each worker takes max_conns/N
270 skeid usage [--config skeid.yaml] [--since ISO8601] [--limit N] [--json]
271 [--backend jsonlog|sqlite|postgresql] [--log-path /path/to/events/]
272 [--db /path/to.sqlite] [--dsn dbi:Pg:...]
273 [--db-user USER] [--db-pass PASS|--db-pass-env ENV]
274 [--api-key-id ID] [--model NAME]
275 skeid keyid [KEY ...] the key id to use in the config's keys: section
276 (reads stdin when given no argument)
277 USAGE
278 }
279  
280 __END__
281  
282 =pod
283  
284 =encoding UTF-8
285  
286 =head1 NAME
287  
288 skeid - Skeid control-plane CLI and proxy launcher
289  
290 =head1 VERSION
291  
292 version 0.003
293  
294 =head1 SYNOPSIS
295  
296 skeid serve [--listen host:port] [--config skeid.yaml] [--admin-api-key KEY] [--workers N]
297 skeid usage [--config skeid.yaml] [--since ISO8601] [--limit N] [--json]
298 [--backend jsonlog|sqlite|postgresql] [--log-path /path/to/events/]
299 [--db /path/to.sqlite] [--dsn dbi:Pg:...]
300 [--db-user USER] [--db-pass PASS | --db-pass-env VAR]
301 [--api-key-id ID] [--model NAME]
302 skeid keyid [KEY ...]
303  
304 =head1 DESCRIPTION
305  
306 Runs the Skeid proxy (L<Langertha::Skeid::Proxy>), reports recorded usage, and prints the
307 customer key id a key routes and bills under. With no command, or when the first argument is an
308 option, the command is C<serve>. An unknown command prints the usage text and exits non-zero.
309  
310 The config file is described in L<Langertha::Skeid/CONFIGURATION>.
311  
312 =head1 COMMANDS
313  
314 =head2 serve
315  
316 skeid serve --config /etc/skeid/skeid.yaml --listen 0.0.0.0:8090 --workers 4
317  
318 Builds the app with L<Langertha::Skeid::Proxy/build_app> and serves it -- with
319 L<Mojo::Server::Daemon>, or L<Mojo::Server::Prefork> when C<--workers> is above 1. Prints the
320 address and config it starts with, and warns for every node whose C<max_conns> cannot be split
321 across the workers and frontends (L<Langertha::Skeid/worker_share_warnings>).
322  
323 =head2 --listen, -l
324  
325 C<host:port> to listen on (default C<127.0.0.1:8090>).
326  
327 =head2 --config, -c
328  
329 The YAML config. A path given here that is not an existing file ends the command with
330 C<ERROR: config file not found: ...> on standard error and exit code 2 -- Skeid does not start
331 without the config it was told to use. Without C<--config>, F<skeid.yaml> in the working
332 directory is used when it exists; when it does not, Skeid starts without a config, with no
333 nodes (the admin API can add them). A config file that disappears while Skeid runs keeps the
334 config in force and is warned about once (L<Langertha::Skeid/maybe_reload_config>).
335  
336 =head2 --admin-api-key
337  
338 Admin API key. It wins over any key the config names, on every reload; without it the key comes
339 from the config, else from C<SKEID_ADMIN_API_KEY> (see L<Langertha::Skeid/admin>). A key on the
340 command line is visible in the process list, so in production prefer C<admin.api_key_env> in
341 the config.
342  
343 =head2 --workers, -w
344  
345 Prefork worker count (default 1; below 1 counts as 1). Each worker admits its share of every
346 node's C<max_conns> and polls capacity probes at its share of the rate (ADR 0010). Under several
347 workers a SQLite usage store is unsafe, and an admin API write reaches only the worker that
348 served it.
349  
350 A usage store with C<flush_interval_ms> holds events in memory until they are written; stopping
351 the server writes them. Stop a prefork server with C<SIGQUIT> (graceful): on C<SIGINT> or
352 C<SIGTERM> Mojolicious's prefork manager kills its workers with C<SIGKILL>, and what they still
353 held is lost. A single process (C<--workers 1>) flushes on C<SIGINT>, C<SIGTERM> and C<SIGQUIT>.
354  
355 =head2 usage
356  
357 skeid usage --config /etc/skeid/skeid.yaml --since 2026-09-01T00:00:00Z --limit 50
358  
359 Prints a usage report: the backend and store (a C<jsonlog> path, the SQLite file, or
360 C<configured DSN>), totals, per API key id, per model, and the newest events. The store is the
361 config's C<usage_store>; any of C<--backend>, C<--log-path>, C<--db>, C<--dsn>, C<--db-user>,
362 C<--db-pass> or C<--db-pass-env> replaces it with a store built from those options alone. Exits
363 0, or 2 after printing C<ERROR: ...> when the report fails (no store configured, say) or
364 C<--config> names no file.
365  
366 =head2 --config, -c
367  
368 As for C<serve>: a missing file is an error; without the option, F<skeid.yaml> is read when it
369 exists.
370  
371 =head2 --since
372  
373 Only events with C<created_at> at or after this ISO 8601 UTC timestamp.
374  
375 =head2 --limit
376  
377 How many recent events to list (default 20, at most 500; below 1 means 20).
378  
379 =head2 --json
380  
381 Print the report as JSON instead of the text summary.
382  
383 =head2 --backend
384  
385 C<jsonlog>, C<sqlite> or C<postgresql>. Implied by C<--log-path>, C<--db> or C<--dsn>.
386  
387 =head2 --log-path, --jsonlog
388  
389 A C<jsonlog> store: its event directory (one file per event) or its JSON-lines file. An
390 existing directory, or a path ending in C</>, is read as a directory; anything else as a file.
391  
392 =head2 --db, --sqlite
393  
394 SQLite database file.
395  
396 =head2 --dsn
397  
398 PostgreSQL DSN, C<dbi:Pg:...>.
399  
400 =head2 --db-user, --db-pass
401  
402 PostgreSQL credentials. A password on the command line lands in the shell history; prefer
403 C<--db-pass-env>.
404  
405 =head2 --db-pass-env
406  
407 Name of an environment variable holding the PostgreSQL password.
408  
409 =head2 --api-key-id
410  
411 Only events of this customer key id.
412  
413 =head2 --model
414  
415 Only events for this served model.
416  
417 =head2 keyid
418  
419 echo -n "$CUSTOMER_KEY" | skeid keyid
420 skeid keyid sk-alice-secret
421  
422 Prints the customer key id (L<Langertha::Skeid/key_id_for_key>) of each key given, one per line
423 -- the id a config's C<keys:> and C<names:> sections use, so the config never holds the key.
424 Without arguments it reads one key per line from standard input, which keeps the key out of the
425 shell history. With no key at all it prints the usage text and exits non-zero.
426  
427 =head1 ENVIRONMENT
428  
429 C<serve> and C<usage> build a L<Langertha::Skeid>, which reads C<SKEID_ROUTE_WAIT_TIMEOUT_MS>,
430 C<SKEID_ROUTE_WAIT_POLL_MS>, C<SKEID_TRUST_KEY_ID_HEADER>, C<SKEID_FRONTEND_COUNT>,
431 C<SKEID_ADMIN_API_KEY>, C<SKEID_USAGE_DB>, C<SKEID_CAPACITY_MAX_AGE_MS> and
432 C<SKEID_CONFIG_RELOAD_INTERVAL> (L<Langertha::Skeid/ENVIRONMENT>), and whatever variables the
433 config names. C<serve> also reads:
434  
435 =head2 OPENBAO_ROLE_ID, OPENBAO_SECRET_ID
436  
437 Both set: upstream keys (C<api_key_ref>) are resolved from OpenBao through AppRole.
438  
439 =head2 OPENBAO_ADDR
440  
441 The OpenBao address (default C<http://127.0.0.1:8200>).
442  
443 =head2 OPENBAO_VERIFY_SSL
444  
445 C<0>, C<false>, C<no> or C<off> disables TLS verification towards OpenBao -- for a dev vault
446 only.
447  
448 =head2 SKEID_UPSTREAM_POOL
449  
450 Upstream connections kept per process (default 100).
451  
452 =head2 SKEID_UPSTREAM_TIMEOUT
453  
454 Seconds an upstream request may take, and may be silent for (default 300; a positive integer).
455 The client's connection is kept open for as long on top of the server's own inactivity timeout,
456 on the routes that call an upstream -- see L<Langertha::Skeid::Proxy/build_app>.
457  
458 =head1 SEE ALSO
459  
460 L<Langertha::Skeid>, L<Langertha::Skeid::Proxy>
461  
462 =head1 SUPPORT
463  
464 =head2 Issues
465  
466 Please report bugs and feature requests on GitHub at
467 L<https://github.com/Getty/langertha-skeid/issues>.
468  
469 =head2 IRC
470  
471 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
472  
473 =head1 CONTRIBUTING
474  
475 Contributions are welcome! Please fork the repository and submit a pull request.
476  
477 =head1 AUTHOR
478  
479 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
480  
481 =head1 COPYRIGHT AND LICENSE
482  
483 This software is copyright (c) 2026 by Torsten Raudssus.
484  
485 This is free software; you can redistribute it and/or modify it under
486 the same terms as the Perl 5 programming language system itself.
487  
488 =cut
↑ Summary blib/lib/Langertha/Skeid/UsageStore.pm »