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

filter: Statementstmt 93.0% 174 / 187 Branchbran 75.7% 53 / 70 Conditioncond 57.5% 42 / 73 MC/DCmcdc 25.0% 10 / 40 Subroutinesub 96.1% 25 / 26 Podpod 100% 5 / 5 totaltotal 77.0% 309 / 401 CC 70 SCAR 32.6
CC
70
Cov
82%
report
55.0
_new_process_nonce
10.9
_write_and_close
9.2
CRAP 25.9
« blib/lib/Langertha/Skeid.pm ↑ Summary blib/lib/Langertha/Skeid/KeyBroker/OpenBao.pm »
1 package Langertha::Skeid::UsageStore::JsonLog;
2 8 our $VERSION = '0.003';
3 # ABSTRACT: Append-only JSON usage store — one file per event, or one line per event
4 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Fcntl qw(O_WRONLY O_CREAT O_EXCL LOCK_EX);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Errno qw(EEXIST EINTR);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Digest::SHA qw(sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use JSON::MaybeXS qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use Langertha::Skeid::UsageStore;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 8 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 14 ▶sub prepare {
Subroutine prepare: called CC=6 SCAR=19.0
Pod documented
31 14 my ($self) = @_;
32 14 my $path = $self->path;
33 14 ▶ if ($self->mode eq 'dir') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq 'dir') { } 12 2 14
34 12 ▶ make_path($path) unless -d $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path 6 6 12
35 } else {
36 2 my $dir = dirname($path);
37 2 ▶ 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 2 2
Condition33%2/6
length $dir and $dir ne "." and not -d $dir
!ll&&!rl&&r
020
length $dir and $dir ne "."
!ll&&!rl&&r
002
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 14 return 1;
40 }
41  
42  
43 14 ▶sub disconnect { return }
Subroutine disconnect: called CC=1 SCAR=0.0
Pod documented
44  
45 8 our %EVENT_ID_STATE;
46 8 my $CREATE_ATTEMPTS = 16;
47 8 my $PROCESS_NONCE_BYTES = 16;
48  
49 7 ▶sub _new_process_nonce {
Subroutine _new_process_nonce: called CC=7 SCAR=23.8
50 7 my $random = '';
51 7 ▶ if (open my $fh, '<:raw', '/dev/urandom') {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<:raw", "/dev/urandom") 7 0 7
52 7 ▶ while (length($random) < $PROCESS_NONCE_BYTES) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $random < $PROCESS_NONCE_BYTES) 7 7 14
53 7 my $chunk = '';
54 7 my $read = sysread($fh, $chunk, $PROCESS_NONCE_BYTES - length($random));
55 7 ▶ if (!defined $read) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $read) 0 7 7
56 0 ▶ next if $! == EINTR;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 4 0 0 0
57 0 last;
58 }
59 7 ▶ last unless $read;
Branch: 1 branch
BranchTrueFalseTotal
unless $read 0 7 7
60 7 $random .= $chunk;
61 }
62 7 close $fh;
63 }
64 7 ▶ return unpack('H*', $random) if length($random) == $PROCESS_NONCE_BYTES;
Branch: 1 branch
BranchTrueFalseTotal
if length $random == $PROCESS_NONCE_BYTES 7 0 7
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 30 ▶sub _event_id {
Subroutine _event_id: called CC=3 SCAR=11.0
75 30 my $pid = $$;
76 30 ▶ 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) 12 18 30
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
12018
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 12 %EVENT_ID_STATE = (
78 pid => $pid,
79 nonce => _new_process_nonce(),
80 sequence => 0,
81 );
82 }
83 30 my $sequence = ++$EVENT_ID_STATE{sequence};
84 30 my $ts = strftime('%Y%m%d-%H%M%S', gmtime());
85 30 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 28 ▶sub _write_and_close {
Subroutine _write_and_close: called CC=9 SCAR=22.1
95 28 my ($self, $fh, $path, $json, $operation) = @_;
96 28 my $error;
97  
98 28 ▶ unless (print {$fh} $json, "\n") {
Branch: 1 branch
BranchTrueFalseTotal
unless (print {$fh;} $json, "\n") 1 27 28
99 1 $error = _io_error($operation, $path, "$!");
100 }
101 28 ▶ if (!$error && $self->fsync) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $error and $self->fsync) 5 23 28
Condition100%3/3
not $error and $self->fsync
!ll&&!rl&&r
1225
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 28 ▶ unless (close $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (close $fh) 2 26 28
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 28 return $error;
114 }
115  
116  
117 30 ▶sub store {
Subroutine store: called CC=9 SCAR=22.1
Pod documented
118 30 my ($self, $event) = @_;
119  
120 30 ▶ if ($self->mode eq 'dir') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "dir") 22 8 30
121 22 for (1 .. $CREATE_ATTEMPTS) {
122 38 my $id = _event_id();
123 38 my $json = encode_json({ %$event, id => $id });
124 38 my $file = File::Spec->catfile($self->path, "${id}.json");
125 38 my $fh;
126 38 ▶ unless (sysopen $fh, $file, O_WRONLY | O_CREAT | O_EXCL, 0666) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $fh, $file, 193, 438) 18 20 38
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 20 my $error = _write_and_close($self, $fh, $file, $json, 'write');
131 20 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 2 18 20
132 2 unlink $file;
133 2 return { ok => 0, error => $error };
134 }
135 18 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 8 my $id = _event_id();
144 8 my $json = encode_json({ %$event, id => $id });
145 8 my $path = $self->path;
146 8 ▶ 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}: $!"} 8 0 8
147 8 ▶ unless (flock($fh, LOCK_EX)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $fh, 2) 0 8 8
148 0 my $error = _io_error('lock', $path, "$!");
149 0 close $fh;
150 0 return { ok => 0, error => $error };
151 }
152 8 my $error = _write_and_close($self, $fh, $path, $json, 'append');
153 8 ▶ return { ok => 0, error => $error } if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 1 7 8
154  
155 7 return { ok => 1, id => $id };
156 }
157  
158  
159 6 ▶sub report {
Subroutine report: called CC=39 SCAR=40.1
Pod documented
160 6 my ($self, $filters) = @_;
161 6 ▶ $filters ||= {};
Condition50%1/2
$filters ||= {}
l!l
60
MC/DC0%0/1
$filters ||= {}
$filters
Truth table50%1/2
A
A= $filters
Aresult
11
00
162 6 my $num = \&Langertha::Skeid::UsageStore::num;
163  
164 6 my @events;
165 6 ▶ if ($self->mode eq 'dir') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq 'dir') { } 5 1 6
166 5 ▶ my @files = sort glob(File::Spec->catfile($self->path, '*.json'));
Subroutine BEGIN: called CC=1 SCAR=0.0
167 5 for my $file (@files) {
168 11 my $text = eval { Langertha::Skeid::UsageStore::read_text_file($file) };
169 11 ▶ next unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 11 11
170 11 my $ev = eval { decode_json($text) };
171 11 ▶ push @events, $ev if ref($ev) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $ev eq "HASH" 11 0 11
172 }
173 } else {
174 1 ▶ if (open my $fh, '<', $self->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", $self->path) 1 0 1
175 1 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 2 1 3
176 2 chomp $line;
177 2 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 2 2
178 2 my $ev = eval { decode_json($line) };
179 2 ▶ push @events, $ev if ref($ev) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $ev eq "HASH" 2 0 2
180 }
181 1 close $fh;
182 }
183 }
184  
185 6 ▶ if (defined $filters->{since} && length $filters->{since}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filters->{"since"} and length $filters->{"since"}) 0 6 6
Condition33%1/3
defined $filters->{"since"} and length $filters->{"since"}
!ll&&!rl&&r
600
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 6 ▶ 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 5 6
Condition66%2/3
defined $filters->{"api_key_id"} and length $filters->{"api_key_id"}
!ll&&!rl&&r
501
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 6 ▶ if (defined $filters->{model} && length $filters->{model}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filters->{"model"} and length $filters->{"model"}) 1 5 6
Condition66%2/3
defined $filters->{"model"} and length $filters->{"model"}
!ll&&!rl&&r
501
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 6 my %totals = (requests => 0, input_tokens => 0, output_tokens => 0, total_tokens => 0, cached_tokens => 0, cache_write_tokens => 0, tool_calls => 0, total_cost_usd => 0);
196 6 my (%by_key, %by_model);
197 6 for my $ev (@events) {
198 11 $totals{requests}++;
199 11 $totals{input_tokens} += $num->($ev->{input_tokens});
200 11 $totals{output_tokens} += $num->($ev->{output_tokens});
201 11 $totals{total_tokens} += $num->($ev->{total_tokens});
202 # Old events predate the field and read undef; num() treats that as zero (k27).
203 11 $totals{cached_tokens} += $num->($ev->{cached_tokens});
204 11 $totals{cache_write_tokens} += $num->($ev->{cache_write_tokens});
205 11 $totals{tool_calls} += $num->($ev->{tool_calls});
206 11 $totals{total_cost_usd} += $num->($ev->{cost_total_usd});
207  
208 11 ▶ my $kid = $ev->{api_key_id} // '';
Condition50%1/2
$ev->{"api_key_id"} // ""
l!l
110
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
209 11 $by_key{$kid}{requests}++;
210 11 $by_key{$kid}{total_tokens} += $num->($ev->{total_tokens});
211 11 $by_key{$kid}{total_cost_usd} += $num->($ev->{cost_total_usd});
212  
213 11 ▶ my $mid = $ev->{model} // '';
Condition50%1/2
$ev->{"model"} // ""
l!l
110
MC/DC0%0/1
$ev->{"model"} // ""
$ev->{"model"}
Truth table50%1/2
A
A= $ev->{"model"}
Aresult
11
00
214 11 $by_model{$mid}{requests}++;
215 11 $by_model{$mid}{total_tokens} += $num->($ev->{total_tokens});
216 11 $by_model{$mid}{total_cost_usd} += $num->($ev->{cost_total_usd});
217 }
218  
219 6 ▶ my $limit = $filters->{limit} // 20;
Condition50%1/2
$filters->{"limit"} // 20
l!l
60
MC/DC0%0/1
$filters->{"limit"} // 20
$filters->{"limit"}
Truth table50%1/2
A
A= $filters->{"limit"}
Aresult
11
00
220 6 my @recent = reverse @events;
221 6 ▶ @recent = @recent[0 .. $limit - 1] if @recent > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if @recent > $limit 1 5 6
222  
223 return {
224 ok => 1,
225 enabled => 1,
226 backend => 'jsonlog',
227 log_path => $self->path,
228 since => ($filters->{since} // ''),
229 totals => \%totals,
230 by_key => [ map {
231 +{ api_key_id => $_, requests => $by_key{$_}{requests}, total_tokens => $by_key{$_}{total_tokens}, total_cost_usd => $by_key{$_}{total_cost_usd} }
232 8 ▶ } sort { ($by_key{$b}{total_cost_usd} || 0) <=> ($by_key{$a}{total_cost_usd} || 0) } keys %by_key ],
Condition50%2/4
$by_key{$b}{'total_cost_usd'} || 0
l!l
02
$by_key{$a}{'total_cost_usd'} || 0
l!l
02
MC/DC0%0/1
$by_key{$b}{'total_cost_usd'} || 0
$by_key{$b}{'total_cost_usd'}
MC/DC0%0/1
$by_key{$a}{'total_cost_usd'} || 0
$by_key{$a}{'total_cost_usd'}
Truth table50%1/2
A
A= $by_key{$b}{'total_cost_usd'}
Aresult
11
00
Truth table50%1/2
A
A= $by_key{$a}{'total_cost_usd'}
Aresult
11
00
233 by_model => [ map {
234 +{ model => $_, requests => $by_model{$_}{requests}, total_tokens => $by_model{$_}{total_tokens}, total_cost_usd => $by_model{$_}{total_cost_usd} }
235 8 ▶ } sort { ($by_model{$b}{total_cost_usd} || 0) <=> ($by_model{$a}{total_cost_usd} || 0) } keys %by_model ],
Condition50%2/4
$by_model{$b}{'total_cost_usd'} || 0
l!l
02
$by_model{$a}{'total_cost_usd'} || 0
l!l
02
MC/DC0%0/1
$by_model{$b}{'total_cost_usd'} || 0
$by_model{$b}{'total_cost_usd'}
MC/DC0%0/1
$by_model{$a}{'total_cost_usd'} || 0
$by_model{$a}{'total_cost_usd'}
Truth table50%1/2
A
A= $by_model{$b}{'total_cost_usd'}
Aresult
11
00
Truth table50%1/2
A
A= $by_model{$a}{'total_cost_usd'}
Aresult
11
00
236 recent => [ map {
237 6 ▶ +{
Condition50%1/2
$filters->{"since"} // ""
l!l
06
MC/DC0%0/1
$filters->{"since"} // ""
$filters->{"since"}
Truth table50%1/2
A
A= $filters->{"since"}
Aresult
11
00
238 id => ($_->{id} // ''), created_at => ($_->{created_at} // ''), api_format => ($_->{api_format} // ''),
239 endpoint => ($_->{endpoint} // ''), api_key_id => ($_->{api_key_id} // ''), model => ($_->{model} // ''),
240 requested_model => ($_->{requested_model} // $_->{model} // ''),
241 node_id => ($_->{node_id} // ''), status_code => $num->($_->{status_code}), ok => ($_->{ok} ? 1 : 0),
242 input_tokens => $num->($_->{input_tokens}), output_tokens => $num->($_->{output_tokens}),
243 total_tokens => $num->($_->{total_tokens}), cached_tokens => $num->($_->{cached_tokens}),
244 cache_write_tokens => $num->($_->{cache_write_tokens}),
245 tool_calls => $num->($_->{tool_calls}),
246 10 ▶ cost_total_usd => $num->($_->{cost_total_usd}),
Branch: 1 branch
BranchTrueFalseTotal
$_->{'ok'} ? : 9 1 10
Condition73%14/19
$_->{"id"} // ""
l!l
91
$_->{"created_at"} // ""
l!l
100
$_->{"api_format"} // ""
l!l
91
$_->{"endpoint"} // ""
l!l
91
$_->{"api_key_id"} // ""
l!l
100
$_->{"model"} // ""
l!l
100
($_->{"requested_model"} // $_->{"model"}) // ""
l!l
100
$_->{"requested_model"} // $_->{"model"}
l!l&&r!l&&!r
910
$_->{"node_id"} // ""
l!l
91
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
247 }
248 } @recent ],
249 };
250 }
251  
252  
253 8 1;
254  
255 __END__
256  
257 =pod
258  
259 =encoding UTF-8
260  
261 =head1 NAME
262  
263 Langertha::Skeid::UsageStore::JsonLog - Append-only JSON usage store — one file per event, or one line per event
264  
265 =head1 VERSION
266  
267 version 0.003
268  
269 =head1 DESCRIPTION
270  
271 The recommended usage store. Writing an event is an append, so it cannot block the event loop
272 on a database round-trip the way the DBI backends can, and an append-only file is the easiest
273 thing to reconcile after an incident.
274  
275 Reporting reads and aggregates the whole log in memory. That is fine for the volumes this is
276 meant for and deliberately not optimised — if a deployment outgrows it, the answer is a real
277 database, not an index on a directory of JSON files.
278  
279 =head2 path
280  
281 Required. Directory (C<mode> C<dir>) or file (C<mode> C<file>) the events are written to.
282  
283 =head2 mode
284  
285 C<dir> (the default) writes one C<< <id>.json >> per event, created exclusively, which never needs
286 a lock and is safe with several writers. C<file> appends one JSON line per event under an
287 exclusive C<flock>; any value other than C<dir> behaves as C<file>. The event id --
288 UTC time, process id, a per-process random nonce and a sequence number -- is stored in the event
289 as C<id>.
290  
291 =head2 fsync
292  
293 When true, an event's bytes are flushed and C<fsync>'d to disk before C<store> returns, at a
294 throughput cost. Default off: ADR 0004 already accepts that one in-flight event is lost on a
295 crash, and the kernel's own writeback covers the rest under normal operation. Opt in where a
296 crash losing an already-answered request's event is unacceptable.
297  
298 =head2 backend
299  
300 Returns C<jsonlog>.
301  
302 =head2 prepare
303  
304 Creates the target directory (or the file's parent directory). Called when the store is
305 configured, not when an event is written.
306  
307 =head2 disconnect
308  
309 No-op — nothing is held open between writes.
310  
311 =head2 store
312  
313 my $res = $store->store($event);
314  
315 Writes one event and returns C<< { ok => 1, id => $id } >>, or C<< { ok => 0, error => … } >>.
316 A write failure is reported, never thrown: losing a usage event must not also fail the request
317 that was already served. The proxy logs such an answer at C<error> level as a lost usage event.
318  
319 =head2 report
320  
321 my $report = $store->report(\%filters);
322  
323 Reads every event, applies the C<since> / C<api_key_id> / C<model> filters, and aggregates
324 totals plus per-key and per-model breakdowns. C<recent> holds the newest C<limit> events
325 (default 20). Unreadable files and lines are skipped. The shape is described in
326 L<Langertha::Skeid::UsageStore/The store contract>.
327  
328 =head1 SEE ALSO
329  
330 L<Langertha::Skeid::UsageStore>, L<Langertha::Skeid::UsageStore::DBI>
331  
332 =head1 SUPPORT
333  
334 =head2 Issues
335  
336 Please report bugs and feature requests on GitHub at
337 L<https://github.com/Getty/langertha-skeid/issues>.
338  
339 =head2 IRC
340  
341 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
342  
343 =head1 CONTRIBUTING
344  
345 Contributions are welcome! Please fork the repository and submit a pull request.
346  
347 =head1 AUTHOR
348  
349 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
350  
351 =head1 COPYRIGHT AND LICENSE
352  
353 This software is copyright (c) 2026 by Torsten Raudssus.
354  
355 This is free software; you can redistribute it and/or modify it under
356 the same terms as the Perl 5 programming language system itself.
357  
358 =cut
« blib/lib/Langertha/Skeid.pm ↑ Summary blib/lib/Langertha/Skeid/KeyBroker/OpenBao.pm »