lib/WWW/Hetzner/CLI/Cmd/Costs.pm

filter: Statementstmt 100% 42 / 42 Branchbran 77.7% 14 / 18 Conditioncond 62.5% 10 / 16 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 1 totaltotal 82.2% 74 / 90 CC 19 SCAR 28.5
CC
19
Cov
87%
execute
20.8
BEGIN
1.0
BEGIN
1.0
CRAP 17.3
« lib/WWW/Hetzner/CLI/Cmd/Network/Cmd/Describe.pm ↑ Summary lib/WWW/Hetzner/CLI/Cmd/FloatingIp/Cmd/Describe.pm »
1 package WWW::Hetzner::CLI::Cmd::Costs;
2 # ABSTRACT: Show the current running cost of the project
3  
4 1 our $VERSION = '0.102';
5  
6 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use MooX::Cmd;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use MooX::Options usage_string => 'USAGE: hcloud.pl costs [--label SELECTOR] [--net] [--json]';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON::MaybeXS qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 1 option label => (
13 is => 'ro',
14 format => 's',
15 short => 'l',
16 doc => 'Label selector applied to every resource list (e.g., env=prod)',
17 );
18  
19  
20 1 option net => (
21 is => 'ro',
22 doc => 'Use net prices instead of gross',
23 );
24  
25  
26 1 option json => (
27 is => 'ro',
28 doc => 'Print the result as JSON',
29 );
30  
31  
32 5 ▶sub execute {
Subroutine execute: called CC=19 SCAR=30.3
Pod undocumented
33 5 my ($self, $args, $chain) = @_;
34  
35 5 my $main = $chain->[0];
36 5 my $cloud = $main->cloud;
37  
38 5 my %params;
39 5 ▶ $params{label_selector} = $self->label if $self->label;
Branch: 1 branch
BranchTrueFalseTotal
if $self->label 1 4 5
40 5 ▶ $params{price} = 'net' if $self->net;
Branch: 1 branch
BranchTrueFalseTotal
if $self->net 2 3 5
41  
42 5 my $costs = $cloud->costs(%params);
43  
44 5 ▶ if ($self->json || $main->output eq 'json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->json or $main->output eq "json") 2 3 5
Condition100%3/3
$self->json or $main->output eq "json"
l!l&&r!l&&!r
113
MC/DC100%2/2
$self->json or $main->output eq "json"
$self->json $main->output eq "json"
Truth table100%3/3
A or B
A= $self->jsonB= $main->output eq "json"
ABresult
1X1
011
000
45 2 print encode_json($costs), "\n";
46 2 return;
47 }
48  
49 3 ▶ my $cur = $costs->{currency} // 'EUR';
Condition50%1/2
$costs->{"currency"} // "EUR"
l!l
30
MC/DC0%0/1
$costs->{"currency"} // "EUR"
$costs->{"currency"}
Truth table50%1/2
A
A= $costs->{"currency"}
Aresult
11
00
50 printf "Prices: %s, %s (VAT %s%%)%s\n",
51 $costs->{price}, $cur,
52 defined $costs->{vat_rate} ? 0 + $costs->{vat_rate} : '-',
53 defined $costs->{label_selector}
54 3 ▶ ? ", label selector: $costs->{label_selector}" : '';
Branch: 2 branches
BranchTrueFalseTotal
defined $costs->{'vat_rate'} ? : 3 0 3
defined $costs->{'label_selector'} ? : 1 2 3
55  
56 3 my $fmt = "%-25s %-14s %-10s %-9s %-11s %11s %12s\n";
57 3 printf $fmt, 'NAME', 'KIND', 'TYPE', 'LOCATION', 'STATUS',
58 "$cur/h", "$cur/month";
59 3 print '-' x 98, "\n";
60  
61 3 for my $r (@{ $costs->{resources} }) {
62 printf $fmt,
63 $r->{name} // $r->{id} // '-',
64 $r->{kind},
65 $r->{type} // '-',
66 $r->{location} // '-',
67 $r->{status} // '-',
68 defined $r->{hourly}
69 ? sprintf('%.4f%s', $r->{hourly}, $r->{hourly_derived} ? '*' : ' ')
70 : '? ',
71 30 ▶ defined $r->{monthly} ? sprintf('%.2f', $r->{monthly}) : '?';
Branch: 3 branches
BranchTrueFalseTotal
defined $r->{'hourly'} ? : 30 0 30
$r->{'hourly_derived'} ? : 6 24 30
defined $r->{'monthly'} ? : 30 0 30
Condition54%6/11
($r->{"name"} // $r->{"id"}) // "-"
l!l
300
$r->{"name"} // $r->{"id"}
l!l&&r!l&&!r
3000
$r->{"type"} // "-"
l!l
300
$r->{"location"} // "-"
l!l
300
$r->{"status"} // "-"
l!l
273
MC/DC0%0/2
($r->{"name"} // $r->{"id"}) // "-"
$r->{"name"} $r->{"id"}
MC/DC0%0/1
$r->{"type"} // "-"
$r->{"type"}
MC/DC0%0/1
$r->{"location"} // "-"
$r->{"location"}
MC/DC100%1/1
$r->{"status"} // "-"
$r->{"status"}
Truth table33%1/3
A // B
A= $r->{"name"}B= $r->{"id"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $r->{"type"}
Aresult
11
00
Truth table50%1/2
A
A= $r->{"location"}
Aresult
11
00
Truth table100%2/2
A
A= $r->{"status"}
Aresult
11
00
72 }
73  
74 3 print '-' x 98, "\n";
75 printf $fmt, 'TOTAL', '', '', '', '',
76 sprintf('%.4f ', $costs->{totals}{hourly}),
77 3 sprintf('%.2f', $costs->{totals}{monthly});
78  
79 print "\n* hourly derived as monthly / $costs->{hours_per_month}h (no hourly price in the price list)\n"
80 3 ▶ if grep { $_->{hourly_derived} } @{ $costs->{resources} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->{"hourly_derived"};} @{$$costs{"resources"};} 3 0 3
81 3 print STDERR "Warning: $_\n" for @{ $costs->{warnings} };
82 }
83  
84 1 1;
85  
86 __END__
« lib/WWW/Hetzner/CLI/Cmd/Network/Cmd/Describe.pm ↑ Summary lib/WWW/Hetzner/CLI/Cmd/FloatingIp/Cmd/Describe.pm »