File Coverage

blib/lib/HPC/Runner/Command/submit_jobs/Logger/JSON.pm
Criterion Covered Total %
statement 27 92 29.3
branch 0 6 0.0
condition 0 4 0.0
subroutine 9 12 75.0
pod 3 3 100.0
total 39 117 33.3


line stmt bran cond sub pod time code
1             package HPC::Runner::Command::submit_jobs::Logger::JSON;
2              
3 1     1   1038 use Moose::Role;
  1         2  
  1         7  
4 1     1   4473 use namespace::autoclean;
  1         3  
  1         8  
5              
6             with 'HPC::Runner::Command::execute_job::Logger::Lock';
7              
8 1     1   78 use JSON;
  1         3  
  1         9  
9 1     1   119 use File::Spec;
  1         3  
  1         19  
10 1     1   4 use Data::UUID;
  1         3  
  1         63  
11 1     1   6 use File::Path qw(make_path remove_tree);
  1         2  
  1         43  
12 1     1   7 use File::Slurp;
  1         3  
  1         53  
13 1     1   5 use DateTime;
  1         2  
  1         19  
14 1     1   5 use Capture::Tiny ':all';
  1         2  
  1         703  
15              
16             =head3 create_json_submission
17              
18             Create the data for the json submission
19              
20             =cut
21              
22             sub create_json_submission {
23 0     0 1   my $self = shift;
24              
25 0           make_path($self->data_dir);
26 0           $self->logger('app_log');
27 0           my $hpc_meta = $self->gen_hpc_meta;
28              
29             # my $json_text = encode_json $hpc_meta;
30             # write_file(File::Spec->catdir($self->data_dir, 'submission.json'), $json_text);
31              
32 0           return $hpc_meta;
33             }
34              
35             =head3 update_json_submission
36              
37             Take the initial submission and update it to contain the hpcmeta
38              
39             We only rerun this here to get the submission status
40              
41             =cut
42              
43             sub update_json_submission {
44 0     0 1   my $self = shift;
45              
46 0           make_path($self->data_dir);
47 0           my $hpc_meta = $self->gen_hpc_meta;
48 0           my $json_text = encode_json $hpc_meta;
49              
50 0           my $file_name = File::Spec->catdir( $self->logdir, 'submission.json' );
51 0           $self->_make_the_dirs( $self->logdir );
52              
53 0           write_file($file_name, $json_text);
54 0           write_file(File::Spec->catdir($self->data_dir, 'submission.json'), $json_text);
55              
56 0           return $hpc_meta;
57             }
58              
59             =head3 gen_hpc_meta
60              
61             Generate the HPC meta from the submission
62              
63             #TODO Check for batches
64              
65             =cut
66              
67             sub gen_hpc_meta {
68 0     0 1   my $self = shift;
69              
70 0           my $hpc_meta = {};
71 0           $hpc_meta->{uuid} = $self->submission_uuid;
72 0 0         $hpc_meta->{project} = $self->project if $self->has_project;
73 0           my $dt = DateTime->now( time_zone => 'local' );
74 0           $hpc_meta->{submission_time} = "$dt";
75 0           $hpc_meta->{jobs} = [];
76 0           $hpc_meta->{submissions} = {};
77 0           $hpc_meta->{schedule} = $self->schedule;
78              
79 0           foreach my $job ( $self->all_schedules ) {
80 0           my $job_obj = {};
81              
82             #Dependencies
83 0           my $ref = $self->graph_job_deps->{$job};
84 0           my $depstring = join( ", ", @{$ref} );
  0            
85 0           my $count_cmd = $self->jobs->{$job}->cmd_counter;
86 0           my $mem = $self->jobs->{$job}->mem;
87 0           my $cpus = $self->jobs->{$job}->cpus_per_task;
88 0           my $walltime = $self->jobs->{$job}->walltime;
89 0           my $cmd_start = $self->jobs->{$job}->{cmd_start};
90 0   0       my $submission_stat = $self->jobs->{$job}->submission_failure || 0;
91              
92 0           $job_obj->{job} = $job;
93 0           $job_obj->{deps} = $depstring;
94 0           $job_obj->{total_tasks} = $count_cmd;
95 0           $job_obj->{walltime} = $walltime;
96 0           $job_obj->{cpus_per_task} = $cpus;
97 0           $job_obj->{mem} = $mem;
98 0           $job_obj->{cmd_start} = $cmd_start;
99 0           $job_obj->{cmd_end} = $cmd_start + $count_cmd;
100 0           $job_obj->{schedule} = [];
101              
102             #I think this should be scheduler_ids
103 0           for ( my $x = 0 ; $x < $self->jobs->{$job}->{num_job_arrays} ; $x++ ) {
104              
105 0           my $obj = {};
106              
107             #index start, index end
108 0 0         next unless $self->jobs->{$job}->batch_indexes->[$x];
109              
110             my $batch_start =
111 0           $self->jobs->{$job}->batch_indexes->[$x]->{'batch_index_start'};
112             my $batch_end =
113 0           $self->jobs->{$job}->batch_indexes->[$x]->{'batch_index_end'};
114 0           my $len = ( $batch_end - $batch_start ) + 1;
115              
116 0           my $logname = $self->jobs->{$job}->lognames->[$x];
117 0 0         if ($logname) {
118             ##TASK IDS ARE 0 INDEXED
119 0           $hpc_meta->{submissions}->{$logname}->{jobname} = $job;
120             ##The entire job
121             $hpc_meta->{submissions}->{$logname}->{job_task_index_start} =
122 0           $cmd_start + 0;
123             $hpc_meta->{submissions}->{$logname}->{job_task_index_end} =
124 0           $cmd_start + $count_cmd - 1;
125              
126             ##This particular batch
127             ##These are 1 indexed to match the command line parameters 1
128             ##TODO clean up 0/1 indexed!!
129             $hpc_meta->{submissions}->{$logname}->{batch_index_start} =
130 0           $batch_start;
131             $hpc_meta->{submissions}->{$logname}->{batch_index_end} =
132 0           $batch_end;
133             }
134              
135 0   0       my $scheduler_id = $self->jobs->{$job}->scheduler_ids->[$x] || '0';
136 0           $obj->{task_indices} = "$batch_start-$batch_end";
137 0           $obj->{total_tasks} = $len;
138 0           $obj->{scheduler_id} = $scheduler_id;
139              
140 0           push( @{ $job_obj->{schedule} }, $obj );
  0            
141             }
142              
143 0           push( @{ $hpc_meta->{jobs} }, $job_obj );
  0            
144             }
145              
146 0           return $hpc_meta;
147             }
148              
149             1;