lib/Langertha/Skeid/UsageStore/JsonLog.pm

filter: Statementstmt 93.2% 180 / 193 Branchbran 75.7% 53 / 70 Conditioncond 55.7% 44 / 79 MC/DCmcdc 23.8% 10 / 42 Subroutinesub 96.0% 24 / 25 Podpod 100% 5 / 5 totaltotal 76.3% 316 / 414 CC 72 SCAR 33.8
CC
72
Cov
81%
report
61.0
_new_process_nonce
10.9
_write_and_close
9.2
CRAP 29.4
« lib/Langertha/Skeid.pm ↑ Summary lib/Langertha/Skeid/OpenAPI.pm »
1 package Langertha::Skeid::UsageStore::JsonLog;
2 25 our $VERSION = '0.004';
3 # ABSTRACT: Append-only JSON usage store - one file per event, or one line per event
4 25 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 25 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 25 ▶use Fcntl qw(O_WRONLY O_CREAT O_EXCL LOCK_EX);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 25 ▶use Errno qw(EEXIST EINTR);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 25 ▶use Digest::SHA qw(sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 25 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 25 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 25 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 25 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 25 ▶use JSON::MaybeXS qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 25 ▶use Langertha::Skeid::UsageStore;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 25 has path => (is => 'ro', required => 1);
21 6 ▶has mode => (is => 'ro', default => sub { 'dir' });
Subroutine __ANON__: called CC=1 SCAR=0.0
22  
23  
24 5 ▶has fsync => (is => 'ro', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26  
27 0 ▶sub backend { 'jsonlog' }
Subroutine backend: not called CC=1 SCAR=6.9
Pod documented
28  
29  
30 38 ▶sub prepare {
Subroutine prepare: called CC=6 SCAR=19.0
Pod documented
31 38 my ($self) = @_;
32 38 my $path = $self->path;
33 38 ▶ if ($self->mode eq 'dir') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq 'dir') { } 34 4 38
34 34 ▶ make_path($path) unless -d $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path 17 17 34
35 } else {
36 4 my $dir = dirname($path);
37 4 ▶ make_path($dir) if length($dir) && $dir ne '.' && !-d $dir;
Branch: 1 branch
BranchTrueFalseTotal
if length $dir and $dir ne "." and not -d $dir 0 4 4
Condition33%2/6
length $dir and $dir ne "." and not -d $dir
!ll&&!rl&&r
040
length $dir and $dir ne "."
!ll&&!rl&&r
004
MC/DC0%0/3
length $dir and $dir ne "." and not -d $dir
length $dir $dir ne "." not -d $dir
Truth table25%1/4
A and B and not(C)
A= length $dirB= $dir ne "."C= not -d $dir
ABCresult
0XX0
10X0
1100
1111
38 }
39 38 return 1;
40 }
41  
42  
43 37 ▶sub disconnect { return }
Subroutine disconnect: called CC=1 SCAR=0.0
Pod documented
44  
45 25 our %EVENT_ID_STATE;
46 25 my $CREATE_ATTEMPTS = 16;
47 25 my $PROCESS_NONCE_BYTES = 16;
48  
49 13 ▶sub _new_process_nonce {
Subroutine _new_process_nonce: called CC=7 SCAR=23.8
50 13 my $random = '';
51 13 ▶ if (open my $fh, '<:raw', '/dev/urandom') {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<:raw", "/dev/urandom") 13 0 13
52 13 ▶ while (length($random) < $PROCESS_NONCE_BYTES) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $random < $PROCESS_NONCE_BYTES) 13 13 26
53 13 my $chunk = '';
54 13 my $read = sysread($fh, $chunk, $PROCESS_NONCE_BYTES - length($random));
55 13 ▶ if (!defined $read) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $read) 0 13 13
56 0 ▶ next if $! == EINTR;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 4 0 0 0
57 0 last;
58 }
59 13 ▶ last unless $read;
Branch: 1 branch
BranchTrueFalseTotal
unless $read 0 13 13
60 13 $random .= $chunk;
61 }
62 13 close $fh;
63 }
64 13 ▶ return unpack('H*', $random) if length($random) == $PROCESS_NONCE_BYTES;
Branch: 1 branch
BranchTrueFalseTotal
if length $random == $PROCESS_NONCE_BYTES 13 0 13
65  
66 # /dev/urandom is present on supported service platforms. Keep a portable
67 # best-effort fallback for other Perl targets without reseeding the process'
68 # global PRNG, which belongs to the application as a whole.
69 0 my $marker = {};
70 0 my @entropy = ($$, Time::HiRes::time(), "$marker", map { rand() } 1 .. 4);
71 0 return substr(sha256_hex(join("\0", @entropy)), 0, $PROCESS_NONCE_BYTES * 2);
72 }
73  
74 73 ▶sub _event_id {
Subroutine _event_id: called CC=3 SCAR=11.0
75 73 my $pid = $$;
76 73 ▶ if (!defined($EVENT_ID_STATE{pid}) || $EVENT_ID_STATE{pid} != $pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $Langertha::Skeid::UsageStore::JsonLog::EVENT_ID_STATE{"pid"} or $Langertha::Skeid::UsageStore::JsonLog::EVENT_ID_STATE{"pid"} != $pid) 18 55 73
Condition66%2/3
not defined $Langertha::Skeid::UsageStore::JsonLog::EVENT_ID_STATE{"pid"} or $Langertha::Skeid::UsageStore::JsonLog::EVENT_ID_STATE{"pid"} != $pid
l!l&&r!l&&!r
18055
MC/DC50%1/2
not defined $Langertha::Skeid::UsageStore::JsonLog::EVENT_ID_STATE{"pid"} or $Langertha::Skeid::UsageStore::JsonLog::EVENT_ID_STATE{"pid"} != $pid
not defined $Langertha::Skeid::UsageStore::JsonLog::EVENT_ID_STATE{"pid"} $Langertha::Skeid::UsageStore::JsonLog::EVENT_ID_STATE{"pid"} != $pid
Truth table66%2/3
not(A) or B
A= not defined $Langertha::Skeid::UsageStore::JsonLog::EVENT_ID_STATE{"pid"}B= $Langertha::Skeid::UsageStore::JsonLog::EVENT_ID_STATE{"pid"} != $pid
ABresult
1X1
011
000
77 18 %EVENT_ID_STATE = (
78 pid => $pid,
79 nonce => _new_process_nonce(),
80 sequence => 0,
81 );
82 }
83 73 my $sequence = ++$EVENT_ID_STATE{sequence};
84 73 my $ts = strftime('%Y%m%d-%H%M%S', gmtime());
85 73 return sprintf('%s-%d-%s-%010d', $ts, $pid, $EVENT_ID_STATE{nonce}, $sequence);
86 }
87  
88 3 ▶sub _io_error {
Subroutine _io_error: called CC=3 SCAR=12.5
89 3 my ($operation, $path, $reason) = @_;
90 3 ▶ $reason = 'unknown I/O error' unless defined($reason) && length($reason);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reason and length $reason 0 3 3
Condition33%1/3
defined $reason and length $reason
!ll&&!rl&&r
003
MC/DC0%0/2
defined $reason and length $reason
defined $reason length $reason
Truth table33%1/3
A and B
A= defined $reasonB= length $reason
ABresult
0X0
100
111
91 3 return "Cannot $operation $path: $reason";
92 }
93  
94 71 ▶sub _write_and_close {
Subroutine _write_and_close: called CC=9 SCAR=22.1
95 71 my ($self, $fh, $path, $json, $operation) = @_;
96 71 my $error;
97  
98 71 ▶ unless (print {$fh} $json, "\n") {
Branch: 1 branch
BranchTrueFalseTotal
unless (print {$fh;} $json, "\n") 1 70 71
99 1 $error = _io_error($operation, $path, "$!");
100 }
101 71 ▶ if (!$error && $self->fsync) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $error and $self->fsync) 5 66 71
Condition100%3/3
not $error and $self->fsync
!ll&&!rl&&r
1655
MC/DC100%2/2
not $error and $self->fsync
not $error $self->fsync
Truth table100%3/3
not(A) and B
A= not $errorB= $self->fsync
ABresult
0X0
100
111
102 5 ▶ unless ($fh->flush) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->flush) 0 5 5
103 0 $error = _io_error('flush', $path, "$!");
104 }
105 5 ▶ unless ($error || $fh->sync) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error or $fh->sync) 1 4 5
Condition66%2/3
$error or $fh->sync
l!l&&r!l&&!r
041
MC/DC50%1/2
$error or $fh->sync
$error $fh->sync
Truth table66%2/3
A or B
A= $errorB= $fh->sync
ABresult
1X1
011
000
106 1 $error = _io_error('sync', $path, "$!");
107 }
108 }
109 71 ▶ unless (close $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (close $fh) 2 69 71
110 2 ▶ $error ||= _io_error('close', $path, "$!");
Condition66%2/3
$error ||= _io_error("close", $path, "$!")
l!l&&r!l&&!r
110
MC/DC0%0/2
$error ||= _io_error("close", $path, "$!")
$error _io_error("close", $path, "$!")
Truth table66%2/3
A ||= B
A= $errorB= _io_error("close", $path, "$!")
ABresult
1X1
011
000
111 }
112  
113 71 return $error;
114 }
115  
116  
117 73 ▶sub store {
Subroutine store: called CC=9 SCAR=22.1
Pod documented
118 73 my ($self, $event) = @_;
119  
120 73 ▶ if ($self->mode eq 'dir') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "dir") 64 9 73
121 64 for (1 .. $CREATE_ATTEMPTS) {
122 80 my $id = _event_id();
123 80 my $json = encode_json({ %$event, id => $id });
124 80 my $file = File::Spec->catfile($self->path, "${id}.json");
125 80 my $fh;
126 80 ▶ unless (sysopen $fh, $file, O_WRONLY | O_CREAT | O_EXCL, 0666) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $fh, $file, 193, 438) 18 62 80
127 18 ▶ next if $! == EEXIST;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 17 17 1 18
128 1 return { ok => 0, error => "Cannot write $file: $!" };
129 }
130 62 my $error = _write_and_close($self, $fh, $file, $json, 'write');
131 62 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 2 60 62
132 2 unlink $file;
133 2 return { ok => 0, error => $error };
134 }
135 60 return { ok => 1, id => $id };
136 }
137 return {
138 1 ok => 0,
139 error => "Cannot store usage event: event id collisions exhausted $CREATE_ATTEMPTS attempts",
140 };
141 }
142  
143 9 my $id = _event_id();
144 9 my $json = encode_json({ %$event, id => $id });
145 9 my $path = $self->path;
146 9 ▶ open my $fh, '>>', $path or return { ok => 0, error => "Cannot append $path: $!" };
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", $path or return {"ok", 0, "error", "Cannot append ${path}: $!"} 9 0 9
147 9 ▶ unless (flock($fh, LOCK_EX)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $fh, 2) 0 9 9
148 0 my $error = _io_error('lock', $path, "$!");
149 0 close $fh;
150 0 return { ok => 0, error => $error };
151 }
152 9 my $error = _write_and_close($self, $fh, $path, $json, 'append');
153 9 ▶ return { ok => 0, error => $error } if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 1 8 9
154  
155 8 return { ok => 1, id => $id };
156 }
157  
158  
159 26 ▶sub report {
Subroutine report: called CC=41 SCAR=41.1
Pod documented
160 26 my ($self, $filters) = @_;
161 26 ▶ $filters ||= {};
Condition50%1/2
$filters ||= {}
l!l
260
MC/DC0%0/1
$filters ||= {}
$filters
Truth table50%1/2
A
A= $filters
Aresult
11
00
162 26 my $num = \&Langertha::Skeid::UsageStore::num;
163  
164 26 my @events;
165 26 ▶ if ($self->mode eq 'dir') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq 'dir') { } 24 2 26
166 24 my @files = sort glob(File::Spec->catfile($self->path, '*.json'));
167 24 for my $file (@files) {
168 82 my $text = eval { Langertha::Skeid::UsageStore::read_text_file($file) };
169 82 ▶ next unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 82 82
170 82 my $ev = eval { decode_json($text) };
171 82 ▶ push @events, $ev if ref($ev) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $ev eq "HASH" 82 0 82
172 }
173 } else {
174 2 ▶ if (open my $fh, '<', $self->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", $self->path) 2 0 2
175 2 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 4 2 6
176 4 chomp $line;
177 4 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 4 4
178 4 my $ev = eval { decode_json($line) };
179 4 ▶ push @events, $ev if ref($ev) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $ev eq "HASH" 4 0 4
180 }
181 2 close $fh;
182 }
183 }
184  
185 26 ▶ if (defined $filters->{since} && length $filters->{since}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filters->{"since"} and length $filters->{"since"}) 0 26 26
Condition33%1/3
defined $filters->{"since"} and length $filters->{"since"}
!ll&&!rl&&r
2600
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
186 0 ▶ @events = grep { ($_->{created_at} // '') ge $filters->{since} } @events;
Condition0%0/2
$_->{"created_at"} // ""
l!l
00
MC/DC0%0/1
$_->{"created_at"} // ""
$_->{"created_at"}
Truth table0%0/2
A
A= $_->{"created_at"}
Aresult
11
00
187 }
188 26 ▶ 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"}) 1 25 26
Condition66%2/3
defined $filters->{"api_key_id"} and length $filters->{"api_key_id"}
!ll&&!rl&&r
2501
MC/DC50%1/2
defined $filters->{"api_key_id"} and length $filters->{"api_key_id"}
defined $filters->{"api_key_id"} length $filters->{"api_key_id"}
Truth table66%2/3
A and B
A= defined $filters->{"api_key_id"}B= length $filters->{"api_key_id"}
ABresult
0X0
100
111
189 1 ▶ @events = grep { ($_->{api_key_id} // '') eq $filters->{api_key_id} } @events;
Condition50%1/2
$_->{"api_key_id"} // ""
l!l
20
MC/DC0%0/1
$_->{"api_key_id"} // ""
$_->{"api_key_id"}
Truth table50%1/2
A
A= $_->{"api_key_id"}
Aresult
11
00
190 }
191 26 ▶ if (defined $filters->{model} && length $filters->{model}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filters->{"model"} and length $filters->{"model"}) 1 25 26
Condition66%2/3
defined $filters->{"model"} and length $filters->{"model"}
!ll&&!rl&&r
2501
MC/DC50%1/2
defined $filters->{"model"} and length $filters->{"model"}
defined $filters->{"model"} length $filters->{"model"}
Truth table66%2/3
A and B
A= defined $filters->{"model"}B= length $filters->{"model"}
ABresult
0X0
100
111
192 1 ▶ @events = grep { ($_->{model} // '') eq $filters->{model} } @events;
Condition50%1/2
$_->{"model"} // ""
l!l
20
MC/DC0%0/1
$_->{"model"} // ""
$_->{"model"}
Truth table50%1/2
A
A= $_->{"model"}
Aresult
11
00
193 }
194  
195 26 my %totals = (requests => 0, input_tokens => 0, output_tokens => 0, total_tokens => 0, cached_tokens => 0, cache_write_tokens => 0, tool_calls => 0, audio_seconds => 0, documents => 0, pages => 0, total_cost_usd => 0);
196 26 my (%by_key, %by_model);
197 26 for my $ev (@events) {
198 84 $totals{requests}++;
199 84 $totals{input_tokens} += $num->($ev->{input_tokens});
200 84 $totals{output_tokens} += $num->($ev->{output_tokens});
201 84 $totals{total_tokens} += $num->($ev->{total_tokens});
202 # Old events predate the field and read undef; num() treats that as zero (k27).
203 84 $totals{cached_tokens} += $num->($ev->{cached_tokens});
204 84 $totals{cache_write_tokens} += $num->($ev->{cache_write_tokens});
205 84 $totals{tool_calls} += $num->($ev->{tool_calls});
206 # Only an audio route's event carries it (skeid k91); every other event adds nothing.
207 84 $totals{audio_seconds} += $num->($ev->{audio_seconds});
208 # Likewise the documents of a rerank event (skeid k92).
209 84 $totals{documents} += $num->($ev->{documents});
210 # And the pages a documents event read off its answer (skeid k94).
211 84 $totals{pages} += $num->($ev->{pages});
212 84 $totals{total_cost_usd} += $num->($ev->{cost_total_usd});
213  
214 84 ▶ my $kid = $ev->{api_key_id} // '';
Condition50%1/2
$ev->{"api_key_id"} // ""
l!l
840
MC/DC0%0/1
$ev->{"api_key_id"} // ""
$ev->{"api_key_id"}
Truth table50%1/2
A
A= $ev->{"api_key_id"}
Aresult
11
00
215 84 $by_key{$kid}{requests}++;
216 84 $by_key{$kid}{total_tokens} += $num->($ev->{total_tokens});
217 84 $by_key{$kid}{audio_seconds} += $num->($ev->{audio_seconds});
218 84 $by_key{$kid}{documents} += $num->($ev->{documents});
219 84 $by_key{$kid}{pages} += $num->($ev->{pages});
220 84 $by_key{$kid}{total_cost_usd} += $num->($ev->{cost_total_usd});
221  
222 84 ▶ my $mid = $ev->{model} // '';
Condition50%1/2
$ev->{"model"} // ""
l!l
840
MC/DC0%0/1
$ev->{"model"} // ""
$ev->{"model"}
Truth table50%1/2
A
A= $ev->{"model"}
Aresult
11
00
223 84 $by_model{$mid}{requests}++;
224 84 $by_model{$mid}{total_tokens} += $num->($ev->{total_tokens});
225 84 $by_model{$mid}{audio_seconds} += $num->($ev->{audio_seconds});
226 84 $by_model{$mid}{documents} += $num->($ev->{documents});
227 84 $by_model{$mid}{pages} += $num->($ev->{pages});
228 84 $by_model{$mid}{total_cost_usd} += $num->($ev->{cost_total_usd});
229 }
230  
231 26 ▶ my $limit = $filters->{limit} // 20;
Condition50%1/2
$filters->{"limit"} // 20
l!l
260
MC/DC0%0/1
$filters->{"limit"} // 20
$filters->{"limit"}
Truth table50%1/2
A
A= $filters->{"limit"}
Aresult
11
00
232 26 my @recent = reverse @events;
233 26 ▶ @recent = @recent[0 .. $limit - 1] if @recent > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if @recent > $limit 1 25 26
234  
235 return {
236 ok => 1,
237 enabled => 1,
238 backend => 'jsonlog',
239 log_path => $self->path,
240 since => ($filters->{since} // ''),
241 totals => \%totals,
242 by_key => [ map {
243 +{ api_key_id => $_, requests => $by_key{$_}{requests}, total_tokens => $by_key{$_}{total_tokens}, audio_seconds => $by_key{$_}{audio_seconds}, documents => $by_key{$_}{documents}, pages => $by_key{$_}{pages}, total_cost_usd => $by_key{$_}{total_cost_usd} }
244 61 ▶ } sort { ($by_key{$b}{total_cost_usd} || 0) <=> ($by_key{$a}{total_cost_usd} || 0) or $a cmp $b } keys %by_key ],
Condition42%3/7
($by_key{$b}{'total_cost_usd'} || 0) <=> ($by_key{$a}{'total_cost_usd'} || 0) || $a cmp $b
l!l&&r!l&&!r
0720
$by_key{$b}{'total_cost_usd'} || 0
l!l
072
$by_key{$a}{'total_cost_usd'} || 0
l!l
072
MC/DC0%0/2
($by_key{$b}{'total_cost_usd'} || 0) <=> ($by_key{$a}{'total_cost_usd'} || 0) || $a cmp $b
$by_key{$b}{'total_cost_usd'} $a cmp $b
MC/DC0%0/1
$by_key{$a}{'total_cost_usd'} || 0
$by_key{$a}{'total_cost_usd'}
Truth table33%1/3
A || B
A= $by_key{$b}{'total_cost_usd'}B= $a cmp $b
ABresult
1X1
011
000
Truth table50%1/2
A
A= $by_key{$a}{'total_cost_usd'}
Aresult
11
00
245 by_model => [ map {
246 +{ model => $_, requests => $by_model{$_}{requests}, total_tokens => $by_model{$_}{total_tokens}, audio_seconds => $by_model{$_}{audio_seconds}, documents => $by_model{$_}{documents}, pages => $by_model{$_}{pages}, total_cost_usd => $by_model{$_}{total_cost_usd} }
247 59 ▶ } sort { ($by_model{$b}{total_cost_usd} || 0) <=> ($by_model{$a}{total_cost_usd} || 0) or $a cmp $b } keys %by_model ],
Condition42%3/7
($by_model{$b}{'total_cost_usd'} || 0) <=> ($by_model{$a}{'total_cost_usd'} || 0) || $a cmp $b
l!l&&r!l&&!r
0740
$by_model{$b}{'total_cost_usd'} || 0
l!l
074
$by_model{$a}{'total_cost_usd'} || 0
l!l
074
MC/DC0%0/2
($by_model{$b}{'total_cost_usd'} || 0) <=> ($by_model{$a}{'total_cost_usd'} || 0) || $a cmp $b
$by_model{$b}{'total_cost_usd'} $a cmp $b
MC/DC0%0/1
$by_model{$a}{'total_cost_usd'} || 0
$by_model{$a}{'total_cost_usd'}
Truth table33%1/3
A || B
A= $by_model{$b}{'total_cost_usd'}B= $a cmp $b
ABresult
1X1
011
000
Truth table50%1/2
A
A= $by_model{$a}{'total_cost_usd'}
Aresult
11
00
248 recent => [ map {
249 26 ▶ +{
Condition50%1/2
$filters->{"since"} // ""
l!l
026
MC/DC0%0/1
$filters->{"since"} // ""
$filters->{"since"}
Truth table50%1/2
A
A= $filters->{"since"}
Aresult
11
00
250 id => ($_->{id} // ''), created_at => ($_->{created_at} // ''), api_format => ($_->{api_format} // ''),
251 endpoint => ($_->{endpoint} // ''), api_key_id => ($_->{api_key_id} // ''), model => ($_->{model} // ''),
252 requested_model => ($_->{requested_model} // $_->{model} // ''),
253 node_id => ($_->{node_id} // ''), status_code => $num->($_->{status_code}), ok => ($_->{ok} ? 1 : 0),
254 input_tokens => $num->($_->{input_tokens}), output_tokens => $num->($_->{output_tokens}),
255 total_tokens => $num->($_->{total_tokens}), cached_tokens => $num->($_->{cached_tokens}),
256 cache_write_tokens => $num->($_->{cache_write_tokens}),
257 tool_calls => $num->($_->{tool_calls}),
258 83 ▶ cost_total_usd => $num->($_->{cost_total_usd}),
Branch: 1 branch
BranchTrueFalseTotal
$_->{'ok'} ? : 78 5 83
Condition73%14/19
$_->{"id"} // ""
l!l
821
$_->{"created_at"} // ""
l!l
830
$_->{"api_format"} // ""
l!l
821
$_->{"endpoint"} // ""
l!l
821
$_->{"api_key_id"} // ""
l!l
830
$_->{"model"} // ""
l!l
830
($_->{"requested_model"} // $_->{"model"}) // ""
l!l
830
$_->{"requested_model"} // $_->{"model"}
l!l&&r!l&&!r
8210
$_->{"node_id"} // ""
l!l
821
MC/DC100%1/1
$_->{"id"} // ""
$_->{"id"}
MC/DC0%0/1
$_->{"created_at"} // ""
$_->{"created_at"}
MC/DC100%1/1
$_->{"api_format"} // ""
$_->{"api_format"}
MC/DC100%1/1
$_->{"endpoint"} // ""
$_->{"endpoint"}
MC/DC0%0/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 table100%2/2
A
A= $_->{"id"}
Aresult
11
00
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 table50%1/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
259 }
260 } @recent ],
261 };
262 }
263  
264  
265 25 1;
266  
267 __END__
« lib/Langertha/Skeid.pm ↑ Summary lib/Langertha/Skeid/OpenAPI.pm »