File Coverage

blib/lib/Net/PulseMeter/Sensor/Timeline.pm
Criterion Covered Total %
statement 34 34 100.0
branch n/a
condition 2 3 66.6
subroutine 13 13 100.0
pod 0 9 0.0
total 49 59 83.0


line stmt bran cond sub pod time code
1             package Net::PulseMeter::Sensor::Timeline;
2 12     12   925 use strict;
  12         69  
  12         408  
3 12     12   61 use warnings 'all';
  12         23  
  12         450  
4              
5 12     12   53 use base qw/Net::PulseMeter::Sensor::Base/;
  12         25  
  12         1268  
6              
7 12         5305 use constant DEFAULTS => {
8             raw_data_ttl => 3600,
9             interval => 60,
10 12     12   66 };
  12         34  
11              
12             sub init {
13 14     14 0 30 my $self = shift;
14 14         26 my $name = shift;
15 14         125 $self->SUPER::init($name, @_);
16 14         29 my $opts = {@_};
17 14   66     208 $self->{$_} = $opts->{$_} || DEFAULTS->{$_} for qw/raw_data_ttl interval/;
18             }
19              
20 29     29 0 2139 sub raw_data_ttl { shift->{raw_data_ttl} }
21 90     90 0 2127 sub interval { shift->{interval} }
22              
23             sub event {
24 26     26 0 3389 my ($self, $value) = @_;
25 26         161 $self->r->multi;
26 26         257 my $current_key = $self->current_raw_data_key;
27 26         132 $self->aggregate_event($current_key, $value);
28 26         47137 $self->r->expire($current_key, $self->raw_data_ttl);
29 26         4762 $self->r->exec;
30             }
31              
32             sub raw_data_key {
33 41     41 0 1611 my ($self, $id) = @_;
34 41         214 sprintf("pulse_meter:raw:%s:%s", $self->name, $id);
35             }
36              
37             sub get_interval_id {
38 44     44 0 1762 my ($self, $time) = @_;
39 44         158 int($time / $self->interval) * $self->interval;
40             }
41              
42 42     42 0 1746 sub current_interval_id { shift->get_interval_id(time) }
43              
44             sub current_raw_data_key {
45 40     40 0 2292 my $self = shift;
46 40         152 $self->raw_data_key($self->current_interval_id);
47             }
48              
49             sub aggregate_event {
50 1     1 0 2 my ($self, $key, $value) = @_;
51 1         3 $self->r->set($key, $value);
52             }
53              
54             1;