Langertha/Metrics.pm

filter: Statementstmt 96.2% 51 / 53 Branchbran 60.0% 6 / 10 Conditioncond 54.5% 12 / 22 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 5 totaltotal 74.3% 84 / 113 CC 16 SCAR 17.6
CC
16
Cov
81%
build_record
10.4
normalize_tool_metrics
8.1
estimate_cost_usd
3.1
CRAP 5.8
« Langertha/Role/Transcription.pm ↑ Summary Langertha/Raid/Parallel.pm »
1 package Langertha::Metrics;
2 3 our $VERSION = '0.502';
3 # ABSTRACT: Backwards-compat facade over Langertha::Usage / Pricing / Cost / UsageRecord
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Langertha::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Langertha::Pricing;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Langertha::Cost;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Langertha::UsageRecord;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 Carp::carp(
13 "Langertha::Metrics is a backwards-compatibility facade. New code should use "
14 . "Langertha::Usage / Langertha::Pricing / Langertha::Cost / Langertha::UsageRecord directly."
15 );
16  
17 # All methods here are kept for backwards compatibility with existing
18 # Skeid/Knarr code. New code should construct Langertha::Usage,
19 # Langertha::Pricing, and Langertha::UsageRecord directly.
20  
21 sub normalize_usage {
22 3 ▶ my ($class, $usage) = @_;
Subroutine normalize_usage: called CC=1 SCAR=0.0
Pod undocumented
23 3 return Langertha::Usage->from_hash($usage)->to_hash;
24 }
25  
26 sub usage_from_response {
27 0 ▶ my ($class, $response) = @_;
Subroutine usage_from_response: not called CC=1 SCAR=6.9
Pod undocumented
28 0 return Langertha::Usage->from_response($response)->to_hash;
29 }
30  
31 sub normalize_tool_metrics {
32 3 ▶ my ($class, $tool_calls) = @_;
Subroutine normalize_tool_metrics: called CC=8 SCAR=20.9
Pod undocumented
33 3 my @names;
34 3 ▶ for my $tc ( @{ $tool_calls || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$tool_calls or [] 0 3 3
35 4 ▶ next unless ref($tc) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tc eq "HASH" 0 4 4
36 4 my $name = $tc->{name};
37 4 ▶ if ( !defined $name && ref( $tc->{function} ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $name and ref $$tc{"function"} eq "HASH") 1 3 4
Condition100%3/3
not defined $name and ref $$tc{"function"} eq "HASH"
!ll&&!rl&&r
211
MC/DC100%2/2
not defined $name and ref $$tc{"function"} eq "HASH"
not defined $name ref $$tc{"function"} eq "HASH"
Truth table100%3/3
not(A) and B
A= not defined $nameB= ref $$tc{"function"} eq "HASH"
ABresult
0X0
100
111
38 1 $name = $tc->{function}{name};
39 }
40 4 ▶ next unless defined $name && length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 1 3 4
Condition66%2/3
defined $name and length $name
!ll&&!rl&&r
103
MC/DC50%1/2
defined $name and length $name
defined $name length $name
Truth table66%2/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
41 3 push @names, $name;
42 }
43 return {
44 3 tool_calls => scalar(@names),
45 tool_names => \@names,
46 };
47 }
48  
49 sub estimate_cost_usd {
50 1 ▶ my ($class, %args) = @_;
Subroutine estimate_cost_usd: called CC=3 SCAR=11.2
Pod undocumented
51 1 ▶ my $usage = Langertha::Usage->from_hash( $args{usage} || {} );
Condition50%1/2
$args{'usage'} || {}
l!l
10
MC/DC0%0/1
$args{'usage'} || {}
$args{'usage'}
Truth table50%1/2
A
A= $args{'usage'}
Aresult
11
00
52 1 ▶ my $rule = $args{pricing} || {};
Condition50%1/2
$args{'pricing'} || {}
l!l
10
MC/DC0%0/1
$args{'pricing'} || {}
$args{'pricing'}
Truth table50%1/2
A
A= $args{'pricing'}
Aresult
11
00
53 1 my $pricing = Langertha::Pricing->new( default_rule => $rule );
54 1 my $cost = $pricing->cost_for( $usage, undef );
55 1 return $cost->to_hash;
56 }
57  
58 sub build_record {
59 2 ▶ my ($class, %args) = @_;
Subroutine build_record: called CC=7 SCAR=23.4
Pod undocumented
60 my $usage = Langertha::Usage->from_hash(
61 $args{usage}
62 || ( ( $args{response} && ref( $args{response} ) eq 'HASH' ) ? $args{response}{usage} : {} )
63 2 ▶ || {}
Branch: 1 branch
BranchTrueFalseTotal
$args{'response'} && ref $args{'response'} eq 'HASH' ? : 0 0 0
Condition25%2/8
$args{'usage'} || ($args{'response'} && ref $args{'response'} eq 'HASH' ? $args{'response'}{'usage'} : {}) || {}
l!l
20
$args{"usage"} or $args{"response"} && ref $args{"response"} eq "HASH" ? $args{"response"}{"usage"} : {}
l!l&&r!l&&!r
200
$args{"response"} and ref $args{"response"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
$args{'usage'} || ($args{'response'} && ref $args{'response'} eq 'HASH' ? $args{'response'}{'usage'} : {}) || {}
$args{"usage"} $args{"response"} ref $args{"response"} eq "HASH"
Truth table25%1/4
A or B and C
A= $args{"usage"}B= $args{"response"}C= ref $args{"response"} eq "HASH"
ABCresult
1XX1
0111
00X0
0100
64 );
65 2 ▶ my $tool = $class->normalize_tool_metrics( $args{tool_calls} || [] );
Condition100%2/2
$args{'tool_calls'} || []
l!l
11
MC/DC100%1/1
$args{'tool_calls'} || []
$args{'tool_calls'}
Truth table100%2/2
A
A= $args{'tool_calls'}
Aresult
11
00
66  
67 2 ▶ my $pricing_rule = $args{pricing} || {};
Condition50%1/2
$args{'pricing'} || {}
l!l
20
MC/DC0%0/1
$args{'pricing'} || {}
$args{'pricing'}
Truth table50%1/2
A
A= $args{'pricing'}
Aresult
11
00
68 2 my $pricing = Langertha::Pricing->new( default_rule => $pricing_rule );
69 2 my $cost = $pricing->cost_for( $usage, $args{model} );
70  
71 my $record = Langertha::UsageRecord->new(
72 usage => $usage,
73 cost => $cost,
74 provider => $args{provider},
75 engine => $args{engine},
76 model => $args{model},
77 route => $args{route},
78 api_key_id => $args{api_key_id},
79 duration_ms => $args{duration_ms},
80 started_at => $args{started_at},
81 finished_at => $args{finished_at},
82 tool_calls => $tool->{tool_calls},
83 tool_names => $tool->{tool_names},
84 pricing_version => $args{pricing_version},
85 2 );
86 2 return $record->to_hash;
87 }
88  
89 3 1;
90  
91 __END__
92  
93 =pod
94  
95 =encoding UTF-8
96  
97 =head1 NAME
98  
99 Langertha::Metrics - Backwards-compat facade over Langertha::Usage / Pricing / Cost / UsageRecord
100  
101 =head1 VERSION
102  
103 version 0.502
104  
105 =head1 SUPPORT
106  
107 =head2 Issues
108  
109 Please report bugs and feature requests on GitHub at
110 L<https://github.com/Getty/langertha/issues>.
111  
112 =head2 IRC
113  
114 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
115  
116 =head1 CONTRIBUTING
117  
118 Contributions are welcome! Please fork the repository and submit a pull request.
119  
120 =head1 AUTHOR
121  
122 Torsten Raudssus <getty@cpan.org>
123  
124 =head1 COPYRIGHT AND LICENSE
125  
126 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
127  
128 This is free software; you can redistribute it and/or modify it under
129 the same terms as the Perl 5 programming language system itself.
130  
131 =cut
« Langertha/Role/Transcription.pm ↑ Summary Langertha/Raid/Parallel.pm »