File Coverage

blib/lib/Siebel/Srvrmgr/ListParser/Output/Duration.pm
Criterion Covered Total %
statement 35 37 94.5
branch 4 6 66.6
condition n/a
subroutine 10 11 90.9
pod 4 4 100.0
total 53 58 91.3


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