blib/lib/HPC/Runner/Command/stats.pm

filter: Statementstmt 45.1% 51 / 113 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 55.0% 11 / 20 Podpod n/a totaltotal 30.3% 62 / 204 CC 28 SCAR 37.4
CC
28
Cov
30%
BUILD
90.0
search_submission
90.0
iter_submissions
30.0
CRAP 41.9
« blib/lib/HPC/Runner/Command/execute_job/Utils/MCE.pm ↑ Summary blib/lib/HPC/Runner/Command/execute_job/Base.pm »
1 package HPC::Runner::Command::stats;
2  
3 1 ▶use MooseX::App::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moose::Util qw/apply_all_roles/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 extends 'HPC::Runner::Command';
6  
7 1 ▶use MooseX::Types::Path::Tiny qw/File Path Paths AbsPath AbsFile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 with 'HPC::Runner::Command::Logger::JSON';
9 1 with 'HPC::Runner::Command::stats::Logger::JSON::Summary';
10 1 with 'HPC::Runner::Command::stats::Logger::JSON::Long';
11 1 with 'HPC::Runner::Command::Logger::Loggers';
12  
13 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Capture::Tiny ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 command_short_description 'Query submissions by project, or jobname';
23 1 command_long_description 'Query submissions by project, or jobname. ' . 'This
24 searches through the tars created during execution.'
25 . ' If you have a large number
26 of submissions you may want to specify a project, '
27 . 'or supply the desired submission with --data_tar.';
28  
29 option 'most_recent' => (
30 is => 'rw',
31 isa => 'Bool',
32 required => 0,
33 default => 1,
34 documentation => q(Show only the most recent submission.),
35 trigger => sub {
36 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
37 0 ▶ $self->all(1) if !$self->most_recent;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->most_recent 0 0 0
38 }
39 1 );
40  
41 option 'all' => (
42 is => 'rw',
43 isa => 'Bool',
44 required => 0,
45 default => 0,
46 documentation => 'Show all submissions.',
47 trigger => sub {
48 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
49 0 ▶ $self->most_recent(1) if !$self->all;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->all 0 0 0
50 },
51 1 cmd_aliases => ['a'],
52 );
53  
54 1 option '+project' => ( documentation => 'Query by project.', );
55  
56 1 option 'jobname' => (
57 is => 'rw',
58 isa => 'Str',
59 documentation => 'Query by jobname',
60 required => 0,
61 predicate => 'has_jobname',
62 );
63  
64 1 option 'summary' => (
65 is => 'rw',
66 isa => 'Bool',
67 documentation =>
68 'Summary view of your jobs - Number of running, completed, failed, successful.',
69 required => 0,
70 default => 1,
71 );
72  
73 option 'long' => (
74 is => 'rw',
75 isa => 'Bool',
76 documentation =>
77 'Long view. More detailed report - Task tags, exit codes, duration, etc.',
78 required => 0,
79 default => 0,
80 trigger => sub {
81 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
82 0 ▶ $self->summary(0) if $self->long;
Branch: 1 branch
BranchTrueFalseTotal
if $self->long 0 0 0
83 },
84 1 cmd_aliases => ['l'],
85 );
86  
87 1 option 'json' => (
88 is => 'rw',
89 isa => 'Bool',
90 default => 0,
91 documentation => 'Output data in json instead of a table. '
92 . 'This option suppresses logging output.',
93 cmd_aliases => ['j'],
94 );
95  
96 has 'task_data' => (
97 is => 'rw',
98 isa => 'HashRef',
99 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 1 clearer => 'clear_task_data',
101 );
102  
103 1 parameter 'stats_type' => (
104 is => 'rw',
105 isa => 'Str',
106 default => 'JSON',
107 documentation => 'hpcrunner.pl stats JSON/Sqlite/Elasticsearch .',
108 );
109  
110 sub BUILD {
111 0 ▶ my $self = shift;
Subroutine BUILD: not called CC=9 SCAR=45.0
112  
113 0 ▶ if ( $self->json && $self->summary ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->json and $self->summary) { } 0 0 0
elsif (not $self->json and $self->summary) { } 0 0 0
elsif ($self->json and $self->long) { } 0 0 0
elsif (not $self->json and $self->long) { } 0 0 0
Condition0%0/3
$self->json and $self->summary
!ll&&!rl&&r
000
MC/DC0%0/2
$self->json and $self->summary
$self->json $self->summary
Truth table0%0/3
A and B
A= $self->jsonB= $self->summary
ABresult
0X0
100
111
114 0 apply_all_roles( $self,
115 'HPC::Runner::Command::stats::Logger::'
116 . $self->stats_type
117 . '::Summary::JSONOutput' );
118 }
119 ▶ elsif ( !$self->json && $self->summary ) {
Condition0%0/3
not $self->json and $self->summary
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->json and $self->summary
not $self->json $self->summary
Truth table0%0/3
not(A) and B
A= not $self->jsonB= $self->summary
ABresult
0X0
100
111
120 0 apply_all_roles( $self,
121 'HPC::Runner::Command::stats::Logger::'
122 . $self->stats_type
123 . '::Summary::TableOutput' );
124 }
125 ▶ elsif ( $self->json && $self->long ) {
Condition0%0/3
$self->json and $self->long
!ll&&!rl&&r
000
MC/DC0%0/2
$self->json and $self->long
$self->json $self->long
Truth table0%0/3
A and B
A= $self->jsonB= $self->long
ABresult
0X0
100
111
126 0 apply_all_roles( $self,
127 'HPC::Runner::Command::stats::Logger::'
128 . $self->stats_type
129 . '::Long::JSONOutput' );
130 }
131 ▶ elsif ( !$self->json && $self->long ) {
Condition0%0/3
not $self->json and $self->long
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->json and $self->long
not $self->json $self->long
Truth table0%0/3
not(A) and B
A= not $self->jsonB= $self->long
ABresult
0X0
100
111
132 0 apply_all_roles( $self,
133 'HPC::Runner::Command::stats::Logger::'
134 . $self->stats_type
135 . '::Long::TableOutput' );
136 }
137 }
138  
139 sub execute {
140 0 ▶ my $self = shift;
Subroutine execute: not called CC=1 SCAR=6.9
141  
142 0 $self->iter_submissions;
143 }
144  
145 sub iter_submissions {
146 0 ▶ my $self = shift;
Subroutine iter_submissions: not called CC=5 SCAR=34.0
147  
148 0 my $results = $self->get_submissions();
149  
150 0 foreach my $result ( @{$results} ) {
151 0 $self->data_dir($result);
152 0 my $submission_file =
153 File::Spec->catdir( $self->data_dir, 'submission.json' );
154 0 ▶ if ( -e $submission_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $submission_file) { } 0 0 0
155 0 my $submission_json = read_file($submission_file);
156 0 my $submission = decode_json($submission_json);
157 0 my $jobref = $submission->{jobs};
158  
159 0 ▶ $self->iter_jobs_summary( $submission, $jobref )
Branch: 1 branch
BranchTrueFalseTotal
if $self->summary 0 0 0
160 if $self->summary;
161 0 ▶ $self->iter_jobs_long( $submission, $jobref ) if $self->long;
Branch: 1 branch
BranchTrueFalseTotal
if $self->long 0 0 0
162 }
163 else {
164 0 $self->screen_log->info( 'Data Tar '
165 . $self->data_dir
166 . ' does not contain any submission info!' );
167 }
168 }
169  
170 }
171  
172 sub get_submissions {
173 0 ▶ my $self = shift;
Subroutine get_submissions: not called CC=5 SCAR=34.0
174  
175 ## Skip over this searching nonsense
176 0 my $results;
177 0 ▶ if ( $self->has_data_dir && $self->data_dir->exists ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_data_dir and $self->data_dir->exists) { } 0 0 0
elsif ($self->has_data_dir and not $self->data_dir->exists) { } 0 0 0
Condition0%0/3
$self->has_data_dir and $self->data_dir->exists
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_data_dir and $self->data_dir->exists
$self->has_data_dir $self->data_dir->exists
Truth table0%0/3
A and B
A= $self->has_data_dirB= $self->data_dir->exists
ABresult
0X0
100
111
178 0 my $file = $self->data_dir;
179 0 $results = ["$file"];
180 }
181 ▶ elsif ( $self->has_data_dir && !$self->data_dir->exists ) {
Condition0%0/3
$self->has_data_dir and not $self->data_dir->exists
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_data_dir and not $self->data_dir->exists
$self->has_data_dir not $self->data_dir->exists
Truth table0%0/3
A and not(B)
A= $self->has_data_dirB= not $self->data_dir->exists
ABresult
0X0
100
111
182 0 $self->screen_log->fatal(
183 'You have supplied a data tar that does not exist.');
184 0 $self->screen_log->fatal(
185 'Data Tar ' . $self->data_dir . ' does not exist' );
186 0 exit 1;
187 }
188 else {
189 0 $results = $self->search_submission;
190 }
191  
192 0 return $results;
193 }
194  
195 sub search_submission {
196 0 ▶ my $self = shift;
Subroutine search_submission: not called CC=9 SCAR=45.0
197  
198 0 my $data_path = "";
199 0 my $data_dir = File::Spec->catdir( $self->cache_dir, '.hpcrunner-data' );
200 0 ▶ if ( $self->project ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->project) { } 0 0 0
201 0 $data_path = File::Spec->catdir( $data_dir, $self->project );
202 }
203 else {
204 0 $data_path = $data_dir;
205 }
206  
207 0 ▶ if ( !path($data_path)->exists && path($data_path)->is_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not path($data_path)->exists and path($data_path)->is_dir) 0 0 0
Condition0%0/3
not path($data_path)->exists and path($data_path)->is_dir
!ll&&!rl&&r
000
MC/DC0%0/2
not path($data_path)->exists and path($data_path)->is_dir
not path($data_path)->exists path($data_path)->is_dir
Truth table0%0/3
not(A) and B
A= not path($data_path)->existsB= path($data_path)->is_dir
ABresult
0X0
100
111
208 0 $self->screen_log->info( 'There is no data logged. '
209 . 'Please ensure you either in the project directory, '
210 . 'or that you have supplied --data_dir to the correct location.'
211 );
212 }
213  
214 0 $self->screen_log->info( 'Searching for submissions in ' . $data_dir );
215  
216 ##TODO In the case of a lot of submissions - get the most recent directory
217 0 my @files = File::Find::Rule->directory()->maxdepth(2)->name(qr/.*UID.*/)
218 ->in($data_path);
219 0 ▶ if ( !$self->json ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->json) 0 0 0
220 0 $self->screen_log->info( 'Found ' . scalar @files . ' submissions.' );
221 0 ▶ $self->screen_log->info('Reporting on the most recent.')
Branch: 1 branch
BranchTrueFalseTotal
if $self->most_recent 0 0 0
222 if $self->most_recent;
223 0 ▶ $self->screen_log->info('Reporting on all submissions.') if $self->all;
Branch: 1 branch
BranchTrueFalseTotal
if $self->all 0 0 0
224 }
225  
226 0 my %stats = ();
227 0 map { my $st = stat($_); $stats{ $st->[9] } = $_ } @files;
228  
229 0 my @sorted_files = ();
230 0 foreach ( sort { $b <=> $a } keys(%stats) ) {
231 0 push( @sorted_files, $stats{$_} );
232 0 ▶ last if $self->most_recent;
Branch: 1 branch
BranchTrueFalseTotal
if $self->most_recent 0 0 0
233 }
234  
235 0 return \@sorted_files;
236 }
237  
238 1 __PACKAGE__->meta()->make_immutable();
239  
240 1 1;
« blib/lib/HPC/Runner/Command/execute_job/Utils/MCE.pm ↑ Summary blib/lib/HPC/Runner/Command/execute_job/Base.pm »