lib/Test2/Harness/UI/Controller/Resources.pm untested

filter: Statementstmt 0.0% 0 / 186 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 315 CC 25 SCAR 40.1
CC
25
Cov
0%
data_stamps
156.0
handle
30.0
get_thing
30.0
CRAP 54.9
1 package Test2::Harness::UI::Controller::Resources;
2 use strict;
3 use warnings;
4  
5 our $VERSION = '0.000150';
6  
7 use DateTime;
8 use Scalar::Util qw/blessed/;
9 use Test2::Harness::UI::Response qw/resp error/;
10 use Test2::Harness::UI::Util qw/share_dir find_job/;
11 use Test2::Harness::UI::Util::DateTimeFormat qw/DTF/;
12 use Test2::Harness::Util::JSON qw/encode_json decode_json/;
13 use Test2::Util::Times qw/render_duration/;
14 use Test2::Harness::UI::UUID qw/uuid_inflate uuid_deflate/;
15  
16 use parent 'Test2::Harness::UI::Controller';
17 use Test2::Harness::UI::Util::HashBase qw/-title/;
18  
19 sub handle {
20 my $self = shift;
21 my ($route) = @_;
22  
23 $self->{+TITLE} = 'YathUI';
24  
25 my $req = $self->{+REQUEST};
26  
27 # Test run, Host, or resource instance
28 my $id = $route->{id} or die error(404 => 'No id provided');
29  
30 # Specific instant
31 my $batch = uuid_inflate($route->{batch});
32  
33 if ($route->{data}) {
34 return $self->data_stamps($req, $id) unless $batch;
35 return $self->data($req, $id, $batch);
36 }
37  
38 my $res = resp(200);
39 $res->add_css('view.css');
40 $res->add_css('resources.css');
41 $res->add_js('resources.js');
42 $res->add_js('runtable.js');
43  
44 my $tx = Text::Xslate->new(path => [share_dir('templates')]);
45  
46 my $base_uri = $req->base->as_string;
47 my $stamp_uri = join '/' => $base_uri . 'resources', 'data', $id;
48 my $res_uri = join '/' => $base_uri . 'resources', $id;
49 $stamp_uri =~ s{/$}{}g;
50 $res_uri =~ s{/$}{}g;
51  
52 my $content = $tx->render(
53 'resources.tx',
54 {
55 user => $req->user,
56 base_uri => $req->base->as_string,
57 stamp_uri => $stamp_uri,
58 res_uri => $res_uri,
59 tailing => $batch ? 0 : 1,
60 selected => $batch ? $batch : undef,
61 }
62 );
63  
64 $res->raw_body($content);
65 return $res;
66 }
67  
68 sub get_thing {
69 my $self = shift;
70 my ($id) = @_;
71  
72 my $schema = $self->{+CONFIG}->schema;
73  
74 my ($thing, $stamp_start, $done_check);
75 my $search_args = {};
76 my $stamp_args = {start => \$stamp_start};
77  
78 my $host_rs = $schema->resultset('Host');
79 my $res_rs = $schema->resultset('Resource');
80 my $run_rs = $schema->resultset('Run');
81  
82 if (!$id || lc($id) eq 'global') {
83 $thing = undef;
84 $search_args->{global} = 1;
85 }
86 else {
87 my $uuid = uuid_inflate($id);
88 if ($uuid && eval { $thing = $run_rs->search({run_id => $uuid})->first }) {
89 $search_args->{run_id} = $uuid;
90 $done_check = sub {
91 return 1 if $thing->complete;
92 return 0;
93 };
94 }
95 elsif (($uuid && eval { $thing = $host_rs->search({host_id => $uuid})->first }) || eval { $thing = $host_rs->search({hostname => $id})->first }) {
96 $search_args->{host_id} = $thing->host_id;
97 }
98 else {
99 die error(404 => 'Invalid Job ID or Host ID');
100 }
101 }
102  
103 return ($thing, $search_args, $stamp_args, $done_check);
104 }
105  
106 sub get_stamps {
107 my $self = shift;
108 my %params = @_;
109  
110 my $search_args = $params{search_args} || {};
111 my $start = $params{start};
112  
113 my $schema = $self->{+CONFIG}->schema;
114 my $dbh = $schema->storage->dbh;
115  
116 my $fields = "";
117 my @vals;
118 if ($search_args->{run_id}) {
119 $fields = "run_id = ?";
120 push @vals => uuid_deflate($search_args->{run_id});
121 }
122 elsif ($search_args->{host_id}) {
123 $fields = "host_id = ?";
124 push @vals => uuid_deflate($search_args->{host_id});
125 }
126  
127 if ($$start) {
128 $fields .= " AND stamp > ?";
129 push @vals => $$start;
130 }
131  
132 my $sth = $dbh->prepare("SELECT resource_batch_id, stamp FROM resource_batch WHERE " . $fields . " ORDER BY stamp ASC");
133 $sth->execute(@vals) or die $sth->errstr;
134 my $rows = $sth->fetchall_arrayref;
135  
136 return unless @$rows;
137  
138 $_->[0] = uuid_inflate($_->[0]) for @$rows;
139  
140 $$start = $rows->[-1]->[1];
141  
142 return $rows;
143 }
144  
145 sub data_stamps {
146 my $self = shift;
147 my ($req, $id) = @_;
148  
149 my $res = resp(200);
150 my ($thing, $search_args, $stamp_args, $done_check) = $self->get_thing($id);
151  
152 my ($complete, @out);
153  
154 if (my $run_id = $search_args->{run_id}) {
155 push @out => { run_id => $run_id };
156 }
157 if (my $host_id = $search_args->{host_id}) {
158 push @out => { host_id => $host_id };
159 }
160  
161 my $start = time;
162 my $advance = sub {
163 return 0 if @out;
164 return 1 if $complete;
165 return 1 if (time - $start) > 600;
166  
167 if ($thing) {
168 if (my $stamps = $self->get_stamps(%$stamp_args, search_args => $search_args)) {
169 push @out => {stamps => $stamps};
170 }
171  
172 # Finish if run is done
173 if ($done_check && $done_check->()) {
174 push @out => {complete => 1};
175 }
176  
177 return 0;
178 }
179  
180 push @out => {complete => 1};
181 return 1;
182 };
183  
184 $res->stream(
185 env => $req->env,
186 content_type => 'application/x-jsonl; charset=utf-8',
187 done => $advance,
188  
189 fetch => sub {
190 return () if $complete;
191  
192 $advance->() unless @out;
193  
194 my $item = shift @out or return ();
195 $complete = 1 if $item->{complete};
196  
197 return encode_json($item) . "\n";
198 },
199 );
200  
201 return $res;
202 }
203  
204 sub data {
205 my $self = shift;
206 my ($req, $id, $batch) = @_;
207  
208 my $res = resp(200);
209 my ($thing, $search_args, $stamp_args, $done_check) = $self->get_thing($id);
210  
211 $res->content_type('application/json');
212 $res->raw_body({
213 resources => $self->render_stamp_resources(search_args => $search_args, batch => $batch),
214 });
215  
216 return $res;
217 }
218  
219 sub render_stamp_resources {
220 my $self = shift;
221 my %params = @_;
222  
223 my $search_args = $params{search_args};
224 my $batch_id = uuid_inflate($params{batch});
225  
226 my $schema = $self->{+CONFIG}->schema;
227 my $res_rs = $schema->resultset('Resource');
228  
229 my @res_list;
230 my $resources = $res_rs->search({resource_batch_id => $batch_id}, {order_by => {'-asc' => 'batch_ord'}});
231 while (my $res = $resources->next) {
232 push @res_list => $self->render_resource($res);
233 }
234  
235 return \@res_list;
236 }
237  
238 sub render_resource {
239 my $self = shift;
240 my ($r) = @_;
241  
242 my $data = $r->data;
243  
244 for my $group (@{$data || []}) {
245 for my $table (@{$group->{tables} || []}) {
246 for my $row (@{$table->{rows} || []}) {
247 my @formats = @{$table->{format} || []};
248  
249 for my $item (@{$row || []}) {
250 my $format = shift @formats or next;
251  
252 unless ($format eq 'duration') {
253 $item = "$item (unsupported format '$format')";
254 next;
255 }
256  
257 $item = render_duration($item);
258 }
259 }
260 }
261 }
262  
263 return {resource => $r->module, groups => $data};
264 }
265  
266  
267 1;
268  
269 __END__