| 1 | package Test2::Harness::UI::Controller::Project; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.000150'; | ||
| 6 | |||
| 7 | use Time::Elapsed qw/elapsed/; | ||
| 8 | use List::Util qw/sum/; | ||
| 9 | use Text::Xslate(); | ||
| 10 | use Test2::Harness::UI::Util qw/share_dir format_duration parse_duration is_invalid_subtest_name/; | ||
| 11 | use Test2::Harness::UI::Response qw/resp error/; | ||
| 12 | use Test2::Harness::Util::JSON qw/encode_json decode_json/; | ||
| 13 | use Test2::Harness::UI::UUID qw/uuid_deflate uuid_inflate/; | ||
| 14 | |||
| 15 | use parent 'Test2::Harness::UI::Controller'; | ||
| 16 | use Test2::Harness::UI::Util::HashBase; | ||
| 17 | |||
| 18 | sub display_duration { | ||
| 19 | my $dur = shift // return "N/A"; | ||
| 20 | return elapsed($dur) || sprintf('%1.1f seconds', $dur); | ||
| 21 | } | ||
| 22 | |||
| 23 | sub title { 'Project Stats' } | ||
| 24 | |||
| 25 | sub users { | ||
| 26 | my $self = shift; | ||
| 27 | my ($project) = @_; | ||
| 28 | |||
| 29 | my $schema = $self->{+CONFIG}->schema; | ||
| 30 | my $dbh = $schema->storage->dbh; | ||
| 31 | |||
| 32 | my $query = <<" EOT"; | ||
| 33 | SELECT users.user_id AS user_id, users.username AS username | ||
| 34 | FROM users | ||
| 35 | JOIN runs USING(user_id) | ||
| 36 | WHERE runs.project_id = ? | ||
| 37 | GROUP BY user_id | ||
| 38 | EOT | ||
| 39 | |||
| 40 | my $sth = $dbh->prepare($query); | ||
| 41 | $sth->execute(uuid_deflate($project->project_id)) or die $sth->errstr; | ||
| 42 | |||
| 43 | my $owner = $project->owner; | ||
| 44 | my @out; | ||
| 45 | for my $row (@{$sth->fetchall_arrayref // []}) { | ||
| 46 | my ($user_id, $username) = @$row; | ||
| 47 | $user_id = uuid_inflate($user_id); | ||
| 48 | my $is_owner = ($owner && $user_id eq $owner->user_id) ? 1 : 0; | ||
| 49 | push @out => {user_id => $user_id, username => $username, owner => $is_owner}; | ||
| 50 | } | ||
| 51 | |||
| 52 | @out = sort { $b->{owner} cmp $a->{owner} || $a->{username} cmp $b->{username} } @out; | ||
| 53 | |||
| 54 | return \@out; | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | sub handle { | ||
| 59 | my $self = shift; | ||
| 60 | my ($route) = @_; | ||
| 61 | |||
| 62 | my $req = $self->request; | ||
| 63 | my $it = $route->{id} or die error(404 => 'No id'); | ||
| 64 | |||
| 65 | my $n = $route->{n} // 25; | ||
| 66 | my $stats = $route->{stats} // 0; | ||
| 67 | |||
| 68 | my $schema = $self->{+CONFIG}->schema; | ||
| 69 | |||
| 70 | my $project; | ||
| 71 | $project = $schema->resultset('Project')->single({name => $it}); | ||
| 72 | if (my $uuid = uuid_inflate($it)) { | ||
| 73 | $project //= $schema->resultset('Project')->single({project_id => $uuid}); | ||
| 74 | } | ||
| 75 | die error(404 => 'Invalid Project') unless $project; | ||
| 76 | |||
| 77 | return $self->html($req, $project, $n) | ||
| 78 | unless $stats; | ||
| 79 | |||
| 80 | return $self->stats($req, $project); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub html { | ||
| 84 | my $self = shift; | ||
| 85 | my ($req, $project, $n) = @_; | ||
| 86 | |||
| 87 | my $tx = Text::Xslate->new(path => [share_dir('templates')]); | ||
| 88 | |||
| 89 | my $res = resp(200); | ||
| 90 | $res->add_css('view.css'); | ||
| 91 | $res->add_css('project.css'); | ||
| 92 | $res->add_js('runtable.js'); | ||
| 93 | $res->add_js('project.js'); | ||
| 94 | $res->add_js('chart.min.js'); | ||
| 95 | |||
| 96 | my $content = $tx->render( | ||
| 97 | 'project.tx', | ||
| 98 | { | ||
| 99 | project => $project, | ||
| 100 | base_uri => $req->base->as_string, | ||
| 101 | n => $n, | ||
| 102 | users => $self->users($project), | ||
| 103 | } | ||
| 104 | ); | ||
| 105 | |||
| 106 | $res->raw_body($content); | ||
| 107 | return $res; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub stats { | ||
| 111 | my $self = shift; | ||
| 112 | my ($req, $project, $n) = @_; | ||
| 113 | |||
| 114 | my $json = $req->content; | ||
| 115 | my $stats = decode_json($json); | ||
| 116 | |||
| 117 | my $res = resp(200); | ||
| 118 | |||
| 119 | $res->stream( | ||
| 120 | env => $req->env, | ||
| 121 | content_type => 'application/x-jsonl; charset=utf-8', | ||
| 122 | cache => 0, | ||
| 123 | |||
| 124 | done => sub { | ||
| 125 | return 0 if @$stats; | ||
| 126 | return 1; | ||
| 127 | }, | ||
| 128 | |||
| 129 | fetch => sub { | ||
| 130 | my $s = shift(@$stats) or return; | ||
| 131 | my $data = $self->build_stat($project => $s); | ||
| 132 | return encode_json($data) . "\n"; | ||
| 133 | }, | ||
| 134 | ); | ||
| 135 | |||
| 136 | return $res; | ||
| 137 | } | ||
| 138 | |||
| 139 | my %VALID_TYPES = ( | ||
| 140 | run_list => 1, | ||
| 141 | coverage => 1, | ||
| 142 | uncovered => 1, | ||
| 143 | user_summary => 1, | ||
| 144 | expensive_users => 1, | ||
| 145 | expensive_files => 1, | ||
| 146 | expensive_subtests => 1, | ||
| 147 | ); | ||
| 148 | |||
| 149 | sub build_stat { | ||
| 150 | my $self = shift; | ||
| 151 | my ($project, $stat) = @_; | ||
| 152 | |||
| 153 | return unless $stat; | ||
| 154 | |||
| 155 | my $type = $stat->{type}; | ||
| 156 | |||
| 157 | return {%$stat, error => "Invalid type '$type'"} unless $VALID_TYPES{$type}; | ||
| 158 | |||
| 159 | $stat->{users} //= []; | ||
| 160 | |||
| 161 | eval { | ||
| 162 | my $meth = "_build_stat_$type"; | ||
| 163 | $self->$meth($project => $stat); | ||
| 164 | 1; | ||
| 165 | } or return {%$stat, error => $@}; | ||
| 166 | |||
| 167 | return $stat; | ||
| 168 | } | ||
| 169 | |||
| 170 | sub parse_date { | ||
| 171 | my $raw = shift; | ||
| 172 | |||
| 173 | my ($m, $d, $y) = split '/', $raw; | ||
| 174 | return sprintf("%04d-%02d-%02d", $y, $m, $d); | ||
| 175 | } | ||
| 176 | |||
| 177 | sub get_add_query { | ||
| 178 | my $self = shift; | ||
| 179 | my ($project, $stat, %overrides) = @_; | ||
| 180 | |||
| 181 | my $n = $overrides{n} // $stat->{n}; | ||
| 182 | my $users = $overrides{users} // $stat->{users}; | ||
| 183 | my $start = $overrides{start_date} // $stat->{start_date}; | ||
| 184 | my $end = $overrides{end_date} // $stat->{end_date}; | ||
| 185 | |||
| 186 | my $range = $start && $end; | ||
| 187 | |||
| 188 | return ('') unless $n || @$users || $range; | ||
| 189 | |||
| 190 | return ("AND run_ord > (SELECT MAX(run_ord) - ? FROM runs)\n", $n) | ||
| 191 | unless @$users || $range; | ||
| 192 | |||
| 193 | my @add_vals; | ||
| 194 | |||
| 195 | my $user_query = 'user_id in (' . join(',' => map { '?' } @$users) . ')'; | ||
| 196 | push @add_vals => map { uuid_deflate($_) } @$users; | ||
| 197 | |||
| 198 | return ("AND $user_query\n", @add_vals) unless $n || $range; | ||
| 199 | |||
| 200 | my $schema = $self->{+CONFIG}->schema; | ||
| 201 | my $dbh = $schema->storage->dbh; | ||
| 202 | |||
| 203 | if ($range) { | ||
| 204 | my $query = <<" EOT"; | ||
| 205 | SELECT min(run_ord) AS min, max(run_ord) AS max | ||
| 206 | FROM runs | ||
| 207 | WHERE project_id = ? | ||
| 208 | AND added >= ? | ||
| 209 | AND added <= ? | ||
| 210 | EOT | ||
| 211 | |||
| 212 | $start = parse_date($start); | ||
| 213 | $end = parse_date($end); | ||
| 214 | |||
| 215 | my $sth = $dbh->prepare($query); | ||
| 216 | $sth->execute(uuid_deflate($project->project_id), $start, $end) or die $sth->errstr; | ||
| 217 | |||
| 218 | my $ords = $sth->fetchrow_hashref; | ||
| 219 | |||
| 220 | my $ord_query = "run_ord >= ? AND run_ord <= ?"; | ||
| 221 | push @add_vals => ($ords->{min}, $ords->{max}); | ||
| 222 | return ("AND $user_query AND $ord_query", @add_vals) if @$users; | ||
| 223 | return ("AND $ord_query", @add_vals); | ||
| 224 | } | ||
| 225 | |||
| 226 | my $query = <<" EOT"; | ||
| 227 | SELECT run_ord, run_id | ||
| 228 | FROM reporting | ||
| 229 | WHERE project_id = ? | ||
| 230 | AND $user_query | ||
| 231 | GROUP BY run_ord, run_id | ||
| 232 | ORDER BY run_ord DESC | ||
| 233 | LIMIT ? | ||
| 234 | EOT | ||
| 235 | |||
| 236 | my $sth = $dbh->prepare($query); | ||
| 237 | $sth->execute(uuid_deflate($project->project_id), @add_vals, $n) or die $sth->errstr; | ||
| 238 | |||
| 239 | my @ids = map { $_->[1] } @{$sth->fetchall_arrayref}; | ||
| 240 | return ('') unless @ids; | ||
| 241 | |||
| 242 | return ("AND run_id IN (" . join(',' => map { '?' } @ids) . ")\n", map { $_ } @ids); | ||
| 243 | } | ||
| 244 | |||
| 245 | |||
| 246 | sub _build_stat_run_list { | ||
| 247 | my $self = shift; | ||
| 248 | my ($project, $stat) = @_; | ||
| 249 | |||
| 250 | my $schema = $self->{+CONFIG}->schema; | ||
| 251 | my $dbh = $schema->storage->dbh; | ||
| 252 | |||
| 253 | my ($add_query, @add_vals) = $self->get_add_query($project, $stat); | ||
| 254 | |||
| 255 | my $query = <<" EOT"; | ||
| 256 | SELECT run_id | ||
| 257 | FROM reporting | ||
| 258 | WHERE project_id = ? | ||
| 259 | $add_query | ||
| 260 | ORDER BY run_id DESC | ||
| 261 | EOT | ||
| 262 | |||
| 263 | my $sth = $dbh->prepare($query); | ||
| 264 | $sth->execute(uuid_deflate($project->project_id), @add_vals) or die $sth->errstr; | ||
| 265 | |||
| 266 | my @ids = map { uuid_inflate($_->[0]) } @{$sth->fetchall_arrayref}; | ||
| 267 | |||
| 268 | my @items = map { $_->TO_JSON } $schema->resultset('Run')->search({run_id => {'-in' => \@ids}}, {order_by => {'-DESC' => 'run_ord'}})->all; | ||
| 269 | |||
| 270 | $stat->{runs} = \@items; | ||
| 271 | } | ||
| 272 | |||
| 273 | sub _build_stat_expensive_files { | ||
| 274 | my $self = shift; | ||
| 275 | my ($project, $stat) = @_; | ||
| 276 | |||
| 277 | my $schema = $self->{+CONFIG}->schema; | ||
| 278 | my $dbh = $schema->storage->dbh; | ||
| 279 | |||
| 280 | my ($add_query, @add_vals) = $self->get_add_query($project, $stat); | ||
| 281 | |||
| 282 | my $query = <<" EOT"; | ||
| 283 | SELECT test_files.filename AS filename, | ||
| 284 | SUM(duration) AS total_duration, | ||
| 285 | AVG(duration) AS average_duration, | ||
| 286 | COUNT(DISTINCT(run_ord)) AS runs, | ||
| 287 | COUNT(duration) AS tries, | ||
| 288 | COUNT(DISTINCT(user_id)) AS users, | ||
| 289 | SUM(pass) AS pass, | ||
| 290 | SUM(fail) AS fail, | ||
| 291 | SUM(retry) AS retry, | ||
| 292 | SUM(abort) AS abort | ||
| 293 | FROM reporting | ||
| 294 | LEFT JOIN test_files USING(test_file_id) | ||
| 295 | WHERE project_id = ? | ||
| 296 | AND subtest IS NULL | ||
| 297 | AND test_file_id IS NOT NULL | ||
| 298 | $add_query | ||
| 299 | GROUP BY filename | ||
| 300 | EOT | ||
| 301 | |||
| 302 | my $sth = $dbh->prepare($query); | ||
| 303 | $sth->execute(uuid_deflate($project->project_id), @add_vals) or die $sth->errstr; | ||
| 304 | |||
| 305 | my @rows; | ||
| 306 | for my $row (sort { $b->[1] <=> $a->[1] } @{$sth->fetchall_arrayref}) { | ||
| 307 | splice( | ||
| 308 | @$row, 6, 0, | ||
| 309 | int($row->[6] / $row->[4] * 100) . '%', | ||
| 310 | int($row->[7] / $row->[4] * 100) . '%', | ||
| 311 | ); | ||
| 312 | $row->[1] = {formatted => display_duration($row->[1]), raw => $row->[1]}; | ||
| 313 | $row->[2] = {formatted => display_duration($row->[2]), raw => $row->[2]}; | ||
| 314 | unshift @$row => {}; | ||
| 315 | push @rows => $row; | ||
| 316 | } | ||
| 317 | |||
| 318 | $stat->{table} = { | ||
| 319 | class => 'expense', | ||
| 320 | sortable => 1, | ||
| 321 | header => ['Test File', 'Total Time', 'Average Time', 'Runs', 'Jobs', 'Users', 'Pass Rate', 'Failure Rate', 'Passes', 'Fails', 'Retries', 'Aborts'], | ||
| 322 | rows => \@rows, | ||
| 323 | }; | ||
| 324 | } | ||
| 325 | |||
| 326 | sub _build_stat_expensive_subtests { | ||
| 327 | my $self = shift; | ||
| 328 | my ($project, $stat) = @_; | ||
| 329 | |||
| 330 | my $schema = $self->{+CONFIG}->schema; | ||
| 331 | my $dbh = $schema->storage->dbh; | ||
| 332 | |||
| 333 | my ($add_query, @add_vals) = $self->get_add_query($project, $stat); | ||
| 334 | |||
| 335 | my $query = <<" EOT"; | ||
| 336 | SELECT test_files.filename AS filename, | ||
| 337 | subtest AS subtest, | ||
| 338 | SUM(duration) AS total_duration, | ||
| 339 | AVG(duration) AS average_duration, | ||
| 340 | COUNT(DISTINCT(run_ord)) AS runs, | ||
| 341 | COUNT(duration) AS tries, | ||
| 342 | COUNT(DISTINCT(user_id)) AS users, | ||
| 343 | SUM(pass) AS pass, | ||
| 344 | SUM(fail) AS fail, | ||
| 345 | SUM(abort) AS abort | ||
| 346 | FROM reporting | ||
| 347 | LEFT JOIN test_files USING(test_file_id) | ||
| 348 | WHERE project_id = ? | ||
| 349 | AND subtest IS NOT NULL | ||
| 350 | AND test_file_id IS NOT NULL | ||
| 351 | $add_query | ||
| 352 | GROUP BY filename, subtest | ||
| 353 | EOT | ||
| 354 | |||
| 355 | my $sth = $dbh->prepare($query); | ||
| 356 | $sth->execute(uuid_deflate($project->project_id), @add_vals) or die $sth->errstr; | ||
| 357 | |||
| 358 | my @rows; | ||
| 359 | for my $row (sort { $b->[2] <=> $a->[2] } @{$sth->fetchall_arrayref}) { | ||
| 360 | splice( | ||
| 361 | @$row, 7, 0, | ||
| 362 | int($row->[7] / $row->[5] * 100) . '%', | ||
| 363 | int($row->[8] / $row->[5] * 100) . '%', | ||
| 364 | ); | ||
| 365 | $row->[2] = {formatted => display_duration($row->[2]), raw => $row->[2]}; | ||
| 366 | $row->[3] = {formatted => display_duration($row->[3]), raw => $row->[3]}; | ||
| 367 | unshift @$row => {}; | ||
| 368 | push @rows => $row; | ||
| 369 | } | ||
| 370 | |||
| 371 | $stat->{table} = { | ||
| 372 | class => 'expense', | ||
| 373 | sortable => 1, | ||
| 374 | header => ['Test File', 'Subtest', 'Total Time', 'Average Time', 'Runs', 'Jobs', 'Users', 'Pass Rate', 'Failure Rate', 'Passes', 'Fails', 'Aborts'], | ||
| 375 | rows => \@rows, | ||
| 376 | }; | ||
| 377 | } | ||
| 378 | |||
| 379 | sub _build_stat_expensive_users { | ||
| 380 | my $self = shift; | ||
| 381 | my ($project, $stat) = @_; | ||
| 382 | |||
| 383 | my $schema = $self->{+CONFIG}->schema; | ||
| 384 | my $dbh = $schema->storage->dbh; | ||
| 385 | |||
| 386 | my ($add_query, @add_vals) = $self->get_add_query($project, $stat); | ||
| 387 | |||
| 388 | my $query = <<" EOT"; | ||
| 389 | SELECT users.username AS username, | ||
| 390 | SUM(duration) AS total_duration, | ||
| 391 | AVG(duration) AS average_duration, | ||
| 392 | COUNT(duration) AS runs, | ||
| 393 | SUM(pass) AS pass, | ||
| 394 | SUM(fail) AS fail, | ||
| 395 | SUM(abort) AS abort | ||
| 396 | FROM reporting | ||
| 397 | LEFT JOIN users USING(user_id) | ||
| 398 | WHERE project_id = ? | ||
| 399 | AND job_key IS NULL | ||
| 400 | AND subtest IS NULL | ||
| 401 | $add_query | ||
| 402 | GROUP BY username | ||
| 403 | EOT | ||
| 404 | |||
| 405 | my $sth = $dbh->prepare($query); | ||
| 406 | $sth->execute(uuid_deflate($project->project_id), @add_vals) or die $sth->errstr; | ||
| 407 | |||
| 408 | my @rows; | ||
| 409 | for my $row (sort { $b->[1] <=> $a->[1] } @{$sth->fetchall_arrayref}) { | ||
| 410 | splice( | ||
| 411 | @$row, 4, 0, | ||
| 412 | int($row->[4] / $row->[3] * 100) . '%', | ||
| 413 | int($row->[5] / $row->[3] * 100) . '%', | ||
| 414 | ); | ||
| 415 | $row->[1] = {formatted => display_duration($row->[1]), raw => $row->[1]}; | ||
| 416 | $row->[2] = {formatted => display_duration($row->[2]), raw => $row->[2]}; | ||
| 417 | unshift @$row => {}; | ||
| 418 | push @rows => $row; | ||
| 419 | } | ||
| 420 | |||
| 421 | $stat->{table} = { | ||
| 422 | class => 'expense', | ||
| 423 | sortable => 1, | ||
| 424 | header => ['User', 'Total Time', 'Average Time', 'Runs', 'Pass Rate', 'Fail Rate', 'Passes', 'Fails', 'Aborts'], | ||
| 425 | rows => \@rows, | ||
| 426 | }; | ||
| 427 | } | ||
| 428 | |||
| 429 | sub _build_stat_user_summary { | ||
| 430 | my $self = shift; | ||
| 431 | my ($project, $stat) = @_; | ||
| 432 | |||
| 433 | my $schema = $self->{+CONFIG}->schema; | ||
| 434 | my $dbh = $schema->storage->dbh; | ||
| 435 | |||
| 436 | my ($add_query, @add_vals) = $self->get_add_query($project, $stat); | ||
| 437 | |||
| 438 | my $query = <<" EOT"; | ||
| 439 | SELECT SUM(duration) AS total_duration, | ||
| 440 | AVG(duration) AS average_duration, | ||
| 441 | COUNT(DISTINCT(run_ord)) AS runs, | ||
| 442 | COUNT(DISTINCT(user_id)) AS users, | ||
| 443 | SUM(pass) AS pass, | ||
| 444 | SUM(fail) AS fail, | ||
| 445 | SUM(retry) AS retry, | ||
| 446 | SUM(abort) AS abort, | ||
| 447 | CASE WHEN test_file_id IS NULL THEN FALSE ELSE TRUE END AS has_file, | ||
| 448 | CASE WHEN subtest IS NULL THEN FALSE ELSE TRUE END AS has_subtest, | ||
| 449 | COUNT(subtest) AS total_subtests, | ||
| 450 | COUNT(test_file_id) AS total_test_files, | ||
| 451 | COUNT(DISTINCT(subtest)) AS unique_subtests, | ||
| 452 | COUNT(DISTINCT(test_file_id)) AS unique_test_files | ||
| 453 | FROM reporting | ||
| 454 | WHERE project_id = ? | ||
| 455 | $add_query | ||
| 456 | GROUP BY has_file, has_subtest | ||
| 457 | ORDER BY has_File, has_subtest | ||
| 458 | EOT | ||
| 459 | |||
| 460 | my $sth = $dbh->prepare($query); | ||
| 461 | $sth->execute(uuid_deflate($project->project_id), @add_vals) or die $sth->errstr; | ||
| 462 | |||
| 463 | my $runs = $sth->fetchrow_hashref; | ||
| 464 | |||
| 465 | return $stat->{text} = "No run data." unless $runs->{runs}; | ||
| 466 | |||
| 467 | my $jobs = $sth->fetchrow_hashref; | ||
| 468 | my $subs = $sth->fetchrow_hashref; | ||
| 469 | |||
| 470 | $stat->{pair_sets} = []; | ||
| 471 | |||
| 472 | push @{$stat->{pair_sets}} => [ | ||
| 473 | ['User Summary'], | ||
| 474 | ["Total unique users" => $runs->{users} // 0], | ||
| 475 | ["Average time per user" => display_duration(($runs->{total_duration} // 0) / $runs->{users})], | ||
| 476 | ["Average runs per user" => $runs->{runs} / $runs->{users}], | ||
| 477 | ] if $runs->{runs} && $runs->{users}; | ||
| 478 | |||
| 479 | push @{$stat->{pair_sets}} => [ | ||
| 480 | ['Run Summary'], | ||
| 481 | ["Total time spent running tests" => display_duration($runs->{total_duration} // 0)], | ||
| 482 | ["Average time per test run" => display_duration($runs->{average_duration} // 0)], | ||
| 483 | ["Total runs" => $runs->{runs} // 0], | ||
| 484 | ["Total incomplete runs" => $runs->{abort} // 0], | ||
| 485 | ["Total passed runs" => $runs->{pass} // 0], | ||
| 486 | ["Total failed runs" => $runs->{fail} // 0], | ||
| 487 | ] if $runs->{runs}; | ||
| 488 | |||
| 489 | push @{$stat->{pair_sets}} => [ | ||
| 490 | ['Job Summary'], | ||
| 491 | ["Average time per job" => display_duration($runs->{total_duration} / $jobs->{total_test_files})], | ||
| 492 | ["Total unique test files" => $jobs->{unique_test_files} // 0], | ||
| 493 | ["Total jobs executed" => $jobs->{total_test_files} // 0], | ||
| 494 | ["Total passed files" => $jobs->{pass} // 0], | ||
| 495 | ["Total failed files" => $jobs->{fail} // 0], | ||
| 496 | ["Total retried files" => $jobs->{retry} // 0], | ||
| 497 | ] if $jobs->{total_test_files}; | ||
| 498 | |||
| 499 | push @{$stat->{pair_sets}} => [ | ||
| 500 | ['Subtest Summary'], | ||
| 501 | ["Average time per subtest" => display_duration($subs->{total_duration} / $subs->{total_subtests})], | ||
| 502 | ["Total unique subtests" => $subs->{unique_subtests} // 0], | ||
| 503 | ["Total subtests executed" => $subs->{total_subtests} // 0], | ||
| 504 | ["Total passed subtests" => $subs->{pass} // 0], | ||
| 505 | ["Total failed sustests" => $subs->{fail} // 0], | ||
| 506 | ["Total retried subtests" => $subs->{retry} // 0], | ||
| 507 | ] if $subs->{total_subtests}; | ||
| 508 | } | ||
| 509 | |||
| 510 | sub _build_stat_uncovered { | ||
| 511 | my $self = shift; | ||
| 512 | my ($project, $stat) = @_; | ||
| 513 | |||
| 514 | my $schema = $self->{+CONFIG}->schema; | ||
| 515 | |||
| 516 | my $users = $stat->{users}; | ||
| 517 | my $field = $schema->resultset('RunField')->search( | ||
| 518 | { | ||
| 519 | 'me.name' => 'coverage', | ||
| 520 | 'me.data' => \'IS NOT NULL', | ||
| 521 | 'run.project_id' => $project->project_id, | ||
| 522 | 'run.has_coverage' => 1, | ||
| 523 | @$users ? (user_id => {'-in' => [map { uuid_deflate($_) } @$users]}) : () | ||
| 524 | }, | ||
| 525 | { | ||
| 526 | join => 'run', | ||
| 527 | order_by => {'-DESC' => 'run.added'}, | ||
| 528 | rows => 1 | ||
| 529 | }, | ||
| 530 | )->first; | ||
| 531 | |||
| 532 | return $stat->{text} = "No coverage data." | ||
| 533 | unless $field; | ||
| 534 | |||
| 535 | my $untested = $field->data->{untested}; | ||
| 536 | my $files = $untested->{files} // []; | ||
| 537 | my $subs = $untested->{subs} // {}; | ||
| 538 | |||
| 539 | my $data = {}; | ||
| 540 | for my $file (sort @$files, keys %$subs) { | ||
| 541 | $data->{$file} //= $subs->{$file} // []; | ||
| 542 | } | ||
| 543 | |||
| 544 | return $stat->{text} = "Full Coverage!" | ||
| 545 | unless keys %$data; | ||
| 546 | |||
| 547 | $stat->{json} = $data; | ||
| 548 | } | ||
| 549 | |||
| 550 | sub _build_stat_coverage { | ||
| 551 | my $self = shift; | ||
| 552 | my ($project, $stat) = @_; | ||
| 553 | |||
| 554 | my $n = $stat->{n}; | ||
| 555 | |||
| 556 | my $schema = $self->{+CONFIG}->schema; | ||
| 557 | |||
| 558 | my $users = $stat->{users}; | ||
| 559 | my @items = reverse $schema->resultset('RunField')->search( | ||
| 560 | { | ||
| 561 | 'me.name' => 'coverage', | ||
| 562 | 'me.data' => \'IS NOT NULL', | ||
| 563 | 'run.project_id' => $project->project_id, | ||
| 564 | 'run.has_coverage' => 1, | ||
| 565 | @$users ? (user_id => {'-in' => [map { uuid_deflate($_) } @$users]}) : () | ||
| 566 | }, | ||
| 567 | { | ||
| 568 | join => 'run', | ||
| 569 | order_by => {'-DESC' => 'run.added'}, | ||
| 570 | $n ? (rows => $n) : (), | ||
| 571 | }, | ||
| 572 | )->all; | ||
| 573 | |||
| 574 | my $labels = []; | ||
| 575 | my $subs = []; | ||
| 576 | my $files = []; | ||
| 577 | for my $item (@items) { | ||
| 578 | next unless $item->data; | ||
| 579 | push @$labels => ''; | ||
| 580 | my $metrics = $item->data->{metrics} // $item->data; | ||
| 581 | push @$files => int($metrics->{files}->{tested} / $metrics->{files}->{total} * 100) if $metrics->{files}->{total}; | ||
| 582 | push @$subs => int($metrics->{subs}->{tested} / $metrics->{subs}->{total} * 100) if $metrics->{subs}->{total}; | ||
| 583 | } | ||
| 584 | |||
| 585 | return $stat->{text} = "No sub or file data." | ||
| 586 | unless @$files || @$subs; | ||
| 587 | |||
| 588 | $stat->{chart} = { | ||
| 589 | type => 'line', | ||
| 590 | data => { | ||
| 591 | labels => $labels, | ||
| 592 | datasets => [ | ||
| 593 | { | ||
| 594 | label => 'Subroutine Coverage', | ||
| 595 | data => $subs, | ||
| 596 | borderColor => 'rgb(50, 255, 50)', | ||
| 597 | backgroundColor => 'rgb(50, 255, 50)', | ||
| 598 | }, | ||
| 599 | { | ||
| 600 | label => 'File Coverage', | ||
| 601 | data => $files, | ||
| 602 | borderColor => 'rgb(50, 50, 255)', | ||
| 603 | backgroundColor => 'rgb(50, 50, 255)', | ||
| 604 | } | ||
| 605 | ], | ||
| 606 | }, | ||
| 607 | options => { | ||
| 608 | elements => { | ||
| 609 | point => { radius => 3 }, | ||
| 610 | line => { borderWidth => 1 }, | ||
| 611 | }, | ||
| 612 | scales => { | ||
| 613 | y => { | ||
| 614 | beginAtZero => \1, | ||
| 615 | ticks => { | ||
| 616 | callback => 'percent', | ||
| 617 | }, | ||
| 618 | }, | ||
| 619 | }, | ||
| 620 | }, | ||
| 621 | }; | ||
| 622 | } | ||
| 623 | |||
| 624 | 1; | ||
| 625 | |||
| 626 | __END__ |