File Coverage

blib/lib/Siebel/Srvrmgr/ListParser/Output/Duration.pm
Criterion Covered Total %
statement 35 37 94.5
branch 6 8 75.0
condition 2 3 66.6
subroutine 10 11 90.9
pod 4 4 100.0
total 57 63 90.4


line stmt bran cond sub pod time code
1             package Siebel::Srvrmgr::ListParser::Output::Duration;
2              
3 44     44   28758 use warnings;
  44         137  
  44         1902  
4 44     44   318 use strict;
  44         1281  
  44         2952  
5 44     44   339 use Moose::Role 2.1604;
  44         1132  
  44         368  
6 44     44   290055 use Carp;
  44         344  
  44         4006  
7 44     44   28915 use DateTime 1.12;
  44         19930369  
  44         26782  
8              
9             our $VERSION = '0.29'; # VERSION
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             confess
101             "The environment SIEBEL_TZ is not configured. See Siebel::Srvrmgr::Daemon perldoc for information"
102             unless ( ( exists( $ENV{SIEBEL_TZ} ) )
103 429 100 66 429   2676 and ( defined( $ENV{SIEBEL_TZ} ) ) );
104 428         1297 my $tmp = $ENV{SIEBEL_TZ};
105              
106             # to avoid problems with taint mode
107 428         2090 $tmp =~ /^([\w\/\_]+)$/;
108 428         21714 return $1;
109             }
110              
111             =head1 METHODS
112              
113             =head2 get_time_zone
114              
115             Returns the C<time_zone> attribute value.
116              
117             =head2 get_start
118              
119             Returns C<start_datetime> attribute value.
120              
121             =head2 get_current
122              
123             Returns C<curr_datetime> attribute value.
124              
125             =head2 get_end
126              
127             Returns C<end_datetime> attribute value.
128              
129             =head2 fix_endtime
130              
131             This method will check the value of C<end_time> attribute for inconsistences and set a sane value to it.
132              
133             Any class using this role B<must> execute this method inside it's BUILD method. If there isn't one, you will
134             need to a create one to do that.
135              
136             =cut
137              
138             sub fix_endtime {
139 428     428 1 947 my $self = shift;
140 428 100       21799 if ( $self->get_end eq '2000-00-00 00:00:00' ) {
141 16         807 $self->_set_end('');
142             }
143             }
144              
145             =head2 is_running
146              
147             This method checks if the object is still (supposely) running by the time it's data was recovered from C<srvrmgr>.
148              
149             If returns true (1) or false (0);
150              
151             =cut
152              
153             sub is_running {
154 0     0 1 0 my $self = shift;
155 0 0       0 return ( $self->get_end eq '' ) ? 1 : 0;
156             }
157              
158             sub _get_now {
159 430     430   2256 return DateTime->now;
160             }
161              
162             =head2 get_datetime
163              
164             Expects as parameter a string in the format of C<start_datetime> attribute.
165              
166             Returns a L<DateTime> object representation of this string using the available timezone and locale information.
167              
168             =cut
169              
170             sub get_datetime {
171 5     5 1 23 my ( $self, $timestamp ) = @_;
172 5         39 my ( $date, $time ) =
173             split( /\s/, $timestamp );
174 5         27 my @date = split( /\-/, $date );
175 5         22 my @time = split( /\:/, $time );
176 5         71 return DateTime->new(
177             year => $date[0],
178             month => $date[1] * 1, #forcing to be stored as a number
179             day => $date[2] * 1,
180             hour => $time[0] * 1,
181             minute => $time[1] * 1,
182             second => $time[2] * 1
183             );
184             }
185              
186             =head2 get_duration
187              
188             Calculates how much time the object spent doing whatever it was doing, or, if it is not finished,
189             how much time already spent doing that (using C<curr_datetime> attribute for that).
190              
191             The return value is in seconds.
192              
193             =cut
194              
195             sub get_duration {
196 4     4 1 10435 my $self = shift;
197 4         13 my $end;
198              
199 4 100       197 if ( $self->get_end ne '' ) {
200 1         55 $end = $self->get_datetime( $self->get_end );
201             }
202             else {
203 3         131 $end = $self->get_current;
204             }
205              
206 4         746 my $duration =
207             $end->subtract_datetime_absolute(
208             $self->get_datetime( $self->get_start ) );
209 4         2672 return $duration->seconds;
210             }
211              
212             =head1 SEE ALSO
213              
214             =over
215              
216             =item *
217              
218             L<Moose::Manual::Roles>
219              
220             =item *
221              
222             L<DateTime>
223              
224             =item *
225              
226             L<Siebel::Srvrmgr::Daemon>
227              
228             =back
229              
230             =head1 AUTHOR
231              
232             Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>.
233              
234             =head1 COPYRIGHT AND LICENSE
235              
236             This software is copyright (c) 2012 of Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>.
237              
238             This file is part of Siebel Monitoring Tools.
239              
240             Siebel Monitoring Tools is free software: you can redistribute it and/or modify
241             it under the terms of the GNU General Public License as published by
242             the Free Software Foundation, either version 3 of the License, or
243             (at your option) any later version.
244              
245             Siebel Monitoring Tools is distributed in the hope that it will be useful,
246             but WITHOUT ANY WARRANTY; without even the implied warranty of
247             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
248             GNU General Public License for more details.
249              
250             You should have received a copy of the GNU General Public License
251             along with Siebel Monitoring Tools. If not, see L<http://www.gnu.org/licenses/>.
252              
253             =cut
254              
255             1;