File Coverage

blib/lib/Siebel/Srvrmgr/ListParser/Output/Duration.pm
Criterion Covered Total %
statement 26 28 92.8
branch 4 6 66.6
condition n/a
subroutine 7 8 87.5
pod 4 4 100.0
total 41 46 89.1


line stmt bran cond sub pod time code
1             package Siebel::Srvrmgr::ListParser::Output::Duration;
2              
3 16     16   10903 use Moose::Role;
  16         25  
  16         98  
4 16     16   60124 use Carp;
  16         26  
  16         880  
5 16     16   11425 use DateTime;
  16         1132964  
  16         5039  
6              
7             =pod
8              
9             =head1 NAME
10              
11             Siebel::Srvrmgr::ListParser::Output::Duration - Moose role to deal with start and end time of objects
12              
13             =head1 SYNOPSIS
14              
15             with 'Siebel::Srvrmgr::ListParser::Output::Duration';
16              
17             =head1 DESCRIPTION
18              
19             This L<Moose> role enables a class to deal with start and end time of it's instances, including calculating the duration
20             of the object life inside the Siebel Server.
21              
22             =head1 ATTRIBUTES
23              
24             =head2 start_datetime
25              
26             A string representing the date and time the object was created.
27              
28             The string will have the following format:
29              
30             YYYY-MM-DD hh:mm:ss
31              
32             This is a required attribute duration object creation and it's read-only.
33              
34             =cut
35              
36             has 'start_datetime' =>
37             ( is => 'ro', isa => 'Str', 'required' => 1, reader => 'get_start' );
38              
39             =head2 curr_datetime
40              
41             A L<DateTime> instance created during the object creation that is using this role.
42              
43             In the absence of a value for C<end_time> attribute, this object will be used to calcule the value
44             returned by C<duration> method.
45              
46             This is a read-only attribute.
47              
48             =cut
49              
50             has 'curr_datetime' => (
51             is => 'ro',
52             isa => 'DateTime',
53             required => 0,
54             builder => '_get_now',
55             reader => 'get_current'
56             );
57              
58             =head2 end_datetime
59              
60             Same thing as C<start_time>, but representing when the object had finish anything that was doing
61             in the Siebel server.
62              
63             It is not a required attribute during creation and it is read-only. The default value for it
64             is an empty string.
65              
66             =cut
67              
68             has 'end_datetime' => (
69             is => 'ro',
70             isa => 'Str',
71             required => 0,
72             reader => 'get_end',
73             writer => '_set_end',
74             default => ''
75             );
76              
77             =head1 METHODS
78              
79             =head2 get_start
80              
81             Returns C<start_datetime> attribute value.
82              
83             =head2 get_current
84              
85             Returns C<curr_datetime> attribute value.
86              
87             =head2 get_end
88              
89             Returns C<end_datetime> attribute value.
90              
91             =head2 fix_endtime
92              
93             This method will check the value of C<end_time> attribute for inconsistences and set a sane value to it.
94              
95             Any class using this role B<must> execute this method inside it's BUILD method. If there isn't one, you will
96             need to a create one to do that.
97              
98             =cut
99              
100             sub fix_endtime {
101              
102 675     675 1 501 my $self = shift;
103              
104 675 100       25213 if ( $self->get_end eq '2000-00-00 00:00:00' ) {
105              
106 16         563 $self->_set_end('');
107              
108             }
109              
110             }
111              
112             =head2 is_running
113              
114             This method checks if the object is still (supposely) running by the time it's data was recovered from C<srvrmgr>.
115              
116             If returns true (1) or false (0);
117              
118             =cut
119              
120             sub is_running {
121              
122 0     0 1 0 my $self = shift;
123              
124 0 0       0 return ( $self->get_end eq '' ) ? 1 : 0;
125              
126             }
127              
128             sub _get_now {
129              
130 676     676   1692 return DateTime->now;
131              
132             }
133              
134             =head2 get_datetime
135              
136             Expects as parameter a string in the format of C<start_datetime> attribute.
137              
138             Returns a L<DateTime> object representation of this string using the available timezone and locale information.
139              
140             =cut
141              
142             sub get_datetime {
143              
144 5     5 1 6 my $self = shift;
145 5         9 my $timestamp = shift;
146              
147 5         22 my ( $date, $time ) = split( /\s/, $timestamp );
148 5         17 my @date = split( /\-/, $date );
149 5         13 my @time = split( /\:/, $time );
150              
151 5         45 return DateTime->new(
152              
153             year => $date[0],
154             month => $date[1] * 1, #forcing to be stored as a number
155             day => $date[2] * 1,
156             hour => $time[0] * 1,
157             minute => $time[1] * 1,
158             second => $time[2] * 1
159              
160             );
161              
162             }
163              
164             =head2 get_duration
165              
166             Calculates how much time the object spent doing whatever it was doing, or, if it is not finished,
167             how much time already spent doing that (using C<curr_datetime> attribute for that).
168              
169             The return value is in seconds.
170              
171             =cut
172              
173             sub get_duration {
174              
175 4     4 1 6459 my $self = shift;
176              
177 4         6 my $end;
178              
179 4 100       146 if ( $self->get_end ne '' ) {
180              
181 1         31 $end = $self->get_datetime( $self->get_end );
182              
183             }
184             else {
185              
186 3         96 $end = $self->get_current;
187              
188             }
189              
190 4         312 my $duration =
191             $end->subtract_datetime_absolute(
192             $self->get_datetime( $self->get_start ) );
193              
194 4         1434 return $duration->seconds;
195              
196             }
197              
198             =head1 SEE ALSO
199              
200             =over
201              
202             =item *
203              
204             L<Moose::Manual::Roles>
205              
206             =item *
207              
208             L<DateTime>
209              
210             =back
211              
212             =head1 AUTHOR
213              
214             Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>.
215              
216             =head1 COPYRIGHT AND LICENSE
217              
218             This software is copyright (c) 2012 of Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>.
219              
220             This file is part of Siebel Monitoring Tools.
221              
222             Siebel Monitoring Tools is free software: you can redistribute it and/or modify
223             it under the terms of the GNU General Public License as published by
224             the Free Software Foundation, either version 3 of the License, or
225             (at your option) any later version.
226              
227             Siebel Monitoring Tools is distributed in the hope that it will be useful,
228             but WITHOUT ANY WARRANTY; without even the implied warranty of
229             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
230             GNU General Public License for more details.
231              
232             You should have received a copy of the GNU General Public License
233             along with Siebel Monitoring Tools. If not, see L<http://www.gnu.org/licenses/>.
234              
235             =cut
236              
237             1;