FQStat/Actions.pm untested

filter: Statementstmt 0.0% 0 / 455 Branchbran 0.0% 0 / 172 Conditioncond 0.0% 0 / 84 MC/DCmcdc 0.0% 0 / 53 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 813 CC 87 SCAR 44.2
CC
87
Cov
0%
show_job_log
306.0
select_sort_field
156.0
change_priority
56.0
CRAP 82.9
1  
2 package App::FQStat::Actions;
3 # App::FQStat is (c) 2007-2009 Steffen Mueller
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the same terms as Perl itself.
7  
8 use strict;
9 use warnings;
10 use Time::HiRes qw/sleep time/;
11 use Term::ANSIScreen qw/RESET locate clline cls/;
12  
13 use App::FQStat::Drawing qw/printline update_display/;
14 use App::FQStat::Input qw/poll_user get_input_key select_multiple_jobs select_job/;
15 use App::FQStat::Debug;
16 use App::FQStat::Config qw/get_config set_config/;
17 use App::FQStat::Colors qw/get_color/;
18  
19  
20 ####################
21 # ACTIONS
22  
23 # Scrolling: set display offset with boundary checking:
24 sub scroll_up {
25 warnenter if ::DEBUG;
26 my $lines = shift || 1;
27 lock($::DisplayOffset);
28 $::DisplayOffset -= $lines;
29 $::DisplayOffset = 0 if $::DisplayOffset < 0;
30 }
31  
32 sub scroll_down {
33 warnenter if ::DEBUG;
34 my $lines = shift || 1;
35 lock($::DisplayOffset);
36 $::DisplayOffset += $lines;
37 my $limit = @{$::Records} - $::Termsize[1]+4;
38 $::DisplayOffset = $limit if $::DisplayOffset > $limit;
39 $::DisplayOffset = 0 if $::DisplayOffset < 0;
40 }
41  
42  
43 sub update_user_name {
44 warnenter if ::DEBUG;
45 my $input = poll_user("User name: ");
46 lock($::User);
47 if (not defined $input or $input =~ /^\s*$/) {
48 $::User = undef;
49 }
50 else {
51 $::User = $input;
52 }
53 update_display(1);
54 }
55  
56 sub set_user_interval {
57 warnenter if ::DEBUG;
58 my $input = poll_user("Desired update interval: ");
59 if (defined $input and $input =~ /^\s*[+-]?(?=\d|\.\d)\d*(?:\.\d*)?(?:[Ee][+-]?\d+)?\s*$/) {
60 $::UserInterval = $input+0;
61 lock($::Interval);
62 $::Interval = $::UserInterval;
63 }
64 update_display(1);
65 }
66  
67 sub select_sort_field {
68 warnenter if ::DEBUG;
69 my @cols = ('status', @::Columns);
70  
71 # determine start sort field
72 my $sort = 0;
73 my $colno = 0;
74 if (defined $::SortField) {
75 foreach my $col (@cols) {
76 if ($col eq $::SortField) {
77 $sort = $colno;
78 last;
79 }
80 $colno++;
81 }
82 }
83  
84 # key mappings
85 my %ckeys = (
86 'D' => sub { $sort--; $sort = @cols-1 if $sort < 0; }, # left
87 'C' => sub { $sort++; $sort = 0 if $sort >= @cols; }, # right
88 );
89  
90 # print instructions
91 locate(1,1);
92 clline();
93 print get_color("selected_cursor");
94 print "Select Sort Field:";
95 print RESET;
96 print " (left/right to select, s/Enter to confirm, n for none, q to cancel)\n";
97  
98 while (1) {
99 App::FQStat::Drawing::draw_header_line($sort+1);
100 my $input = get_input_key();
101 if (defined $input) {
102 if ($input eq 's' or $input =~ /\n/ or $input =~ /\r/) { # select
103 $::SortField = $cols[$sort];
104 App::FQStat::Scanner::sort_current($::Records);
105 return 1; # redraw
106 }
107 elsif ($input eq 'n') {
108 $::SortField = undef;
109 return 1; # redraw
110 }
111 elsif ($input eq 'q') {
112 return 1;
113 }
114 elsif ($input eq '[') {
115 my $key = get_input_key(0.01);
116 if (defined $key and exists $ckeys{$key}) {
117 $ckeys{$key}->($key);
118 }
119 }
120 } # end if defined input
121 } # end while
122 }
123  
124 sub toggle_reverse_sort {
125 warnenter if ::DEBUG;
126 ::debug "Reversing sort order";
127 lock($::RecordsReversed);
128 if ($::RecordsReversed == 1) { $::RecordsReversed = 0 }
129 else { $::RecordsReversed = 1 }
130 App::FQStat::Scanner::reverse_records($::Records);
131 return 1; # redraw
132 }
133  
134  
135 sub kill_jobs {
136 warnenter if ::DEBUG;
137 # print instructions
138 locate(1,1);
139 clline();
140 print get_color("user_instructions");
141 print "Kill jobs: Select with Space, span with 's', hit 'k' to kill or 'q' to cancel.";
142 print RESET;
143 print "\n";
144  
145 my ($selected, $key) = select_multiple_jobs( ['q', 'k'] );
146  
147 if ($key eq 'q' or @$selected == 0) {
148 # cancel
149 return 1; # redraw
150 }
151 elsif ($key eq 'k') {
152 my $confirm = poll_user("Really kill? Type 'yes' to kill: ");
153 if ($confirm =~ /^\s*yes\s*$/i) {
154 my $jobs = $::Records;
155 my @ids = sort { $a <=> $b } map { $jobs->[$_][::F_id] } @$selected;
156 cls();
157 locate(3,1);
158 print get_color("warning") . "Killing the following jobs:" . RESET();
159 print "\n", join("\n", @ids);
160 print "\n";
161 foreach my $dead (@ids) {
162 if ( App::FQStat::System::run(get_config("qdelcmd"), $dead) ) {
163 # This branch doesn't seem to be entered because qdel is braindead and doesn't
164 # signal failure with exit($POSITIVE_INTEGER)
165 print get_color("warning"), "WARNING: Something went wrong. Return value: $!", RESET;
166 print "\n(Hit Enter to continue)";
167 my $tmp = <STDIN>;
168 last;
169 }
170 }
171 sleep 2;
172 update_display(1);
173 return;
174 }
175 else {
176 return 1; # redraw
177 }
178 } # end if $key is 'k'
179 else {
180 die "Invalid key which stopped selection mode. (Sanity check)";
181 }
182 return 1;
183 }
184  
185  
186 sub change_priority {
187 warnenter if ::DEBUG;
188 # print instructions
189 locate(1,1);
190 clline();
191 print get_color("user_instructions");
192 print "Change priority: Select with Space, span with 's', hit 'p' to set priority or 'q' to cancel.";
193 print RESET;
194 print "\n";
195  
196 my ($selected, $key) = select_multiple_jobs( ['q', 'p'] );
197  
198 if ($key eq 'q' or @$selected == 0) {
199 # cancel
200 return 1; # redraw
201 }
202 elsif ($key eq 'p') {
203 my $prio = poll_user("Which priority should these jobs be set to? ");
204 if ($prio =~ /^\s*[+-]?\d+\s*$/i) {
205 my $integerprio = $prio+=0; # make an integer
206 my $jobs = $::Records;
207 my @ids = sort { $a <=> $b } map { $jobs->[$_][::F_id] } @$selected;
208 cls();
209 locate(3,1);
210 print get_color("warning"), "Setting priority for the following jobs:", RESET();
211 print "\n", join("\n", @ids);
212 print "\n";
213  
214 foreach my $job (@ids) {
215 if ( App::FQStat::System::run(get_config("qaltercmd"), '-p', $integerprio, $job) ) {
216 print "\n", get_color("warning"), "WARNING: Something went wrong. Return value: $!", RESET;
217 print "\n(Hit 'q' to quit or any other key to continue)";
218 my $tmp = get_input_key(1e9);
219 last if (defined $tmp and $tmp eq 'q');
220 }
221 print "\n";
222 }
223 sleep 2;
224 update_display(1);
225 return;
226 }
227 else {
228 return 1; # redraw
229 }
230 } # end if $key is 'p'
231 else {
232 die "Invalid key which stopped selection mode. (Sanity check)";
233 }
234 return 1;
235 }
236  
237  
238  
239 sub hold_jobs {
240 warnenter if ::DEBUG;
241 # print instructions
242 locate(1,1);
243 clline();
244 print get_color("user_instructions");
245 print "Hold jobs: Select with Space, span with 's', hit 'o' to hold or 'q' to cancel.";
246 print RESET;
247 print "\n";
248  
249 my ($selected, $key) = select_multiple_jobs( ['q', 'o'] );
250  
251 if ($key eq 'q' or @$selected == 0) {
252 # cancel
253 return 1; # redraw
254 }
255 elsif ($key eq 'o') {
256 my $jobs = $::Records;
257 my @ids = sort { $a <=> $b } map { $jobs->[$_][::F_id] } @$selected;
258 cls();
259 locate(3,1);
260 print get_color("warning"), "Setting the following jobs on hold:", RESET();
261 print "\n", join("\n", @ids);
262 print "\n";
263  
264 foreach my $job (@ids) {
265 if ( App::FQStat::System::run(get_config("qaltercmd"), '-h', 'u', $job) ) {
266 print "\n", get_color("warning"), "WARNING: Something went wrong. Return value: $!", RESET;
267 print "\n(Hit 'q' to quit or any other key to continue)";
268 my $tmp = get_input_key(1e9);
269 last if (defined $tmp and $tmp eq 'q');
270 }
271 print "\n";
272 }
273 sleep 2;
274 update_display(1);
275 return;
276 } # end if $key is 'o'
277 else {
278 die "Invalid key which stopped selection mode. (Sanity check)";
279 }
280 return 1;
281 }
282  
283  
284 sub resume_jobs {
285 warnenter if ::DEBUG;
286 # print instructions
287 locate(1,1);
288 clline();
289 print get_color("user_instructions");
290 print "Resume jobs: Select with Space, span with 's', hit 'o' or 'O' to resume or 'q' to cancel.";
291 print RESET;
292 print "\n";
293  
294 my ($selected, $key) = select_multiple_jobs( ['q', 'o', 'O'] );
295  
296 if ($key eq 'q' or @$selected == 0) {
297 # cancel
298 return 1; # redraw
299 }
300 elsif ($key eq 'o' or $key eq 'O') {
301 my $jobs = $::Records;
302 my @ids = sort { $a <=> $b } map { $jobs->[$_][::F_id] } @$selected;
303 cls();
304 locate(3,1);
305 print get_color("warning"), "Resuming the following jobs:", RESET();
306 print "\n", join("\n", @ids);
307 print "\n";
308  
309 foreach my $job (@ids) {
310 if ( App::FQStat::System::run(get_config("qaltercmd"), '-h', 'U', $job) ) {
311 print "\n", get_color("warning"), "WARNING: Something went wrong. Return value: $!", RESET;
312 print "\n(Hit 'q' to quit or any other key to continue)";
313 my $tmp = get_input_key(1e9);
314 last if (defined $tmp and $tmp eq 'q');
315 }
316 print "\n";
317 }
318 sleep 2;
319 update_display(1);
320 return;
321 } # end if $key is 'o' or 'O'
322 else {
323 die "Invalid key which stopped selection mode. (Sanity check)";
324 }
325 return 1;
326 }
327  
328  
329 sub clear_job_error_state {
330 warnenter if ::DEBUG;
331 # print instructions
332 locate(1,1);
333 clline();
334 print get_color("user_instructions");
335 print "Clear error state: Select with Space, span with 's', hit 'c' to apply or 'q' to cancel.";
336 print RESET;
337 print "\n";
338  
339 my ($selected, $key) = select_multiple_jobs( ['q', 'c'] );
340  
341 if ($key eq 'q' or @$selected == 0) {
342 # cancel
343 return 1; # redraw
344 }
345 elsif ($key eq 'c') {
346 my $jobs = $::Records;
347 my @ids = sort { $a <=> $b } map { $jobs->[$_][::F_id] } @$selected;
348 cls();
349 locate(3,1);
350 print get_color("warning"), "Clearing the error state of the following jobs:", RESET();
351 print "\n", join("\n", @ids);
352 print "\n";
353  
354 foreach my $job (@ids) {
355 if ( App::FQStat::System::run(get_config("qmodcmd"), '-cj', $job) ) {
356 print "\n", get_color("warning"), "WARNING: Something went wrong. Return value: $!", RESET;
357 print "\n(Hit 'q' to quit or any other key to continue)";
358 my $tmp = get_input_key(1e9);
359 last if (defined $tmp and $tmp eq 'q');
360 }
361 print "\n";
362 }
363 sleep 2;
364 update_display(1);
365 return;
366 } # end if $key is 'c'
367 else {
368 die "Invalid key which stopped selection mode. (Sanity check)";
369 }
370 return 1;
371 }
372  
373  
374 sub update_highlighted_user_name {
375 warnenter if ::DEBUG;
376 my $input = poll_user("User name to highlight: ");
377 if (not defined $input or $input =~ /^\s*$/) {
378 $::HighlightUser = undef;
379 update_display(1);
380 return;
381 }
382
383 my $regex;
384 eval { $regex = qr/$input/; };
385 if ($@ or not defined $regex) {
386 show_warning("Invalid regular expression!");
387 update_display(1);
388 return;
389 }
390 $::HighlightUser = $regex;
391 update_display(1);
392 return;
393 }
394  
395  
396 sub change_dependencies {
397 warnenter if ::DEBUG;
398 # print instructions
399 locate(1,1);
400 clline();
401 print get_color("user_instructions");
402 print "Change deps of jobs: Select with Space, span with 's', hit 'd' to confirm or 'q' to cancel.";
403 print RESET;
404 print "\n";
405  
406 my ($selected, $key) = select_multiple_jobs( ['q', 'd'] );
407  
408 if ($key eq 'q' or @$selected == 0) {
409 # cancel
410 return 1; # redraw
411 }
412 elsif ($key eq 'd') {
413 locate(1,1);
414 clline();
415 print get_color("user_instructions");
416 print "Jobs to depend on: Select with Space, span with 's', hit 'd' to confirm or 'q' to cancel.";
417 print RESET;
418 print "\n";
419  
420 my ($dependencies, $key) = select_multiple_jobs( ['q', 'd'] );
421 if ($key eq 'q' or @$dependencies == 0) {
422 # cancel
423 return 1; # redraw
424 }
425  
426 my $jobs = $::Records;
427 my $deplist = join (',', map { $jobs->[$_][::F_id] } @$dependencies);
428
429 my @ids = sort { $a <=> $b } map { $jobs->[$_][::F_id] } @$selected;
430 cls();
431 locate(3,1);
432 print get_color("warning"), "Changing the dependencies of the following jobs:", RESET();
433 print "\n", join("\n", @ids);
434 print "\n";
435  
436 foreach my $job (@ids) {
437 if ( App::FQStat::System::run(get_config("qaltercmd"), $job, '-hold_jid', $deplist) ) {
438 print "\n", get_color("warning"), "WARNING: Something went wrong. Return value: $!", RESET;
439 print "\n(Hit 'q' to quit or any other key to continue)";
440 my $tmp = get_input_key(1e9);
441 last if (defined $tmp and $tmp eq 'q');
442 }
443 print "\n";
444 }
445 sleep 2;
446 update_display(1);
447 return;
448 } # end if $key is 'o'
449 else {
450 die "Invalid key which stopped selection mode. (Sanity check)";
451 }
452 return 1;
453 }
454  
455  
456  
457 sub show_job_details {
458 warnenter if ::DEBUG;
459 # print instructions
460 locate(1,1);
461 clline();
462 print get_color("user_instructions");
463 print "Show job details: Select with Space/Enter, 'q' to cancel.";
464 print RESET;
465 print "\n";
466  
467 my ($selected, $key) = select_job();
468  
469 if ($key eq 'q' or @$selected == 0) {
470 # cancel
471 return 1; # redraw
472 }
473 else {
474 my $jobs = $::Records;
475 my $id = $jobs->[ $selected->[0] ][::F_id]; # get job id
476 my $qstat = get_config("qstatcmd");
477 my $output = App::FQStat::System::run_capture($qstat, '-j', $id); # perhaps IPC::Cmd or IPC::Run or even just Open3 would be better here?
478 if ($output =~ /^\s*$/ms) {
479 print get_color("warning"), "WARNING: Something went wrong. Return value: $!", RESET;
480 print "\n(Hit Enter to continue)";
481 my $tmp = <STDIN>;
482 return 1;
483 }
484 cls();
485 my $color = get_color("warning");
486 my $reset = RESET;
487 $output =~ s/^([^:]+:)/$color$1$reset/mg;
488 print $output;
489 print "\n(Hit any key to continue)";
490 my $tmp = get_input_key(1e9);
491  
492 # FIXME! We could do better than just puke to STDOUT.
493 } # end if selection okay
494 return 1; # doesn't happen
495 }
496  
497  
498  
499  
500 sub show_job_log {
501 warnenter if ::DEBUG;
502 # print instructions
503 locate(1,1);
504 clline();
505 print get_color("user_instructions");
506 print "Show job log: Select with Space/Enter or 'l', 'q' to cancel.";
507 print RESET;
508 print "\n";
509  
510 my ($selected, $key) = select_job(['q','l']);
511  
512 if ($key eq 'q' or @$selected == 0) {
513 # cancel
514 return 1; # redraw
515 }
516 else {
517 my $jobs = $::Records;
518 my $id = $jobs->[ $selected->[0] ][::F_id]; # get job id
519 my $qstat = get_config("qstatcmd");
520 my $output = App::FQStat::System::run_capture($qstat, '-j', $id); # perhaps IPC::Cmd or IPC::Run or even just Open3 would be better here?
521 if ($output =~ /^\s*$/ms) {
522 print get_color("warning"), "WARNING: Something went wrong. Return value: $!", RESET;
523 print "\n(Hit Enter to continue)";
524 my $tmp = <STDIN>;
525 return 1;
526 }
527 cls();
528  
529 my @o = split /\n/, $output;
530 my $err;
531 my $out;
532 my $cwd;
533 foreach my $line (@o) {
534 if ( $line =~ /^std(err|out)_path_list:/ ) {
535 my $match = $1;
536 $line =~ s/^std(?:err|out)_path_list:\s*//;
537 chomp $line;
538 if ($match eq 'err') { $err = $line }
539 else { $out = $line }
540 last if defined $out and defined $err;
541 }
542 elsif ( $line =~ /^cwd:/ ) {
543 $cwd = $line;
544 $cwd =~ s/^cwd:\s*//;
545 chomp $cwd;
546 }
547 }
548 $err =~ s/^stderr_path_list:\s*// if defined $err;
549 $out =~ s/^stdout_path_list:\s*// if defined $out;
550  
551 if ( not defined $cwd ) {
552 print "Could not determine current working directory for locating the logs.\n";
553 my $tmp = get_input_key(300);
554 return 1;
555 }
556 elsif ( not defined $out and not defined $err ) {
557 # couldn't find log
558 print "No log could be found for this job. Owner didn't set stdout nor stderr redirection?\n";
559 my $tmp = get_input_key(300);
560 return 1;
561 }
562 elsif ( defined $out and defined $err and $out eq $err ) {
563 undef $err; # only show once
564 }
565  
566 my $cmd;
567 if (defined $out) {
568 if ($out !~ /^\//) {
569 $out = File::Spec->catdir($cwd, $out);
570 }
571 $cmd .= " $out";
572 }
573 if (defined $err) {
574 if ($err !~ /^\//) {
575 $err = File::Spec->catdir($cwd, $err);
576 }
577 $cmd .= " $err";
578 }
579 App::FQStat::System::run("sh", '-c', qq(cat $cmd | less));
580  
581 } # end if selection okay
582 return 1; # doesn't happen
583 }
584  
585  
586 sub delete_color_scheme {
587 warnenter if ::DEBUG;
588 my $name = poll_user("Delete which color scheme? ");
589 if ($name =~ /^\s*(\w+)\s*$/i) {
590 my $schemeName = lc($1);
591 return 1 if $schemeName eq 'default';
592 my $schemes = get_config("color_schemes");
593 if (exists($schemes->{$schemeName})) {
594 delete $schemes->{$schemeName};
595 }
596 }
597 return 1;
598 }
599  
600 sub save_color_scheme {
601 warnenter if ::DEBUG;
602 my $name = poll_user("Save as which color scheme? ");
603 if ($name =~ /^\s*(\w+)\s*$/i) {
604 my $schemeName = lc($1);
605 return 1 if $schemeName eq 'default';
606 my $schemes = get_config("color_schemes");
607 $schemes->{$schemeName} = {%{ get_config('colors') }};
608 }
609 return 1;
610 }
611  
612 sub toggle_summary_mode {
613 warnenter if ::DEBUG;
614 {
615 lock($::SummaryMode);
616 $::SummaryMode = ($::SummaryMode+1) % 2;
617 set_config("summary_mode", $::SummaryMode);
618 }
619 return 1;
620 }
621  
622  
623 sub toggle_summary_name_clustering {
624 warnenter if ::DEBUG;
625 $::Summary = [];
626 my $cluster = get_config("summary_clustering")||0;
627 $cluster = ($cluster+1)%2;
628 set_config("summary_clustering", $cluster);
629 return 1;
630 }
631  
632  
633 sub show_manual {
634 warnenter if ::DEBUG;
635 cls();
636 my $heading = get_color("menu_normal");
637 my $h = get_color("warning");
638 my $r = RESET;
639 print <<"HERE";
640 ${heading} fqstat v$App::FQStat::VERSION - Interactive front-end for qstat $r
641 Commands:
642 ${h}'h' ${r} Show this (H)elp screen
643 ${h}'q' ${r} (Q)uit
644 ${h}F10 ${r} Show Menu
645 ${h}F5 ${r} Refresh data from qstat and redraw
646 ${h}'S' ${r} Toggle Summary Mode
647 ${h}Up- / Down-Arrow${r} Scroll up/down if possible
648 ${h}Page-Up / -Down ${r} Scroll one page up/down if possible
649 ${h}Pos1 / End ${r} Jump to beginning / end
650 ${h}Space / Enter ${r} Show detailed job info
651  
652 ${h}'u' ${r} Enter (U)ser name whose jobs to display
653 ${h}'H' ${r} (H)ightlight a user's jobs
654 ${h}'i' ${r} Set the desired update (I)nterval
655 ${h}'s' ${r} Select the field to (S)ort by
656 ${h}'r' ${r} Toggle display (R)eversal
657 ${h}'l' ${r} Show job (l)og
658 ${h}'k' ${r} (Kill), Select jobs for Deletion
659 ${h}'p' ${r} Change (P)riority of selected jobs
660 ${h}'o/O' ${r} H(o)ld jobs / Resume j(O)bs
661 ${h}'c' ${r} (C)lear error state of jobs
662 (In Summary Mode: Toggle Clustering)
663 ${h}'d' ${r} Change job (d)ependencies
664 fqstat is (c) 2007-2009 Steffen Mueller. This program is free software; you
665 can redistribute it and/or modify it under the same terms as Perl itself.
666 HERE
667 my $input = Term::ReadKey::ReadKey(1e9);
668 return 1; # redraw
669 }
670  
671  
672  
673 1;
674  
675