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.02'; |
5
|
1
|
|
|
1
|
|
526
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
6
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
21
|
|
7
|
1
|
|
|
1
|
|
4
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
47
|
|
8
|
1
|
|
|
1
|
|
292
|
use Time::HiRes qw( ); |
|
1
|
|
|
|
|
919
|
|
|
1
|
|
|
|
|
22
|
|
9
|
1
|
|
|
1
|
|
6
|
use List::Util qw( ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
410
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
6
|
sub ts { Time::HiRes::time } |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub param (\%$;$) { |
14
|
7
|
|
|
7
|
0
|
10
|
my ( $param, $name, $default ) = @_; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
croak "expected parameter '$name'" |
17
|
7
|
50
|
66
|
|
|
160
|
if !exists $param->{ $name } |
18
|
|
|
|
|
|
|
&& !defined $default; |
19
|
|
|
|
|
|
|
|
20
|
6
|
50
|
|
|
|
11
|
return $default unless exists $param->{ $name }; |
21
|
6
|
|
|
|
|
12
|
return $param->{ $name }; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub new { |
26
|
7
|
|
|
7
|
1
|
7956
|
my ( $class, %param ) = @_; |
27
|
7
|
|
|
|
|
18
|
my $length = param %param, 'length', undef; |
28
|
|
|
|
|
|
|
|
29
|
6
|
100
|
100
|
|
|
247
|
croak "parameter 'length' expected a positive integer" |
30
|
|
|
|
|
|
|
unless $length =~ /^\d*$/ |
31
|
|
|
|
|
|
|
&& $length > 0; |
32
|
|
|
|
|
|
|
|
33
|
3
|
|
|
|
|
10
|
my $self = { |
34
|
|
|
|
|
|
|
length => $length, |
35
|
|
|
|
|
|
|
count => 0, |
36
|
|
|
|
|
|
|
tracked => { }, |
37
|
|
|
|
|
|
|
history => [ ], |
38
|
|
|
|
|
|
|
average => undef, |
39
|
|
|
|
|
|
|
}; |
40
|
|
|
|
|
|
|
|
41
|
3
|
|
|
|
|
8
|
return bless $self, $class; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub start { |
46
|
7
|
|
|
7
|
1
|
621
|
my $self = shift; |
47
|
|
|
|
|
|
|
|
48
|
7
|
100
|
|
|
|
101
|
croak 'expected one or more identifiers' |
49
|
|
|
|
|
|
|
unless @_; |
50
|
|
|
|
|
|
|
|
51
|
6
|
|
|
|
|
11
|
foreach ( @_ ) { |
52
|
8
|
100
|
|
|
|
16
|
croak "already tracking $_" |
53
|
|
|
|
|
|
|
if $self->is_tracking( $_ ) |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
5
|
|
|
|
|
11
|
my $ts = ts; |
57
|
5
|
|
|
|
|
18
|
$self->{ tracked }{ $_ } = $ts foreach @_; |
58
|
|
|
|
|
|
|
|
59
|
5
|
|
|
|
|
22
|
return @_; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub stop { |
64
|
6
|
|
|
6
|
1
|
599
|
my $self = shift; |
65
|
|
|
|
|
|
|
|
66
|
6
|
100
|
|
|
|
75
|
croak 'expected one or more identifiers' |
67
|
|
|
|
|
|
|
unless @_; |
68
|
|
|
|
|
|
|
|
69
|
5
|
|
|
|
|
8
|
foreach ( @_ ) { |
70
|
5
|
100
|
|
|
|
8
|
croak "not tracking $_" |
71
|
|
|
|
|
|
|
unless $self->is_tracking( $_ ) |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
4
|
|
|
|
|
8
|
my $ts = ts; |
75
|
|
|
|
|
|
|
|
76
|
4
|
|
|
|
|
11
|
foreach ( @_ ) { |
77
|
4
|
|
|
|
|
6
|
my $spent = $ts - $self->{ tracked }{ $_ }; |
78
|
4
|
|
|
|
|
7
|
delete $self->{ tracked }{ $_ }; |
79
|
|
|
|
|
|
|
|
80
|
4
|
|
|
|
|
5
|
push @{ $self->{ history } }, $spent; |
|
4
|
|
|
|
|
4
|
|
81
|
4
|
|
|
|
|
8
|
++$self->{ count }; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
4
|
|
|
|
|
9
|
while ( $self->{ count } > $self->{ length } ) { |
85
|
1
|
|
|
|
|
2
|
shift @{ $self->{ history } }; |
|
1
|
|
|
|
|
2
|
|
86
|
1
|
|
|
|
|
2
|
--$self->{ count }; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
4
|
|
|
|
|
4
|
$self->{ average } = List::Util::sum0( @{ $self->{ history } } ) / $self->{ count }; |
|
4
|
|
|
|
|
14
|
|
90
|
|
|
|
|
|
|
|
91
|
4
|
|
|
|
|
15
|
return @_; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
5
|
|
|
5
|
1
|
405
|
sub avg { $_[ 0 ]->{ average } } |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
25
|
100
|
|
25
|
1
|
213
|
sub is_tracking { exists $_[ 0 ]->{ tracked }{ $_[ 1 ] } ? 1 : 0 } |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
1; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
__END__ |