| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Data::Decimate; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 33776 | use strict; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 23 |  | 
| 4 | 1 |  |  | 1 |  | 4 | use warnings; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 20 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 1 |  |  | 1 |  | 14 | use 5.010; | 
|  | 1 |  |  |  |  | 2 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 3 | use Exporter qw/import/; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 83 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | our @EXPORT_OK = qw(decimate); | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | =head1 NAME | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | Data::Decimate - A module that allows to decimate a data feed. | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | use Data::Decimate qw(decimate); | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | my @data = ( | 
| 21 |  |  |  |  |  |  | {epoch  => 1479203101, | 
| 22 |  |  |  |  |  |  | #... | 
| 23 |  |  |  |  |  |  | }, | 
| 24 |  |  |  |  |  |  | {epoch  => 1479203102, | 
| 25 |  |  |  |  |  |  | #... | 
| 26 |  |  |  |  |  |  | }, | 
| 27 |  |  |  |  |  |  | {epoch  => 1479203103, | 
| 28 |  |  |  |  |  |  | #... | 
| 29 |  |  |  |  |  |  | }, | 
| 30 |  |  |  |  |  |  | #... | 
| 31 |  |  |  |  |  |  | {epoch  => 1479203114, | 
| 32 |  |  |  |  |  |  | #... | 
| 33 |  |  |  |  |  |  | }, | 
| 34 |  |  |  |  |  |  | {epoch  => 1479203117, | 
| 35 |  |  |  |  |  |  | #... | 
| 36 |  |  |  |  |  |  | }, | 
| 37 |  |  |  |  |  |  | {epoch  => 1479203118, | 
| 38 |  |  |  |  |  |  | #... | 
| 39 |  |  |  |  |  |  | }, | 
| 40 |  |  |  |  |  |  | #... | 
| 41 |  |  |  |  |  |  | ); | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | my $output = Data::Decimate::decimate(15, \@data); | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | #epoch=1479203114 , decimate_epoch=1479203115 | 
| 46 |  |  |  |  |  |  | print $output->[0]->{epoch}; | 
| 47 |  |  |  |  |  |  | print $output->[0]->{decimate_epoch}; | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | A module that allows you to resample a data feed | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | =cut | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | our $VERSION = '0.03'; | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | =head1 SUBROUTINES/METHODS | 
| 58 |  |  |  |  |  |  | =cut | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | =head2 decimate | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | Decimate a given data based on sampling frequency. | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | =cut | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | sub decimate { | 
| 67 | 2 |  |  | 2 | 1 | 19674 | my ($interval, $data) = @_; | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 2 | 50 | 33 |  |  | 27 | if (not defined $interval or not defined $data or ref($data) ne "ARRAY") { | 
|  |  |  | 33 |  |  |  |  | 
| 70 | 0 |  |  |  |  | 0 | die "interval and data are required parameters."; | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 2 |  |  |  |  | 3 | my @res; | 
| 74 | 2 |  |  |  |  | 4 | my $el = $data->[0]; | 
| 75 | 2 |  |  |  |  | 2 | my $decimate_epoch; | 
| 76 | 2 | 50 |  |  |  | 6 | $decimate_epoch = do { | 
| 77 | 1 |  |  | 1 |  | 396 | use integer; | 
|  | 1 |  |  |  |  | 8 |  | 
|  | 1 |  |  |  |  | 4 |  | 
| 78 | 2 |  |  |  |  | 10 | (($el->{epoch} + $interval - 1) / $interval) * $interval; | 
| 79 |  |  |  |  |  |  | } if $data->[0]; | 
| 80 | 2 |  |  |  |  | 4 | $el->{count}          = 1; | 
| 81 | 2 |  |  |  |  | 5 | $el->{decimate_epoch} = $decimate_epoch; | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 2 | 50 |  |  |  | 6 | push @res, $el if $data->[0]; | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 2 |  |  |  |  | 9 | for (my $i = 1; $i < @$data; $i++) { | 
| 86 | 268 |  |  |  |  | 184 | $el             = $data->[$i]; | 
| 87 | 268 |  |  |  |  | 135 | $decimate_epoch = do { | 
| 88 | 1 |  |  | 1 |  | 61 | use integer; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 2 |  | 
| 89 | 268 |  |  |  |  | 245 | (($el->{epoch} + $interval - 1) / $interval) * $interval; | 
| 90 |  |  |  |  |  |  | }; | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | # same decimate_epoch | 
| 93 | 268 | 100 |  |  |  | 306 | if ($decimate_epoch == $res[-1]->{decimate_epoch}) { | 
| 94 | 251 |  |  |  |  | 153 | $res[-1]->{count}++; | 
| 95 | 251 |  |  |  |  | 167 | $el->{decimate_epoch} = $decimate_epoch; | 
| 96 | 251 |  |  |  |  | 224 | $el->{count}          = $res[-1]->{count}; | 
| 97 | 251 |  |  |  |  | 149 | $res[-1]              = $el; | 
| 98 | 251 |  |  |  |  | 311 | next; | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | # fill in the gaps if any | 
| 102 | 17 |  |  |  |  | 29 | while ($res[-1]->{decimate_epoch} + $interval < $decimate_epoch) { | 
| 103 | 1 |  |  |  |  | 1 | my %clone = %{$res[-1]}; | 
|  | 1 |  |  |  |  | 6 |  | 
| 104 | 1 |  |  |  |  | 3 | $clone{count} = 0; | 
| 105 | 1 |  |  |  |  | 2 | $clone{decimate_epoch} += $interval; | 
| 106 | 1 |  |  |  |  | 3 | push @res, \%clone; | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | # and finally add the current element | 
| 110 | 17 |  |  |  |  | 14 | $el->{count}          = 1; | 
| 111 | 17 |  |  |  |  | 24 | $el->{decimate_epoch} = $decimate_epoch; | 
| 112 | 17 |  |  |  |  | 21 | push @res, $el; | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  |  | 
| 115 | 2 |  |  |  |  | 6 | return \@res; | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | =head1 AUTHOR | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | Binary.com, C<< <support at binary.com> >> | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | =head1 BUGS | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | Please report any bugs or feature requests to C<bug-data-resample at rt.cpan.org>, or through | 
| 125 |  |  |  |  |  |  | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Decimate>.  I will be notified, and then you'll | 
| 126 |  |  |  |  |  |  | automatically be notified of progress on your bug as I make changes. | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | =head1 SUPPORT | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | You can find documentation for this module with the perldoc command. | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | perldoc Data::Decimate | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | You can also look for information at: | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | =over 4 | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =item * RT: CPAN's request tracker (report bugs here) | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-Decimate> | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | =item * AnnoCPAN: Annotated CPAN documentation | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | L<http://annocpan.org/dist/Data-Decimate> | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =item * CPAN Ratings | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | L<http://cpanratings.perl.org/d/Data-Decimate> | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | =item * Search CPAN | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | L<http://search.cpan.org/dist/Data-Decimate/> | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | =back | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =head1 ACKNOWLEDGEMENTS | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | =cut | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | 1; |