lib/App/Yath/Plugin/YathUIDB.pm untested

filter: Statementstmt 0.0% 0 / 196 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 327 CC 20 SCAR 29.3
CC
20
Cov
0%
grab_rerun
42.0
get_coverage_searches
30.0
test_map_from_coverage_rows
20.0
CRAP 18.8
1 package App::Yath::Plugin::YathUIDB;
2 use strict;
3 use warnings;
4  
5 our $VERSION = '0.000150';
6  
7 use Test2::Harness::UI::Util qw/config_from_settings/;
8 use Test2::Harness::Util::JSON qw/decode_json/;
9 use Test2::Harness::Util qw/mod2file looks_like_uuid/;
10  
11 use App::Yath::Options;
12 use parent 'App::Yath::Plugin';
13  
14 option_group {prefix => 'yathui', category => "YathUI Options"} => sub {
15 option user => (
16 type => 's',
17 description => 'Username to attach to the data sent to the db',
18 default => sub { $ENV{USER} },
19 );
20  
21 option schema => (
22 type => 's',
23 default => 'PostgreSQL',
24 long_examples => [' PostgreSQL', ' MySQL'],
25 description => "What type of DB/schema to use when using a temporary database",
26 );
27  
28 option port => (
29 type => 's',
30 long_examples => [' 8080'],
31 description => 'Port to use when running a local server',
32 default => 8080,
33 );
34  
35 option port_command => (
36 type => 's',
37 long_examples => [' get_port.sh', ' get_port.sh --pid $$'],
38 description => 'Use a command to get a port number. "$$" will be replaced with the PID of the yath process',
39 );
40  
41 option resources => (
42 type => 'd',
43 description => 'Send resource info (for supported resources) to yathui at the specified interval in seconds (5 if not specified)',
44 long_examples => ['', '=5'],
45 autofill => 5,
46 );
47  
48 option only => (
49 type => 'b',
50 description => 'Only use the YathUI renderer',
51 );
52  
53 option db => (
54 type => 'b',
55 description => 'Add the YathUI DB renderer in addition to other renderers',
56 );
57  
58 option only_db => (
59 type => 'b',
60 description => 'Only use the YathUI DB renderer',
61 );
62  
63 option render => (
64 type => 'b',
65 description => 'Add the YathUI renderer in addition to other renderers',
66 );
67  
68 post 200 => sub {
69 my %params = @_;
70 my $settings = $params{settings};
71  
72 my $yathui = $settings->yathui;
73  
74 if ($settings->check_prefix('display')) {
75 my $display = $settings->display;
76 if ($yathui->only) {
77 $display->renderers = {
78 '@' => ['Test2::Harness::Renderer::UI'],
79 'Test2::Harness::Renderer::UI' => [],
80 }
81 }
82 elsif ($yathui->only_db) {
83 $display->renderers = {
84 '@' => ['Test2::Harness::Renderer::UIDB'],
85 'Test2::Harness::Renderer::UIDB' => [],
86 }
87 }
88 elsif ($yathui->render) {
89 unless ($display->renderers->{'Test2::Harness::Renderer::UI'}) {
90 push @{$display->renderers->{'@'}} => 'Test2::Harness::Renderer::UI';
91 $display->renderers->{'Test2::Harness::Renderer::UI'} = [];
92 }
93 }
94 elsif ($yathui->db) {
95 unless ($display->renderers->{'Test2::Harness::Renderer::UIDB'}) {
96 push @{$display->renderers->{'@'}} => 'Test2::Harness::Renderer::UIDB';
97 $display->renderers->{'Test2::Harness::Renderer::UIDB'} = [];
98 }
99 }
100 }
101 };
102 };
103  
104 option_group {prefix => 'yathui-db', category => "YathUI Options"} => sub {
105 option config => (
106 type => 's',
107 description => "Module that implements 'MODULE->yath_ui_config(%params)' which should return a Test2::Harness::UI::Config instance.",
108 );
109  
110 option driver => (
111 type => 's',
112 description => "DBI Driver to use",
113 long_examples => [' Pg', 'mysql', 'MariaDB'],
114 );
115  
116 option name => (
117 type => 's',
118 description => 'Name of the database to use for yathui',
119 );
120  
121 option user => (
122 type => 's',
123 description => 'Username to use when connecting to the db',
124 );
125  
126 option pass => (
127 type => 's',
128 description => 'Password to use when connecting to the db',
129 );
130  
131 option dsn => (
132 type => 's',
133 description => 'DSN to use when connecting to the db',
134 );
135  
136 option host => (
137 type => 's',
138 description => 'hostname to use when connecting to the db',
139 );
140  
141 option port => (
142 type => 's',
143 description => 'port to use when connecting to the db',
144 );
145  
146 option socket => (
147 type => 's',
148 description => 'socket to use when connecting to the db',
149 );
150  
151 option flush_interval => (
152 type => 's',
153 long_examples => [' 2', ' 1.5'],
154 description => 'When buffering DB writes, force a flush when an event is recieved at least N seconds after the last flush.',
155 );
156  
157 option buffering => (
158 type => 's',
159 long_examples => [ ' none', ' job', ' diag', ' run' ],
160 description => 'Type of buffering to use, if "none" then events are written to the db one at a time, which is SLOW',
161 default => 'diag',
162 );
163  
164 option coverage => (
165 type => 'b',
166 description => 'Pull coverage data directly from the database (default: off)',
167 default => 0,
168 );
169  
170 option durations => (
171 type => 'b',
172 description => 'Pull duration data directly from the database (default: off)',
173 default => 0,
174 );
175  
176 option duration_limit => (
177 type => 's',
178 description => 'Limit the number of runs to look at for durations data (default: 10)',
179 default => 25,
180 );
181  
182 option publisher => (
183 type => 's',
184 description => 'When using coverage or duration data, only use data uploaded by this user',
185 );
186 };
187  
188 sub get_coverage_searches {
189 my ($plugin, $settings, $changes) = @_;
190  
191 my ($changes_exclude_loads, $changes_exclude_opens);
192 if ($settings->check_prefix('finder')) {
193 my $finder = $settings->finder;
194 $changes_exclude_loads = $finder->changes_exclude_loads;
195 $changes_exclude_opens = $finder->changes_exclude_opens;
196 }
197  
198 my @searches;
199 for my $source_file (keys %$changes) {
200 my $changed_sub_map = $changes->{$source_file};
201 my @changed_subs = keys %$changed_sub_map;
202  
203 my $search = {'source_file.filename' => $source_file};
204 unless ($changed_sub_map->{'*'} || !@changed_subs) {
205 my %seen;
206  
207 my @inject;
208 push @inject => '*' unless $changes_exclude_loads;
209 push @inject => '<>' unless $changes_exclude_opens;
210  
211 $search->{'source_sub.subname'} = {'IN' => [grep { !$seen{$_}++} @inject, @changed_subs]};
212 }
213  
214 push @searches => $search;
215 }
216  
217 return @searches;
218 }
219  
220 sub get_coverage_rows {
221 my ($plugin, $settings, $changes) = @_;
222  
223 my $ydb = $settings->prefix('yathui-db') or return;
224 return unless $ydb->coverage;
225  
226 my $config = config_from_settings($settings);
227 my $schema = $config->schema;
228 my $pname = $settings->yathui->project or die "yathui-project is required.\n";
229 my $project = $schema->resultset('Project')->find({name => $pname}) or die "Invalid project '$pname'.\n";
230 my $run = $project->last_covered_run(user => $ydb->publisher) or return;
231  
232 my @searches = $plugin->get_coverage_searches($settings, $changes) or return;
233 return $run->expanded_coverages({'-or' => \@searches});
234 }
235  
236 my %CATEGORIES = (
237 '*' => 'loads',
238 '<>' => 'opens',
239 );
240 sub test_map_from_coverage_rows {
241 my ($plugin, $coverages) = @_;
242  
243 my %tests;
244 while (my $cover = $coverages->next()) {
245 my $test = $cover->test_filename or next;
246  
247 if (my $manager = $cover->manager_package) {
248 unless ($tests{$test}) {
249 if (eval { require(mod2file($manager)); 1 }) {
250 $tests{$test} = {manager => $manager, subs => [], loads => [], opens => []};
251 }
252 else {
253 warn "Error loading manager '$manager'. Running entire test '$test'.\nError:\n====\n$@\n====\n";
254 $tests{$test} = 0;
255 next;
256 }
257 }
258  
259 my $cat = $CATEGORIES{$cover->source_subname} // 'subs';
260 push @{$tests{$test}->{$cat}} => @{$cover->metadata};
261 }
262 else {
263 $tests{$test} //= 0;
264 }
265 }
266  
267 return \%tests;
268 }
269  
270 sub get_coverage_tests {
271 my ($plugin, $settings, $changes) = @_;
272  
273 my $ydb = $settings->prefix('yathui-db') or return;
274 return unless $ydb->coverage;
275  
276 my $coverages = $plugin->get_coverage_rows($settings, $changes) or return;
277  
278 my $tests = $plugin->test_map_from_coverage_rows($coverages);
279  
280 return $plugin->search_entries_from_test_map($tests, $changes, $settings);
281 }
282  
283 sub search_entries_from_test_map {
284 my ($plugin, $tests, $changes, $settings) = @_;
285  
286 my @out;
287 for my $test (keys %$tests) {
288 my $meta = $tests->{$test};
289 my $manager = $meta ? delete $meta->{manager} : undef;
290  
291 unless ($meta && $manager) {
292 push @out => $test;
293 next;
294 }
295  
296 unless (eval { push @out => [ $test, $manager->test_parameters($test, $meta, $changes, undef, $settings) ]; 1 }) {
297 warn "Error processing coverage data for '$test' using manager '$manager'. Running entire test to be safe.\nError:\n====\n$@\n====\n";
298 push @out => $test;
299 }
300 }
301  
302 return @out;
303 }
304  
305 sub duration_data {
306 my ($plugin, $settings) = @_;
307 my $ydb = $settings->prefix('yathui-db') or return;
308 return unless $ydb->durations;
309  
310 my $config = config_from_settings($settings);
311 my $schema = $config->schema;
312 my $pname = $settings->yathui->project or die "yathui-project is required.\n";
313 my $project = $schema->resultset('Project')->find({name => $pname}) or die "Invalid project '$pname'.\n";
314  
315 my %args = (user => $ydb->publisher, limit => $ydb->duration_limit);
316 if (my $yui = $settings->prefix('yathui')) {
317 $args{short} = $yui->medium_duration;
318 $args{medium} = $yui->long_duration;
319  
320 # TODO
321 #$args{median} = $yui->median_durations;
322 }
323  
324 return $project->durations(%args);
325 }
326  
327 sub grab_rerun {
328 my $this = shift;
329 my ($rerun, %params) = @_;
330  
331 return (0) if $rerun =~ m/\.jsonl(\.gz|\.bz2)?/;
332  
333 my $settings = $params{settings};
334 my $mode_hash = $params{mode_hash} //= {all => 1};
335  
336 my $config = config_from_settings($settings);
337 my $schema = $config->schema;
338  
339 my ($ok, $err, $run);
340 if ($rerun eq '1') {
341 my $project_name = $settings->yathui->project;
342 my $username = $settings->yathui->user // $ENV{USER};
343 $ok = eval { $run = $schema->vague_run_search(query => {}, project_name => $project_name, username => $username); 1 };
344 $err = $@;
345 }
346 elsif (looks_like_uuid($rerun)) {
347 $ok = eval { $run = $schema->vague_run_search(query => {}, source => $rerun); 1 };
348 $err = $@;
349 }
350 else {
351 return (0);
352 }
353  
354 unless ($run) {
355 print $ok ? "No previous run found\n" : "Error getting rerun data from yathui database: $err\n";
356 return (1);
357 }
358  
359 print "Re-Running " . join(', ', sort keys %$mode_hash) . " tests from run id: " . $run->run_id . "\n";
360  
361 my $data = $run->rerun_data;
362  
363 return (1, $data);
364 }
365  
366 1;