File Coverage

blib/lib/App/RecordStream/Aggregator/Percentile.pm
Criterion Covered Total %
statement 36 45 80.0
branch 2 2 100.0
condition n/a
subroutine 9 13 69.2
pod 0 8 0.0
total 47 68 69.1


line stmt bran cond sub pod time code
1             package App::RecordStream::Aggregator::Percentile;
2              
3             our $VERSION = "4.0.24";
4              
5 6     6   3071 use strict;
  6         14  
  6         158  
6 6     6   39 use warnings;
  6         14  
  6         142  
7              
8 6     6   269 use App::RecordStream::Aggregator::InjectInto::Field;
  6         13  
  6         134  
9 6     6   32 use App::RecordStream::DomainLanguage::Registry;
  6         13  
  6         141  
10              
11 6     6   29 use base qw(App::RecordStream::Aggregator::InjectInto::Field);
  6         13  
  6         2245  
12              
13             sub new
14             {
15 2     2 0 103 my $class = shift;
16 2         7 my $percentile = shift;
17 2         4 my $field = shift;
18              
19 2         18 my $this = $class->SUPER::new($field);
20 2         11 $this->{'percentile'} = $percentile;
21              
22 2         27 return $this;
23             }
24              
25             sub new_from_valuation
26             {
27 0     0 0 0 my $class = shift;
28 0         0 my $percentile = shift;
29 0         0 my $valuation = shift;
30              
31 0         0 my $this = $class->SUPER::new_from_valuation($valuation);
32 0         0 $this->{'percentile'} = $percentile;
33              
34 0         0 return $this;
35             }
36              
37             sub initial {
38 2     2 0 7 return [];
39             }
40              
41             sub combine_field
42             {
43 200     200 0 340 my $this = shift;
44 200         301 my $cookie = shift;
45 200         302 my $value = shift;
46              
47 200         361 push @$cookie, $value;
48 200         742 return $cookie;
49             }
50              
51             sub squish
52             {
53 2     2 0 17 my $this = shift;
54 2         6 my $cookie = shift;
55              
56 2         7 my $percentile = $this->{'percentile'};
57              
58 2         18 my @sorted = sort { $a <=> $b } @$cookie;
  198         331  
59              
60 2         13 my $index = int( (scalar @sorted) * ($percentile / 100));
61              
62 2 100       10 if ( $index == scalar @sorted )
63             {
64 1         5 $index--;
65             }
66              
67 2         23 return $sorted[$index];
68             }
69              
70             sub short_usage
71             {
72 0     0 0   return "value of pXX for field";
73             }
74              
75             sub long_usage
76             {
77 0     0 0   print <
78             Usage: per,,
79             Finds the field value which percent of values are less than.
80              
81             This is computed by creating an array of all values, sorting, and indexing into it at the
82             floor((percentile / 100) * length) point
83             EOF
84             }
85              
86             sub argct
87             {
88 0     0 0   return 2;
89             }
90              
91             App::RecordStream::Aggregator->register_implementation('percentile', __PACKAGE__);
92             App::RecordStream::Aggregator->register_implementation('perc', __PACKAGE__);
93              
94             App::RecordStream::DomainLanguage::Registry::register_vfn(__PACKAGE__, 'new_from_valuation', 'percentile', 'SCALAR', 'VALUATION');
95             App::RecordStream::DomainLanguage::Registry::register_vfn(__PACKAGE__, 'new_from_valuation', 'perc', 'SCALAR', 'VALUATION');
96              
97             1;