File Coverage

blib/lib/Business/SLA.pm
Criterion Covered Total %
statement 66 67 98.5
branch 17 18 94.4
condition 7 15 46.6
subroutine 18 18 100.0
pod 15 15 100.0
total 123 133 92.4


line stmt bran cond sub pod time code
1             package Business::SLA;
2              
3 3     3   4443 use strict;
  3         9  
  3         114  
4 3     3   19 use warnings;
  3         5  
  3         104  
5              
6 3     3   29 use vars qw($VERSION);
  3         7  
  3         2676  
7             $VERSION = '0.05';
8              
9             =head1 NAME
10              
11             Business::SLA -
12              
13             =head1 SYNOPSIS
14              
15             use Business::SLA;
16              
17             my $SLAObj = Business::SLA->new(
18             BusinessHours => new Business::Hours,
19             InHoursDefault => '2 real hours',
20             OutOfHoursDefault => '1 business hour',
21             );
22              
23             # or set/change options later
24             $SLAObj->SetBusinessHours( new Business::Hours );
25             $SLAObj->SetInHoursDefault('2 real hours');
26             $SLAObj->SetOutOfHoursDefault('1 business hour');
27              
28             # add service levels
29             $SLAObj->Add( '2 real hours' => RealMinutes => 2*60 );
30             $SLAObj->Add( '1 business hour' => BusinessMinutes => 60 );
31             $SLAObj->Add( 'next business minute' );
32              
33             =head1 DESCRIPTION
34              
35             This module is a simple tool for handling operations related to
36             Service Level Agreements.
37              
38             =head1 METHODS
39              
40             =head2 new
41              
42             Creates and returns new Business::SLA object.
43              
44             Takes a hash with values of L, L
45             and L options. You can ommit these options
46             and set them latter using methods (see below).
47              
48             =cut
49              
50             sub new {
51 19     19 1 42525 my $class = shift;
52              
53 19   33     151 my $self = bless( { @_ }, ref($class) || $class );
54              
55 19         53 return ($self);
56             }
57              
58             =head2 SetBusinessHours
59              
60             Sets a L object to use for calculations.
61             This module works without this option, but looses most functionality
62             you can get with it.
63              
64             It's possible use any object that API-compatible with L.
65              
66             =cut
67              
68             sub SetBusinessHours {
69 2     2 1 24 my $self = shift;
70 2         5 my $bizhours = shift;
71              
72 2         11 return $self->{'BusinessHours'} = $bizhours;
73             }
74              
75             =head2 BusinessHours
76              
77             Returns the current L object or undef if
78             it's not set.
79              
80             =cut
81              
82             sub BusinessHours {
83 45     45 1 582 my $self = shift;
84              
85 45         176 return $self->{'BusinessHours'};
86             }
87              
88             =head2 SetInHoursDefault
89              
90             Sets the default service level for times inside of business hours.
91              
92             Takes a service level.
93              
94             =cut
95              
96             sub SetInHoursDefault {
97 1     1 1 6 my $self = shift;
98 1         3 my $sla = shift;
99              
100 1         4 return $self->{'InHoursDefault'} = $sla;
101             }
102              
103             =head2 InHoursDefault
104              
105             Returns the default service level for times inside of business hours.
106              
107             =cut
108              
109             sub InHoursDefault {
110 6     6 1 14 my $self = shift;
111              
112 6         38 return $self->{'InHoursDefault'};
113             }
114              
115             =head2 SetOutOfHoursDefault
116              
117             Sets the default service level for times outside of business hours.
118              
119             Takes a service level.
120              
121             Note that L are used for calculations, so this
122             option makes not much sense without L
123             set|/SetBusinessHours>.
124              
125             =cut
126              
127             sub SetOutOfHoursDefault {
128 1     1 1 5 my $self = shift;
129 1         4 my $sla = shift;
130              
131 1         4 $self->{'OutOfHoursDefault'} = $sla;
132             }
133              
134             =head2 OutOfHoursDefault
135              
136             Returns the default service level for times outside of business hours.
137              
138             =cut
139              
140             sub OutOfHoursDefault {
141 4     4 1 12 my $self = shift;
142              
143 4         20 return $self->{'OutOfHoursDefault'};
144             }
145              
146             =head2 IsInHours
147              
148             Returns true if the date passed in is in business hours, and false otherwise.
149             If no L, returns true by default.
150              
151             Takes a date in Unix time format (number of seconds since the epoch).
152              
153             =cut
154              
155             sub IsInHours {
156 8     8 1 4386 my $self = shift;
157 8         11 my $date = shift;
158              
159             # if no business hours are set, by definition we're in hours
160 8 100       21 if ( my $bhours = $self->BusinessHours ) {
161 4 100       13 return $bhours->first_after($date) == $date? 1 : 0;
162             }
163 4         20 return 1;
164             }
165              
166             =head2 SLA
167              
168             Returns the default servise level for the specified time.
169              
170             Takes a date in Unix time format (number of seconds since the epoch).
171              
172             =cut
173              
174             sub SLA {
175 4     4 1 4553 my $self = shift;
176 4         18 my $date = shift;
177              
178 4 100       13 if ( $self->IsInHours($date) ) {
179 3         837 return $self->InHoursDefault;
180             }
181             else {
182 1         1517 return $self->OutOfHoursDefault;
183             }
184             }
185              
186             =head2 Add
187              
188             Adds or replaces a service level definition.
189              
190             Takes a service level and a hash with agreements. In the hash you
191             can define BusinessMinutes, RealMinutes and StartImmediately boolean
192             option.
193              
194             =cut
195              
196             sub Add {
197 19     19 1 73 my $self = shift;
198 19         25 my $sla = shift;
199              
200 19         89 return $self->{'hash'}->{$sla} = { @_ };
201             }
202              
203             =head2 AddRealMinutes
204              
205             The number of real minutes to add for the specified SLA.
206              
207             Takes a service level.
208              
209             =cut
210              
211             sub AddRealMinutes {
212 14     14 1 27 my $self = shift;
213 14         21 my $sla = shift;
214              
215 14 100       342 return 0 unless exists $self->{'hash'}{ $sla }{'RealMinutes'};
216 4   50     30 return $self->{'hash'}{ $sla }{'RealMinutes'} || 0;
217             }
218              
219             =head2 AddBusinessMinutes
220              
221             The number of business minutes to add for the specified SLA.
222              
223             Takes a service level.
224              
225             =cut
226              
227             sub AddBusinessMinutes {
228 16     16 1 48 my $self = shift;
229 16         27 my $sla = shift;
230              
231 16 100       36 return undef unless $self->BusinessHours;
232 5 100       103 return 0 unless exists $self->{'hash'}{ $sla }{'BusinessMinutes'};
233 2   50     13 return $self->{'hash'}{ $sla }{'BusinessMinutes'} || 0;
234             }
235              
236             =head2 StartImmediately
237              
238             Returns true if things should be started immediately for a service
239             level. See also L and L.
240              
241             Takes the service level.
242              
243             =cut
244              
245             sub StartImmediately {
246 26     26 1 60 my $self = shift;
247 26         27 my $sla = shift;
248              
249 26   100     206 return $self->{'hash'}{ $sla }{'StartImmediately'} || 0;
250             }
251              
252             =head2 Starts
253              
254             Returns the starting time, given a date and a service level.
255              
256             If the service level's been defined as L then returns
257             the same date, as well this also happens if L
258             not set|/SetBusinessHours>.
259              
260             Takes a date in Unix time format (number of seconds since the epoch)
261             and a service level.
262              
263             =cut
264              
265             sub Starts {
266 26     26 1 13271 my $self = shift;
267 26         92 my $date = shift;
268 26   33     65 my $sla = shift || $self->SLA( $date );
269              
270 26 100       54 return $date if $self->StartImmediately( $sla );
271              
272 18 100       54 if ( my $bhours = $self->BusinessHours ) {
273 6         103 return $bhours->first_after( $date );
274             }
275             else {
276 12         48 return $date;
277             }
278             }
279              
280             =head2 Due
281              
282             Returns the due time, given an SLA and a date.
283              
284             Takes a date in Unix time format (number of seconds since the epoch)
285             and the hash key for the SLA.
286              
287             =cut
288              
289             sub Due {
290 6     6 1 2535 my $self = shift;
291 6         9 my $date = shift;
292 6   33     22 my $sla = shift || $self->SLA( $date );
293              
294             # find start time
295 6         16 my $due = $self->Starts($date, $sla);
296              
297             # don't add business minutes unless we have some set
298 6 50       15 if ( my $bminutes = $self->AddBusinessMinutes($sla) ) {
299 0         0 $due = $self->BusinessHours->add_seconds( $due, 60 * $bminutes );
300             }
301              
302 6         16 $due += ( 60 * $self->AddRealMinutes($sla) );
303              
304 6         32 return $due;
305             }
306              
307             =head1 SUPPORT
308              
309             Send email to bug-business-sla@rt.cpan.org
310              
311             =head1 AUTHOR
312              
313             Linda Julien
314             Best Practical Solutions, LLC
315             leira@bestpractical.com
316             http://www.bestpractical.com
317              
318             =head1 COPYRIGHT
319              
320             This program is free software; you can redistribute
321             it and/or modify it under the same terms as Perl itself.
322              
323             The full text of the license can be found in the
324             LICENSE file included with this module.
325              
326              
327             =head1 SEE ALSO
328              
329             perl(1), L.
330              
331             =cut
332              
333              
334             1;