ProgressMonitor/Stringify/Fields/ETA.pm

filter: Statementstmt 83.4% 101 / 121 Branchbran 39.2% 11 / 28 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 94.7% 18 / 19 Podpod 50.0% 1 / 2 totaltotal 71.3% 132 / 185 CC 19 SCAR 22.9
CC
19
Cov
72%
render
23.9
checkAttributeValues
9.6
completed
6.0
CRAP 9.9
« ProgressMonitor/Stringify/AbstractMonitor.pm ↑ Summary ProgressMonitor/Stringify/Fields/Bar.pm »
1 package ProgressMonitor::Stringify::Fields::ETA;
2
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 2 ▶use ProgressMonitor::State;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 require ProgressMonitor::Stringify::Fields::AbstractField if 0;
8
9 2 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 2 ▶use constant MINUTE => 60;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use constant HOUR => 60 * MINUTE;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use constant DAY => 24 * HOUR;
Subroutine BEGIN: called CC=1 SCAR=0.0
14
15 2 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 use classes
17 2 ▶ extends => 'ProgressMonitor::Stringify::Fields::AbstractField',
Subroutine BEGIN: called CC=1 SCAR=0.0
18 new => 'new',
19 attrs_pr => ['start', 'index', 'lastHH', 'lastMM', 'lastSS', 'lastDelim', 'lastTime', 'lastLeft'],
20 2 ;
21
22 sub new
23 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
24 1 my $class = shift;
25 1 my $cfg = shift;
26
27 1 my $self = $class->SUPER::_new($cfg, $CLASS);
28
29 1 $cfg = $self->_get_cfg;
30
31 # we only wish to portray up to 23:59:49 at this point
32 #
33 1 my $delim = $cfg->get_mainDelimiter;
34 1 my $delimLen = length($delim);
35 1 $self->_set_width(2 + $delimLen + 2 + $delimLen + 2);
36
37 1 my $ofc = $cfg->get_unknownCharacter;
38 1 $self->{$ATTR_start} = 0;
39 1 $self->{$ATTR_index} = 0;
40 1 $self->{$ATTR_lastHH} = $self->{$ATTR_lastMM} = $self->{$ATTR_lastSS} = "$ofc$ofc";
41 1 $self->{$ATTR_lastDelim} = $delim;
42 1 $self->{$ATTR_lastTime} = 0;
43 1 $self->{$ATTR_lastLeft} = 0;
44
45 1 return $self;
46 }
47
48 sub render
49 3 ▶{
Subroutine render: called CC=11 SCAR=31.7
Pod documented
50 3 my $self = shift;
51 3 my $state = shift;
52 3 my $ticks = shift;
53 3 my $totalTicks = shift;
54 3 my $clean = shift;
55
56 3 my $now = time;
57 3 my $timeSinceStart = $now - $self->{$ATTR_start};
58 3 my $cfg = $self->_get_cfg;
59 3 my $hh = $self->{$ATTR_lastHH};
60 3 my $mm = $self->{$ATTR_lastMM};
61 3 my $ss = $self->{$ATTR_lastSS};
62 3 my $delim = $self->{$ATTR_lastDelim};
63
64 3 ▶ if (!$self->{$ATTR_start})
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{$ProgressMonitor::Stringify::Fields::ETA::ATTR_start}) { } 1 2 3
65 {
66 # this is the first call - just render 'unknown'
67 #
68 1 $self->{$ATTR_start} = $self->{$ATTR_lastTime} = $now;
69 }
70 else
71 {
72
73 2 ▶ if ($state == STATE_DONE)
Branch: 1 branch
BranchTrueFalseTotal
if ($state == 3) { } 1 1 2
74 {
75 1 my $ofc = $cfg->get_unknownCharacter;
76 1 $hh = $mm = $ss = "$ofc$ofc";
77 1 ▶ ($hh, $mm, $ss) = $self->__fmtHMS($timeSinceStart) if $timeSinceStart < DAY;
Branch: 1 branch
BranchTrueFalseTotal
if $timeSinceStart < 86400 1 0 1
78 }
79 else
80 {
81 # to avoid too much flickering, only update at the given rate
82 #
83 1 ▶ if ($now >= $self->{$ATTR_lastTime} + $cfg->get_maxUpdateRate)
Branch: 1 branch
BranchTrueFalseTotal
if ($now >= $self->{$ProgressMonitor::Stringify::Fields::ETA::ATTR_lastTime} + $cfg->get_maxUpdateRate) 0 1 1
84 {
85 # flicker delimiter
86 #
87 0 my $seq = $cfg->get_idleDelimiterSequence;
88 0 $delim = $seq->[$self->{$ATTR_index}++ % @$seq];
89
90 # try to ensure we have some information to predict on
91 #
92 0 ▶ my $ratio = defined($totalTicks) && $totalTicks > 0 ? $ticks / $totalTicks : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $totalTicks && $totalTicks > 0 ? : 0 0 0
Condition0%0/3
defined $totalTicks and $totalTicks > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $totalTicks and $totalTicks > 0
defined $totalTicks $totalTicks > 0
Truth table0%0/3
A and B
A= defined $totalTicksB= $totalTicks > 0
ABresult
0X0
100
111
93 0 ▶ if ($ratio > $cfg->get_waitForRatio)
Branch: 1 branch
BranchTrueFalseTotal
if ($ratio > $cfg->get_waitForRatio) 0 0 0
94 {
95 0 my $left = int($timeSinceStart * ((1 - $ratio) / $ratio));
96 0 ▶ $left = DAY if $left > DAY;
Branch: 1 branch
BranchTrueFalseTotal
if $left > 86400 0 0 0
97 0 ▶ if ($clean || $left != $self->{$ATTR_lastLeft})
Branch: 1 branch
BranchTrueFalseTotal
if ($clean or $left != $self->{$ProgressMonitor::Stringify::Fields::ETA::ATTR_lastLeft}) 0 0 0
Condition0%0/3
$clean or $left != $self->{$ProgressMonitor::Stringify::Fields::ETA::ATTR_lastLeft}
l!l&&r!l&&!r
000
MC/DC0%0/2
$clean or $left != $self->{$ProgressMonitor::Stringify::Fields::ETA::ATTR_lastLeft}
$clean $left != $self->{$ProgressMonitor::Stringify::Fields::ETA::ATTR_lastLeft}
Truth table0%0/3
A or B
A= $cleanB= $left != $self->{$ProgressMonitor::Stringify::Fields::ETA::ATTR_lastLeft}
ABresult
1X1
011
000
98 {
99 0 ($hh, $mm, $ss) = $self->__fmtHMS($left);
100 0 $delim = $cfg->get_mainDelimiter;
101 }
102 0 $self->{$ATTR_lastLeft} = $left;
103 }
104 0 $self->{$ATTR_lastTime} = $now;
105 }
106 }
107 }
108
109 3 $self->{$ATTR_lastHH} = $hh;
110 3 $self->{$ATTR_lastMM} = $mm;
111 3 $self->{$ATTR_lastSS} = $ss;
112 3 $self->{$ATTR_lastDelim} = $delim;
113 3 return sprintf("%s%s%s%s%s", $hh, $delim, $mm, $delim, $ss);
114 }
115
116 sub completed
117 0 ▶{
Subroutine completed: not called CC=2 SCAR=17.9
Pod undocumented
118 0 my $self = shift;
119
120 # We're done - report the actual time it took
121 # but check for overflow
122 #
123 0 my $cfg = $self->_get_cfg;
124 0 my $ofc = $cfg->get_unknownCharacter;
125 0 my ($hh, $mm, $ss);
126 0 $hh = $mm = $ss = "$ofc$ofc";
127 0 my $timeSinceStart = time - $self->{$ATTR_start};
128 0 ▶ ($hh, $mm, $ss) = $self->__fmtHMS($timeSinceStart) if $timeSinceStart < DAY;
Branch: 1 branch
BranchTrueFalseTotal
if $timeSinceStart < 86400 0 0 0
129 0 my $delim = $cfg->get_mainDelimiter;
130
131 0 return sprintf("%s%s%s%s%s", $hh, $delim, $mm, $delim, $ss);
132 }
133
134 sub __fmtHMS
135 1 ▶{
Subroutine __fmtHMS: called CC=1 SCAR=0.0
136 1 my $self = shift;
137 1 my $time = shift;
138
139 1 my $fmt = '%02u';
140 1 my $hh = sprintf($fmt, int($time / DAY));
141 1 my $mm = sprintf("%02u", int(($time % DAY) / MINUTE));
142 1 my $ss = sprintf("%02u", $time % MINUTE);
143
144 1 return ($hh, $mm, $ss);
145 }
146
147 ###
148
149 package ProgressMonitor::Stringify::Fields::ETAConfiguration;
150
151 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
152 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
153
154 2 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
155 use classes
156 2 ▶ extends => 'ProgressMonitor::Stringify::Fields::AbstractFieldConfiguration',
Subroutine BEGIN: called CC=1 SCAR=0.0
157 attrs => ['unknownCharacter', 'mainDelimiter', 'idleDelimiterSequence', 'waitForRatio', 'maxUpdateRate'],
158 2 ;
159
160 sub defaultAttributeValues
161 1 ▶{
Subroutine defaultAttributeValues: called CC=1 SCAR=0.0
162 1 my $self = shift;
163
164 return {
165 1 %{$self->SUPER::defaultAttributeValues()},
166 unknownCharacter => '-',
167 mainDelimiter => ':',
168 idleDelimiterSequence => [' ', ':'],
169 waitForRatio => 0.01,
170 maxUpdateRate => 1,
171 };
172 }
173
174 sub checkAttributeValues
175 1 ▶{
Subroutine checkAttributeValues: called CC=8 SCAR=22.6
176 1 my $self = shift;
177
178 1 $self->SUPER::checkAttributeValues;
179
180 1 ▶ X::Usage->throw("unknownCharacter should have a length of 1") unless length($self->get_unknownCharacter) == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->get_unknownCharacter == 1 0 1 1
181 1 my $seq = $self->get_idleDelimiterSequence;
182 1 ▶ X::Usage->throw("idleDelimiterSequence must be an array") unless ref($seq) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $seq eq "ARRAY" 0 1 1
183 1 my $len = length($self->get_mainDelimiter);
184 1 for (@$seq)
185 {
186 2 ▶ X::Usage->throw("all idleDelimiterSequence elements must have same length as mainDelimiter") if length($_) != $len;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ != $len 0 2 2
187 }
188 1 ▶ X::Usage->throw("0 < waitForRatio <= 1") if ($self->get_waitForRatio < 0 || $self->get_waitForRatio > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_waitForRatio < 0 or $self->get_waitForRatio > 1 0 1 1
Condition33%1/3
$self->get_waitForRatio < 0 or $self->get_waitForRatio > 1
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->get_waitForRatio < 0 or $self->get_waitForRatio > 1
$self->get_waitForRatio < 0 $self->get_waitForRatio > 1
Truth table33%1/3
A or B
A= $self->get_waitForRatio < 0B= $self->get_waitForRatio > 1
ABresult
1X1
011
000
189 1 ▶ X::Usage->throw("maxUpdateRate can not be negative") if $self->get_waitForRatio < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_waitForRatio < 0 0 1 1
190
191 1 return;
192 }
193
194 ############################
195
196 =head1 NAME
197
198 ProgressMonitor::Stringify::Field::ETA - a field implementation that renders progress
199 as a time-to-completion.
200
201 =head1 VERSION
202
203 Version 0.01
204
205 =head1 DESCRIPTION
206
207 @@TODO@@
208
209 =head1 AUTHOR
210
211 Kenneth Olwing, C<< <knth at cpan.org> >>
212
213 =head1 BUGS
214
215 I wouldn't be surprised! If you can come up with a minimal test that shows the
216 problem I might be able to take a look. Even better, send me a patch.
217
218 Please report any bugs or feature requests to
219 C<bug-progressmonitor at rt.cpan.org>, or through the web interface at
220 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=ProgressMonitor>.
221 I will be notified, and then you'll automatically be notified of progress on
222 your bug as I make changes.
223
224 =head1 SUPPORT
225
226 You can find documentation for this module with the perldoc command.
227
228 perldoc ProgressMonitor
229
230 You can also look for information at:
231
232 =over 4
233
234 =item * AnnoCPAN: Annotated CPAN documentation
235
236 L<http://annocpan.org/dist/ProgressMonitor>
237
238 =item * CPAN Ratings
239
240 L<http://cpanratings.perl.org/d/ProgressMonitor>
241
242 =item * RT: CPAN's request tracker
243
244 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ProgressMonitor>
245
246 =item * Search CPAN
247
248 L<http://search.cpan.org/dist/ProgressMonitor>
249
250 =back
251
252 =head1 ACKNOWLEDGEMENTS
253
254 Thanks to my family. I'm deeply grateful for you!
255
256 =head1 COPYRIGHT & LICENSE
257
258 Copyright 2006,2007 Kenneth Olwing, all rights reserved.
259
260 This program is free software; you can redistribute it and/or modify it
261 under the same terms as Perl itself.
262
263 =cut
264
265 2 1; # End of ProgressMonitor::Stringify::Fields::ETA
« ProgressMonitor/Stringify/AbstractMonitor.pm ↑ Summary ProgressMonitor/Stringify/Fields/Bar.pm »