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

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 198 CC 14 SCAR 34.9
CC
14
Cov
0%
data
90.0
handle
12.0
interval
12.0
CRAP 32.7
1 package Test2::Harness::UI::Controller::Interactions;
2 use strict;
3 use warnings;
4  
5 our $VERSION = '0.000150';
6  
7 use DateTime;
8 use Data::GUID;
9 use Scalar::Util qw/blessed/;
10 use Test2::Harness::UI::Response qw/resp error/;
11 use Test2::Harness::UI::Util qw/share_dir find_job/;
12 use Test2::Harness::Util::JSON qw/encode_json/;
13 use Test2::Harness::UI::UUID qw/uuid_inflate/;
14  
15 use parent 'Test2::Harness::UI::Controller';
16 use Test2::Harness::UI::Util::HashBase qw/-title/;
17  
18 sub handle {
19 my $self = shift;
20 my ($route) = @_;
21  
22 $self->{+TITLE} = 'YathUI';
23  
24 my $req = $self->{+REQUEST};
25  
26 my $id = uuid_inflate($route->{id}) or die error(404 => 'No event id provided');
27 my $context = $route->{context} // 1;
28 die error(400 => 'Invalid context value') unless $context =~ m/^\d+$/;
29 return $self->data($id, $context) if $route->{data};
30  
31 my $res = resp(200);
32 $res->add_css('view.css');
33 $res->add_css('interactions.css');
34 $res->add_js('runtable.js');
35 $res->add_js('jobtable.js');
36 $res->add_js('eventtable.js');
37 $res->add_js('interactions.js');
38  
39 my $tx = Text::Xslate->new(path => [share_dir('templates')]);
40  
41 my $base_uri = $req->base->as_string;
42 my $data_uri = join '/' => $base_uri . 'interactions', 'data', $id, $context;
43  
44 my $content = $tx->render(
45 'interactions.tx',
46 {
47 base_uri => $req->base->as_string,
48 event_id => $id,
49 user => $req->user,
50 data_uri => $data_uri,
51 context_count => $context,
52 }
53 );
54  
55 $res->raw_body($content);
56 return $res;
57 }
58  
59 sub data {
60 my $self = shift;
61 my ($id, $context) = @_;
62  
63 my $schema = $self->{+CONFIG}->schema;
64 # Get event
65 my $event = $schema->resultset('Event')->search({event_id => $id})->first
66 or die error(404 => 'Invalid Event');
67  
68 my $stamp = $event->get_column('stamp') or die "No stamp?!";
69  
70 # Get job
71 my $job = $event->job_key or die error(500 => "Could not find job");
72  
73 # Get run from event
74 my $run = $job->run or die error(500 => "Could not find run");
75  
76 # Get tests from run where the start and end surround the event
77 my $job_rs = $run->jobs(
78 {
79 job_key => {'!=' => $job->job_key},
80 ended => {'>=' => $stamp},
81 '-or' => [
82 {launch => {'<=' => $stamp}},
83 {start => {'<=' => $stamp}},
84 ],
85 },
86 {order_by => 'job_ord'},
87 );
88  
89 my $req = $self->{+REQUEST};
90 my $res = resp(200);
91  
92 my ($event_rs, %seen_events);
93 my @out = (
94 {type => 'run', data => $run},
95 {type => 'job', data => $job->glance_data},
96 {type => 'event', data => $event->line_data},
97 {type => 'count', data => $job_rs->count},
98 );
99  
100 my $advance = sub {
101 return 0 if @out;
102  
103 while ($event_rs) {
104 if (my $e = $event_rs->next) {
105 if ($e->is_subtest) {
106 next unless $self->subtest_in_context($e, $event->stamp, $context);
107 }
108  
109 # If event is in a subtest show the whole subtest, top level subtest
110 $e = $e->parent while $e->nested;
111  
112 next if $seen_events{$e->event_id}++;
113  
114 push @out => {type => 'event', data => $e->line_data};
115 return 0;
116 }
117  
118 $event_rs = undef;
119 }
120  
121 if (my $job = $job_rs->next) {
122 push @out => {type => 'job', data => $job->glance_data};
123  
124 $event_rs = $job->events(
125 {
126 '-or' => [
127 {is_subtest => 1, nested => 0},
128 {
129 '-and' => [
130 {stamp => {'<=' => $self->interval($stamp, '+', $context)}},
131 {stamp => {'>=' => $self->interval($stamp, '-', $context)}},
132 ],
133 },
134 ],
135 },
136 {order_by => 'event_ord'},
137 );
138  
139 return 0;
140 }
141  
142 return 1;
143 };
144  
145 $res->stream(
146 env => $req->env,
147 content_type => 'application/x-jsonl; charset=utf-8',
148 done => $advance,
149  
150 fetch => sub {
151 $advance->() unless @out;
152 return @out ? encode_json(shift(@out)) . "\n" : ();
153 },
154 );
155  
156 return $res;
157 }
158  
159 sub subtest_in_context {
160 my $self = shift;
161 my ($event, $stamp, $context) = @_;
162  
163 my $f = $event->facets or return 1;
164  
165 my $parent = $f->{parent} or return 1;
166 my $start = $parent->{start_stamp} or return 1;
167 my $stop = $parent->{stop_stamp} or return 1;
168  
169 $start = DateTime->from_epoch(epoch => $start, time_zone => 'local');
170 $stop = DateTime->from_epoch(epoch => $stop, time_zone => 'local');
171  
172 # Need an extra nanosecond because is_between does not check for >= or <= just > or <.
173 $start->subtract(seconds => $context, nanoseconds => 1);
174 $stop->add(seconds => $context, nanoseconds => 1);
175  
176 return $stamp->is_between($start, $stop) ? 1 : 0;
177 }
178  
179 sub interval {
180 my $self = shift;
181 my ($stamp, $op, $context) = @_;
182  
183 my $driver = $self->{+CONFIG}->db_driver;
184  
185 return \"timestamp '$stamp' $op INTERVAL '$context' seconds" if $driver eq 'PostgreSQL';
186  
187 # *Sigh* MySQL
188 return \"DATE_ADD('$stamp', INTERVAL $context second)" if $op eq '+';
189 return \"DATE_SUB('$stamp', INTERVAL $context second)";
190 }
191  
192 1;
193  
194 __END__