blib/lib/MYDan/Oncall/Policy.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 34 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 181 CC 18 SCAR 37.5
CC
18
Cov
0%
load
110.0
get
20.0
set
12.0
CRAP 42.7
1 package MYDan::Oncall::Policy;
2  
3 use strict;
4 use warnings;
5  
6 use Carp;
7 use DateTime;
8 use YAML::XS;
9  
10 use MYDan::Oncall::Period;
11  
12 use constant { DURATION => '00:00 ~ 23:59', PERIOD => 1, NULL => '' };
13  
14 =head1 SYNOPSIS
15  
16 use MYDan::Oncall::Policy;
17  
18 MYDan::Oncall::Policy->new( $conf )->dump( $cache );
19  
20 my $policy = MYDan::Oncall::Policy->load( $cache );
21 my $now = time;
22 my $level = 2;
23  
24 $policy->set( $now - 86400, $now + 86400 );
25  
26 my $who = $policy->get( $now, $level );
27 my %list = $policy->list( $level );
28  
29 =head1 CONFIGURATION
30  
31 A YAML file that contains a stream of site definitions,
32 each a HASH with the following keys:
33  
34 I<required>:
35  
36 pivot: a date expression, for rotation
37 queue: a list of items to rotate through
38  
39 I<optional>:
40  
41 site: default '', name of site
42 period: default 1
43 timezone: default 'local'
44 duration: default '00:00 ~ 23:59'
45 day: days of coverage, default all
46 level: levels of coverage, default all
47 reverse: default 0, reverse escalation order if 1
48  
49 Coverage is processed in sequential order until met or defaulted to the
50 I<last> site ( or in reverse order, and default to the I<first> site if
51 'reverse' is set )
52  
53 Hence I<duration>, I<level>, and I<day> do not apply to the default site.
54  
55 I<example>:
56  
57 ---
58 site: cn
59 pivot: 2017.06.10
60 queue:
61 - user1
62 - user2
63 - user3
64 ---
65 site: us
66 pivot: 2017.06.11 20:00
67 timezone: America/Los_Angeles
68 duration: '19:10 ~ 7:20'
69 period: 7
70 level: [ 1, 2 ]
71 day: [ 1, 2, 3, 4, 5 ]
72 queue:
73 - usr1
74 - usr2
75 - usr3
76  
77 =cut
78 sub new
79 {
80 my $self = shift;
81 $self->load( @_ );
82 }
83  
84 =head1 METHODS
85  
86 =head3 load( $path )
87  
88 Loads object from $path
89  
90 =cut
91 sub load
92 {
93 my ( $class, $conf, %param ) = splice @_;
94 croak "empty config" unless my @conf = YAML::XS::LoadFile $conf;
95  
96 @conf = reverse @conf if $param{reverse} || 0;
97 $conf = $conf[-1];
98 return $conf if ref $conf eq ( $class = ref $class || $class );
99 delete @$conf{ qw( duration level day ) };
100  
101 for my $conf ( @conf )
102 {
103 my $error = 'invalid definition' . YAML::XS::Dump $conf;
104 croak $error unless $conf && ref $conf eq 'HASH';
105  
106 map { $conf->{$_} || croak "$error: $_ not defined" } qw( queue pivot );
107 $conf->{time_zone} = delete $conf->{timezone} || 'local';
108  
109 croak "$error: queue: not ARRAY" if ref $conf->{queue} ne 'ARRAY';
110 croak "$error: invalid pivot" unless $conf->{pivot} =
111 MYDan::Oncall->epoch( @$conf{ qw( pivot time_zone ) } );
112  
113 for my $key ( qw( level day ) )
114 {
115 my $val = delete $conf->{$key} || [];
116 my $ref = ref $val;
117  
118 $val = $ref ? [] : [ split /\D+/, $val ] if $ref ne 'ARRAY';
119 $conf->{$key} = $val if @$val;
120 }
121  
122 $conf->{site} ||= NULL;
123 $conf->{level} = { map { $_ => 1 } @{ $conf->{level} || [] } };
124 $conf->{cycle} = ( $conf->{period} ||= PERIOD ) * @{ $conf->{queue} };
125 $conf->{duration} = MYDan::Oncall::Period->new( $conf->{duration} || DURATION )
126 }
127 bless \@conf, $class;
128 }
129  
130 =head3 dump( $path )
131  
132 Dumps object to $path
133  
134 =cut
135 sub dump
136 {
137 my ( $self, $path ) = splice @_;
138 YAML::XS::DumpFile $path, $self if $path;
139 return $self;
140 }
141  
142 =head3 set( $begin, $end )
143  
144 Sets the scope
145  
146 =cut
147 sub set
148 {
149 my $self = shift;
150 my ( $begin, $end ) = map { ! ref $_ ? DateTime->from_epoch( epoch => $_ )
151 : $_->isa( 'DateTime' ) ? $_ : croak 'invalid time input' } @_;
152  
153 for my $conf ( @$self )
154 {
155 my $cycle = $conf->{cycle};
156 my $pivot = DateTime->from_epoch
157 ( epoch => $conf->{pivot}, time_zone => $conf->{time_zone} );
158  
159 $pivot->add( days => int( ( $begin->epoch - $pivot->epoch )
160 / ( MYDan::Oncall::DAY * $cycle ) ) * $cycle );
161  
162 $pivot->subtract( days => $cycle ) while $begin->epoch < $pivot->epoch;
163  
164 @$conf{ qw( range event ) } =
165 $conf->{duration}->dump( $pivot, $end, %$conf );
166  
167 $conf->{range}->intersect
168 ( $conf->{range}->new->load( $begin->epoch, $end->epoch ) );
169 }
170 return $self;
171 }
172  
173 =head3 get( $time, $level )
174  
175 Returns the event at $time for $level
176  
177 =cut
178 sub get
179 {
180 my ( $self, $time, $level ) = splice @_;
181  
182 for my $conf ( @$self )
183 {
184 my ( $range, $queue ) = @$conf{ qw( range queue ) };
185  
186 last unless $range;
187 next if %{ $conf->{level} } && ! $conf->{level}{$level}
188 || ! defined $range->index( $time );
189  
190 my $i = int( ( $time - $conf->{pivot} )
191 / ( MYDan::Oncall::DAY * $conf->{period} ) );
192  
193 if ( $conf->{reverse} ) { $i += $level }
194 else { $i -= $level; $i += @$queue while $i < 0 }
195  
196 return { site => $conf->{site}, item => $queue->[ $i % @$queue ] };
197 }
198 return undef;
199 }
200  
201 =head3 list( $level )
202  
203 Returns a HASH of events indexed by time for $level
204  
205 =cut
206 sub list
207 {
208 my ( $self, $level ) = splice @_;
209 my $prev = { item => NULL };
210 my %list = map { $_ => 1 } map { @{ $_->{event} } } @$self;
211  
212 for my $time ( sort { $a <=> $b } keys %list )
213 {
214 my $conf = $self->get( $time, $level );
215 if ( ! $conf || $conf->{item} eq $prev->{item} )
216 { delete $list{$time} } else { $prev = $list{$time} = $conf }
217 }
218 return wantarray ? %list : \%list;
219 }
220  
221 1;