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.23";
4              
5 6     6   3260 use strict;
  6         14  
  6         143  
6 6     6   26 use warnings;
  6         13  
  6         138  
7              
8 6     6   378 use App::RecordStream::Aggregator::InjectInto::Field;
  6         13  
  6         136  
9 6     6   34 use App::RecordStream::DomainLanguage::Registry;
  6         13  
  6         143  
10              
11 6     6   30 use base qw(App::RecordStream::Aggregator::InjectInto::Field);
  6         12  
  6         1930  
12              
13             sub new
14             {
15 2     2 0 16 my $class = shift;
16 2         6 my $percentile = shift;
17 2         3 my $field = shift;
18              
19 2         16 my $this = $class->SUPER::new($field);
20 2         10 $this->{'percentile'} = $percentile;
21              
22 2         11 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 324 my $this = shift;
44 200         316 my $cookie = shift;
45 200         336 my $value = shift;
46              
47 200         346 push @$cookie, $value;
48 200         595 return $cookie;
49             }
50              
51             sub squish
52             {
53 2     2 0 15 my $this = shift;
54 2         4 my $cookie = shift;
55              
56 2         7 my $percentile = $this->{'percentile'};
57              
58 2         17 my @sorted = sort { $a <=> $b } @$cookie;
  198         293  
59              
60 2         11 my $index = int( (scalar @sorted) * ($percentile / 100));
61              
62 2 100       8 if ( $index == scalar @sorted )
63             {
64 1         3 $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;