blib/lib/Langertha/Skeid/UsageStore/DBI.pm

filter: Statementstmt 90.6% 483 / 533 Branchbran 69.4% 164 / 236 Conditioncond 46.4% 66 / 142 MC/DCmcdc 14.1% 11 / 78 Subroutinesub 97.2% 71 / 73 Podpod 100% 8 / 8 totaltotal 75.0% 803 / 1070 CC 189 SCAR 27.3
CC
189
Cov
78%
_report
64.5
dbh
30.9
__ANON__
30.0
CRAP 15.4
« lib/Langertha/Skeid/Registry.pm ↑ Summary lib/Langertha/Skeid/Proxy.pm »
1 package Langertha::Skeid::UsageStore::DBI;
2 15 our $VERSION = '0.004';
3 # ABSTRACT: SQLite and PostgreSQL usage store
4 15 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use File::ShareDir qw(dist_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Fcntl qw(LOCK_EX LOCK_NB);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use JSON::MaybeXS qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 15 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 15 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 15 ▶use Mojo::Util qw(steady_time);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 15 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 15 ▶use Sys::Hostname ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 15 ▶use Langertha::Skeid::UsageStore;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 15 has backend => (is => 'ro', required => 1);
24 15 has dsn => (is => 'ro', required => 1);
25 25 ▶has user => (is => 'ro', default => sub { '' });
Subroutine __ANON__: called CC=1 SCAR=0.0
26 25 ▶has password => (is => 'ro', default => sub { '' });
Subroutine __ANON__: called CC=1 SCAR=0.0
27 3 ▶has path => (is => 'ro', default => sub { '' });
Subroutine __ANON__: called CC=1 SCAR=0.0
28 25 ▶has schema_file => (is => 'ro', default => sub { '' });
Subroutine __ANON__: called CC=1 SCAR=0.0
29 25 ▶has auto_migrate => (is => 'ro', default => sub { 1 });
Subroutine __ANON__: called CC=1 SCAR=0.0
30 16 ▶has flush_interval_ms => (is => 'ro', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
31 15 has on_lost => (is => 'rw');
32 13 ▶has spool_dir => (is => 'ro', default => sub { '' });
Subroutine __ANON__: called CC=1 SCAR=0.0
33  
34 15 has _dbh => (is => 'rw');
35 58 ▶has _queue => (is => 'ro', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
36 15 has _flush_timer => (is => 'rw');
37 # Reconnect backoff (k110): no connect before _reconnect_at (steady clock); _backoff_s is the
38 # wait the last failure set, _connect_error the masked error it failed with.
39 58 ▶has _reconnect_at => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
40 58 ▶has _backoff_s => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
41 58 ▶has _connect_error => (is => 'rw', default => sub { '' });
Subroutine __ANON__: called CC=1 SCAR=0.0
42 # Prepared while the database was down (spool_dir): the schema is applied on the first connect.
43 58 ▶has _schema_pending => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
44 58 ▶has _spool_pending => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
45 58 ▶has _spool_announced => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
46 15 has _replay_timer => (is => 'rw');
47  
48 # The values ADR 0027 names; tests lower them.
49 15 our $BACKOFF_START_S = 1;
50 15 our $BACKOFF_MAX_S = 60;
51 15 our $REPLAY_BATCH = 500;
52  
53  
54 53 ▶sub prepare {
Subroutine prepare: called CC=9 SCAR=22.4
Pod documented
55 53 my ($self) = @_;
56 53 ▶ if (length $self->spool_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->spool_dir) 11 42 53
57 11 ▶ $self->_spool_pending(1) if $self->_spool_files('any');
Branch: 1 branch
BranchTrueFalseTotal
if $self->_spool_files("any") 1 10 11
58 11 my $dbh = eval { $self->dbh };
59 11 ▶ unless ($dbh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh) 1 10 11
60 1 my $err = $@;
61 1 ▶ return unless defined($err) && length($err); # no DBI: as without a spool
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err and length $err 0 1 1
Condition33%1/3
defined $err and length $err
!ll&&!rl&&r
001
MC/DC0%0/2
defined $err and length $err
defined $err length $err
Truth table33%1/3
A and B
A= defined $errB= length $err
ABresult
0X0
100
111
62 1 ▶ if ($self->auto_migrate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_migrate) 1 0 1
63 1 $self->_schema_path; # a missing file still croaks here
64 1 $self->_schema_pending(1);
65 }
66 1 warn 'skeid: usage database unreachable at start, usage events go to the spool in '
67 . $self->spool_dir . ': ' . $self->_error_text($err) . "\n";
68 1 $self->_spool_announced(1);
69 1 return 1;
70 }
71 }
72 52 ▶ my $dbh = $self->dbh or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->dbh or return 51 0 51
73 51 ▶ $self->_apply_schema($dbh) if $self->auto_migrate;
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_migrate 51 0 51
74 51 $self->_schedule_replay;
75 51 return 1;
76 }
77  
78 53 ▶sub _schema_path {
Subroutine _schema_path: called CC=3 SCAR=11.3
79 53 my ($self) = @_;
80 53 my $schema_file = $self->schema_file;
81 53 ▶ $schema_file = $self->shipped_schema_file unless length $schema_file;
Branch: 1 branch
BranchTrueFalseTotal
unless length $schema_file 53 0 53
82 53 ▶ croak "usage schema file not found: $schema_file" unless -f $schema_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $schema_file 0 53 53
83 53 return $schema_file;
84 }
85  
86 52 ▶sub _apply_schema {
Subroutine _apply_schema: called CC=5 SCAR=16.1
87 52 my ($self, $dbh) = @_;
88 52 my $sql = Langertha::Skeid::UsageStore::read_text_file($self->_schema_path);
89 312 my @stmts = grep { /\S/ } map {
90 52 my $s = $_;
91 312 $s =~ s/^\s+//;
92 312 $s =~ s/\s+$//;
93 312 $s;
94 } split /;\s*(?:\n|$)/, $sql;
95 # Tables first, then any column an older table is missing, then the rest. The index
96 # statements reference columns, so on an upgraded table they only work after the ALTER --
97 # running the file top to bottom fails on exactly the deployments this exists for.
98 52 my (@tables, @rest);
99 52 for my $stmt (@stmts) {
100 312 ▶ if ($stmt =~ /\A\s*CREATE\s+TABLE\b/i) { push @tables, $stmt }
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ /\A\s*CREATE\s+TABLE\b/i) { } 52 260 312
101 260 else { push @rest, $stmt }
102 }
103 52 $dbh->do($_) for @tables;
104 52 $self->_add_missing_columns($dbh);
105 52 $dbh->do($_) for @rest;
106 52 return 1;
107 }
108  
109 # The shipped schema is CREATE TABLE IF NOT EXISTS, so it does nothing to a table that already
110 # exists -- a deployment that upgrades Skeid keeps its old columns and every insert naming a
111 # new one fails. Usage failures are reported rather than thrown, so that would show up as
112 # silently missing billing data. Adding the column is the whole migration story; nothing here
113 # ever drops or rewrites one.
114 15 my @ADDED_COLUMNS = (
115 ['requested_model', 'TEXT'],
116 # Prompt-cache read count (k27). Nullable on purpose: an old row that predates it reads NULL
117 # ("was not measured"), which is not the same as a measured zero. BIGINT is INTEGER affinity
118 # in SQLite and a 64-bit integer in PostgreSQL, so one type serves both ALTER statements.
119 ['cached_tokens', 'BIGINT'],
120 # UTF-8 bytes a streamed request relayed (skeid #36). Nullable: only streamed events carry it.
121 ['content_bytes', 'BIGINT'],
122 # Prompt-cache read / write cost (skeid #28), already part of cost_total_usd. Nullable: an old
123 # row reads NULL ("was not priced apart"). DOUBLE PRECISION is REAL affinity in SQLite.
124 ['cost_cache_read_usd', 'DOUBLE PRECISION'],
125 ['cost_cache_write_usd', 'DOUBLE PRECISION'],
126 # Prompt-cache write count (skeid #41), beside cached_tokens. Nullable: an old row reads NULL.
127 ['cache_write_tokens', 'BIGINT'],
128 # Seconds of audio an audio route's node reported (skeid k91, ADR 0021). Nullable: an event
129 # whose node did not say reads NULL ("not measured"), not a measured zero.
130 ['audio_seconds', 'DOUBLE PRECISION'],
131 # Documents a rerank request carried (skeid k92). Nullable: only a rerank event has a count.
132 ['documents', 'BIGINT'],
133 # Pages a documents request converted (skeid k94, ADR 0028), read off the answer's
134 # json_content. Nullable: only a documents event whose answer carried a page count has one.
135 ['pages', 'BIGINT'],
136 );
137  
138 52 ▶sub _add_missing_columns {
Subroutine _add_missing_columns: called CC=5 SCAR=16.1
139 52 my ($self, $dbh) = @_;
140  
141 52 ▶ my $existing = eval {
Branch: 1 branch
BranchTrueFalseTotal
my $existing = eval { do { my $sth = $dbh->prepare("SELECT * FROM usage_events WHERE 1=0"); $sth->execute; my(%cols) = map({lc $_, 1;} @{$sth->{"NAME_lc"} or [];}); $sth->finish; \%cols } } or return 52 0 52
142 52 my $sth = $dbh->prepare('SELECT * FROM usage_events WHERE 1=0');
143 52 $sth->execute;
144 52 ▶ my %cols = map { lc($_) => 1 } @{ $sth->{NAME_lc} || [] };
Condition50%1/2
$sth->{'NAME_lc'} || []
l!l
520
MC/DC0%0/1
$sth->{'NAME_lc'} || []
$sth->{'NAME_lc'}
Truth table50%1/2
A
A= $sth->{'NAME_lc'}
Aresult
11
00
145 52 $sth->finish;
146 52 \%cols;
147 } or return;
148  
149 52 for my $column (@ADDED_COLUMNS) {
150 468 my ($name, $type) = @$column;
151 468 ▶ next if $existing->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $existing->{$name} 419 49 468
152 49 eval { $dbh->do("ALTER TABLE usage_events ADD COLUMN $name $type") };
153 }
154 52 return 1;
155 }
156  
157  
158 274 ▶sub dbh {
Subroutine dbh: called CC=20 SCAR=34.3
Pod documented
159 274 my ($self) = @_;
160 274 my $cached = $self->_dbh;
161 274 ▶ return $cached if $cached;
Branch: 1 branch
BranchTrueFalseTotal
if $cached 129 145 274
162 145 ▶ return unless length($self->backend) && length($self->dsn);
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->backend and length $self->dsn 0 145 145
Condition33%1/3
length $self->backend and length $self->dsn
!ll&&!rl&&r
00145
MC/DC0%0/2
length $self->backend and length $self->dsn
length $self->backend length $self->dsn
Truth table33%1/3
A and B
A= length $self->backendB= length $self->dsn
ABresult
0X0
100
111
163  
164 145 ▶ eval { require DBI } or return;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require DBI } } or return 145 0 145
165  
166 145 ▶ if ($self->backend eq 'sqlite' && length $self->path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->backend eq "sqlite" and length $self->path) 145 0 145
Condition33%1/3
$self->backend eq "sqlite" and length $self->path
!ll&&!rl&&r
00145
MC/DC0%0/2
$self->backend eq "sqlite" and length $self->path
$self->backend eq "sqlite" length $self->path
Truth table33%1/3
A and B
A= $self->backend eq "sqlite"B= length $self->path
ABresult
0X0
100
111
167 145 my $dir = dirname($self->path);
168 145 ▶ if (defined $dir && length $dir && $dir ne '.' && !-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dir and length $dir and $dir ne "." and not -d $dir) 0 145 145
Condition33%3/9
defined $dir and length $dir and $dir ne "." and not -d $dir
!ll&&!rl&&r
01450
defined $dir and length $dir and $dir ne "."
!ll&&!rl&&r
00145
defined $dir and length $dir
!ll&&!rl&&r
00145
MC/DC0%0/4
defined $dir and length $dir and $dir ne "." and not -d $dir
defined $dir length $dir $dir ne "." not -d $dir
Truth table20%1/5
A and B and C and not(D)
A= defined $dirB= length $dirC= $dir ne "."D= not -d $dir
ABCDresult
0XXX0
10XX0
110X0
11100
11111
169 0 make_path($dir);
170 }
171 }
172  
173 145 my %connect_attr = (
174 RaiseError => 1,
175 PrintError => 0,
176 AutoCommit => 1,
177 );
178 145 ▶ $connect_attr{sqlite_unicode} = 1 if $self->backend eq 'sqlite';
Branch: 1 branch
BranchTrueFalseTotal
if $self->backend eq "sqlite" 145 0 145
179  
180 145 my $wait = $self->_reconnect_at - _now();
181 145 ▶ if ($wait > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wait > 0) 43 102 145
182 43 die sprintf("usage database unreachable (next connect attempt in %.1fs): %s\n",
183 $wait, $self->_connect_error);
184 }
185  
186 102 my $dbh = eval {
187 102 ▶ my $h = DBI->connect($self->dsn, $self->user, $self->password, \%connect_attr)
Branch: 1 branch
BranchTrueFalseTotal
my $h = "DBI"->connect($self->dsn, $self->user, $self->password, \%connect_attr) or die +("DBI"->errstr // "failed to connect usage database") . "\n" 63 0 63
Condition0%0/2
"DBI"->errstr // "failed to connect usage database"
l!l
00
MC/DC0%0/1
"DBI"->errstr // "failed to connect usage database"
"DBI"->errstr
Truth table0%0/2
A
A= "DBI"->errstr
Aresult
11
00
188 or die((DBI->errstr // 'failed to connect usage database') . "\n");
189 # Prepared while the database was down (spool_dir): the schema waited for this connect.
190 63 ▶ if ($self->_schema_pending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_schema_pending) 1 62 63
191 1 ▶ unless (eval { $self->_apply_schema($h); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->_apply_schema($h); 1 } }) 0 1 1
192 0 my $err = $@;
193 0 eval { $h->disconnect };
194 0 die $err;
195 }
196 1 $self->_schema_pending(0);
197 }
198 63 $h;
199 };
200 102 ▶ unless ($dbh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh) 39 63 102
201 39 ▶ my $err = $@ || 'failed to connect usage database';
Condition50%1/2
$@ || 'failed to connect usage database'
l!l
390
MC/DC0%0/1
$@ || 'failed to connect usage database'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
202 # Back off (k110): one blocking connect per deadline while the database is away, never one
203 # per event or per flush tick.
204 39 ▶ my $backoff = $self->_backoff_s
Branch: 1 branch
BranchTrueFalseTotal
$self->_backoff_s ? : 26 13 39
205 ? min($self->_backoff_s * 2, $BACKOFF_MAX_S)
206 : $BACKOFF_START_S;
207 39 $self->_backoff_s($backoff);
208 39 $self->_reconnect_at(_now() + $backoff);
209 39 $self->_connect_error($self->_error_text($err));
210 39 die $err;
211 }
212 63 $self->_backoff_s(0);
213 63 $self->_reconnect_at(0);
214 63 $self->_connect_error('');
215 63 $self->_spool_announced(0);
216 63 $self->_dbh($dbh);
217 63 return $dbh;
218 }
219  
220 # The steady clock the backoff is measured on; a test moves it.
221 49 ▶sub _now { steady_time() }
Subroutine _now: called CC=1 SCAR=0.0
222  
223  
224 31 ▶sub disconnect {
Subroutine disconnect: called CC=2 SCAR=7.6
Pod documented
225 31 my ($self) = @_;
226 31 ▶ if (defined(my $timer = $self->_replay_timer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $timer = $self->_replay_timer)) 0 31 31
227 0 Mojo::IOLoop->remove($timer);
228 0 $self->_replay_timer(undef);
229 }
230 31 $self->flush;
231 31 $self->_drop_handle;
232 31 return;
233 }
234  
235 # Forget the handle without flushing: what _with_handle does to a handle a failed statement left
236 # dead, in the middle of a flush as well -- where a flush of its own would be a re-entry.
237 44 ▶sub _drop_handle {
Subroutine _drop_handle: called CC=2 SCAR=7.0
238 44 my ($self) = @_;
239 44 ▶ my $dbh = $self->_dbh or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->_dbh or return 44 0 44
240 44 eval { $dbh->disconnect };
241 44 $self->_dbh(undef);
242 44 return;
243 }
244  
245  
246 55 ▶sub shipped_schema_file {
Subroutine shipped_schema_file: called CC=10 SCAR=25.3
Pod documented
247 55 my ($self) = @_;
248 55 ▶ my $name = ($self->backend eq 'postgresql') ? 'usage_events.postgresql.sql' : 'usage_events.sqlite.sql';
Branch: 1 branch
BranchTrueFalseTotal
$self->backend eq 'postgresql' ? : 1 54 55
249 55 my @candidates;
250  
251 # Installed/runtime lookup via dist sharedir.
252 55 my $share_dir = eval { dist_dir('Langertha-Skeid') };
253 55 ▶ if (!$@ && defined($share_dir) && length($share_dir)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@ and defined $share_dir and length $share_dir) 55 0 55
Condition33%2/6
not $@ and defined $share_dir and length $share_dir
!ll&&!rl&&r
0055
not $@ and defined $share_dir
!ll&&!rl&&r
0055
MC/DC0%0/3
not $@ and defined $share_dir and length $share_dir
not $@ defined $share_dir length $share_dir
Truth table25%1/4
not(A) and B and C
A= not $@B= defined $share_dirC= length $share_dir
ABCresult
0XX0
10X0
1100
1111
254 55 push @candidates, File::Spec->catfile($share_dir, 'sql', $name);
255 }
256  
257 # Dev + dzil test fallback: walk up from this file looking for the repo's share/.
258 55 my $dir = dirname(dirname(dirname(dirname(__FILE__))));
259 55 for (1 .. 6) {
260 330 push @candidates, File::Spec->catfile($dir, 'share', 'sql', $name);
261 330 push @candidates, File::Spec->catfile($dir, 'sql', $name);
262 330 my $parent = dirname($dir);
263 330 ▶ last if !defined($parent) || $parent eq $dir;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $parent or $parent eq $dir 0 330 330
Condition33%1/3
not defined $parent or $parent eq $dir
l!l&&r!l&&!r
00330
MC/DC0%0/2
not defined $parent or $parent eq $dir
not defined $parent $parent eq $dir
Truth table33%1/3
not(A) or B
A= not defined $parentB= $parent eq $dir
ABresult
1X1
011
000
264 330 $dir = $parent;
265 }
266  
267 55 for my $path (@candidates) {
268 55 ▶ return $path if -f $path;
Branch: 1 branch
BranchTrueFalseTotal
if -f $path 55 0 55
269 }
270  
271 0 return $candidates[0];
272 }
273  
274  
275 154 ▶sub store {
Subroutine store: called CC=7 SCAR=19.5
Pod documented
276 154 my ($self, $event) = @_;
277 154 ▶ if ($self->_writes_behind) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_writes_behind) 33 121 154
278 33 push @{ $self->_queue }, { %$event };
279 33 $self->_arm_flush;
280 33 return { ok => 1, queued => 1 };
281 }
282 # Whatever was queued while the loop ran goes first, so the table keeps the order the events
283 # arrived in.
284 121 ▶ $self->flush if @{ $self->_queue };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->_queue;} 1 120 121
285 65 ▶ my $res = $self->_with_handle(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
286 65 my ($dbh) = @_;
287 65 return { ok => 1, $self->_insert($dbh, $event) };
288 121 });
289 121 ▶ if ($res->{ok}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"ok"}) 52 69 121
290 52 $self->_schedule_replay;
291 52 return $res;
292 }
293 # A live handle refused the event: spooling it would only replay the refusal.
294 69 ▶ return $res if $self->_dbh || !length($self->spool_dir);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_dbh or not length $self->spool_dir 30 39 69
Condition100%3/3
$self->_dbh or not length $self->spool_dir
l!l&&r!l&&!r
32739
MC/DC100%2/2
$self->_dbh or not length $self->spool_dir
$self->_dbh not length $self->spool_dir
Truth table100%3/3
A or not(B)
A= $self->_dbhB= not length $self->spool_dir
ABresult
1X1
011
000
295 39 my $spool_err = $self->_spool_events([$event]);
296 39 ▶ unless ($spool_err) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spool_err) 39 0 39
297 39 $self->_announce_spool($res->{error});
298 39 return { ok => 1, spooled => 1 };
299 }
300 0 return $self->_failure("$res->{error}; spool write failed: $spool_err");
301 }
302  
303 # Queue only while a loop runs to fire the flush timer. Outside one -- a script calling
304 # usage.record, the prefork manager before it forks -- there is no request path to keep free and
305 # a queue that nothing would ever write.
306 154 ▶sub _writes_behind {
Subroutine _writes_behind: called CC=3 SCAR=11.0
307 154 my ($self) = @_;
308 154 ▶ return 0 unless $self->flush_interval_ms > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->flush_interval_ms > 0 65 89 154
309 89 ▶ return Mojo::IOLoop->is_running ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
'Mojo::IOLoop'->is_running ? : 33 56 89
310 }
311  
312 33 ▶sub _arm_flush {
Subroutine _arm_flush: called CC=2 SCAR=6.9
313 33 my ($self) = @_;
314 33 ▶ return if defined $self->_flush_timer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_flush_timer 9 24 33
315 24 weaken(my $weak = $self);
316 2 ▶ $self->_flush_timer(Mojo::IOLoop->timer($self->flush_interval_ms / 1000 => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
317 2 ▶ return unless $weak;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak 0 2 2
318 2 $weak->_flush_timer(undef);
319 2 $weak->flush;
320 24 }));
321 24 return;
322 }
323  
324  
325 68 ▶sub flush {
Subroutine flush: called CC=10 SCAR=23.2
Pod documented
326 68 my ($self) = @_;
327 68 ▶ if (defined(my $timer = $self->_flush_timer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $timer = $self->_flush_timer)) 22 46 68
328 22 Mojo::IOLoop->remove($timer);
329 22 $self->_flush_timer(undef);
330 }
331 68 my @events = splice @{ $self->_queue };
332 68 ▶ return { ok => 1, written => 0 } unless @events;
Branch: 1 branch
BranchTrueFalseTotal
unless @events 44 24 68
333  
334 14 ▶ my $batch = $self->_with_handle(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
335 14 my ($dbh) = @_;
336 14 $dbh->begin_work;
337 14 my $done = eval {
338 14 $self->_insert($dbh, $_) for @events;
339 9 $dbh->commit;
340 9 1;
341 };
342 14 ▶ unless ($done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 5 9 14
343 5 my $err = $@;
344 5 eval { $dbh->rollback };
345 5 die $err;
346 }
347 9 return { ok => 1, written => scalar @events };
348 24 });
349 24 ▶ if ($batch->{ok}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($batch->{"ok"}) 9 15 24
350 9 $self->_schedule_replay;
351 9 return $batch;
352 }
353  
354 # No handle left: the reconnect failed (or the backoff runs), and one attempt per event would
355 # only repeat it.
356 15 ▶ return _flush_answer(0, $self->_spool_or_lose(\@events, $batch->{error}))
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_dbh 12 3 15
357 unless $self->_dbh;
358  
359 3 my ($written, $spooled, $lost, $error) = (0, 0, 0);
360 3 ▶ while (@events) {
Branch: 1 branch
BranchTrueFalseTotal
if (@events) 5 3 8
361 5 my $event = shift @events;
362 5 ▶ my $one = $self->_with_handle(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
363 5 my ($dbh) = @_;
364 5 $self->_insert($dbh, $event);
365 2 return { ok => 1 };
366 5 });
367 5 ▶ if ($one->{ok}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($one->{"ok"}) 2 3 5
368 2 $written++;
369 2 next;
370 }
371 3 ▶ if ($self->_dbh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_dbh) 3 0 3
372 # Refused on a live handle: this event alone is lost, and never spooled.
373 3 $lost++;
374 3 $error = $one->{error};
375 3 $self->_report_lost($event, $error);
376 3 next;
377 }
378 # The handle died under the single writes and did not come back: this event and the rest
379 # go with it.
380 0 my ($s, $l, $e) = $self->_spool_or_lose([$event, @events], $one->{error});
381 0 $spooled += $s;
382 0 $lost += $l;
383 0 ▶ $error = $e if $l;
Branch: 1 branch
BranchTrueFalseTotal
if $l 0 0 0
384 0 last;
385 }
386 3 ▶ $self->_schedule_replay if $written;
Branch: 1 branch
BranchTrueFalseTotal
if $written 1 2 3
387 3 return _flush_answer($written, $spooled, $lost, $error);
388 }
389  
390 15 ▶sub _flush_answer {
Subroutine _flush_answer: called CC=4 SCAR=13.9
391 15 my ($written, $spooled, $lost, $error) = @_;
392 return {
393 15 ▶ ok => ($lost ? 0 : 1),
Branch: 3 branches
BranchTrueFalseTotal
$lost ? : 9 6 15
$spooled ? : 6 9 15
$lost ? : 9 6 15
394 written => $written,
395 ($spooled ? (spooled => $spooled) : ()),
396 ($lost ? (lost => $lost, error => $error) : ()),
397 };
398 }
399  
400 # Events that could not be written for want of a connection: to the spool when there is one,
401 # else -- or when the spool write fails -- reported lost. Answers ($spooled, $lost, $error).
402 12 ▶sub _spool_or_lose {
Subroutine _spool_or_lose: called CC=4 SCAR=14.0
403 12 my ($self, $events, $err) = @_;
404 12 ▶ if (length $self->spool_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->spool_dir) 6 6 12
405 6 my $spool_err = $self->_spool_events($events);
406 6 ▶ unless ($spool_err) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spool_err) 6 0 6
407 6 $self->_announce_spool($err);
408 6 return (scalar(@$events), 0, undef);
409 }
410 0 $err = $self->_error_text($err) . "; spool write failed: $spool_err";
411 }
412 6 my $failure = $self->_failure($err);
413 6 $self->_report_lost($_, $failure->{error}) for @$events;
414 6 return (0, scalar(@$events), $failure->{error});
415 }
416  
417 # Fail loud once per outage: spooled events are not lost, but the database is away.
418 45 ▶sub _announce_spool {
Subroutine _announce_spool: called CC=2 SCAR=6.9
419 45 my ($self, $err) = @_;
420 45 ▶ return if $self->_spool_announced;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_spool_announced 38 7 45
421 7 $self->_spool_announced(1);
422 7 warn 'skeid: usage database unreachable, usage events go to the spool in '
423 . $self->spool_dir . ' until it is back: ' . $self->_error_text($err) . "\n";
424 7 return;
425 }
426  
427 11 ▶sub _report_lost {
Subroutine _report_lost: called CC=4 SCAR=17.9
428 11 my ($self, $event, $err) = @_;
429 11 my $cb = $self->on_lost;
430 11 ▶ return if $cb && eval { $cb->($event, $err); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if $cb and eval { do { &$cb($event, $err); 1 } } 11 0 11
Condition33%1/3
$cb and eval { do { &$cb($event, $err); 1 } }
!ll&&!rl&&r
0011
MC/DC0%0/2
$cb and eval { do { &$cb($event, $err); 1 } }
$cb eval { do { &$cb($event, $err); 1 } }
Truth table33%1/3
A and B
A= $cbB= eval { do { &$cb($event, $err); 1 } }
ABresult
0X0
100
111
431 # A timer callback that dies takes the reactor's error path; a lost event must still be said.
432 0 ▶ warn 'skeid: usage event lost: request_id=' . ($event->{request_id} // '')
Condition0%0/2
$event->{"request_id"} // ""
l!l
00
MC/DC0%0/1
$event->{"request_id"} // ""
$event->{"request_id"}
Truth table0%0/2
A
A= $event->{"request_id"}
Aresult
11
00
433 . ' store=' . $self->backend . ': ' . $err . "\n";
434 0 return;
435 }
436  
437 # Runs $work with the store's handle and returns its answer, or { ok => 0, error } when it dies.
438 # On a failure that left the handle dead the handle is dropped, connected once more and $work
439 # retried once on the new one (skeid k71). Never more than that, never a wait.
440 184 ▶sub _with_handle {
Subroutine _with_handle: called CC=8 SCAR=21.0
441 184 my ($self, $work) = @_;
442  
443 184 my $dbh = eval { $self->dbh };
444 184 ▶ return $self->_failure($@ || 'failed to connect usage database') unless $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 69 115 184
Condition50%1/2
$@ || 'failed to connect usage database'
l!l
690
MC/DC0%0/1
$@ || 'failed to connect usage database'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
445  
446 115 my $res = eval { $work->($dbh) };
447 115 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 93 22 115
448 22 my $err = $@;
449 22 ▶ return $self->_failure($err) if $self->_handle_alive($dbh);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_handle_alive($dbh) 9 13 22
450  
451 13 $self->_drop_handle;
452 13 my $fresh = eval { $self->dbh };
453 13 ▶ return $self->_failure('usage database connection lost, reconnect failed: '
Branch: 1 branch
BranchTrueFalseTotal
unless $fresh 11 2 13
Condition50%1/2
$@ || 'no database handle'
l!l
110
MC/DC0%0/1
$@ || 'no database handle'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
454 .($@ || 'no database handle')) unless $fresh;
455  
456 2 $res = eval { $work->($fresh) };
457 2 ▶ return $res || $self->_failure($@);
Condition33%1/3
$res || $self->_failure($@)
l!l&&r!l&&!r
200
MC/DC0%0/2
$res || $self->_failure($@)
$res $self->_failure($@)
Truth table33%1/3
A || B
A= $resB= $self->_failure($@)
ABresult
1X1
011
000
458 }
459  
460 # Asked only after a statement failed, so the round-trip a ping costs on PostgreSQL is never
461 # paid by a healthy write. DBD::Pg keeps Active set on a connection the server dropped; ping
462 # is what notices.
463 22 ▶sub _handle_alive {
Subroutine _handle_alive: called CC=3 SCAR=11.0
464 22 my ($self, $dbh) = @_;
465 22 ▶ return 0 unless $dbh->{Active};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"Active"} 13 9 22
466 9 ▶ return eval { $dbh->ping } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $dbh->ping } } ? : 9 0 9
467 }
468  
469 95 ▶sub _failure {
Subroutine _failure: called CC=1 SCAR=0.0
470 95 my ($self, $err) = @_;
471 95 return { ok => 0, error => $self->_error_text($err) };
472 }
473  
474 # A DBI error names the DSN it failed on, and a DSN may carry the password. Nothing that
475 # leaves this store may (ADR 0003).
476 143 ▶sub _error_text {
Subroutine _error_text: called CC=3 SCAR=11.8
477 143 my ($self, $err) = @_;
478 143 ▶ $err = 'unknown database error' unless defined($err) && length($err);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err and length $err 0 143 143
Condition33%1/3
defined $err and length $err
!ll&&!rl&&r
00143
MC/DC0%0/2
defined $err and length $err
defined $err length $err
Truth table33%1/3
A and B
A= defined $errB= length $err
ABresult
0X0
100
111
479 143 $err =~ s/\b(password|pwd)=[^;'"\s)]*/$1=***/gi;
480 143 $err =~ s/\s+$//;
481 143 return $err;
482 }
483  
484 # The columns of one event, in the order _insert binds them. The spool writes exactly these keys
485 # of an event and nothing else (k110), so a new column is added here once and reaches both.
486 15 my @EVENT_COLUMNS = qw(
487 created_at request_id api_format endpoint api_key_id provider engine model node_id route_url
488 status_code ok duration_ms input_tokens output_tokens total_tokens cached_tokens tool_calls
489 cost_input_usd cost_output_usd cost_total_usd error_type error_message requested_model
490 content_bytes cost_cache_read_usd cost_cache_write_usd cache_write_tokens audio_seconds
491 documents pages
492 );
493 # Nullable, written as NULL when the event has no such key -- "not measured", not a zero:
494 # cached_tokens an event that carried no cache count
495 # content_bytes only a streamed request carries it
496 # cost_cache_read_usd,
497 # cost_cache_write_usd an event from a caller that does not price the cache apart
498 # cache_write_tokens an event that carried no cache write count
499 # audio_seconds only an audio route's event, and only when its node reported it
500 # documents only a rerank or documents event
501 # pages only a documents event whose answer said how many pages it was
502 # requested_model falls back to model, then ''.
503 15 my $INSERT_SQL = 'INSERT INTO usage_events (' . join(', ', @EVENT_COLUMNS) . ') VALUES ('
504 . join(', ', ('?') x @EVENT_COLUMNS) . ')';
505  
506 137 ▶sub _insert {
Subroutine _insert: called CC=6 SCAR=19.3
507 137 my ($self, $dbh, $event) = @_;
508 137 my $sth = $dbh->prepare_cached($INSERT_SQL);
509 $sth->execute(map {
510 120 $_ eq 'requested_model'
511 ? ($event->{requested_model} // $event->{model} // '')
512 3720 ▶ : $event->{$_}
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'requested_model' ? : 120 3600 3720
Condition60%3/5
($event->{"requested_model"} // $event->{"model"}) // ""
l!l
1200
$event->{"requested_model"} // $event->{"model"}
l!l&&r!l&&!r
41790
MC/DC0%0/2
($event->{"requested_model"} // $event->{"model"}) // ""
$event->{"requested_model"} $event->{"model"}
Truth table66%2/3
A // B
A= $event->{"requested_model"}B= $event->{"model"}
ABresult
1X1
011
000
513 } @EVENT_COLUMNS);
514  
515 116 ▶ return ($self->backend eq 'sqlite' && $dbh->can('sqlite_last_insert_rowid'))
Branch: 1 branch
BranchTrueFalseTotal
$self->backend eq 'sqlite' && $dbh->can('sqlite_last_insert_rowid') ? : 116 0 116
Condition33%1/3
$self->backend eq "sqlite" and $dbh->can("sqlite_last_insert_rowid")
!ll&&!rl&&r
00116
MC/DC0%0/2
$self->backend eq "sqlite" and $dbh->can("sqlite_last_insert_rowid")
$self->backend eq "sqlite" $dbh->can("sqlite_last_insert_rowid")
Truth table33%1/3
A and B
A= $self->backend eq "sqlite"B= $dbh->can("sqlite_last_insert_rowid")
ABresult
0X0
100
111
516 ? ( id => Langertha::Skeid::UsageStore::num($dbh->sqlite_last_insert_rowid) )
517 : ();
518 }
519  
520 # Replay's insert (k110): skipped when the event is already in the table. The key is
521 # request_id with created_at -- the spool holds the event as it was built, so a copy that made it
522 # in carries both, while a client that sends the same x-request-id twice does not lose its
523 # second, real event. Without a request_id there is no key: the event is written. Answers 1 for
524 # a row written, 0 for one skipped.
525 53 ▶sub _insert_once {
Subroutine _insert_once: called CC=4 SCAR=13.9
526 53 my ($self, $dbh, $event) = @_;
527 53 my $request_id = $event->{request_id};
528 53 ▶ if (defined($request_id) && length($request_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $request_id and length $request_id) 52 1 53
Condition66%2/3
defined $request_id and length $request_id
!ll&&!rl&&r
0152
MC/DC50%1/2
defined $request_id and length $request_id
defined $request_id length $request_id
Truth table66%2/3
A and B
A= defined $request_idB= length $request_id
ABresult
0X0
100
111
529 my $found = $dbh->selectrow_arrayref(
530 'SELECT 1 FROM usage_events WHERE request_id = ? AND created_at = ?',
531 52 undef, $request_id, $event->{created_at});
532 52 ▶ return 0 if $found;
Branch: 1 branch
BranchTrueFalseTotal
if $found 4 48 52
533 }
534 49 $self->_insert($dbh, $event);
535 49 return 1;
536 }
537  
538 # --- The spool (k110, ADR 0027) ----------------------------------------------------------------
539 #
540 # Writers append to usage-spool-<host>-<pid>.jsonl, one per process. A replay first claims a
541 # writer's file by renaming it to <name>.replay-<host>-<pid>-<n> under the file's lock; a writer
542 # that was waiting for that lock sees the path no longer names its file and opens a new one. A
543 # claimed file is then worked under its own lock, which only replays ever take -- so a writer
544 # waits at most for a rename, never for a database. Temporary files start with a dot and match
545 # neither pattern.
546  
547 15 my $SPOOL_FILE = qr/\Ausage-spool-[A-Za-z0-9._-]+\.jsonl\z/;
548 15 my $CLAIMED_FILE = qr/\Ausage-spool-[A-Za-z0-9._-]+\.jsonl\.replay-[A-Za-z0-9._-]+\z/;
549 15 my $CLAIM_SEQ = 0;
550 15 my $HOST;
551  
552 58 ▶sub _host {
Subroutine _host: called CC=4 SCAR=14.5
553 58 ▶ return $HOST //= do {
Condition66%2/3
$HOST //= do { my $host = eval { do { Sys::Hostname::hostname() } } // ""; $host =~ s/[^A-Za-z0-9.-]+/_/g; length $host ? $host : "localhost" }
l!l&&r!l&&!r
5710
MC/DC0%0/2
$HOST //= do { my $host = eval { do { Sys::Hostname::hostname() } } // ""; $host =~ s/[^A-Za-z0-9.-]+/_/g; length $host ? $host : "localhost" }
$HOST do { my $host = eval { do { Sys::Hostname::hostname() } } // ""; $host =~ s/[^A-Za-z0-9.-]+/_/g; length $host ? $host : "localhost" }
Truth table66%2/3
A //= B
A= $HOSTB= do { my $host = eval { do { Sys::Hostname::hostname() } } // ""; $host =~ s/[^A-Za-z0-9.-]+/_/g; length $host ? $host : "localhost" }
ABresult
1X1
011
000
554 1 ▶ my $host = eval { Sys::Hostname::hostname() } // '';
Condition50%1/2
eval { do { Sys::Hostname::hostname() } } // ""
l!l
10
MC/DC0%0/1
eval { do { Sys::Hostname::hostname() } } // ""
eval { do { Sys::Hostname::hostname() } }
Truth table50%1/2
A
A= eval { do { Sys::Hostname::hostname() } }
Aresult
11
00
555 1 $host =~ s/[^A-Za-z0-9.-]+/_/g;
556 1 ▶ length($host) ? $host : 'localhost';
Branch: 1 branch
BranchTrueFalseTotal
length $host ? : 1 0 1
557 };
558 }
559  
560 56 ▶sub _spool_files {
Subroutine _spool_files: called CC=5 SCAR=16.1
561 56 my ($self, $kind) = @_;
562 56 ▶ opendir(my $dh, $self->spool_dir) or return;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $self->spool_dir or return 46 10 56
563 my @names = sort grep {
564 46 ▶ $kind eq 'writer' ? $_ =~ $SPOOL_FILE
Branch: 2 branches
BranchTrueFalseTotal
$kind eq 'writer' ? : 47 83 130
$kind eq 'claimed' ? : 47 36 83
Condition100%3/3
$_ =~ /$SPOOL_FILE/ or $_ =~ /$CLAIMED_FILE/
l!l&&r!l&&!r
1332
MC/DC100%2/2
$_ =~ /$SPOOL_FILE/ or $_ =~ /$CLAIMED_FILE/
$_ =~ /$SPOOL_FILE/ $_ =~ /$CLAIMED_FILE/
Truth table100%3/3
A or B
A= $_ =~ /$SPOOL_FILE/B= $_ =~ /$CLAIMED_FILE/
ABresult
1X1
011
000
565 : $kind eq 'claimed' ? $_ =~ $CLAIMED_FILE
566 : ($_ =~ $SPOOL_FILE || $_ =~ $CLAIMED_FILE)
567 } readdir $dh;
568 46 closedir $dh;
569 46 return @names;
570 }
571  
572 # True when the open $fh is still the file at $path: a replay may have renamed or removed it
573 # between the open and the lock.
574 75 ▶sub _same_file {
Subroutine _same_file: called CC=4 SCAR=15.6
575 75 my ($fh, $path) = @_;
576 75 ▶ my @open = stat $fh or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my(@open) = stat $fh or return 0 75 0 75
577 75 ▶ my @named = stat $path or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my(@named) = stat $path or return 0 75 0 75
578 75 ▶ return $open[0] == $named[0] && $open[1] == $named[1];
Condition33%1/3
$open[0] == $named[0] && $open[1] == $named[1]
!ll&&!rl&&r
0075
MC/DC0%0/2
$open[0] == $named[0] && $open[1] == $named[1]
$open[0] == $named[0] $open[1] == $named[1]
Truth table33%1/3
A && B
A= $open[0] == $named[0]B= $open[1] == $named[1]
ABresult
0X0
100
111
579 }
580  
581 # Appends $events to this process's spool file. Answers undef, or the error -- never dies.
582 45 ▶sub _spool_events {
Subroutine _spool_events: called CC=3 SCAR=11.6
583 45 my ($self, $events) = @_;
584 45 my $ok = eval { $self->_append_spool($events); 1 };
585 45 ▶ return $@ || 'unknown spool error' unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 45 45
Condition0%0/2
$@ || 'unknown spool error'
l!l
00
MC/DC0%0/1
$@ || 'unknown spool error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
586 45 $self->_spool_pending(1);
587 45 return undef;
588 }
589  
590 45 ▶sub _append_spool {
Subroutine _append_spool: called CC=12 SCAR=26.6
591 45 my ($self, $events) = @_;
592 45 my $dir = $self->spool_dir;
593 45 ▶ unless (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 7 38 45
594 7 make_path($dir, { error => \my $mkerr });
595 7 ▶ die "Cannot create spool directory $dir\n" unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 7 7
596 }
597 # Only the columns: whatever else an event hash carries never reaches the disk.
598 my $lines = join '', map {
599 45 my $event = $_;
600 47 ▶ encode_json({ map { exists $event->{$_} ? ($_ => $event->{$_}) : () } @EVENT_COLUMNS }) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
exists $event->{$_} ? : 752 705 1457
601 } @$events;
602 45 my $path = File::Spec->catfile($dir, 'usage-spool-' . _host() . "-$$.jsonl");
603  
604 45 for (1 .. 5) {
605 45 ▶ open my $fh, '>>', $path or die "Cannot append $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", $path or die "Cannot append ${path}: $!\n" 45 0 45
606 45 binmode $fh;
607 45 ▶ flock($fh, LOCK_EX) or die "Cannot lock $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 2 or die "Cannot lock ${path}: $!\n" 45 0 45
608 45 ▶ unless (_same_file($fh, $path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_same_file($fh, $path)) 0 45 45
609 0 close $fh; # claimed by a replay meanwhile: append to a new file
610 0 next;
611 }
612 45 ▶ print {$fh} $lines or die "Cannot append $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} $lines or die "Cannot append ${path}: $!\n" 45 0 45
613 45 ▶ $fh->flush or die "Cannot flush $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$fh->flush or die "Cannot flush ${path}: $!\n" 45 0 45
614 45 ▶ $fh->sync or die "Cannot sync $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$fh->sync or die "Cannot sync ${path}: $!\n" 45 0 45
615 45 ▶ close $fh or die "Cannot close $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Cannot close ${path}: $!\n" 45 0 45
616 45 return 1;
617 }
618 0 die "Cannot append $path: claimed by a replay on every attempt\n";
619 }
620  
621  
622 15 ▶sub replay_spool {
Subroutine replay_spool: called CC=10 SCAR=23.3
Pod documented
623 15 my ($self) = @_;
624 15 my %res = (ok => 1, replayed => 0, consumed => 0, remaining => 0);
625 15 ▶ return \%res unless length $self->spool_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->spool_dir 0 15 15
626 15 my $dir = $self->spool_dir;
627  
628 # Claim: rename every writer file this process can lock right now.
629 15 for my $name ($self->_spool_files('writer')) {
630 13 my $path = File::Spec->catfile($dir, $name);
631 13 ▶ open my $fh, '<', $path or next;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or next 13 0 13
632 13 ▶ flock($fh, LOCK_EX | LOCK_NB) or next;
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 6 or next 13 0 13
633 13 ▶ rename($path, "$path.replay-" . _host() . "-$$-" . ++$CLAIM_SEQ) if _same_file($fh, $path);
Branch: 1 branch
BranchTrueFalseTotal
if _same_file($fh, $path) 13 0 13
634 13 close $fh;
635 }
636  
637 15 my $budget = $REPLAY_BATCH;
638 15 for my $name ($self->_spool_files('claimed')) {
639 17 ▶ last if $budget <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $budget <= 0 0 17 17
640 17 my $one = $self->_replay_file(File::Spec->catfile($dir, $name), $budget);
641 17 $res{replayed} += $one->{replayed};
642 17 $res{consumed} += $one->{consumed};
643 17 $budget -= $one->{consumed};
644 17 ▶ next if $one->{ok};
Branch: 1 branch
BranchTrueFalseTotal
if $one->{"ok"} 16 1 17
645 1 @res{qw(ok error)} = (0, $one->{error});
646 1 last;
647 }
648  
649 15 ▶ $res{remaining} = $self->_spool_files('any') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_spool_files('any') ? : 3 12 15
650 15 $self->_spool_pending($res{remaining});
651 15 return \%res;
652 }
653  
654 17 ▶sub _replay_file {
Subroutine _replay_file: called CC=15 SCAR=28.8
655 17 my ($self, $path, $budget) = @_;
656 17 my %none = (ok => 1, replayed => 0, consumed => 0);
657 17 ▶ open my $fh, '<', $path or return \%none; # finished by another process
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or return \%none 17 0 17
658 17 ▶ flock($fh, LOCK_EX | LOCK_NB) or return \%none; # being replayed by another process
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 6 or return \%none 17 0 17
659 17 ▶ return \%none unless _same_file($fh, $path);
Branch: 1 branch
BranchTrueFalseTotal
unless _same_file($fh, $path) 0 17 17
660 17 binmode $fh;
661 17 my @lines = <$fh>;
662 17 my @take = splice @lines, 0, $budget;
663  
664 17 my @events;
665 17 for my $i (0 .. $#take) {
666 55 ▶ next unless $take[$i] =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $take[$i] =~ /\S/ 0 55 55
667 55 my $event = eval { decode_json($take[$i]) };
668 55 ▶ if (ref($event) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $event eq "HASH") 54 1 55
669 54 push @events, $event;
670 54 next;
671 }
672 # A torn last line (the process died mid-write, the disk filled up): nothing to replay.
673 1 warn "skeid: unreadable line dropped from usage spool $path\n";
674 }
675  
676 17 my $written = $self->_replay_events(\@events);
677 return { ok => 0, replayed => 0, consumed => 0, error => $written->{error} }
678 17 ▶ unless $written->{ok};
Branch: 1 branch
BranchTrueFalseTotal
unless $written->{"ok"} 1 16 17
679  
680 # Committed: only now do the lines leave the spool. Should this fail, they stay and the next
681 # replay skips them as already present.
682 16 ▶ if (@lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) { } 2 14 16
683 2 my ($vol, $dir) = File::Spec->splitpath($path);
684 2 my $tmp = File::Spec->catpath($vol, $dir, ".tmp-$$-" . ++$CLAIM_SEQ);
685 2 ▶ if (open my $out, '>', $tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $out, ">", $tmp) 2 0 2
686 2 binmode $out;
687 2 ▶ if ((print {$out} @lines) && $out->flush && $out->sync && close($out)) {
Branch: 1 branch
BranchTrueFalseTotal
if (print {$out;} @lines and $out->flush and $out->sync and close $out) { } 2 0 2
Condition33%3/9
print {$out;} @lines and $out->flush and $out->sync and close $out
!ll&&!rl&&r
002
print {$out;} @lines and $out->flush and $out->sync
!ll&&!rl&&r
002
print {$out;} @lines and $out->flush
!ll&&!rl&&r
002
MC/DC0%0/4
print {$out;} @lines and $out->flush and $out->sync and close $out
print {$out;} @lines $out->flush $out->sync close $out
Truth table20%1/5
A and B and C and D
A= print {$out;} @linesB= $out->flushC= $out->syncD= close $out
ABCDresult
0XXX0
10XX0
110X0
11100
11111
688 2 ▶ rename($tmp, $path) or unlink $tmp;
Branch: 1 branch
BranchTrueFalseTotal
rename $tmp, $path or unlink $tmp 2 0 2
689 } else {
690 0 unlink $tmp;
691 }
692 }
693 } else {
694 14 unlink $path;
695 }
696 16 close $fh;
697 16 return { ok => 1, replayed => $written->{written}, consumed => scalar(@take) };
698 }
699  
700 # One transaction for the batch. A refusal on a live handle falls back to one event at a time,
701 # so a single event the database will never take is reported lost and does not hold up the
702 # rest; a database that went away again stops the replay with the spool untouched.
703 17 ▶sub _replay_events {
Subroutine _replay_events: called CC=7 SCAR=27.5
704 17 my ($self, $events) = @_;
705 17 ▶ return { ok => 1, written => 0 } unless @$events;
Branch: 1 branch
BranchTrueFalseTotal
unless @$events 0 17 17
706  
707 16 ▶ my $batch = $self->_with_handle(sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
708 16 my ($dbh) = @_;
709 16 $dbh->begin_work;
710 16 my $written = eval {
711 16 my $n = 0;
712 16 $n += $self->_insert_once($dbh, $_) for @$events;
713 16 $dbh->commit;
714 16 $n;
715 };
716 16 ▶ unless (defined $written) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $written) 0 16 16
717 0 my $err = $@;
718 0 eval { $dbh->rollback };
719 0 die $err;
720 }
721 16 return { ok => 1, written => $written };
722 17 });
723 17 ▶ return $batch if $batch->{ok} || !$self->_dbh;
Branch: 1 branch
BranchTrueFalseTotal
if $batch->{"ok"} or not $self->_dbh 17 0 17
Condition66%2/3
$batch->{"ok"} or not $self->_dbh
l!l&&r!l&&!r
1610
MC/DC0%0/2
$batch->{"ok"} or not $self->_dbh
$batch->{"ok"} not $self->_dbh
Truth table66%2/3
A or not(B)
A= $batch->{"ok"}B= not $self->_dbh
ABresult
1X1
011
000
724  
725 0 my $written = 0;
726 0 for my $event (@$events) {
727 0 ▶ my $one = $self->_with_handle(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
728 0 my ($dbh) = @_;
729 0 return { ok => 1, written => $self->_insert_once($dbh, $event) };
730 0 });
731 0 ▶ if ($one->{ok}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($one->{"ok"}) 0 0 0
732 0 $written += $one->{written};
733 0 next;
734 }
735 0 ▶ return $one unless $self->_dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_dbh 0 0 0
736 0 $self->_report_lost($event, $one->{error});
737 }
738 0 return { ok => 1, written => $written };
739 }
740  
741 # After a write that succeeded: replay one batch of the spool. Inside the loop on the next tick,
742 # batch after batch while there is progress; outside it -- prepare at start, a script -- one
743 # batch at once, the rest with the next write.
744 113 ▶sub _schedule_replay {
Subroutine _schedule_replay: called CC=5 SCAR=25.7
745 113 my ($self) = @_;
746 113 ▶ return unless $self->_spool_pending && length $self->spool_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_spool_pending and length $self->spool_dir 107 6 113
Condition66%2/3
$self->_spool_pending and length $self->spool_dir
!ll&&!rl&&r
10706
MC/DC50%1/2
$self->_spool_pending and length $self->spool_dir
$self->_spool_pending length $self->spool_dir
Truth table66%2/3
A and B
A= $self->_spool_pendingB= length $self->spool_dir
ABresult
0X0
100
111
747 6 ▶ unless (Mojo::IOLoop->is_running) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Mojo::IOLoop"->is_running) 6 0 6
748 6 $self->replay_spool;
749 6 return;
750 }
751 0 ▶ return if defined $self->_replay_timer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_replay_timer 0 0 0
752 0 weaken(my $weak = $self);
753 0 ▶ $self->_replay_timer(Mojo::IOLoop->timer(0 => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
754 0 ▶ return unless $weak;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak 0 0 0
755 0 $weak->_replay_timer(undef);
756 0 my $res = $weak->replay_spool;
757 0 ▶ $weak->_schedule_replay if $res->{ok} && $res->{consumed} && $res->{remaining};
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"ok"} and $res->{"consumed"} and $res->{"remaining"} 0 0 0
Condition0%0/6
$res->{"ok"} and $res->{"consumed"} and $res->{"remaining"}
!ll&&!rl&&r
000
$res->{"ok"} and $res->{"consumed"}
!ll&&!rl&&r
000
MC/DC0%0/3
$res->{"ok"} and $res->{"consumed"} and $res->{"remaining"}
$res->{"ok"} $res->{"consumed"} $res->{"remaining"}
Truth table0%0/4
A and B and C
A= $res->{"ok"}B= $res->{"consumed"}C= $res->{"remaining"}
ABCresult
0XX0
10X0
1100
1111
758 0 }));
759 0 return;
760 }
761  
762  
763 17 ▶sub report {
Subroutine report: called CC=3 SCAR=11.0
Pod documented
764 17 my ($self, $filters) = @_;
765 17 ▶ $filters ||= {};
Condition50%1/2
$filters ||= {}
l!l
170
MC/DC0%0/1
$filters ||= {}
$filters
Truth table50%1/2
A
A= $filters
Aresult
11
00
766 17 $self->flush;
767  
768 17 ▶ my $report = $self->_with_handle(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
769 17 my ($dbh) = @_;
770 17 return $self->_report($dbh, $filters);
771 17 });
772 17 ▶ return $report->{ok} ? $report : { %$report, enabled => 0 };
Branch: 1 branch
BranchTrueFalseTotal
$report->{'ok'} ? : 16 1 17
773 }
774  
775 17 ▶sub _report {
Subroutine _report: called CC=27 SCAR=41.7
776 17 my ($self, $dbh, $filters) = @_;
777 17 my $num = \&Langertha::Skeid::UsageStore::num;
778  
779 17 ▶ my $limit = $filters->{limit} // 20;
Condition100%2/2
$filters->{"limit"} // 20
l!l
143
MC/DC100%1/1
$filters->{"limit"} // 20
$filters->{"limit"}
Truth table100%2/2
A
A= $filters->{"limit"}
Aresult
11
00
780  
781 17 my @where;
782 my @bind;
783 17 ▶ if (defined $filters->{since} && length $filters->{since}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filters->{"since"} and length $filters->{"since"}) 0 17 17
Condition33%1/3
defined $filters->{"since"} and length $filters->{"since"}
!ll&&!rl&&r
1700
MC/DC0%0/2
defined $filters->{"since"} and length $filters->{"since"}
defined $filters->{"since"} length $filters->{"since"}
Truth table33%1/3
A and B
A= defined $filters->{"since"}B= length $filters->{"since"}
ABresult
0X0
100
111
784 0 push @where, 'created_at >= ?';
785 0 push @bind, $filters->{since};
786 }
787 17 ▶ if (defined $filters->{api_key_id} && length $filters->{api_key_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filters->{"api_key_id"} and length $filters->{"api_key_id"}) 0 17 17
Condition33%1/3
defined $filters->{"api_key_id"} and length $filters->{"api_key_id"}
!ll&&!rl&&r
1700
MC/DC0%0/2
defined $filters->{"api_key_id"} and length $filters->{"api_key_id"}
defined $filters->{"api_key_id"} length $filters->{"api_key_id"}
Truth table33%1/3
A and B
A= defined $filters->{"api_key_id"}B= length $filters->{"api_key_id"}
ABresult
0X0
100
111
788 0 push @where, 'api_key_id = ?';
789 0 push @bind, $filters->{api_key_id};
790 }
791 17 ▶ if (defined $filters->{model} && length $filters->{model}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filters->{"model"} and length $filters->{"model"}) 0 17 17
Condition33%1/3
defined $filters->{"model"} and length $filters->{"model"}
!ll&&!rl&&r
1700
MC/DC0%0/2
defined $filters->{"model"} and length $filters->{"model"}
defined $filters->{"model"} length $filters->{"model"}
Truth table33%1/3
A and B
A= defined $filters->{"model"}B= length $filters->{"model"}
ABresult
0X0
100
111
792 0 push @where, 'model = ?';
793 0 push @bind, $filters->{model};
794 }
795  
796 17 ▶ my $where_sql = @where ? ('WHERE ' . join(' AND ', @where)) : '';
Branch: 1 branch
BranchTrueFalseTotal
@where ? : 0 17 17
797  
798 17 ▶ my $totals = $dbh->selectrow_hashref(
Condition50%1/2
$dbh->selectrow_hashref("SELECT\n COUNT(*) AS requests,\n COALESCE(SUM(input_tokens), 0) AS input_tokens,\n COALESCE(SUM(output_tokens), 0) AS output_tokens,\n COALESCE(SUM(total_tokens), 0) AS total_tokens,\n COALESCE(SUM(cached_tokens), 0) AS cached_tokens,\n COALESCE(SUM(cache_write_tokens), 0) AS cache_write_tokens,\n COALESCE(SUM(tool_calls), 0) AS tool_calls,\n COALESCE(SUM(audio_seconds), 0) AS audio_seconds,\n COALESCE(SUM(documents), 0) AS documents,\n COALESCE(SUM(pages), 0) AS pages,\n COALESCE(SUM(cost_total_usd), 0) AS total_cost_usd\n FROM usage_events $where_sql", undef, @bind) || {}
l!l
160
MC/DC0%0/1
$dbh->selectrow_hashref("SELECT\n COUNT(*) AS requests,\n COALESCE(SUM(input_tokens), 0) AS input_tokens,\n COALESCE(SUM(output_tokens), 0) AS output_tokens,\n COALESCE(SUM(total_tokens), 0) AS total_tokens,\n COALESCE(SUM(cached_tokens), 0) AS cached_tokens,\n COALESCE(SUM(cache_write_tokens), 0) AS cache_write_tokens,\n COALESCE(SUM(tool_calls), 0) AS tool_calls,\n COALESCE(SUM(audio_seconds), 0) AS audio_seconds,\n COALESCE(SUM(documents), 0) AS documents,\n COALESCE(SUM(pages), 0) AS pages,\n COALESCE(SUM(cost_total_usd), 0) AS total_cost_usd\n FROM usage_events $where_sql", undef, @bind) || {}
$dbh->selectrow_hashref("SELECT\n COUNT(*) AS requests,\n COALESCE(SUM(input_tokens), 0) AS input_tokens,\n COALESCE(SUM(output_tokens), 0) AS output_tokens,\n COALESCE(SUM(total_tokens), 0) AS total_tokens,\n COALESCE(SUM(cached_tokens), 0) AS cached_tokens,\n COALESCE(SUM(cache_write_tokens), 0) AS cache_write_tokens,\n COALESCE(SUM(tool_calls), 0) AS tool_calls,\n COALESCE(SUM(audio_seconds), 0) AS audio_seconds,\n COALESCE(SUM(documents), 0) AS documents,\n COALESCE(SUM(pages), 0) AS pages,\n COALESCE(SUM(cost_total_usd), 0) AS total_cost_usd\n FROM usage_events $where_sql", undef, @bind)
Truth table50%1/2
A
A= $dbh->selectrow_hashref("SELECT\n COUNT(*) AS requests,\n COALESCE(SUM(input_tokens), 0) AS input_tokens,\n COALESCE(SUM(output_tokens), 0) AS output_tokens,\n COALESCE(SUM(total_tokens), 0) AS total_tokens,\n COALESCE(SUM(cached_tokens), 0) AS cached_tokens,\n COALESCE(SUM(cache_write_tokens), 0) AS cache_write_tokens,\n COALESCE(SUM(tool_calls), 0) AS tool_calls,\n COALESCE(SUM(audio_seconds), 0) AS audio_seconds,\n COALESCE(SUM(documents), 0) AS documents,\n COALESCE(SUM(pages), 0) AS pages,\n COALESCE(SUM(cost_total_usd), 0) AS total_cost_usd\n FROM usage_events $where_sql", undef, @bind)
Aresult
11
00
799 "SELECT
800 COUNT(*) AS requests,
801 COALESCE(SUM(input_tokens), 0) AS input_tokens,
802 COALESCE(SUM(output_tokens), 0) AS output_tokens,
803 COALESCE(SUM(total_tokens), 0) AS total_tokens,
804 COALESCE(SUM(cached_tokens), 0) AS cached_tokens,
805 COALESCE(SUM(cache_write_tokens), 0) AS cache_write_tokens,
806 COALESCE(SUM(tool_calls), 0) AS tool_calls,
807 COALESCE(SUM(audio_seconds), 0) AS audio_seconds,
808 COALESCE(SUM(documents), 0) AS documents,
809 COALESCE(SUM(pages), 0) AS pages,
810 COALESCE(SUM(cost_total_usd), 0) AS total_cost_usd
811 FROM usage_events $where_sql",
812 undef,
813 @bind,
814 ) || {};
815  
816 16 ▶ my $by_key = $dbh->selectall_arrayref(
Condition50%1/2
$dbh->selectall_arrayref("SELECT\n COALESCE(api_key_id, '') AS api_key_id,\n COUNT(*) AS requests,\n COALESCE(SUM(total_tokens), 0) AS total_tokens,\n COALESCE(SUM(audio_seconds), 0) AS audio_seconds,\n COALESCE(SUM(documents), 0) AS documents,\n COALESCE(SUM(pages), 0) AS pages,\n COALESCE(SUM(cost_total_usd), 0) AS total_cost_usd\n FROM usage_events\n $where_sql\n GROUP BY api_key_id\n ORDER BY total_cost_usd DESC, requests DESC, api_key_id ASC", {'Slice', {}}, @bind) || []
l!l
160
MC/DC0%0/1
$dbh->selectall_arrayref("SELECT\n COALESCE(api_key_id, '') AS api_key_id,\n COUNT(*) AS requests,\n COALESCE(SUM(total_tokens), 0) AS total_tokens,\n COALESCE(SUM(audio_seconds), 0) AS audio_seconds,\n COALESCE(SUM(documents), 0) AS documents,\n COALESCE(SUM(pages), 0) AS pages,\n COALESCE(SUM(cost_total_usd), 0) AS total_cost_usd\n FROM usage_events\n $where_sql\n GROUP BY api_key_id\n ORDER BY total_cost_usd DESC, requests DESC, api_key_id ASC", {'Slice', {}}, @bind) || []
$dbh->selectall_arrayref("SELECT\n COALESCE(api_key_id, '') AS api_key_id,\n COUNT(*) AS requests,\n COALESCE(SUM(total_tokens), 0) AS total_tokens,\n COALESCE(SUM(audio_seconds), 0) AS audio_seconds,\n COALESCE(SUM(documents), 0) AS documents,\n COALESCE(SUM(pages), 0) AS pages,\n COALESCE(SUM(cost_total_usd), 0) AS total_cost_usd\n FROM usage_events\n $where_sql\n GROUP BY api_key_id\n ORDER BY total_cost_usd DESC, requests DESC, api_key_id ASC", {'Slice', {}}, @bind)
Truth table50%1/2
A
A= $dbh->selectall_arrayref("SELECT\n COALESCE(api_key_id, '') AS api_key_id,\n COUNT(*) AS requests,\n COALESCE(SUM(total_tokens), 0) AS total_tokens,\n COALESCE(SUM(audio_seconds), 0) AS audio_seconds,\n COALESCE(SUM(documents), 0) AS documents,\n COALESCE(SUM(pages), 0) AS pages,\n COALESCE(SUM(cost_total_usd), 0) AS total_cost_usd\n FROM usage_events\n $where_sql\n GROUP BY api_key_id\n ORDER BY total_cost_usd DESC, requests DESC, api_key_id ASC", {'Slice', {}}, @bind)
Aresult
11
00
817 "SELECT
818 COALESCE(api_key_id, '') AS api_key_id,
819 COUNT(*) AS requests,
820 COALESCE(SUM(total_tokens), 0) AS total_tokens,
821 COALESCE(SUM(audio_seconds), 0) AS audio_seconds,
822 COALESCE(SUM(documents), 0) AS documents,
823 COALESCE(SUM(pages), 0) AS pages,
824 COALESCE(SUM(cost_total_usd), 0) AS total_cost_usd
825 FROM usage_events
826 $where_sql
827 GROUP BY api_key_id
828 ORDER BY total_cost_usd DESC, requests DESC, api_key_id ASC",
829 { Slice => {} },
830 @bind,
831 ) || [];
832  
833 16 ▶ my $by_model = $dbh->selectall_arrayref(
Condition50%1/2
$dbh->selectall_arrayref("SELECT\n COALESCE(model, '') AS model,\n COUNT(*) AS requests,\n COALESCE(SUM(total_tokens), 0) AS total_tokens,\n COALESCE(SUM(audio_seconds), 0) AS audio_seconds,\n COALESCE(SUM(documents), 0) AS documents,\n COALESCE(SUM(pages), 0) AS pages,\n COALESCE(SUM(cost_total_usd), 0) AS total_cost_usd\n FROM usage_events\n $where_sql\n GROUP BY model\n ORDER BY total_cost_usd DESC, requests DESC, model ASC", {'Slice', {}}, @bind) || []
l!l
160
MC/DC0%0/1
$dbh->selectall_arrayref("SELECT\n COALESCE(model, '') AS model,\n COUNT(*) AS requests,\n COALESCE(SUM(total_tokens), 0) AS total_tokens,\n COALESCE(SUM(audio_seconds), 0) AS audio_seconds,\n COALESCE(SUM(documents), 0) AS documents,\n COALESCE(SUM(pages), 0) AS pages,\n COALESCE(SUM(cost_total_usd), 0) AS total_cost_usd\n FROM usage_events\n $where_sql\n GROUP BY model\n ORDER BY total_cost_usd DESC, requests DESC, model ASC", {'Slice', {}}, @bind) || []
$dbh->selectall_arrayref("SELECT\n COALESCE(model, '') AS model,\n COUNT(*) AS requests,\n COALESCE(SUM(total_tokens), 0) AS total_tokens,\n COALESCE(SUM(audio_seconds), 0) AS audio_seconds,\n COALESCE(SUM(documents), 0) AS documents,\n COALESCE(SUM(pages), 0) AS pages,\n COALESCE(SUM(cost_total_usd), 0) AS total_cost_usd\n FROM usage_events\n $where_sql\n GROUP BY model\n ORDER BY total_cost_usd DESC, requests DESC, model ASC", {'Slice', {}}, @bind)
Truth table50%1/2
A
A= $dbh->selectall_arrayref("SELECT\n COALESCE(model, '') AS model,\n COUNT(*) AS requests,\n COALESCE(SUM(total_tokens), 0) AS total_tokens,\n COALESCE(SUM(audio_seconds), 0) AS audio_seconds,\n COALESCE(SUM(documents), 0) AS documents,\n COALESCE(SUM(pages), 0) AS pages,\n COALESCE(SUM(cost_total_usd), 0) AS total_cost_usd\n FROM usage_events\n $where_sql\n GROUP BY model\n ORDER BY total_cost_usd DESC, requests DESC, model ASC", {'Slice', {}}, @bind)
Aresult
11
00
834 "SELECT
835 COALESCE(model, '') AS model,
836 COUNT(*) AS requests,
837 COALESCE(SUM(total_tokens), 0) AS total_tokens,
838 COALESCE(SUM(audio_seconds), 0) AS audio_seconds,
839 COALESCE(SUM(documents), 0) AS documents,
840 COALESCE(SUM(pages), 0) AS pages,
841 COALESCE(SUM(cost_total_usd), 0) AS total_cost_usd
842 FROM usage_events
843 $where_sql
844 GROUP BY model
845 ORDER BY total_cost_usd DESC, requests DESC, model ASC",
846 { Slice => {} },
847 @bind,
848 ) || [];
849  
850 16 ▶ my $recent = $dbh->selectall_arrayref(
Condition50%1/2
$dbh->selectall_arrayref("SELECT\n id, created_at, api_format, endpoint, api_key_id, model, requested_model, node_id, status_code, ok,\n input_tokens, output_tokens, total_tokens, cached_tokens, cache_write_tokens, tool_calls, cost_total_usd\n FROM usage_events\n $where_sql\n ORDER BY id DESC\n LIMIT ?", {'Slice', {}}, @bind, $limit) || []
l!l
160
MC/DC0%0/1
$dbh->selectall_arrayref("SELECT\n id, created_at, api_format, endpoint, api_key_id, model, requested_model, node_id, status_code, ok,\n input_tokens, output_tokens, total_tokens, cached_tokens, cache_write_tokens, tool_calls, cost_total_usd\n FROM usage_events\n $where_sql\n ORDER BY id DESC\n LIMIT ?", {'Slice', {}}, @bind, $limit) || []
$dbh->selectall_arrayref("SELECT\n id, created_at, api_format, endpoint, api_key_id, model, requested_model, node_id, status_code, ok,\n input_tokens, output_tokens, total_tokens, cached_tokens, cache_write_tokens, tool_calls, cost_total_usd\n FROM usage_events\n $where_sql\n ORDER BY id DESC\n LIMIT ?", {'Slice', {}}, @bind, $limit)
Truth table50%1/2
A
A= $dbh->selectall_arrayref("SELECT\n id, created_at, api_format, endpoint, api_key_id, model, requested_model, node_id, status_code, ok,\n input_tokens, output_tokens, total_tokens, cached_tokens, cache_write_tokens, tool_calls, cost_total_usd\n FROM usage_events\n $where_sql\n ORDER BY id DESC\n LIMIT ?", {'Slice', {}}, @bind, $limit)
Aresult
11
00
851 "SELECT
852 id, created_at, api_format, endpoint, api_key_id, model, requested_model, node_id, status_code, ok,
853 input_tokens, output_tokens, total_tokens, cached_tokens, cache_write_tokens, tool_calls, cost_total_usd
854 FROM usage_events
855 $where_sql
856 ORDER BY id DESC
857 LIMIT ?",
858 { Slice => {} },
859 @bind,
860 $limit,
861 ) || [];
862  
863 return {
864 ok => 1,
865 enabled => 1,
866 backend => $self->backend,
867 db_path => ($self->backend eq 'sqlite' ? ($self->path // '') : ''),
868 since => ($filters->{since} // ''),
869 totals => {
870 requests => $num->($totals->{requests}),
871 input_tokens => $num->($totals->{input_tokens}),
872 output_tokens => $num->($totals->{output_tokens}),
873 total_tokens => $num->($totals->{total_tokens}),
874 cached_tokens => $num->($totals->{cached_tokens}),
875 cache_write_tokens => $num->($totals->{cache_write_tokens}),
876 tool_calls => $num->($totals->{tool_calls}),
877 audio_seconds => $num->($totals->{audio_seconds}),
878 documents => $num->($totals->{documents}),
879 pages => $num->($totals->{pages}),
880 total_cost_usd => $num->($totals->{total_cost_usd}),
881 },
882 by_key => [ map {
883 +{
884 api_key_id => ($_->{api_key_id} // ''),
885 requests => $num->($_->{requests}),
886 total_tokens => $num->($_->{total_tokens}),
887 audio_seconds => $num->($_->{audio_seconds}),
888 documents => $num->($_->{documents}),
889 pages => $num->($_->{pages}),
890 45 ▶ total_cost_usd => $num->($_->{total_cost_usd}),
Condition50%1/2
$_->{"api_key_id"} // ""
l!l
450
MC/DC0%0/1
$_->{"api_key_id"} // ""
$_->{"api_key_id"}
Truth table50%1/2
A
A= $_->{"api_key_id"}
Aresult
11
00
891 }
892 } @$by_key ],
893 by_model => [ map {
894 +{
895 model => ($_->{model} // ''),
896 requests => $num->($_->{requests}),
897 total_tokens => $num->($_->{total_tokens}),
898 audio_seconds => $num->($_->{audio_seconds}),
899 documents => $num->($_->{documents}),
900 pages => $num->($_->{pages}),
901 45 ▶ total_cost_usd => $num->($_->{total_cost_usd}),
Condition50%1/2
$_->{"model"} // ""
l!l
450
MC/DC0%0/1
$_->{"model"} // ""
$_->{"model"}
Truth table50%1/2
A
A= $_->{"model"}
Aresult
11
00
902 }
903 } @$by_model ],
904 recent => [ map {
905 16 ▶ +{
Branch: 1 branch
BranchTrueFalseTotal
$self->backend eq 'sqlite' ? : 16 0 16
Condition50%2/4
$self->path // ""
l!l
160
$filters->{"since"} // ""
l!l
016
MC/DC0%0/1
$self->path // ""
$self->path
MC/DC0%0/1
$filters->{"since"} // ""
$filters->{"since"}
Truth table50%1/2
A
A= $self->path
Aresult
11
00
Truth table50%1/2
A
A= $filters->{"since"}
Aresult
11
00
906 id => $num->($_->{id}),
907 created_at => ($_->{created_at} // ''),
908 api_format => ($_->{api_format} // ''),
909 endpoint => ($_->{endpoint} // ''),
910 api_key_id => ($_->{api_key_id} // ''),
911 model => ($_->{model} // ''),
912 requested_model => ($_->{requested_model} // $_->{model} // ''),
913 node_id => ($_->{node_id} // ''),
914 status_code => $num->($_->{status_code}),
915 ok => ($_->{ok} ? 1 : 0),
916 input_tokens => $num->($_->{input_tokens}),
917 output_tokens => $num->($_->{output_tokens}),
918 total_tokens => $num->($_->{total_tokens}),
919 cached_tokens => $num->($_->{cached_tokens}),
920 cache_write_tokens => $num->($_->{cache_write_tokens}),
921 tool_calls => $num->($_->{tool_calls}),
922 52 ▶ cost_total_usd => $num->($_->{cost_total_usd}),
Branch: 1 branch
BranchTrueFalseTotal
$_->{'ok'} ? : 50 2 52
Condition76%13/17
$_->{"created_at"} // ""
l!l
520
$_->{"api_format"} // ""
l!l
475
$_->{"endpoint"} // ""
l!l
475
$_->{"api_key_id"} // ""
l!l
511
$_->{"model"} // ""
l!l
520
($_->{"requested_model"} // $_->{"model"}) // ""
l!l
520
$_->{"requested_model"} // $_->{"model"}
l!l&&r!l&&!r
4750
$_->{"node_id"} // ""
l!l
475
MC/DC0%0/1
$_->{"created_at"} // ""
$_->{"created_at"}
MC/DC100%1/1
$_->{"api_format"} // ""
$_->{"api_format"}
MC/DC100%1/1
$_->{"endpoint"} // ""
$_->{"endpoint"}
MC/DC100%1/1
$_->{"api_key_id"} // ""
$_->{"api_key_id"}
MC/DC0%0/1
$_->{"model"} // ""
$_->{"model"}
MC/DC0%0/2
($_->{"requested_model"} // $_->{"model"}) // ""
$_->{"requested_model"} $_->{"model"}
MC/DC100%1/1
$_->{"node_id"} // ""
$_->{"node_id"}
Truth table50%1/2
A
A= $_->{"created_at"}
Aresult
11
00
Truth table100%2/2
A
A= $_->{"api_format"}
Aresult
11
00
Truth table100%2/2
A
A= $_->{"endpoint"}
Aresult
11
00
Truth table100%2/2
A
A= $_->{"api_key_id"}
Aresult
11
00
Truth table50%1/2
A
A= $_->{"model"}
Aresult
11
00
Truth table66%2/3
A // B
A= $_->{"requested_model"}B= $_->{"model"}
ABresult
1X1
011
000
Truth table100%2/2
A
A= $_->{"node_id"}
Aresult
11
00
923 }
924 } @$recent ],
925 };
926 }
927  
928  
929 15 1;
930  
931 __END__
« lib/Langertha/Skeid/Registry.pm ↑ Summary lib/Langertha/Skeid/Proxy.pm »