line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package Time::Spent; |
3
|
|
|
|
|
|
|
# ABSTRACT: Track events and calculate a rolling average of time, er, spent |
4
|
|
|
|
|
|
|
$Time::Spent::VERSION = '0.01'; |
5
|
1
|
|
|
1
|
|
535
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
24
|
|
6
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
22
|
|
7
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
49
|
|
8
|
1
|
|
|
1
|
|
359
|
use Time::HiRes qw( ); |
|
1
|
|
|
|
|
1068
|
|
|
1
|
|
|
|
|
24
|
|
9
|
1
|
|
|
1
|
|
5
|
use List::Util qw( ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
443
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
7
|
sub ts { Time::HiRes::time } |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub param (\%$;$) { |
14
|
7
|
|
|
7
|
0
|
12
|
my ( $param, $name, $default ) = @_; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
croak "expected parameter '$name'" |
17
|
7
|
50
|
66
|
|
|
184
|
if !exists $param->{ $name } |
18
|
|
|
|
|
|
|
&& !defined $default; |
19
|
|
|
|
|
|
|
|
20
|
6
|
50
|
|
|
|
12
|
return $default unless exists $param->{ $name }; |
21
|
6
|
|
|
|
|
12
|
return $param->{ $name }; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub new { |
26
|
7
|
|
|
7
|
1
|
9235
|
my ( $class, %param ) = @_; |
27
|
7
|
|
|
|
|
19
|
my $length = param %param, 'length', undef; |
28
|
|
|
|
|
|
|
|
29
|
6
|
100
|
100
|
|
|
249
|
croak "parameter 'length' expected a positive integer" |
30
|
|
|
|
|
|
|
unless $length =~ /^\d*$/ |
31
|
|
|
|
|
|
|
&& $length > 0; |
32
|
|
|
|
|
|
|
|
33
|
3
|
|
|
|
|
13
|
my $self = { |
34
|
|
|
|
|
|
|
length => $length, |
35
|
|
|
|
|
|
|
count => 0, |
36
|
|
|
|
|
|
|
tracked => { }, |
37
|
|
|
|
|
|
|
history => [ ], |
38
|
|
|
|
|
|
|
average => undef, |
39
|
|
|
|
|
|
|
}; |
40
|
|
|
|
|
|
|
|
41
|
3
|
|
|
|
|
11
|
return bless $self, $class; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub start { |
46
|
7
|
|
|
7
|
1
|
681
|
my $self = shift; |
47
|
|
|
|
|
|
|
|
48
|
7
|
100
|
|
|
|
141
|
croak 'expected one or more identifiers' |
49
|
|
|
|
|
|
|
unless @_; |
50
|
|
|
|
|
|
|
|
51
|
6
|
|
|
|
|
11
|
foreach ( @_ ) { |
52
|
8
|
100
|
|
|
|
18
|
croak "already tracking $_" |
53
|
|
|
|
|
|
|
if $self->is_tracking( $_ ) |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
5
|
|
|
|
|
12
|
my $ts = ts; |
57
|
5
|
|
|
|
|
22
|
$self->{ tracked }{ $_ } = $ts foreach @_; |
58
|
|
|
|
|
|
|
|
59
|
5
|
|
|
|
|
24
|
return @_; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub stop { |
64
|
6
|
|
|
6
|
1
|
635
|
my $self = shift; |
65
|
|
|
|
|
|
|
|
66
|
6
|
100
|
|
|
|
99
|
croak 'expected one or more identifiers' |
67
|
|
|
|
|
|
|
unless @_; |
68
|
|
|
|
|
|
|
|
69
|
5
|
|
|
|
|
10
|
foreach ( @_ ) { |
70
|
5
|
100
|
|
|
|
14
|
croak "not tracking $_" |
71
|
|
|
|
|
|
|
unless $self->is_tracking( $_ ) |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
4
|
|
|
|
|
13
|
my $ts = ts; |
75
|
|
|
|
|
|
|
|
76
|
4
|
|
|
|
|
14
|
foreach ( @_ ) { |
77
|
4
|
|
|
|
|
11
|
my $spent = $ts - $self->{ tracked }{ $_ }; |
78
|
4
|
|
|
|
|
8
|
delete $self->{ tracked }{ $_ }; |
79
|
|
|
|
|
|
|
|
80
|
4
|
|
|
|
|
6
|
push @{ $self->{ history } }, $spent; |
|
4
|
|
|
|
|
10
|
|
81
|
4
|
|
|
|
|
12
|
++$self->{ count }; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
4
|
|
|
|
|
13
|
while ( $self->{ count } > $self->{ length } ) { |
85
|
1
|
|
|
|
|
2
|
shift @{ $self->{ history } }; |
|
1
|
|
|
|
|
3
|
|
86
|
1
|
|
|
|
|
4
|
--$self->{ count }; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
4
|
|
|
|
|
8
|
$self->{ average } = List::Util::sum0( @{ $self->{ history } } ) / $self->{ count }; |
|
4
|
|
|
|
|
25
|
|
90
|
|
|
|
|
|
|
|
91
|
4
|
|
|
|
|
17
|
return @_; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
5
|
|
|
5
|
1
|
420
|
sub avg { $_[ 0 ]->{ average } } |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
25
|
100
|
|
25
|
1
|
512
|
sub is_tracking { exists $_[ 0 ]->{ tracked }{ $_[ 1 ] } ? 1 : 0 } |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
1; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
__END__ |