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.25";
4              
5 6     6   4475 use strict;
  6         15  
  6         158  
6 6     6   33 use warnings;
  6         14  
  6         147  
7              
8 6     6   400 use App::RecordStream::Aggregator::InjectInto::Field;
  6         11  
  6         150  
9 6     6   46 use App::RecordStream::DomainLanguage::Registry;
  6         15  
  6         136  
10              
11 6     6   25 use base qw(App::RecordStream::Aggregator::InjectInto::Field);
  6         22  
  6         2696  
12              
13             sub new
14             {
15 2     2 0 89 my $class = shift;
16 2         3 my $percentile = shift;
17 2         5 my $field = shift;
18              
19 2         14 my $this = $class->SUPER::new($field);
20 2         14 $this->{'percentile'} = $percentile;
21              
22 2         20 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 6 return [];
39             }
40              
41             sub combine_field
42             {
43 200     200 0 276 my $this = shift;
44 200         227 my $cookie = shift;
45 200         231 my $value = shift;
46              
47 200         279 push @$cookie, $value;
48 200         492 return $cookie;
49             }
50              
51             sub squish
52             {
53 2     2 0 10 my $this = shift;
54 2         4 my $cookie = shift;
55              
56 2         5 my $percentile = $this->{'percentile'};
57              
58 2         14 my @sorted = sort { $a <=> $b } @$cookie;
  198         218  
59              
60 2         9 my $index = int( (scalar @sorted) * ($percentile / 100));
61              
62 2 100       16 if ( $index == scalar @sorted )
63             {
64 1         2 $index--;
65             }
66              
67 2         17 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;