ETLp/Audit/Browser/Controller/Schedule.pm untested

filter: Statementstmt 0.0% 0 / 138 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 200 CC 14 SCAR 27.2
CC
14
Cov
0%
edit
30.0
save
30.0
_schedule_profile
20.0
CRAP 15.1
1 package ETLp::Audit::Browser::Controller::Schedule;
2 use Moose;
3 extends 'ETLp::Audit::Browser::Controller::Base';
4 use CGI::Application::Plugin::ValidateRM (qw/check_rm/);
5 use Data::Dumper;
6  
7 sub list {
8 my $self = shift;
9 my $q = $self->query;
10 my $config_name = $q->param('config_name');
11 my $section_name = $q->param('section_name');
12 my $status = $q->param('status');
13 my $page = $q->param('page') || 1;
14 my $sections;
15  
16 $self->logger->debug("Looking for schedules");
17 my $schedules = $self->model->get_schedule_details(
18 {
19 config_name => $config_name,
20 section_name => $section_name,
21 status => $status,
22 page => $page,
23 no_paging => 1
24 }
25 );
26 $self->logger->debug("Got schedules");
27  
28 my $scheduler_status = $self->model->get_scheduler_status;
29  
30 my $config_files =
31 $self->model->get_config_files($self->conf->param('app_conf_dir'));
32  
33 if ($config_name) {
34 $sections =
35 $self->model->get_sections(
36 $self->conf->param('app_conf_dir') . '/' . $config_name);
37 }
38  
39 return $self->tt_process(
40 {
41 schedules => $schedules,
42 config_files => $config_files,
43 config_name => $config_name,
44 section_name => $section_name,
45 sections => $sections,
46 status => $status,
47 scheduler_status => $scheduler_status
48 }
49 );
50 }
51  
52 # Form for editing a schedule
53 sub edit {
54 my $self = shift;
55 my $errs = shift;
56 my $q = $self->query;
57 my ($config, $section, $sections, $dependencies);
58  
59 my $schedule_id = $q->param('schedule_id') || undef;
60 my $schedule = $self->model->get_schedule($schedule_id);
61 my $months = $self->model->get_months;
62 my $dows = $self->model->get_dows;
63 my $schedule_month = $self->model->get_schedule_month($schedule_id);
64 my $schedule_dows = $self->model->get_schedule_dows($schedule_id);
65 my $schedule_doms = $self->model->get_contracted_doms($schedule_id);
66 my $schedule_minutes = $self->model->get_contracted_minutes($schedule_id);
67 my $schedule_hours = $self->model->get_contracted_hours($schedule_id);
68 my $config_files =
69 $self->model->get_config_files($self->conf->param('app_conf_dir'));
70  
71 $self->logger->debug('Available config files: ' . Dumper($config_files));
72
73 $dependencies = [];
74  
75 if ($schedule) {
76 ($config, $section) = $self->model->get_job($schedule);
77  
78 $self->logger->debug("Config: $config");
79 $self->logger->debug("Section: $section");
80  
81 $sections =
82 $self->model->get_sections(
83 $self->conf->param('app_conf_dir') . '/' . $config);
84  
85 $self->logger->debug('Sections: ' . Dumper($sections));
86 $dependencies = [
87 $self->model->get_dependencies(
88 #{
89 config_dir => $self->conf->param('app_conf_dir'),
90 config_file => $config,
91 section => $section
92 #}
93 )
94 ];
95 } else {
96 if (@$config_files > 0) {
97 $sections =
98 $self->model->get_sections(
99 $self->conf->param('app_conf_dir') . '/' . $config_files->[0]);
100 $config = $config_files->[0] unless $config;
101  
102 if (@$sections > 0) {
103 $dependencies = [
104 $self->model->get_dependencies(
105 #{
106 config_dir => $self->conf->param('app_conf_dir'),
107 config_file => $config,
108 section => $sections->[0],
109 #}
110 )
111 ];
112 }
113 }
114 }
115  
116 return $self->tt_process(
117 {
118 schedule => $schedule,
119 months => $months,
120 dows => $dows,
121 schedule_month => $schedule_month,
122 schedule_dows => $schedule_dows,
123 schedule_doms => $schedule_doms,
124 schedule_minutes => $schedule_minutes,
125 schedule_hours => $schedule_hours,
126 config => $config,
127 section => $section,
128 config_files => $config_files,
129 sections => $sections,
130 dependencies => join(' -> ', @$dependencies),
131 errs => $errs,
132 }
133 );
134 }
135  
136 # Save the schedule
137 sub save {
138 my $self = shift;
139  
140 # validate user input
141 my ($results, $err_page) = $self->check_rm('edit', '_schedule_profile');
142 return $err_page if ($err_page);
143  
144 my $q = $results->valid;
145  
146 my $crontab_file =
147 $self->conf->param('crontab_publish_dir') . '/'
148 . $self->conf->param('crontab_owner');
149 my $pipeline_script = $self->conf->param('pipeline_script');
150  
151 if ($q->{'delete'}) {
152 $self->model->delete($q->{schedule_id});
153 $self->model->publish_crontab(
154 {
155 crontab_file => $crontab_file,
156 pipeline_script => $pipeline_script
157 }
158 );
159 return $self->redirect($self->conf->param('root_url') . '/schedule');
160 }
161  
162 # We've concatenated the config file name to the section name to ensure
163 # that the section name is unique across config files (so that the
164 # Javascript onChange event would fire if an Ajax call updated a section
165 # with the same section name from a different config). However, we now need
166 # to strip the config_file off again
167 my $config_file = $q->{config_file};
168 if ($q->{section} =~ /^$config_file\-(.*)$/) {
169 $q->{section} = $1;
170 }
171  
172 $q->{user_id} = $self->session->param('user_id');
173  
174 $self->logger->debug(Dumper($q));
175 my $schedule_id = $self->model->save($q);
176  
177 $self->logger->debug("Crontab file: $crontab_file");
178 $self->logger->debug("Pipeline script: $pipeline_script");
179  
180 #$self->model->publish_crontab(
181 # {crontab_file => $crontab_file, pipeline_script => $pipeline_script});
182  
183 if ($q->{schedule_id}) {
184 $self->session->param('message', 'Schedule Updated');
185 } else {
186 $self->session->param('message', 'Schedule Created');
187 }
188  
189 return $self->redirect(
190 $self->conf->param('root_url') . '/schedule/edit/' . $schedule_id);
191 }
192  
193 # Process ajax call to update the section list drop down if the config item
194 # changes
195 sub update_sections {
196 my $self = shift;
197 my $config_file = $self->query->param('config_file');
198 my $show_blank = $self->query->param('show_blank') || 0;
199
200 return $self->model->get_section_options(
201 {
202 app_conf_dir => $self->conf->param('app_conf_dir'),
203 config_file => $config_file,
204 show_blank => $show_blank
205 }
206 );
207 }
208  
209 # Return the crontab from the database
210 sub view_crontab {
211 my $self = shift;
212 my $crontab =
213 $self->model->generate_crontab($self->conf->param('pipeline_script'));
214 return $self->tt_process({crontab => $crontab});
215 }
216  
217 # Disable the entire crontab
218 sub disable_crontabe {
219 my $self = shift;
220 return $self->_set_crontab_status('disabled');
221 }
222  
223 # Enable the entire crontab
224 sub enable_crontab {
225 my $self = shift;
226 return $self->_set_crontab_status('enabled');
227 }
228  
229 sub _set_crontab_status {
230 my $self = shift;
231 my $status = shift;
232  
233 my $crontab_file =
234 $self->conf->param('crontab_publish_dir') . '/'
235 . $self->conf->param('crontab_owner');
236  
237 my $pipeline_script = $self->conf->param('pipeline_script');
238  
239 # save the status to the database
240 $self->model->set_scheduler_status($status);
241  
242 $self->logger->debug("Publishing the crontab");
243 # Publish the crontab now that the status has changed
244 $self->model->publish_crontab(
245 {crontab_file => $crontab_file, pipeline_script => $pipeline_script});
246  
247 return $self->redirect($self->conf->param('root_url') . '/schedule/');
248 }
249  
250 # This runmode is used to return the chain of pipelines dependent on the one
251 # provided
252 sub update_dependencies {
253 my $self = shift;
254 my $config_file = $self->query->param('config');
255 my $section = $self->query->param('section');
256  
257 if ($section =~ /^$config_file\-(.*)$/) {
258 $section = $1;
259 }
260  
261 return join(
262 ' -> ',
263 $self->model->get_dependencies(
264 #{
265 config_dir => $self->conf->param('app_conf_dir'),
266 config_file => $config_file,
267 section => $section
268 #}
269 )
270 );
271 }
272  
273 sub module {
274 return 'Schedules';
275 }
276  
277 # This is a validation profile that checks the user inut for a schedule.
278 # Refer to http://search.cpan.org/dist/Data-FormValidator for documentation
279 # of its syntax
280 sub _schedule_profile {
281 my $self = shift;
282 return {
283 required => [qw/schedule_description config_file section/],
284 optional => [
285 qw/schedule_comment dow_id schedule_hours schedule_minutes status
286 schedule_dom_id month_id schedule_doms schedule_id delete/
287 ],
288 constraints => {
289 dow_id => sub {
290 return $self->model->validate_days_of_week(shift);
291 },
292 schedule_hours => sub {
293 return $self->model->validate_hours(shift);
294 },
295 schedule_minutes => sub {
296 return $self->model->validate_minutes(shift);
297 },
298 status => qr/^1$/,
299 schedule_dom_id => sub {
300 return $self->model->validate_days_of_month(shift);
301 },
302 schedule_doms => sub {
303 return $self->model->validate_days_of_month(shift);
304 },
305 schedule_id => qr/^\d+$/,
306 month_id => qr/^\d+$/,
307 delete => qr/^Delete$/,
308 config_file => [
309 # Check that the config file exists
310 {
311 name => 'no_config',
312 constraint => sub {
313 my ($self, $config_file) = @_;
314 $config_file =
315 $self->conf->param('app_conf_dir') . '/'
316 . $config_file;
317 return $self->model->config_exists($config_file);
318 },
319 params => [$self, 'config_file']
320 },
321 # Ensure that the section actually exists in the config file
322 {
323 name => 'no_section',
324 constraint => sub {
325 my ($self, $config_file, $section) = @_;
326  
327 # Remove the prefixed config name from the section name
328 if ($section =~ /^$config_file\-(.*)$/) {
329 $section = $1;
330 }
331  
332 $config_file =
333 $self->conf->param('app_conf_dir') . '/'
334 . $config_file;
335 # There's no point in continuing unless the config file
336 # exists. But the check above raises the error so we
337 # will return 1 (true) if it doesn't. We don't want to
338 # return two errors for the same thing
339 return 1
340 unless ($self->model->config_exists($config_file));
341  
342 my $sections = $self->model->get_sections($config_file);
343 return grep(/^$section$/, @$sections) ? 1 : 0;
344 },
345 params => [$self, qw/config_file section/]
346 }
347 ]
348 },
349 msgs => {
350 any_errors => 'some_errors',
351 constraints => {
352 no_config => 'The config file does not exist',
353 no_section => 'The section is not on the config file',
354 }
355 }
356 };
357 }
358  
359 sub setup {
360 my $self = shift;
361 $self->start_mode('list');
362 $self->run_modes(
363 [
364 qw/list edit save update_sections view_crontab
365 disable_crontab enable_crontab update_dependencies/
366 ]
367 );
368 }
369 1;