Ordo/Command/Ls.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 51 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 230 CC 20 SCAR 43.4
CC
20
Cov
0%
_print_table
182.0
_add_cluster_and_children
30.0
execute
20.0
CRAP 77.1
1 package App::Ordo::Command::Ls;
2 use Moo;
3 use feature qw(say);
4 use utf8;
5 use open ':std', ':utf8';
6  
7 extends 'App::Ordo::Command::Base';
8  
9 use App::Ordo qw($CURRENT_PATH epoch_to_tminus epoch_to_duration);
10 use Term::ANSIColor qw(colored);
11 use Text::Table::Tiny 1.02 qw(generate_table);
12 use JSON::PP;
13  
14 sub name { "ls" }
15 sub summary { "List jobs and clusters in current path" }
16 sub usage { "<path> [--wide|-l] [--deps] [--tree] [--json]" }
17  
18 sub option_spec {
19 return {
20 'wide|l' => 'Show all columns including %CPU/%MEM/NEEDS',
21 'json|j' => 'Output as JSON',
22 };
23 }
24  
25 has 'base_cluster_id' => (is => 'rw');
26 has 'cluster_by_id' => (is => 'rw');
27 has 'items' => (is => 'rw');
28 has 'json' => (is => 'rw', default => sub { JSON::PP->new->ascii->pretty->allow_nonref } );
29  
30 sub execute {
31 my ( $self, $opt, $name ) = @_;
32  
33 my $res = $self->api->call( 'find_cluster', { name => $name } );
34  
35 unless ( $res->{success} && $res->{clusters} && @{ $res->{clusters} } ) {
36 say colored( ["bold yellow"], "No items found in current path" );
37 return;
38 }
39  
40 # Show local time
41 my $now = scalar localtime;
42 say colored( ["bright_black"], "Local time is $now" ) unless $opt->{json};
43  
44 $self->base_cluster_id($res->{clusters}[0]->{id});
45 $self->items([]);
46  
47 $self->cluster_by_id( { map { $_->{id} => $_ } @{ $res->{clusters} } } );
48  
49 # Build hierarchy from current cluster
50 my $base_path = $CURRENT_PATH;
51 $self->_add_cluster_and_children( $self->base_cluster_id );
52  
53 if ( $opt->{json} ) {
54 print $self->json->encode($res);
55 return;
56 }
57  
58 $self->_print_table( $self->items, $opt );
59 }
60  
61 sub _add_cluster_and_children {
62 my ( $self, $cluster_id, $base_path ) = @_;
63  
64 my $cluster = $self->cluster_by_id->{$cluster_id} or return;
65  
66 my $cluster_path = $cluster_id == $self->base_cluster_id ? ''
67 : $base_path ? "$base_path/$cluster->{name}"
68 : $cluster->{name};
69 push @{ $self->items },
70 {
71 id => $cluster->{id},
72 full_path => $cluster_path,
73 jobstate => $cluster->{jobstate} || '-',
74 server_name => '',
75 pid => '',
76 last_start => epoch_to_tminus( $cluster->{started} ),
77 duration => epoch_to_duration( $cluster->{started}, $cluster->{ended} ),
78 next_start => epoch_to_tminus( $cluster->{next_start} ),
79 cal_name => $cluster->{cal_name},
80 pctcpu => '',
81 pctmem => '',
82 needs => $cluster->{needs} || {},
83 is_cluster => 1,
84 } unless $cluster_id == $self->base_cluster_id;
85  
86 # Jobs
87 my @jobs = sort { $a->{id} <=> $b->{id} } @{ $cluster->{jobs} || [] };
88 for my $job (@jobs) {
89 push @{ $self->items },
90 {
91 id => $job->{id},
92 full_path => $cluster_path ? "$cluster_path/$job->{name}" : $job->{name},
93 jobstate => $job->{jobstate} || '-',
94 server_name => $job->{server_name} || '',
95 pid => $job->{pid} // '',
96 last_start => epoch_to_tminus( $job->{started} ),
97 duration => epoch_to_duration( $job->{started}, $job->{ended} ),
98 next_start => epoch_to_tminus( $job->{next_start} ),
99 cal_name => '',
100 pctcpu => $job->{pctcpu} // '',
101 pctmem => $job->{pctmem} // '',
102 needs => $job->{needs} || {},
103 is_cluster => 0,
104 };
105 }
106  
107 # Sub-clusters
108 my @children = grep { $_->{parent_id} && $_->{parent_id} == $cluster_id } values %{ $self->cluster_by_id };
109 @children = sort { $a->{id} <=> $b->{id} } @children;
110  
111 for my $child (@children) {
112 $self->_add_cluster_and_children( $child->{id}, $cluster_path );
113 }
114 }
115  
116 sub _print_table {
117 my ( $self, $items, $opt ) = @_;
118  
119 my @headers = ( "ID", "PATH", "STATE", "SERVER", "PID", "LAST_START", "DURATION", "NEXT_START", "CALENDAR" );
120 push @headers, ( "%CPU", "%MEM", "NEEDS" ) if $opt->{wide};
121  
122 my @rows = ( \@headers );
123  
124 unshift @$items, {
125 id => $self->base_cluster_id,
126 full_path => $self->cluster_by_id->{$self->base_cluster_id}->{name},
127 jobstate => $self->cluster_by_id->{$self->base_cluster_id}->{jobstate},
128 is_cluster => 1.
129 };
130  
131 for my $item (@$items) {
132 my @deps = sort keys %{ $item->{needs} || {} };
133  
134 my $state_color =
135 $item->{jobstate} =~ /^(complete|ice|prunded)/ ? 'green'
136 : $item->{jobstate} eq 'immutable' ? 'cyan'
137 : $item->{jobstate} eq 'running' ? 'magenta'
138 : $item->{jobstate} =~ /^(failed|hold|zombie)/ ? 'red'
139 : $item->{jobstate} =~ /^(ready|waiting|looping|retrying)$/ ? 'yellow'
140 : 'white';
141  
142 my $path_display =
143 $item->{is_cluster}
144 ? colored( ["bold blue"], $item->{full_path} . '/')
145 : $item->{full_path};
146  
147 unless ($self->base_cluster_id == $item->{id} && $item->{is_cluster}) {
148 $path_display = ' ' . $path_display;
149 }
150  
151 my @row = (
152 $item->{id}, $path_display,
153 colored( ["bold $state_color"], $item->{jobstate} ),
154 $item->{server_name} || '',
155 $item->{pid} // '',
156 $item->{last_start} || '',
157 $item->{duration} || '',
158 $item->{next_start} || '',
159 $item->{cal_name} ? $item->{cal_name} : '',
160 );
161  
162 push @row, ( $item->{pctcpu} // '', $item->{pctmem} // '', @deps ? join( ",", @deps ) : '', ) if $opt->{wide};
163  
164 if ( $opt->{deps} && @deps ) {
165 push @rows, \@row;
166 push @rows, [ ('') x 9, "← $_" ] for @deps;
167 }
168 else {
169 push @rows, \@row;
170 }
171 }
172  
173 say generate_table(
174 rows => \@rows,
175 header_row => 1,
176 style => 'boxrule',
177 );
178 }
179  
180 1;