blib/lib/Langertha/Skeid/UsageStore.pm

filter: Statementstmt 89.5% 60 / 67 Branchbran 69.3% 43 / 62 Conditioncond 48.8% 44 / 90 MC/DCmcdc 15.5% 7 / 45 Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 60.1% 166 / 276 CC 69 SCAR 49.2
CC
69
Cov
67%
normalize_config
200.5
for_config
25.5
_flush_interval_ms
3.0
CRAP 137.5
« /home/cpancover/.cpan/build/Langertha-Skeid-0.003-0/bin/skeid ↑ Summary blib/lib/Langertha/Skeid/Protocol/Anthropic.pm »
1 package Langertha::Skeid::UsageStore;
2 68 our $VERSION = '0.003';
3 # ABSTRACT: Usage event sink — config normalization and backend factory
4 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 68 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 39 ▶sub normalize_config {
Subroutine normalize_config: called CC=50 SCAR=53.0
Pod documented
10 39 my ($class, $cfg, %opts) = @_;
11 39 ▶ $cfg ||= {};
Condition50%1/2
$cfg ||= {}
l!l
390
MC/DC0%0/1
$cfg ||= {}
$cfg
Truth table50%1/2
A
A= $cfg
Aresult
11
00
12 39 ▶ croak 'usage_store must be a hashref' unless ref($cfg) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cfg eq "HASH" 0 39 39
13  
14 39 ▶ my $backend = lc($cfg->{backend} // '');
Condition100%2/2
$cfg->{"backend"} // ""
l!l
2811
MC/DC100%1/1
$cfg->{"backend"} // ""
$cfg->{"backend"}
Truth table100%2/2
A
A= $cfg->{"backend"}
Aresult
11
00
15 39 ▶ $backend = 'sqlite' if !$backend && (defined($cfg->{sqlite_path}) || defined($cfg->{path}) || defined($cfg->{db_path}));
Branch: 1 branch
BranchTrueFalseTotal
if not $backend and defined $cfg->{"sqlite_path"} || defined $cfg->{"path"} || defined $cfg->{"db_path"} 10 29 39
Condition77%7/9
not $backend and defined $cfg->{"sqlite_path"} || defined $cfg->{"path"} || defined $cfg->{"db_path"}
!ll&&!rl&&r
28110
defined $cfg->{"sqlite_path"} or defined $cfg->{"path"} or defined $cfg->{"db_path"}
l!l&&r!l&&!r
1001
defined $cfg->{"sqlite_path"} or defined $cfg->{"path"}
l!l&&r!l&&!r
1001
MC/DC50%2/4
not $backend and defined $cfg->{"sqlite_path"} || defined $cfg->{"path"} || defined $cfg->{"db_path"}
not $backend defined $cfg->{"sqlite_path"} defined $cfg->{"path"} defined $cfg->{"db_path"}
Truth table60%3/5
not(A) and B or C or D
A= not $backendB= defined $cfg->{"sqlite_path"}C= defined $cfg->{"path"}D= defined $cfg->{"db_path"}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
16 39 ▶ $backend = 'postgresql' if !$backend && defined($cfg->{dsn}) && $cfg->{dsn} =~ /^dbi:Pg:/i;
Branch: 1 branch
BranchTrueFalseTotal
if not $backend and defined $cfg->{"dsn"} and $cfg->{"dsn"} =~ /^dbi:Pg:/i 1 38 39
Condition66%4/6
not $backend and defined $cfg->{"dsn"} and $cfg->{"dsn"} =~ /^dbi:Pg:/i
!ll&&!rl&&r
3801
not $backend and defined $cfg->{"dsn"}
!ll&&!rl&&r
3801
MC/DC33%1/3
not $backend and defined $cfg->{"dsn"} and $cfg->{"dsn"} =~ /^dbi:Pg:/i
not $backend defined $cfg->{"dsn"} $cfg->{"dsn"} =~ /^dbi:Pg:/i
Truth table50%2/4
not(A) and B and C
A= not $backendB= defined $cfg->{"dsn"}C= $cfg->{"dsn"} =~ /^dbi:Pg:/i
ABCresult
0XX0
10X0
1100
1111
17 39 ▶ $backend = 'jsonlog' if !$backend && defined($cfg->{log_path});
Branch: 1 branch
BranchTrueFalseTotal
if not $backend and defined $cfg->{"log_path"} 0 39 39
Condition33%1/3
not $backend and defined $cfg->{"log_path"}
!ll&&!rl&&r
3900
MC/DC0%0/2
not $backend and defined $cfg->{"log_path"}
not $backend defined $cfg->{"log_path"}
Truth table33%1/3
not(A) and B
A= not $backendB= defined $cfg->{"log_path"}
ABresult
0X0
100
111
18 39 ▶ $backend = 'sqlite' unless length $backend;
Branch: 1 branch
BranchTrueFalseTotal
unless length $backend 0 39 39
19 39 ▶ $backend = 'postgresql' if $backend =~ /^postgres/;
Branch: 1 branch
BranchTrueFalseTotal
if $backend =~ /^postgres/ 1 38 39
20 39 ▶ $backend = 'jsonlog' if $backend =~ /^json/;
Branch: 1 branch
BranchTrueFalseTotal
if $backend =~ /^json/ 15 24 39
21  
22 39 ▶ if ($backend eq 'sqlite') {
Branch: 1 branch
BranchTrueFalseTotal
if ($backend eq "sqlite") 23 16 39
23 23 ▶ my $path = $cfg->{sqlite_path} // $cfg->{path} // $cfg->{db_path} // $opts{default_sqlite_path};
Condition44%4/9
(($cfg->{"sqlite_path"} // $cfg->{"path"}) // $cfg->{"db_path"}) // $opts{"default_sqlite_path"}
l!l&&r!l&&!r
2300
($cfg->{"sqlite_path"} // $cfg->{"path"}) // $cfg->{"db_path"}
l!l&&r!l&&!r
2300
$cfg->{"sqlite_path"} // $cfg->{"path"}
l!l&&r!l&&!r
2120
MC/DC0%0/4
(($cfg->{"sqlite_path"} // $cfg->{"path"}) // $cfg->{"db_path"}) // $opts{"default_sqlite_path"}
$cfg->{"sqlite_path"} $cfg->{"path"} $cfg->{"db_path"} $opts{"default_sqlite_path"}
Truth table40%2/5
A // B // C // D
A= $cfg->{"sqlite_path"}B= $cfg->{"path"}C= $cfg->{"db_path"}D= $opts{"default_sqlite_path"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
24 23 ▶ if (!defined($path) || !length($path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path and length $path) 0 23 23
Condition33%1/3
defined $path and length $path
!ll&&!rl&&r
0023
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table33%1/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
25 0 croak 'usage_store.sqlite_path (or path/db_path) is required for sqlite backend';
26 }
27 return {
28 backend => 'sqlite',
29 path => $path,
30 dsn => "dbi:SQLite:dbname=$path",
31 user => '',
32 password => '',
33 schema_file => ($cfg->{schema_file} // ''),
34 23 ▶ auto_migrate => exists($cfg->{auto_migrate}) ? ($cfg->{auto_migrate} ? 1 : 0) : 1,
Branch: 2 branches
BranchTrueFalseTotal
exists $cfg->{'auto_migrate'} ? : 2 21 23
$cfg->{'auto_migrate'} ? : 2 0 2
Condition100%2/2
$cfg->{"schema_file"} // ""
l!l
221
MC/DC100%1/1
$cfg->{"schema_file"} // ""
$cfg->{"schema_file"}
Truth table100%2/2
A
A= $cfg->{"schema_file"}
Aresult
11
00
35 flush_interval_ms => $class->_flush_interval_ms($cfg),
36 };
37 }
38  
39 16 ▶ if ($backend eq 'postgresql') {
Branch: 1 branch
BranchTrueFalseTotal
if ($backend eq "postgresql") 1 15 16
40 1 my $dsn = $cfg->{dsn};
41 1 ▶ if (!defined($dsn) || !length($dsn)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dsn and length $dsn) 0 1 1
Condition33%1/3
defined $dsn and length $dsn
!ll&&!rl&&r
001
MC/DC0%0/2
defined $dsn and length $dsn
defined $dsn length $dsn
Truth table33%1/3
A and B
A= defined $dsnB= length $dsn
ABresult
0X0
100
111
42 0 ▶ my $host = $cfg->{host} // '127.0.0.1';
Condition0%0/2
$cfg->{"host"} // "127.0.0.1"
l!l
00
MC/DC0%0/1
$cfg->{"host"} // "127.0.0.1"
$cfg->{"host"}
Truth table0%0/2
A
A= $cfg->{"host"}
Aresult
11
00
43 0 ▶ my $port = $cfg->{port} // 5432;
Condition0%0/2
$cfg->{"port"} // 5432
l!l
00
MC/DC0%0/1
$cfg->{"port"} // 5432
$cfg->{"port"}
Truth table0%0/2
A
A= $cfg->{"port"}
Aresult
11
00
44 0 ▶ my $name = $cfg->{dbname} // $cfg->{database} // 'skeid';
Condition0%0/5
($cfg->{"dbname"} // $cfg->{"database"}) // "skeid"
l!l
00
$cfg->{"dbname"} // $cfg->{"database"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($cfg->{"dbname"} // $cfg->{"database"}) // "skeid"
$cfg->{"dbname"} $cfg->{"database"}
Truth table0%0/3
A // B
A= $cfg->{"dbname"}B= $cfg->{"database"}
ABresult
1X1
011
000
45 0 $dsn = "dbi:Pg:dbname=$name;host=$host;port=$port";
46 }
47 1 ▶ my $password = defined($cfg->{password}) ? $cfg->{password} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $cfg->{'password'} ? : 0 1 1
48 1 ▶ if (!length($password) && defined($cfg->{password_env}) && length($cfg->{password_env})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $password and defined $cfg->{"password_env"} and length $cfg->{"password_env"}) 0 1 1
Condition33%2/6
not length $password and defined $cfg->{"password_env"} and length $cfg->{"password_env"}
!ll&&!rl&&r
100
not length $password and defined $cfg->{"password_env"}
!ll&&!rl&&r
010
MC/DC0%0/3
not length $password and defined $cfg->{"password_env"} and length $cfg->{"password_env"}
not length $password defined $cfg->{"password_env"} length $cfg->{"password_env"}
Truth table25%1/4
not(A) and B and C
A= not length $passwordB= defined $cfg->{"password_env"}C= length $cfg->{"password_env"}
ABCresult
0XX0
10X0
1100
1111
49 0 ▶ $password = $ENV{$cfg->{password_env}} // '';
Condition0%0/2
$ENV{$cfg->{"password_env"}} // ""
l!l
00
MC/DC0%0/1
$ENV{$cfg->{"password_env"}} // ""
$ENV{$cfg->{"password_env"}}
Truth table0%0/2
A
A= $ENV{$cfg->{"password_env"}}
Aresult
11
00
50 }
51 return {
52 backend => 'postgresql',
53 path => '',
54 dsn => $dsn,
55 user => ($cfg->{user} // ''),
56 password => $password,
57 schema_file => ($cfg->{schema_file} // ''),
58 1 ▶ auto_migrate => exists($cfg->{auto_migrate}) ? ($cfg->{auto_migrate} ? 1 : 0) : 1,
Branch: 2 branches
BranchTrueFalseTotal
exists $cfg->{'auto_migrate'} ? : 0 1 1
$cfg->{'auto_migrate'} ? : 0 0 0
Condition50%2/4
$cfg->{"user"} // ""
l!l
01
$cfg->{"schema_file"} // ""
l!l
01
MC/DC0%0/1
$cfg->{"user"} // ""
$cfg->{"user"}
MC/DC0%0/1
$cfg->{"schema_file"} // ""
$cfg->{"schema_file"}
Truth table50%1/2
A
A= $cfg->{"user"}
Aresult
11
00
Truth table50%1/2
A
A= $cfg->{"schema_file"}
Aresult
11
00
59 flush_interval_ms => $class->_flush_interval_ms($cfg),
60 };
61 }
62  
63 15 ▶ if ($backend eq 'jsonlog') {
Branch: 1 branch
BranchTrueFalseTotal
if ($backend eq "jsonlog") 15 0 15
64 15 ▶ my $path = $cfg->{log_path} // $cfg->{path} // '';
Condition40%2/5
($cfg->{"log_path"} // $cfg->{"path"}) // ""
l!l
150
$cfg->{"log_path"} // $cfg->{"path"}
l!l&&r!l&&!r
0150
MC/DC0%0/2
($cfg->{"log_path"} // $cfg->{"path"}) // ""
$cfg->{"log_path"} $cfg->{"path"}
Truth table33%1/3
A // B
A= $cfg->{"log_path"}B= $cfg->{"path"}
ABresult
1X1
011
000
65 15 ▶ croak 'usage_store.log_path (or path) is required for jsonlog backend' unless length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless length $path 0 15 15
66 15 ▶ my $mode = $cfg->{mode} // '';
Condition100%2/2
$cfg->{"mode"} // ""
l!l
87
MC/DC100%1/1
$cfg->{"mode"} // ""
$cfg->{"mode"}
Truth table100%2/2
A
A= $cfg->{"mode"}
Aresult
11
00
67 15 ▶ if (!length($mode)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $mode) 7 8 15
68 7 ▶ $mode = (-d $path || $path =~ m{/$}) ? 'dir' : 'file';
Branch: 1 branch
BranchTrueFalseTotal
-d $path || $path =~ m[/$] ? : 7 0 7
Condition66%2/3
-d $path or $path =~ m[/$]
l!l&&r!l&&!r
610
MC/DC0%0/2
-d $path or $path =~ m[/$]
-d $path $path =~ m[/$]
Truth table66%2/3
A or B
A= -d $pathB= $path =~ m[/$]
ABresult
1X1
011
000
69 }
70 return {
71 backend => 'jsonlog',
72 path => $path,
73 mode => $mode,
74 15 ▶ fsync => ($cfg->{fsync} ? 1 : 0),
Branch: 1 branch
BranchTrueFalseTotal
$cfg->{'fsync'} ? : 1 14 15
75 };
76 }
77  
78 0 croak "unsupported usage_store backend '$backend'";
79 }
80  
81 # Write-behind for the DBI backends (skeid k78). 0 -- the default -- is the synchronous write
82 # every deployment had before; anything else widens the loss window, so it has to be asked for
83 # with a number that means what it says.
84 24 ▶sub _flush_interval_ms {
Subroutine _flush_interval_ms: called CC=3 SCAR=11.0
85 24 my ($class, $cfg) = @_;
86 24 ▶ my $ms = $cfg->{flush_interval_ms} // 0;
Condition100%2/2
$cfg->{"flush_interval_ms"} // 0
l!l
1212
MC/DC100%1/1
$cfg->{"flush_interval_ms"} // 0
$cfg->{"flush_interval_ms"}
Truth table100%2/2
A
A= $cfg->{"flush_interval_ms"}
Aresult
11
00
87 24 ▶ croak 'usage_store.flush_interval_ms must be a whole number of milliseconds (0 = write each event at once)'
Branch: 1 branch
BranchTrueFalseTotal
unless $ms =~ /\A\d+\z/ 3 21 24
88 unless $ms =~ /\A\d+\z/;
89 21 return 0 + $ms;
90 }
91  
92  
93 32 ▶sub for_config {
Subroutine for_config: called CC=16 SCAR=32.4
Pod documented
94 32 my ($class, $cfg) = @_;
95 32 ▶ return unless ref($cfg) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cfg eq "HASH" 0 32 32
96 32 ▶ my $backend = $cfg->{backend} // '';
Condition50%1/2
$cfg->{"backend"} // ""
l!l
320
MC/DC0%0/1
$cfg->{"backend"} // ""
$cfg->{"backend"}
Truth table50%1/2
A
A= $cfg->{"backend"}
Aresult
11
00
97 32 ▶ return unless length $backend;
Branch: 1 branch
BranchTrueFalseTotal
unless length $backend 0 32 32
98  
99 32 ▶ if ($backend eq 'jsonlog') {
Branch: 1 branch
BranchTrueFalseTotal
if ($backend eq "jsonlog") 14 18 32
100 14 require Langertha::Skeid::UsageStore::JsonLog;
101 return Langertha::Skeid::UsageStore::JsonLog->new(
102 path => ($cfg->{path} // ''),
103 mode => ($cfg->{mode} // 'dir'),
104 14 ▶ fsync => ($cfg->{fsync} ? 1 : 0),
Branch: 1 branch
BranchTrueFalseTotal
$cfg->{'fsync'} ? : 1 13 14
Condition50%2/4
$cfg->{"path"} // ""
l!l
140
$cfg->{"mode"} // "dir"
l!l
140
MC/DC0%0/1
$cfg->{"path"} // ""
$cfg->{"path"}
MC/DC0%0/1
$cfg->{"mode"} // "dir"
$cfg->{"mode"}
Truth table50%1/2
A
A= $cfg->{"path"}
Aresult
11
00
Truth table50%1/2
A
A= $cfg->{"mode"}
Aresult
11
00
105 );
106 }
107  
108 18 require Langertha::Skeid::UsageStore::DBI;
109 return Langertha::Skeid::UsageStore::DBI->new(
110 backend => $backend,
111 dsn => ($cfg->{dsn} // ''),
112 user => ($cfg->{user} // ''),
113 password => ($cfg->{password} // ''),
114 path => ($cfg->{path} // ''),
115 schema_file => ($cfg->{schema_file} // ''),
116 auto_migrate => (exists($cfg->{auto_migrate}) ? ($cfg->{auto_migrate} ? 1 : 0) : 1),
117 18 ▶ flush_interval_ms => ($cfg->{flush_interval_ms} // 0),
Branch: 2 branches
BranchTrueFalseTotal
exists $cfg->{'auto_migrate'} ? : 18 0 18
$cfg->{'auto_migrate'} ? : 18 0 18
Condition50%6/12
$cfg->{"dsn"} // ""
l!l
180
$cfg->{"user"} // ""
l!l
180
$cfg->{"password"} // ""
l!l
180
$cfg->{"path"} // ""
l!l
180
$cfg->{"schema_file"} // ""
l!l
180
$cfg->{"flush_interval_ms"} // 0
l!l
180
MC/DC0%0/1
$cfg->{"dsn"} // ""
$cfg->{"dsn"}
MC/DC0%0/1
$cfg->{"user"} // ""
$cfg->{"user"}
MC/DC0%0/1
$cfg->{"password"} // ""
$cfg->{"password"}
MC/DC0%0/1
$cfg->{"path"} // ""
$cfg->{"path"}
MC/DC0%0/1
$cfg->{"schema_file"} // ""
$cfg->{"schema_file"}
MC/DC0%0/1
$cfg->{"flush_interval_ms"} // 0
$cfg->{"flush_interval_ms"}
Truth table50%1/2
A
A= $cfg->{"dsn"}
Aresult
11
00
Truth table50%1/2
A
A= $cfg->{"user"}
Aresult
11
00
Truth table50%1/2
A
A= $cfg->{"password"}
Aresult
11
00
Truth table50%1/2
A
A= $cfg->{"path"}
Aresult
11
00
Truth table50%1/2
A
A= $cfg->{"schema_file"}
Aresult
11
00
Truth table50%1/2
A
A= $cfg->{"flush_interval_ms"}
Aresult
11
00
118 );
119 }
120  
121  
122 441 ▶sub num {
Subroutine num: called CC=2 SCAR=6.9
Pod documented
123 441 my ($v) = @_;
124 441 ▶ return 0 unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 25 416 441
125 416 return 0 + $v;
126 }
127  
128  
129 38 ▶sub read_text_file {
Subroutine read_text_file: called CC=2 SCAR=7.0
Pod documented
130 38 my ($path) = @_;
131 38 ▶ open my $fh, '<', $path or die "Cannot open $path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or die "Cannot open ${path}: $!" 38 0 38
132 38 local $/;
133 38 my $text = <$fh>;
134 38 close $fh;
135 38 return $text;
136 }
137  
138  
139 68 1;
140  
141 __END__
142  
143 =pod
144  
145 =encoding UTF-8
146  
147 =head1 NAME
148  
149 Langertha::Skeid::UsageStore - Usage event sink — config normalization and backend factory
150  
151 =head1 VERSION
152  
153 version 0.003
154  
155 =head1 DESCRIPTION
156  
157 A usage store is where a L<Langertha::Skeid> usage event goes to become durable. One event is
158 written per forwarded request, including failures, and it is the billing unit — which is why
159 swapping the backend must never change what an event I<means>. See
160 F<docs/adr/0004-usage-events-are-the-billing-unit.md>.
161  
162 This module owns the config shape and hands back the object that implements it:
163 L<Langertha::Skeid::UsageStore::JsonLog> or L<Langertha::Skeid::UsageStore::DBI>.
164  
165 =head2 The store contract
166  
167 A store object answers C<backend> (its name), C<prepare> (create what it needs; called once
168 when the store is configured, may croak), C<store($event)>, C<report(\%filters)> and
169 C<disconnect>. A store that can hold events back also answers C<flush> (write what it holds,
170 answer like C<store> with C<written> and C<lost> counts) and C<disconnect> flushes before it lets
171 go; L<Langertha::Skeid::UsageStore::DBI> with C<flush_interval_ms> is one. C<store> and
172 C<report> report failure in their answer rather than dying: the request an event describes has
173 already been served. The proxy logs a failed C<store> at
174 C<error> level as a lost usage event, with the request id and the backend name -- never a DSN,
175 a path or a key. An error text must not carry a secret either; the DBI store masks a
176 C<password=> from its DSN.
177  
178 C<store> returns C<< { ok => 1 } >> (with an C<id> where the backend has one),
179 C<< { ok => 1, queued => 1 } >> for an event held for a later C<flush> -- whose failures the store
180 reports itself, the proxy's answer having gone out by then -- or
181 C<< { ok => 0, error => $message } >>. C<report> takes the filters C<since> (an ISO 8601 UTC
182 timestamp, compared as a string), C<api_key_id>, C<model> (the served model) and C<limit>, and
183 returns
184  
185 {
186 ok => 1, enabled => 1, backend => 'jsonlog', since => '...',
187 db_path => '...', # DBI stores: the SQLite file, '' for postgresql
188 log_path => '...', # jsonlog: the event directory or file
189 totals => { requests, input_tokens, output_tokens, total_tokens, cached_tokens,
190 cache_write_tokens, tool_calls, total_cost_usd },
191 by_key => [ { api_key_id, requests, total_tokens, total_cost_usd }, ... ],
192 by_model => [ { model, requests, total_tokens, total_cost_usd }, ... ],
193 recent => [ { id, created_at, api_format, endpoint, api_key_id, model, requested_model,
194 node_id, status_code, ok, input_tokens, output_tokens, total_tokens,
195 cached_tokens, cache_write_tokens, tool_calls, cost_total_usd }, ... ],
196 }
197  
198 or C<< { ok => 0, enabled => 0, error => $message } >>. The breakdowns are ordered by cost,
199 highest first; C<recent> is newest first.
200  
201 =head2 normalize_config
202  
203 my $normalized = Langertha::Skeid::UsageStore->normalize_config($cfg, %opts);
204  
205 Turns a user-supplied C<usage_store> config into the canonical hashref Skeid keeps on its
206 C<usage_store> attribute. Backend selection is inference-first, because most configs name only
207 one thing: an explicit C<backend> wins, otherwise a sqlite path key, a C<dbi:Pg:> DSN or a
208 C<log_path> decides. C<password_env> reads the password from the environment so it never has
209 to sit in the file.
210  
211 C<default_sqlite_path> supplies the path for a sqlite config that names none.
212  
213 The keys it reads, per backend:
214  
215 =over 4
216  
217 =item * C<backend> -- C<jsonlog>, C<sqlite> or C<postgresql> (anything starting with C<json> or
218 C<postgres> counts). Absent: C<sqlite_path>, C<path> or C<db_path> means sqlite, a C<dbi:Pg:>
219 C<dsn> means postgresql, C<log_path> means jsonlog, and nothing at all means sqlite -- which then
220 needs a path. A C<path> alone is therefore a sqlite file; a jsonlog store must say so.
221  
222 =item * jsonlog -- C<log_path> (or C<path>), required; C<mode> C<dir> or C<file>, default C<dir>
223 when the path is an existing directory or ends in C</>, else C<file>; C<fsync> (default off).
224  
225 =item * sqlite -- C<sqlite_path> (or C<path>, C<db_path>), required; C<schema_file> (default:
226 the shipped one); C<auto_migrate> (default on); C<flush_interval_ms> (default C<0>, write each
227 event at once; see L<Langertha::Skeid::UsageStore::DBI/flush_interval_ms>).
228  
229 =item * postgresql -- C<dsn>, else one built from C<host> (default C<127.0.0.1>), C<port> (5432)
230 and C<dbname> or C<database> (C<skeid>); C<user>; C<password>, or C<password_env> naming the
231 variable that holds it; C<schema_file>; C<auto_migrate> (default on); C<flush_interval_ms>, as
232 for sqlite.
233  
234 =back
235  
236 Croaks on a config that is not a hashref, a missing path, an unknown backend, or a
237 C<flush_interval_ms> that is not a whole number of milliseconds.
238  
239 =head2 for_config
240  
241 my $store = Langertha::Skeid::UsageStore->for_config($normalized);
242  
243 Builds the store object for a normalized config, or returns nothing when no backend is
244 configured. Does not touch the filesystem or connect — call C<prepare> for that, so that
245 constructing a Skeid object never has a side effect on disk.
246  
247 =head2 num
248  
249 my $n = Langertha::Skeid::UsageStore::num($row->{total_tokens});
250  
251 Numeric coercion that treats undef as zero. Reports sum columns that may be C<NULL> on an empty
252 table, so this is the one place that is allowed to be lax about it.
253  
254 =head2 read_text_file
255  
256 my $sql = Langertha::Skeid::UsageStore::read_text_file($path);
257  
258 Slurps a file, dying with the path on failure.
259  
260 =head1 SEE ALSO
261  
262 =over 4
263  
264 =item * L<Langertha::Skeid::UsageStore::JsonLog>, L<Langertha::Skeid::UsageStore::DBI>
265  
266 =item * L<Langertha::Skeid/record_usage>, L<Langertha::Skeid/usage_report> -- what writes and reads a store
267  
268 =back
269  
270 =head1 SUPPORT
271  
272 =head2 Issues
273  
274 Please report bugs and feature requests on GitHub at
275 L<https://github.com/Getty/langertha-skeid/issues>.
276  
277 =head2 IRC
278  
279 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
280  
281 =head1 CONTRIBUTING
282  
283 Contributions are welcome! Please fork the repository and submit a pull request.
284  
285 =head1 AUTHOR
286  
287 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
288  
289 =head1 COPYRIGHT AND LICENSE
290  
291 This software is copyright (c) 2026 by Torsten Raudssus.
292  
293 This is free software; you can redistribute it and/or modify it under
294 the same terms as the Perl 5 programming language system itself.
295  
296 =cut
« /home/cpancover/.cpan/build/Langertha-Skeid-0.003-0/bin/skeid ↑ Summary blib/lib/Langertha/Skeid/Protocol/Anthropic.pm »