blib/lib/App/RecordStream/Aggregator/PercentileMap.pm

filter: Statementstmt 37.9% 22 / 58 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 42.8% 6 / 14 Podpod 0.0% 0 / 8 totaltotal 33.3% 28 / 84 CC 4 SCAR 13.6
CC
4
Cov
35%
squish
12.0
_make_percentiles
6.0
new
2.0
CRAP 3.9
« blib/lib/App/RecordStream/Operation/substream.pm ↑ Summary blib/lib/App/RecordStream/Operation/frommultire.pm »
1 package App::RecordStream::Aggregator::PercentileMap;
2  
3 6 our $VERSION = "4.0.23";
4  
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 ▶use App::RecordStream::Aggregator::InjectInto::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use App::RecordStream::DomainLanguage::Registry;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶use base qw(App::RecordStream::Aggregator::InjectInto::Field);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub _make_percentiles
14 {
15 0 ▶ my $percentiles = shift;
Subroutine _make_percentiles: not called CC=2 SCAR=17.9
16  
17 0 ▶ if(ref($percentiles) eq "ARRAY")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $percentiles eq "ARRAY") 0 0 0
18 {
19 0 return $percentiles;
20 }
21  
22 # be careful, split(' ', ...) is extreme magic split, not split on one space
23 0 return [split(' ', $percentiles)];
24 }
25  
26 sub new
27 {
28 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
29 0 my $percentiles = shift;
30 0 my $field = shift;
31  
32 0 my $this = $class->SUPER::new($field);
33 0 $this->{'percentiles'} = _make_percentiles($percentiles);
34  
35 0 return $this;
36 }
37  
38 sub new_from_valuation
39 {
40 0 ▶ my $class = shift;
Subroutine new_from_valuation: not called CC=1 SCAR=6.9
Pod undocumented
41 0 my $percentiles = shift;
42 0 my $valuation = shift;
43  
44 0 my $this = $class->SUPER::new_from_valuation($valuation);
45 0 $this->{'percentiles'} = _make_percentiles($percentiles);
46  
47 0 return $this;
48 }
49  
50 sub initial {
51 0 ▶ return [];
Subroutine initial: not called CC=1 SCAR=6.9
Pod undocumented
52 }
53  
54 sub combine_field
55 {
56 0 ▶ my $this = shift;
Subroutine combine_field: not called CC=1 SCAR=6.9
Pod undocumented
57 0 my $cookie = shift;
58 0 my $value = shift;
59  
60 0 push @$cookie, $value;
61 0 return $cookie;
62 }
63  
64 sub squish
65 {
66 0 ▶ my $this = shift;
Subroutine squish: not called CC=3 SCAR=24.8
Pod undocumented
67 0 my $cookie = shift;
68  
69 0 my @sorted = sort { $a <=> $b } @$cookie;
70  
71 0 my %ret;
72  
73 0 for my $percentile (@{$this->{'percentiles'}})
74 {
75 0 my $index = int((scalar @sorted) * ($percentile / 100));
76  
77 0 ▶ if($index == scalar(@sorted))
Branch: 1 branch
BranchTrueFalseTotal
if ($index == scalar @sorted) 0 0 0
78 {
79 0 $index--;
80 }
81  
82 0 $ret{$percentile} = $sorted[$index];
83 }
84  
85 0 return \%ret;
86 }
87  
88 sub short_usage
89 {
90 1 ▶ return "map of percentile values for field";
Subroutine short_usage: called CC=1 SCAR=0.0
Pod undocumented
91 }
92  
93 sub long_usage
94 {
95 0 ▶ print <<EOF;
Subroutine long_usage: not called CC=1 SCAR=6.9
Pod undocumented
96 Usage: percmap,<percentiles>,<field>
97 Finds the field values which <percentiles> percent of values are less than.
98  
99 This is computed by creating an array of all values, sorting, and indexing
100 into it at the floor((percentile / 100) * length) point
101  
102 <percentiles> will be perl split to determine percentiles to compute.
103  
104 Output is a hash whose keys are percentiles and whose values are
105 corresponding field values.
106 EOF
107 }
108  
109 sub argct
110 {
111 0 ▶ return 2;
Subroutine argct: not called CC=1 SCAR=6.9
Pod undocumented
112 }
113  
114 6 App::RecordStream::Aggregator->register_implementation('percentilemap', __PACKAGE__);
115 6 App::RecordStream::Aggregator->register_implementation('percmap', __PACKAGE__);
116  
117 6 App::RecordStream::DomainLanguage::Registry::register_vfn(__PACKAGE__, 'new_from_valuation', 'percentilemap', 'SCALAR', 'VALUATION');
118 6 App::RecordStream::DomainLanguage::Registry::register_vfn(__PACKAGE__, 'new_from_valuation', 'percmap', 'SCALAR', 'VALUATION');
119  
120 6 1;
« blib/lib/App/RecordStream/Operation/substream.pm ↑ Summary blib/lib/App/RecordStream/Operation/frommultire.pm »