Zapp/Controller/Plan.pm

filter: Statementstmt 10.2% 13 / 127 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 44.4% 4 / 9 Podpod 0.0% 0 / 5 totaltotal 7.9% 17 / 215 CC 47 SCAR 59.7
CC
47
Cov
7%
save_plan
702.0
list_plans
210.0
delete_plan
42.0
CRAP 390.8
↑ Summary Zapp/Task/Request.pm »
1 package Zapp::Controller::Plan;
2 1 ▶use Mojo::Base 'Mojolicious::Controller', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Mojo::JSON qw( decode_json encode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use List::Util qw( uniqstr );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Zapp::Util qw( build_data_from_params );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # Zapp: Now, like all great plans, my strategy is so simple an idiot
8 # could have devised it.
9  
10 ▶sub edit_plan( $self ) {
Subroutine edit_plan: not called CC=3 SCAR=24.8
Pod undocumented
11 my @tasks =
12 0 ▶ sort grep { !ref $_ && eval { $_->isa('Zapp::Task') } }
Branch: 1 branch
BranchTrueFalseTotal
not ref $_ and eval { do { $_->isa("Zapp::Task") } } 0 0 0
13 values $self->minion->tasks->%*;
14 0 ▶ my $plan = $self->stash( 'plan' ) || $self->app->get_plan( $self->stash( 'plan_id' ) );
Condition0%0/3
$self->stash('plan') || $self->app->get_plan($self->stash('plan_id'))
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->stash('plan') || $self->app->get_plan($self->stash('plan_id'))
$self->stash('plan') $self->app->get_plan($self->stash('plan_id'))
Truth table0%0/3
A || B
A= $self->stash('plan')B= $self->app->get_plan($self->stash('plan_id'))
ABresult
1X1
011
000
15 0 return $self->render(
16 'zapp/plan/edit',
17 plan => $plan,
18 tasks => \@tasks,
19 );
20 }
21  
22 ▶sub save_plan( $self ) {
Subroutine save_plan: not called CC=26 SCAR=65.5
Pod undocumented
23  
24 0 my $plan_id = $self->stash( 'plan_id' );
25 my $plan = {
26 0 map { $_ => $self->param( $_ ) }
27 qw( label description ),
28 };
29 0 my $tasks = build_data_from_params( $self, 'task' );
30 0 my $form_inputs = build_data_from_params( $self, 'input' );
31  
32 # XXX: Create transaction routine for Yancy::Backend
33 0 ▶ if ( $plan_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plan_id) { } 0 0 0
34 0 $self->yancy->backend->set( zapp_plans => $plan_id, $plan );
35 }
36 else {
37 0 $plan_id = $self->yancy->backend->create( zapp_plans => $plan );
38 }
39 0 $plan->{plan_id} = $plan_id;
40  
41 # Validate all incoming data.
42 0 my @errors;
43 0 for my $i ( 0..$#$form_inputs ) {
44 0 my $input = $form_inputs->[ $i ];
45 0 ▶ if ( $input->{name} =~ /\P{Word}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input->{"name"} =~ /\P{Word}/u) 0 0 0
46 0 my @chars = uniqstr sort $input->{name} =~ /\P{Word}/g;
47 push @errors, {
48 name => "input[$i].name",
49 error => qq{Input name "$input->{name}" has invalid characters: }
50 0 . join( '', map { "<kbd>$_</kbd>" } @chars ),
51 };
52 }
53 my $type = $self->app->zapp->types->{ $input->{type} }
54 0 ▶ or die qq{Could not find type "$input->{type}"};
Branch: 1 branch
BranchTrueFalseTotal
my $type = $self->app->zapp->types->{$input->{"type"}} or die qq[Could not find type "$input->{"type"}"] 0 0 0
55 0 eval {
56 0 $input->{config} = $type->process_config( $self, $input->{config} );
57 };
58 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
59 0 push @errors, {
60 name => "input[$i].name",
61 error => qq{Error validating input config "$input->{name}" type "$input->{type}": $@},
62 };
63 }
64 }
65  
66 0 for my $i ( 0..$#$tasks ) {
67 0 my $task = $tasks->[ $i ];
68 0 ▶ if ( $task->{name} =~ /\P{Word}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($task->{"name"} =~ /\P{Word}/u) 0 0 0
69 0 my @chars = uniqstr sort $task->{name} =~ /\P{Word}/g;
70 push @errors, {
71 name => "task[$i].name",
72 error => qq{Task name "$task->{name}" has invalid characters: }
73 0 . join( '', map { "<kbd>$_</kbd>" } @chars ),
74 };
75 }
76 }
77  
78 0 my $f = $self->app->formula;
79 0 for my $name ( grep /^task\[\d+\]\.input/, $self->req->params->names->@* ) {
80 0 my ( $value ) = $self->param( $name ) =~ /^=(.+)/;
81 0 ▶ next unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 0 0
82 0 local $@;
83 0 eval { $f->parse( $value ) };
84 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
85 0 push @errors, {
86 name => $name,
87 error => qq{Failed to parse formula: $@},
88 };
89 }
90 }
91  
92 0 ▶ if ( @errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 0 0 0
93 0 $self->log->error( "Error saving plan: " . $self->dumper( \@errors ) );
94 0 $self->stash(
95 status => 400,
96 plan => {
97 %$plan,
98 tasks => $tasks,
99 inputs => $form_inputs,
100 },
101 errors => \@errors,
102 );
103 0 return $self->edit_plan;
104 }
105  
106 # XXX: Create sync routine for Yancy::Backend that takes a set of
107 # items and updates the schema to look exactly like that (deleting,
108 # updating, inserting as needed)
109 my %tasks_to_delete
110 0 = map { $_->{task_id} => 1 }
111 $self->yancy->list( zapp_plan_tasks => { plan_id => $plan_id } );
112 0 my $parent_task_id;
113 0 for my $task ( @$tasks ) {
114 0 my $task_id = $task->{task_id};
115  
116 # XXX: Auto-encode/-decode JSON fields in Yancy schema
117 0 for my $json_field ( qw( input ) ) {
118 0 $task->{ $json_field } = encode_json( $task->{ $json_field } );
119 }
120  
121 0 ▶ if ( $task_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($task_id) { } 0 0 0
122 0 delete $tasks_to_delete{ $task_id };
123 0 $self->yancy->backend->set( zapp_plan_tasks => $task_id, $task );
124 }
125 else {
126 0 delete $task->{task_id};
127 0 $task_id = $task->{task_id} = $self->yancy->backend->create( zapp_plan_tasks => {
128 %$task, plan_id => $plan_id,
129 } );
130 }
131  
132 0 ▶ if ( $parent_task_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_task_id) 0 0 0
133 0 my ( @existing_parents ) = $self->yancy->list( zapp_plan_task_parents => { task_id => $task_id } );
134 0 for my $parent ( @existing_parents ) {
135 # We're supposed to have this row, so ignore it
136 0 ▶ next if grep { $parent->{parent_task_id} eq $_ } ( $parent_task_id );
Branch: 1 branch
BranchTrueFalseTotal
if grep {$parent->{"parent_task_id"} eq $_;} $parent_task_id 0 0 0
137 # We're not supposed to have this row, so delete it
138 0 $self->yancy->backend->delete( zapp_plan_task_parents => { $parent->%{qw( task_id parent_task_id )} } );
139 }
140 0 for my $new_parent ( $parent_task_id ) {
141 # We already have this row, so ignore it
142 0 ▶ next if grep { $new_parent eq $_->{parent_task_id} } @existing_parents;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$new_parent eq $_->{"parent_task_id"};} @existing_parents 0 0 0
143 # We don't have this row, so create it
144 0 $self->yancy->backend->create( zapp_plan_task_parents => {
145 task_id => $task_id,
146 parent_task_id => $parent_task_id,
147 });
148 }
149 }
150 0 $parent_task_id = $task_id;
151  
152 }
153  
154 0 for my $task_id ( keys %tasks_to_delete ) {
155 0 $self->yancy->delete( zapp_plan_tasks => $task_id );
156 0 my ( @existing_parents ) = $self->yancy->list( zapp_plan_task_parents => { task_id => $task_id } );
157 0 for my $parent ( @existing_parents ) {
158 0 $self->yancy->backend->delete( zapp_plan_task_parents => { $parent->%{qw( task_id parent_task_id )} } );
159 }
160 }
161  
162 0 my %input_to_delete = map { $_->{name} => $_ } $self->yancy->list( zapp_plan_inputs => { plan_id => $plan_id } );
163 0 for my $i ( 0 .. $#$form_inputs ) {
164 0 my $form_input = $form_inputs->[ $i ];
165 0 $form_input->{rank} = $i;
166 # XXX: Auto-encode/-decode JSON fields in Yancy schema
167 0 for my $json_field ( qw( value config ) ) {
168 0 $form_input->{ $json_field } = encode_json( $form_input->{ $json_field } );
169 }
170  
171 0 my $name = $form_input->{name};
172 0 ▶ if ( $input_to_delete{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_to_delete{$name}) { } 0 0 0
173 0 delete $input_to_delete{ $name };
174 $self->yancy->backend->set(
175 0 zapp_plan_inputs => { plan_id => $plan_id, $form_input->%{'name'} }, $form_input,
176 );
177 }
178 else {
179 0 $self->yancy->backend->create(
180 zapp_plan_inputs => { %$form_input, plan_id => $plan_id },
181 );
182 }
183 }
184 0 for my $name ( keys %input_to_delete ) {
185 # XXX: Fix yancy backend composite keys to allow arrayref of
186 # ordered columns
187 0 $self->yancy->backend->delete( zapp_plan_inputs => { plan_id => $plan_id, name => $name } );
188 }
189  
190 0 $self->redirect_to( 'zapp.edit_plan' => { plan_id => $plan_id } );
191 }
192  
193 ▶sub delete_plan( $self ) {
Subroutine delete_plan: not called CC=6 SCAR=37.4
Pod undocumented
194 0 my $plan_id = $self->stash( 'plan_id' );
195 0 my $plan = $self->yancy->get( zapp_plans => $plan_id );
196 0 ▶ if ( $self->req->method eq 'GET' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->req->method eq "GET") 0 0 0
197 0 return $self->render(
198 'zapp/plan/delete',
199 plan => $plan,
200 );
201 }
202 0 $self->yancy->delete( zapp_plans => $plan_id );
203 # Clean up if foreign keys are disabled...
204 # XXX: This can be removed when Yancy fixes the SQLite backend to
205 # always enable foreign keys
206 # XXX: Yancy should allow a query to the delete() function
207 0 for my $task ( $self->yancy->list( zapp_plan_tasks => { plan_id => $plan_id } ) ) {
208 0 for my $parent ( $self->yancy->list( zapp_plan_task_parents => { task_id => $task->{task_id} } ) ) {
209 0 $self->yancy->delete( zapp_plan_task_parents => { $parent->%{qw( task_id parent_task_id )} } );
210 }
211 0 $self->yancy->delete( zapp_plan_tasks => $task->{task_id} );
212 }
213 0 for my $input ( $self->yancy->list( zapp_plan_inputs => { plan_id => $plan_id } ) ) {
214 0 $self->yancy->delete( zapp_plan_inputs => { $input->%{qw( plan_id name )} } );
215 }
216 0 for my $run ( $self->yancy->list( zapp_runs => { plan_id => $plan_id } ) ) {
217 0 $self->yancy->backend->set( zapp_runs => $run->{run_id}, { plan_id => undef } );
218 }
219  
220 0 $self->redirect_to( 'zapp.list_plans' );
221 }
222  
223 ▶sub list_plans( $self ) {
Subroutine list_plans: not called CC=14 SCAR=53.5
Pod undocumented
224 0 my @plans = $self->yancy->list( zapp_plans => {}, {} );
225 0 for my $plan ( @plans ) {
226 my ( $last_run ) = $self->yancy->list(
227 zapp_runs => {
228 0 $plan->%{'plan_id'},
229 },
230 { order_by => { -desc => [qw( created started finished )] } },
231 );
232 0 ▶ next if !$last_run;
Branch: 1 branch
BranchTrueFalseTotal
unless $last_run 0 0 0
233  
234 0 $plan->{ last_run } = $last_run;
235 }
236 # XXX: Order should be:
237 # 1. Jobs that have no started
238 # 2. Jobs that have no finished
239 # 3. Jobs by finished datetime
240 # 4. Jobs by started datetime
241 # 5. Jobs by created datetime
242 # 6. Plans by created datetime
243 @plans = sort {
244 0 !!( $b->{last_run} // '' ) cmp !!( $a->{last_run} // '' )
245 || (
246 defined $a->{last_run} && (
247 ( $b->{last_run}{state} =~ /(in)?active/n ) cmp ( $a->{last_run}{state} =~ /(in)?active/n )
248 || ($b->{last_run}{finished}//'') cmp ($a->{last_run}{finished}//'')
249 || ($b->{last_run}{started}//'') cmp ($a->{last_run}{started}//'')
250 )
251 )
252 || $b->{created} cmp $a->{created}
253 0 ▶ } @plans;
Branch: 1 branch
BranchTrueFalseTotal
!!($Zapp::Controller::Plan::b->{"last_run"} // "") cmp !!($Zapp::Controller::Plan::a->{"last_run"} // "") or defined $Zapp::Controller::Plan::a->{"last_run"} and $Zapp::Controller::Plan::b->{"last_run"}{"state"} =~ /(in)?active/nu cmp $Zapp::Controller::Plan::a->{"last_run"}{"state"} =~ /(in)?active/nu || ($Zapp::Controller::Plan::b->{"last_run"}{"finished"} // "") cmp ($Zapp::Controller::Plan::a->{"last_run"}{"finished"} // "") || ($Zapp::Controller::Plan::b->{"last_run"}{"started"} // "") cmp ($Zapp::Controller::Plan::a->{"last_run"}{"started"} // "") or $Zapp::Controller::Plan::b->{"created"} cmp $Zapp::Controller::Plan::a->{"created"} 0 0 0
Condition0%0/24
!!($Zapp::Controller::Plan::b->{"last_run"} // "") cmp !!($Zapp::Controller::Plan::a->{"last_run"} // "") or defined $Zapp::Controller::Plan::a->{"last_run"} and $Zapp::Controller::Plan::b->{"last_run"}{"state"} =~ /(in)?active/nu cmp $Zapp::Controller::Plan::a->{"last_run"}{"state"} =~ /(in)?active/nu || ($Zapp::Controller::Plan::b->{"last_run"}{"finished"} // "") cmp ($Zapp::Controller::Plan::a->{"last_run"}{"finished"} // "") || ($Zapp::Controller::Plan::b->{"last_run"}{"started"} // "") cmp ($Zapp::Controller::Plan::a->{"last_run"}{"started"} // "")
l!l&&r!l&&!r
000
$Zapp::Controller::Plan::b->{"last_run"} // ""
l!l
00
$Zapp::Controller::Plan::a->{"last_run"} // ""
l!l
00
defined $Zapp::Controller::Plan::a->{"last_run"} and $Zapp::Controller::Plan::b->{"last_run"}{"state"} =~ /(in)?active/nu cmp $Zapp::Controller::Plan::a->{"last_run"}{"state"} =~ /(in)?active/nu || ($Zapp::Controller::Plan::b->{"last_run"}{"finished"} // "") cmp ($Zapp::Controller::Plan::a->{"last_run"}{"finished"} // "") || ($Zapp::Controller::Plan::b->{"last_run"}{"started"} // "") cmp ($Zapp::Controller::Plan::a->{"last_run"}{"started"} // "")
!ll&&!rl&&r
000
$Zapp::Controller::Plan::b->{"last_run"}{"state"} =~ /(in)?active/nu cmp $Zapp::Controller::Plan::a->{"last_run"}{"state"} =~ /(in)?active/nu or ($Zapp::Controller::Plan::b->{"last_run"}{"finished"} // "") cmp ($Zapp::Controller::Plan::a->{"last_run"}{"finished"} // "") or ($Zapp::Controller::Plan::b->{"last_run"}{"started"} // "") cmp ($Zapp::Controller::Plan::a->{"last_run"}{"started"} // "")
l!l&&r!l&&!r
000
$Zapp::Controller::Plan::b->{"last_run"}{"state"} =~ /(in)?active/nu cmp $Zapp::Controller::Plan::a->{"last_run"}{"state"} =~ /(in)?active/nu or ($Zapp::Controller::Plan::b->{"last_run"}{"finished"} // "") cmp ($Zapp::Controller::Plan::a->{"last_run"}{"finished"} // "")
l!l&&r!l&&!r
000
$Zapp::Controller::Plan::b->{"last_run"}{"finished"} // ""
l!l
00
$Zapp::Controller::Plan::a->{"last_run"}{"finished"} // ""
l!l
00
$Zapp::Controller::Plan::b->{"last_run"}{"started"} // ""
l!l
00
$Zapp::Controller::Plan::a->{"last_run"}{"started"} // ""
l!l
00
MC/DC0%0/5
!!($Zapp::Controller::Plan::b->{"last_run"} // "") cmp !!($Zapp::Controller::Plan::a->{"last_run"} // "") or defined $Zapp::Controller::Plan::a->{"last_run"} and $Zapp::Controller::Plan::b->{"last_run"}{"state"} =~ /(in)?active/nu cmp $Zapp::Controller::Plan::a->{"last_run"}{"state"} =~ /(in)?active/nu || ($Zapp::Controller::Plan::b->{"last_run"}{"finished"} // "") cmp ($Zapp::Controller::Plan::a->{"last_run"}{"finished"} // "") || ($Zapp::Controller::Plan::b->{"last_run"}{"started"} // "") cmp ($Zapp::Controller::Plan::a->{"last_run"}{"started"} // "")
$Zapp::Controller::Plan::b->{"last_run"} defined $Zapp::Controller::Plan::a->{"last_run"} $Zapp::Controller::Plan::b->{"last_run"}{"state"} =~ /(in)?active/nu cmp $Zapp::Controller::Plan::a->{"last_run"}{"state"} =~ /(in)?active/nu $Zapp::Controller::Plan::b->{"last_run"}{"finished"} $Zapp::Controller::Plan::b->{"last_run"}{"started"}
MC/DC0%0/1
$Zapp::Controller::Plan::a->{"last_run"} // ""
$Zapp::Controller::Plan::a->{"last_run"}
MC/DC0%0/1
$Zapp::Controller::Plan::a->{"last_run"}{"finished"} // ""
$Zapp::Controller::Plan::a->{"last_run"}{"finished"}
MC/DC0%0/1
$Zapp::Controller::Plan::a->{"last_run"}{"started"} // ""
$Zapp::Controller::Plan::a->{"last_run"}{"started"}
Truth table0%0/6
A or B and C or D or E
A= $Zapp::Controller::Plan::b->{"last_run"}B= defined $Zapp::Controller::Plan::a->{"last_run"}C= $Zapp::Controller::Plan::b->{"last_run"}{"state"} =~ /(in)?active/nu cmp $Zapp::Controller::Plan::a->{"last_run"}{"state"} =~ /(in)?active/nuD= $Zapp::Controller::Plan::b->{"last_run"}{"finished"}E= $Zapp::Controller::Plan::b->{"last_run"}{"started"}
ABCDEresult
1XXXX1
011XX1
0101X1
010011
00XXX0
010000
Truth table0%0/2
A
A= $Zapp::Controller::Plan::a->{"last_run"}
Aresult
11
00
Truth table0%0/2
A
A= $Zapp::Controller::Plan::a->{"last_run"}{"finished"}
Aresult
11
00
Truth table0%0/2
A
A= $Zapp::Controller::Plan::a->{"last_run"}{"started"}
Aresult
11
00
254 0 $self->render( 'zapp/plan/list', plans => \@plans );
255 }
256  
257 ▶sub get_plan( $self ) {
Subroutine get_plan: not called CC=2 SCAR=17.9
Pod undocumented
258 0 ▶ my $plan = $self->app->get_plan( $self->param( 'plan_id' ) ) // return $self->reply->not_found;
Condition0%0/2
$self->app->get_plan($self->param("plan_id")) // (return $self->reply->not_found)
l!l
00
MC/DC0%0/1
$self->app->get_plan($self->param("plan_id")) // (return $self->reply->not_found)
$self->app->get_plan($self->param("plan_id"))
Truth table0%0/2
A
A= $self->app->get_plan($self->param("plan_id"))
Aresult
11
00
259 0 my @runs = $self->yancy->list( zapp_runs => { plan_id => $plan->{plan_id} }, { limit => 10 } );
260 0 my @triggers = $self->yancy->list( zapp_triggers => { plan_id => $plan->{plan_id} } );
261 0 return $self->render(
262 'zapp/plan/view',
263 plan => $plan,
264 runs => \@runs,
265 triggers => \@triggers,
266 );
267 }
268  
269 1 1;
270  
271 __END__
↑ Summary Zapp/Task/Request.pm »