« /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 ||= {}
MC/DC0%0/1
$cfg ||= {}
$cfg
Truth table50%1/2
A
A= $cfg
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 39 | ▶ | croak 'usage_store must be a hashref' unless ref($cfg) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 39 | ▶ | my $backend = lc($cfg->{backend} // ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$cfg->{"backend"} // ""
MC/DC100%1/1
$cfg->{"backend"} // ""
$cfg->{"backend"}
Truth table100%2/2
A
A= $cfg->{"backend"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 39 | ▶ | $backend = 'sqlite' if !$backend && (defined($cfg->{sqlite_path}) || defined($cfg->{path}) || defined($cfg->{db_path})); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition77%7/9
not $backend and defined $cfg->{"sqlite_path"} || defined $cfg->{"path"} || defined $cfg->{"db_path"}
defined $cfg->{"sqlite_path"} or defined $cfg->{"path"} or defined $cfg->{"db_path"}
defined $cfg->{"sqlite_path"} or defined $cfg->{"path"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 39 | ▶ | $backend = 'postgresql' if !$backend && defined($cfg->{dsn}) && $cfg->{dsn} =~ /^dbi:Pg:/i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
not $backend and defined $cfg->{"dsn"} and $cfg->{"dsn"} =~ /^dbi:Pg:/i
not $backend and defined $cfg->{"dsn"}
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 39 | ▶ | $backend = 'jsonlog' if !$backend && defined($cfg->{log_path}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $backend and defined $cfg->{"log_path"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 39 | ▶ | $backend = 'sqlite' unless length $backend; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 39 | ▶ | $backend = 'postgresql' if $backend =~ /^postgres/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 39 | ▶ | $backend = 'jsonlog' if $backend =~ /^json/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 39 | ▶ | if ($backend eq 'sqlite') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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"}
($cfg->{"sqlite_path"} // $cfg->{"path"}) // $cfg->{"db_path"}
$cfg->{"sqlite_path"} // $cfg->{"path"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 23 | ▶ | if (!defined($path) || !length($path)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $path and length $path
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
$cfg->{"schema_file"} // ""
MC/DC100%1/1
$cfg->{"schema_file"} // ""
$cfg->{"schema_file"}
Truth table100%2/2
A
A= $cfg->{"schema_file"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | flush_interval_ms => $class->_flush_interval_ms($cfg), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 16 | ▶ | if ($backend eq 'postgresql') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | my $dsn = $cfg->{dsn}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | ▶ | if (!defined($dsn) || !length($dsn)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $dsn and length $dsn
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | my $host = $cfg->{host} // '127.0.0.1'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$cfg->{"host"} // "127.0.0.1"
MC/DC0%0/1
$cfg->{"host"} // "127.0.0.1"
$cfg->{"host"}
Truth table0%0/2
A
A= $cfg->{"host"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | my $port = $cfg->{port} // 5432; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$cfg->{"port"} // 5432
MC/DC0%0/1
$cfg->{"port"} // 5432
$cfg->{"port"}
Truth table0%0/2
A
A= $cfg->{"port"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | my $name = $cfg->{dbname} // $cfg->{database} // 'skeid'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
($cfg->{"dbname"} // $cfg->{"database"}) // "skeid"
$cfg->{"dbname"} // $cfg->{"database"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | $dsn = "dbi:Pg:dbname=$name;host=$host;port=$port"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | ▶ | my $password = defined($cfg->{password}) ? $cfg->{password} : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | ▶ | if (!length($password) && defined($cfg->{password_env}) && length($cfg->{password_env})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not length $password and defined $cfg->{"password_env"} and length $cfg->{"password_env"}
not length $password and defined $cfg->{"password_env"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | $password = $ENV{$cfg->{password_env}} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$ENV{$cfg->{"password_env"}} // ""
MC/DC0%0/1
$ENV{$cfg->{"password_env"}} // ""
$ENV{$cfg->{"password_env"}}
Truth table0%0/2
A
A= $ENV{$cfg->{"password_env"}}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%2/4
$cfg->{"user"} // ""
$cfg->{"schema_file"} // ""
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"}
Truth table50%1/2
A
A= $cfg->{"schema_file"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | flush_interval_ms => $class->_flush_interval_ms($cfg), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 15 | ▶ | if ($backend eq 'jsonlog') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 15 | ▶ | my $path = $cfg->{log_path} // $cfg->{path} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition40%2/5
($cfg->{"log_path"} // $cfg->{"path"}) // ""
$cfg->{"log_path"} // $cfg->{"path"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 15 | ▶ | croak 'usage_store.log_path (or path) is required for jsonlog backend' unless length $path; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 15 | ▶ | my $mode = $cfg->{mode} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$cfg->{"mode"} // ""
MC/DC100%1/1
$cfg->{"mode"} // ""
$cfg->{"mode"}
Truth table100%2/2
A
A= $cfg->{"mode"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 15 | ▶ | if (!length($mode)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 7 | ▶ | $mode = (-d $path || $path =~ m{/$}) ? 'dir' : 'file'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-d $path or $path =~ m[/$]
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[/$]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | backend => 'jsonlog', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | path => $path, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | mode => $mode, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 15 | ▶ | fsync => ($cfg->{fsync} ? 1 : 0), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$cfg->{"flush_interval_ms"} // 0
$cfg->{"flush_interval_ms"}
Truth table100%2/2
A
A= $cfg->{"flush_interval_ms"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 24 | ▶ | croak 'usage_store.flush_interval_ms must be a whole number of milliseconds (0 = write each event at once)' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 32 | ▶ | my $backend = $cfg->{backend} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$cfg->{"backend"} // ""
MC/DC0%0/1
$cfg->{"backend"} // ""
$cfg->{"backend"}
Truth table50%1/2
A
A= $cfg->{"backend"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 32 | ▶ | return unless length $backend; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 32 | ▶ | if ($backend eq 'jsonlog') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%2/4
$cfg->{"path"} // ""
$cfg->{"mode"} // "dir"
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"}
Truth table50%1/2
A
A= $cfg->{"mode"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%6/12
$cfg->{"dsn"} // ""
$cfg->{"user"} // ""
$cfg->{"password"} // ""
$cfg->{"path"} // ""
$cfg->{"schema_file"} // ""
$cfg->{"flush_interval_ms"} // 0
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"}
Truth table50%1/2
A
A= $cfg->{"user"}
Truth table50%1/2
A
A= $cfg->{"password"}
Truth table50%1/2
A
A= $cfg->{"path"}
Truth table50%1/2
A
A= $cfg->{"schema_file"}
Truth table50%1/2
A
A= $cfg->{"flush_interval_ms"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||