lib/App/Standby/Group.pm

filter: Statementstmt 24.8% 37 / 149 Branchbran 5.2% 2 / 38 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 42.8% 9 / 21 Podpod 100% 4 / 4 totaltotal 24.1% 52 / 215 CC 24 SCAR 27.1
CC
24
Cov
21%
_set_ordering
42.0
get_contacts
30.0
_trigger_contact
20.0
CRAP 15.1
« lib/App/Standby/Service.pm ↑ Summary lib/App/Standby/Service/HTTP.pm »
1 package App::Standby::Group;
2 3 $App::Standby::Group::VERSION = '0.04';
3 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 $App::Standby::Group::AUTHORITY = 'cpan:TEX';
5 }
6 # ABSTRACT: Core logic
7  
8 3 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # use IO::Handle;
16 # use autodie;
17 # use MooseX::Params::Validate;
18 # use Carp;
19 # use English qw( -no_match_vars );
20 # use Data::Dumper;
21 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # extends ...
24 3 extends 'App::Standby';
25 # has ...
26 3 has 'services' => (
27 'is' => 'rw',
28 'isa' => 'HashRef',
29 'lazy' => 1,
30 'builder' => '_init_services',
31 );
32  
33 3 has 'group_id' => (
34 'is' => 'ro',
35 'isa' => 'Int',
36 'required' => 1,
37 );
38  
39 3 has 'name' => (
40 'is' => 'ro',
41 'isa' => 'Str',
42 'required' => 1,
43 );
44 # with ...
45 # initializers ...
46 2 ▶sub _init_services {
Subroutine _init_services: called CC=5 SCAR=24.6
47 2 my $self = shift;
48  
49 2 my %want_sv;
50 my %has_sv;
51 2 my $sql = 'SELECT id,name,class,desc FROM group_services WHERE group_id = ?';
52 2 my $sth = $self->dbh()->prepexec($sql,$self->group_id());
53 2 ▶ if(!$sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 2 2
54 0 $self->logger()->log( message => 'Failed to get required services from DB due to SQL error: '.$self->dbh()->errstr, level => 'error', );
55 0 return \%has_sv;
56 }
57 2 ▶ while( my ($sv_id, $sv_name, $sv_class, $sv_desc) = $sth->fetchrow_array()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $sth->fetchrow_array) 0 2 2
58 0 $want_sv{$sv_name}->{'id'} = $sv_id;
59 0 $want_sv{$sv_name}->{'class'} = 'App::Standby::Service::'.$sv_class;
60 0 $want_sv{$sv_name}->{'desc'} = $sv_desc;
61 }
62 2 $sth->finish();
63  
64 # try to load each requested service
65 2 foreach my $sv_name ( sort keys %want_sv) {
66 0 my $sv_class = $want_sv{$sv_name}->{'class'};
67 0 my $sv_desc = $want_sv{$sv_name}->{'desc'};
68 0 my $load_result = $self->_load_class($sv_class);
69 0 ▶ if($load_result ne $sv_class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($load_result ne $sv_class) 0 0 0
70 0 $self->logger()->log( message => 'Failed to load required class: '.$sv_class.' w/ error: '.$load_result, level => 'error', );
71 0 next;
72 }
73 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 0 my $Service = $sv_class->new({
75 'name' => $sv_name,
76 'description' => $sv_desc,
77 'group_id' => $self->group_id(),
78 'dbh' => $self->dbh(),
79 'logger' => $self->logger(),
80 });
81 0 $has_sv{$sv_name} = $Service;
82 0 $self->logger()->log( message => "Initialized $sv_name service for ".$self->name().' ('.$self->group_id().')', level => 'debug', );
83 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
84 0 $self->logger()->log( message => "Failed to init $sv_name service for ".$self->name().' ('.$self->group_id().'):'.$_, level => 'warning', );
85 0 };
86 }
87  
88 2 return \%has_sv;;
89 }
90  
91 # your code here ...
92 0 ▶sub _config_values {
Subroutine _config_values: not called CC=1 SCAR=6.9
93 0 my $self = shift;
94 0 my $key = shift;
95  
96 0 return $self->SUPER::_config_values($key, $self->group_id());
97 }
98  
99 0 ▶sub _update_services {
Subroutine _update_services: not called CC=2 SCAR=17.9
100 0 my $self = shift;
101 0 my $contact_ref = shift;
102  
103 0 my $count = 0;
104 0 foreach my $name (sort keys %{$self->services()}) {
105 0 my $service = $self->services()->{$name};
106 0 $self->logger()->log( message => "Service: ".$name.' starting ...', level => 'debug', );
107 0 ▶ try {
Subroutine __ANON__: not called CC=2 SCAR=17.9
108 0 ▶ if($service->update($contact_ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service->update($contact_ref)) { } 0 0 0
109 0 $self->logger()->log( message => "Updated service ".$name, level => 'debug', );
110 0 $count++;
111 } else {
112 0 $self->logger()->log( message => "Failed to update service ".$name, level => 'warning', );
113 }
114 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 0 $self->logger()->log( message => "Failed to update service ".$name.'. Error: '.$_, level => 'warning', );
116 0 };
117 0 $self->logger()->log( message => "Service: ".$name.' done.', level => 'debug', );
118 }
119  
120 0 return $count;
121 }
122  
123 0 ▶sub set_janitor {
Subroutine set_janitor: not called CC=2 SCAR=17.9
Pod documented
124 0 my $self = shift;
125 0 my $contact_id = shift;
126  
127 0 my $contact_ref = $self->get_contacts();
128 # set the last janitor as the last person in the list, so the chances of being notified
129 # are the least for the last janitor
130 0 push @$contact_ref, shift @$contact_ref;
131 # put the new janitor on the first place, leave everything else as it was before
132 # This didn't work: my @new_users = (grep { $_->{'id'} == $contact_id } @$contact_ref, grep { $_->{'id'} != $contact_id } @$contact_ref);
133 0 my @l1 = grep { $_->{'id'} == $contact_id } @$contact_ref;
134 0 my @l2 = grep { $_->{'id'} != $contact_id } @$contact_ref;
135 0 my @new_contacts = (@l1, @l2);
136 #$self->logger()->log( message => "New Ordering: ".Dumper(\@new_users), level => 'debug', );
137  
138 # Example:
139 # Current queue: Jim (on duty), Jon, Paul, Dave
140 # Paul wants to be the new janitor
141 # Push Jim to the last position:
142 # Jon, Paul, Dave, Jim
143 # Push Paul to the first position:
144 # Paul, Jon, Dave, Jim
145  
146 0 ▶ if($self->_update_services($self->_set_ordering(\@new_contacts))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_update_services($self->_set_ordering(\@new_contacts))) { } 0 0 0
147 0 $self->logger()->log( message => "Updated all services", level => 'debug', );
148 } else {
149 0 $self->logger()->log( message => "Failed to update all services", level => 'debug', );
150 }
151  
152 0 return \@new_contacts;
153 }
154  
155 0 ▶sub _set_ordering {
Subroutine _set_ordering: not called CC=6 SCAR=37.4
156 0 my $self = shift;
157 0 my $contact_ref = shift;
158  
159 0 my $sql = 'UPDATE contacts SET ordinal = ? WHERE group_id = ?';
160 0 my $sth = $self->dbh()->prepare($sql);
161 0 ▶ if(!$sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
162 0 $self->logger()->log( message => "Query prepare ($sql) failed w/ error: ".$self->dbh()->errstr, level => 'error', );
163 0 return;
164 }
165 0 ▶ if(!$sth->execute(0,$contact_ref->[0]->{'group_id'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth->execute(0, $contact_ref->[0]{"group_id"})) 0 0 0
166 0 $self->logger()->log( message => "Query execute ($sql) failed w/ error: ".$sth->errstr, level => 'error', );
167 }
168 0 $sth->finish();
169  
170 0 $sql = 'UPDATE contacts SET ordinal = ? WHERE id = ?';
171 0 $sth = $self->dbh()->prepare($sql);
172 0 ▶ if(!$sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
173 0 $self->logger()->log( message => "Query prepare ($sql) failed w/ error: ".$self->dbh()->errstr, level => 'error', );
174 0 return;
175 }
176  
177 0 my $ordinal = 1;
178 0 foreach my $user (@$contact_ref) {
179 0 ▶ if(!$sth->execute($ordinal++,$user->{'id'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth->execute($ordinal++, $user->{"id"})) 0 0 0
180 0 $self->logger()->log( message => "Query execute ($sql) failed w/ error: ".$sth->errstr, level => 'error', );
181 }
182 }
183 0 $sth->finish();
184  
185 0 $contact_ref = $self->get_contacts();
186  
187 0 return $contact_ref;
188 }
189  
190  
191 0 ▶sub enable_contact {
Subroutine enable_contact: not called CC=3 SCAR=24.8
Pod documented
192 0 my $self = shift;
193 0 my $contact_id = shift;
194  
195 0 ▶ if($self->_trigger_contact($contact_id,1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_trigger_contact($contact_id, 1)) { } 0 0 0
196 0 ▶ if($self->_update_services($self->_set_ordering($self->get_contacts()))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_update_services($self->_set_ordering($self->get_contacts))) { } 0 0 0
197 0 return 1;
198 } else {
199 0 return;
200 }
201 } else {
202 0 return;
203 }
204 }
205  
206  
207 0 ▶sub disable_contact {
Subroutine disable_contact: not called CC=3 SCAR=24.8
Pod documented
208 0 my $self = shift;
209 0 my $contact_id = shift;
210  
211 0 ▶ if($self->_trigger_contact($contact_id,0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_trigger_contact($contact_id, 0)) { } 0 0 0
212 0 ▶ if($self->_update_services($self->_set_ordering($self->get_contacts()))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_update_services($self->_set_ordering($self->get_contacts))) { } 0 0 0
213 0 return 1;
214 } else {
215 0 return;
216 }
217 } else {
218 0 return;
219 }
220 }
221  
222 0 ▶sub _trigger_contact {
Subroutine _trigger_contact: not called CC=4 SCAR=30.0
223 0 my $self = shift;
224 0 my $contact_id = shift;
225 0 ▶ my $is_enabled = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
226  
227 0 my $sql = 'UPDATE contacts SET is_enabled = ?, ordinal = ? WHERE id = ?';
228 0 my $sth = $self->dbh()->prepare($sql);
229  
230 0 ▶ if(!$sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
231 # prepare failed
232 0 return;
233 }
234  
235 0 ▶ if(!$sth->execute($is_enabled,9999,$contact_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sth->execute($is_enabled, 9999, $contact_id)) { } 0 0 0
236 # update failed
237 0 return;
238 } else {
239 0 return 1;
240 }
241 }
242  
243  
244 0 ▶sub get_contacts {
Subroutine get_contacts: not called CC=5 SCAR=34.0
Pod documented
245 0 my $self = shift;
246 0 my $all = shift;
247  
248 0 my @users = ();
249  
250 0 my $sql = 'SELECT id,name,cellphone,ordinal FROM contacts WHERE group_id = ? AND ordinal > 0';
251 0 ▶ if(!$all) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($all) 0 0 0
252 0 $sql .= ' AND is_enabled = 1';
253 }
254 0 $sql .= ' ORDER BY ordinal';
255 0 my $sth = $self->dbh()->prepare($sql);
256 0 ▶ if(!$sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
257 0 $self->logger()->log( message => 'Failed to prepare SQL '.$sql.' w/ error: '.$self->dbh()->errstr, level => 'error', );
258 0 return \@users;
259 }
260  
261 0 ▶ if(!$sth->execute($self->group_id())) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth->execute($self->group_id)) 0 0 0
262 0 $self->logger()->log( message => 'Failed to execute stmt w/ error: '.$sth->errstr, level => 'error', );
263 0 return \@users;
264 }
265  
266 0 ▶ while(my ($id,$name,$cellphone,$ordinal) = $sth->fetchrow_array()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $sth->fetchrow_array) 0 0 0
267 0 push(@users, {
268 'id' => $id,
269 'group_id' => $self->group_id(),
270 'name' => $name,
271 'cellphone' => $cellphone,
272 'ordinal' => $ordinal,
273 });
274 0 $self->logger()->log( message => 'User: '.$name, level => 'debug', );
275 }
276 0 $sth->finish();
277  
278 0 return \@users;
279 }
280  
281 3 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
282 3 __PACKAGE__->meta->make_immutable;
283  
284 3 1;
285  
286 __END__
287  
288 =pod
289  
290 =encoding utf-8
291  
292 =head1 NAME
293  
294 App::Standby::Group - Core logic
295  
296 =head1 METHODS
297  
298 =head2 set_janitor
299  
300 set the person on duty to the top of the list
301  
302 =head2 enable_contact
303  
304 Enable the given contact id.
305  
306 =head2 disable_contact
307  
308 Disable the given contact id.
309  
310 =head2 get_contacts
311  
312 Return an array ref containing all active users in the current group w/ their
313 current ordering.
314  
315 If the second argument is true it will return disabled users, too.
316  
317 =head1 NAME
318  
319 App::Standby::Group - Core logic
320  
321 =head1 AUTHOR
322  
323 Dominik Schulz <dominik.schulz@gauner.org>
324  
325 =head1 COPYRIGHT AND LICENSE
326  
327 This software is copyright (c) 2012 by Dominik Schulz.
328  
329 This is free software; you can redistribute it and/or modify it under
330 the same terms as the Perl 5 programming language system itself.
331  
332 =cut
« lib/App/Standby/Service.pm ↑ Summary lib/App/Standby/Service/HTTP.pm »