lib/WWW/Hetzner/Cloud/Costs.pm

filter: Statementstmt 100% 86 / 86 Branchbran 82.1% 23 / 28 Conditioncond 59.0% 26 / 44 MC/DCmcdc 28.0% 7 / 25 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 78.7% 152 / 193 CC 41 SCAR 29.0
CC
41
Cov
85%
calculate
29.4
__ANON__
7.3
__ANON__
5.1
CRAP 18.2
« lib/WWW/Hetzner/CLI/Cmd/PrimaryIp/Cmd/Describe.pm ↑ Summary lib/WWW/Hetzner/CLI/Cmd/Network/Cmd/Describe.pm »
1 package WWW::Hetzner::Cloud::Costs;
2 # ABSTRACT: Current running cost of a Hetzner Cloud project
3  
4 60 our $VERSION = '0.102';
5  
6 60 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 60 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 60 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 60 has client => (
12 is => 'ro',
13 required => 1,
14 weak_ref => 1,
15 );
16  
17  
18 # Hours after which hourly billing reaches the monthly cap; the ratio between
19 # price_monthly and price_hourly for every item that has both.
20 60 our $HOURS_PER_MONTH = 624;
21  
22 13 ▶sub calculate {
Subroutine calculate: called CC=27 SCAR=33.8
Pod documented
23 13 my ($self, %opts) = @_;
24  
25 13 ▶ my $price = $opts{price} // 'gross';
Condition100%2/2
$opts{"price"} // "gross"
l!l
58
MC/DC100%1/1
$opts{"price"} // "gross"
$opts{"price"}
Truth table100%2/2
A
A= $opts{"price"}
Aresult
11
00
26 13 ▶ croak "price must be 'net' or 'gross', not '$price'"
Branch: 1 branch
BranchTrueFalseTotal
unless $price eq "net" or $price eq "gross" 1 12 13
Condition100%3/3
$price eq "net" or $price eq "gross"
l!l&&r!l&&!r
481
MC/DC100%2/2
$price eq "net" or $price eq "gross"
$price eq "net" $price eq "gross"
Truth table100%3/3
A or B
A= $price eq "net"B= $price eq "gross"
ABresult
1X1
011
000
27 unless $price eq 'net' || $price eq 'gross';
28  
29 12 my %filter;
30 $filter{label_selector} = $opts{label_selector}
31 12 ▶ if defined $opts{label_selector} && length $opts{label_selector};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"label_selector"} and length $opts{"label_selector"} 2 10 12
Condition66%2/3
defined $opts{"label_selector"} and length $opts{"label_selector"}
!ll&&!rl&&r
1002
MC/DC50%1/2
defined $opts{"label_selector"} and length $opts{"label_selector"}
defined $opts{"label_selector"} length $opts{"label_selector"}
Truth table66%2/3
A and B
A= defined $opts{"label_selector"}B= length $opts{"label_selector"}
ABresult
0X0
100
111
32  
33 12 my $client = $self->client;
34 12 my $pricing = $client->pricing->get;
35  
36 204 ▶ my $amount = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
37 204 my ($p) = @_;
38 204 ▶ return undef unless ref $p eq 'HASH' && defined $p->{$price};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $p eq "HASH" and defined $p->{$price} 24 180 204
Condition66%2/3
ref $p eq "HASH" and defined $p->{$price}
!ll&&!rl&&r
240180
MC/DC50%1/2
ref $p eq "HASH" and defined $p->{$price}
ref $p eq "HASH" defined $p->{$price}
Truth table66%2/3
A and B
A= ref $p eq "HASH"B= defined $p->{$price}
ABresult
0X0
100
111
39 180 return 0 + $p->{$price};
40 12 };
41  
42 48 ▶ my $by_location = sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
43 48 my ($entries, $key) = @_;
44 48 my %index;
45 48 ▶ for my $entry (@{ $entries // [] }) {
Condition50%1/2
$entries // []
l!l
480
MC/DC0%0/1
$entries // []
$entries
Truth table50%1/2
A
A= $entries
Aresult
11
00
46 72 ▶ for my $p (@{ $entry->{prices} // [] }) {
Condition50%1/2
$entry->{"prices"} // []
l!l
720
MC/DC0%0/1
$entry->{"prices"} // []
$entry->{"prices"}
Truth table50%1/2
A
A= $entry->{"prices"}
Aresult
11
00
47 $index{ $entry->{$key} }{ $p->{location} } = {
48 hourly => $amount->($p->{price_hourly}),
49 96 monthly => $amount->($p->{price_monthly}),
50 };
51 }
52 }
53 48 return \%index;
54 12 };
55  
56 12 my $server_prices = $by_location->($pricing->server_types, 'name');
57 12 my $lb_prices = $by_location->($pricing->load_balancer_types, 'name');
58 12 my $pip_prices = $by_location->($pricing->primary_ips, 'type');
59 12 my $fip_prices = $by_location->($pricing->floating_ips, 'type');
60 12 ▶ my $volume_gb = $amount->(($pricing->volume // {})->{price_per_gb_month});
Condition50%1/2
$pricing->volume // {}
l!l
120
MC/DC0%0/1
$pricing->volume // {}
$pricing->volume
Truth table50%1/2
A
A= $pricing->volume
Aresult
11
00
61 12 ▶ my $backup_pct = ($pricing->server_backup // {})->{percentage};
Condition50%1/2
$pricing->server_backup // {}
l!l
120
MC/DC0%0/1
$pricing->server_backup // {}
$pricing->server_backup
Truth table50%1/2
A
A= $pricing->server_backup
Aresult
11
00
62  
63 12 my (@rows, @warnings);
64  
65 101 ▶ my $add = sub {
Subroutine __ANON__: called CC=7 SCAR=19.9
66 101 my (%row) = @_;
67 101 my $p = delete $row{_price};
68 101 ▶ if (!defined $p || !defined $p->{monthly}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $p && defined $p->{'monthly'}) { } 1 100 101
Condition66%2/3
defined $p && defined $p->{'monthly'}
!ll&&!rl&&r
10100
MC/DC50%1/2
defined $p && defined $p->{'monthly'}
defined $p defined $p->{'monthly'}
Truth table66%2/3
A && B
A= defined $pB= defined $p->{'monthly'}
ABresult
0X0
100
111
69 push @warnings, sprintf "no %s price for %s %s (%s) in %s",
70 $price, $row{kind}, $row{name} // $row{id},
71 1 ▶ $row{type} // '-', $row{location} // '-';
Condition42%3/7
$row{"name"} // $row{"id"}
l!l&&r!l&&!r
100
$row{"type"} // "-"
l!l
10
$row{"location"} // "-"
l!l
10
MC/DC0%0/2
$row{"name"} // $row{"id"}
$row{"name"} $row{"id"}
MC/DC0%0/1
$row{"type"} // "-"
$row{"type"}
MC/DC0%0/1
$row{"location"} // "-"
$row{"location"}
Truth table33%1/3
A // B
A= $row{"name"}B= $row{"id"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $row{"type"}
Aresult
11
00
Truth table50%1/2
A
A= $row{"location"}
Aresult
11
00
72 1 $row{hourly} = $row{monthly} = undef;
73 }
74 else {
75 100 $row{monthly} = _round($p->{monthly});
76 100 ▶ if (defined $p->{hourly}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p->{'hourly'}) { } 80 20 100
77 80 $row{hourly} = _round($p->{hourly});
78 80 $row{hourly_derived} = 0;
79 }
80 else {
81 20 $row{hourly} = _round($p->{monthly} / $HOURS_PER_MONTH);
82 20 $row{hourly_derived} = 1;
83 }
84 }
85 101 push @rows, \%row;
86 12 };
87  
88 12 for my $s (@{ $client->servers->list_all(%filter) }) {
89 31 my %base = (
90 id => $s->id,
91 name => $s->name,
92 type => $s->server_type,
93 location => $s->location,
94 status => $s->status,
95 );
96 31 my $p = _lookup($server_prices, $base{type}, $base{location});
97 31 $add->(kind => 'server', %base, _price => $p);
98  
99 31 ▶ if (defined $s->backup_window && $p && defined $backup_pct) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->backup_window and $p and defined $backup_pct) 10 21 31
Condition66%4/6
defined $s->backup_window and $p and defined $backup_pct
!ll&&!rl&&r
21010
defined $s->backup_window and $p
!ll&&!rl&&r
21010
MC/DC33%1/3
defined $s->backup_window and $p and defined $backup_pct
defined $s->backup_window $p defined $backup_pct
Truth table50%2/4
A and B and C
A= defined $s->backup_windowB= $pC= defined $backup_pct
ABCresult
0XX0
10X0
1100
1111
100 10 my $f = $backup_pct / 100;
101 $add->(kind => 'server_backup', %base, _price => {
102 monthly => defined $p->{monthly} ? $p->{monthly} * $f : undef,
103 10 ▶ hourly => defined $p->{hourly} ? $p->{hourly} * $f : undef,
Branch: 2 branches
BranchTrueFalseTotal
defined $p->{'monthly'} ? : 10 0 10
defined $p->{'hourly'} ? : 10 0 10
104 });
105 }
106 }
107  
108 12 for my $v (@{ $client->volumes->list_all(%filter) }) {
109 10 ▶ $add->(
Branch: 1 branch
BranchTrueFalseTotal
defined $volume_gb ? : 10 0 10
Condition50%2/4
$v->size // 0
l!l
100
$v->size // 0
l!l
100
MC/DC0%0/1
$v->size // 0
$v->size
MC/DC0%0/1
$v->size // 0
$v->size
Truth table50%1/2
A
A= $v->size
Aresult
11
00
Truth table50%1/2
A
A= $v->size
Aresult
11
00
110 kind => 'volume',
111 id => $v->id,
112 name => $v->name,
113 type => ($v->size // 0) . ' GB',
114 location => $v->location,
115 status => $v->status,
116 size => $v->size,
117 _price => defined $volume_gb
118 ? { monthly => ($v->size // 0) * $volume_gb } : undef,
119 );
120 }
121  
122 12 for my $ip (@{ $client->primary_ips->list_all(%filter) }) {
123 30 my $type = $ip->type;
124 30 ▶ my $p = exists $pip_prices->{$type // ''}
Branch: 1 branch
BranchTrueFalseTotal
exists $$pip_prices{$type // ''} ? : 20 10 30
Condition50%1/2
$type // ""
l!l
300
MC/DC0%0/1
$type // ""
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
125 ? _lookup($pip_prices, $type, $ip->location)
126 : { hourly => 0, monthly => 0 }; # unpriced type, e.g. ipv6: free
127 30 ▶ $add->(
Branch: 1 branch
BranchTrueFalseTotal
$ip->is_assigned ? : 20 10 30
128 kind => 'primary_ip',
129 id => $ip->id,
130 name => $ip->name,
131 type => $type,
132 location => $ip->location,
133 status => $ip->is_assigned ? 'assigned' : 'unassigned',
134 _price => $p,
135 );
136 }
137  
138 12 for my $ip (@{ $client->floating_ips->list_all(%filter) }) {
139 10 ▶ $add->(
Branch: 1 branch
BranchTrueFalseTotal
$ip->is_assigned ? : 10 0 10
140 kind => 'floating_ip',
141 id => $ip->id,
142 name => $ip->name,
143 type => $ip->type,
144 location => $ip->location,
145 status => $ip->is_assigned ? 'assigned' : 'unassigned',
146 _price => _lookup($fip_prices, $ip->type, $ip->location),
147 );
148 }
149  
150 12 for my $lb (@{ $client->load_balancers->list_all(%filter) }) {
151 10 $add->(
152 kind => 'load_balancer',
153 id => $lb->id,
154 name => $lb->name,
155 type => $lb->type_name,
156 location => $lb->location_name,
157 _price => _lookup($lb_prices, $lb->type_name, $lb->location_name),
158 );
159 }
160  
161 12 my ($hourly, $monthly) = (0, 0);
162 12 for my $r (@rows) {
163 101 ▶ next unless defined $r->{monthly};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r->{"monthly"} 1 100 101
164 100 $hourly += $r->{hourly};
165 100 $monthly += $r->{monthly};
166 }
167  
168 return {
169 currency => $pricing->currency,
170 vat_rate => $pricing->vat_rate,
171 price => $price,
172 label_selector => $filter{label_selector},
173 12 hours_per_month => $HOURS_PER_MONTH,
174 resources => \@rows,
175 totals => {
176 hourly => _round($hourly),
177 monthly => _round($monthly),
178 },
179 warnings => \@warnings,
180 };
181 }
182  
183  
184 71 ▶sub _lookup {
Subroutine _lookup: called CC=3 SCAR=12.5
185 71 my ($index, $name, $location) = @_;
186 71 ▶ return undef unless defined $name && defined $location;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and defined $location 0 71 71
Condition33%1/3
defined $name and defined $location
!ll&&!rl&&r
0071
MC/DC0%0/2
defined $name and defined $location
defined $name defined $location
Truth table33%1/3
A and B
A= defined $nameB= defined $location
ABresult
0X0
100
111
187 71 return $index->{$name}{$location};
188 }
189  
190 224 ▶sub _round {
Subroutine _round: called CC=1 SCAR=0.0
191 224 my ($n) = @_;
192 224 return 0 + sprintf('%.6f', $n);
193 }
194  
195  
196 60 1;
197  
198 __END__
« lib/WWW/Hetzner/CLI/Cmd/PrimaryIp/Cmd/Describe.pm ↑ Summary lib/WWW/Hetzner/CLI/Cmd/Network/Cmd/Describe.pm »