| 1 | package Test2::Harness::UI::Controller::Coverage; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.000150'; | ||
| 6 | |||
| 7 | use Data::GUID; | ||
| 8 | use List::Util qw/max/; | ||
| 9 | use Test2::Harness::UI::Response qw/resp error/; | ||
| 10 | use Test2::Harness::Util::JSON qw/encode_json encode_pretty_json decode_json/; | ||
| 11 | use Test2::Harness::UI::UUID qw/uuid_inflate/; | ||
| 12 | |||
| 13 | use parent 'Test2::Harness::UI::Controller'; | ||
| 14 | use Test2::Harness::UI::Util::HashBase; | ||
| 15 | |||
| 16 | sub title { 'Coverage' } | ||
| 17 | |||
| 18 | sub handle { | ||
| 19 | my $self = shift; | ||
| 20 | my ($route) = @_; | ||
| 21 | |||
| 22 | my $req = $self->{+REQUEST}; | ||
| 23 | my $res = resp(200); | ||
| 24 | |||
| 25 | my $schema = $self->{+CONFIG}->schema; | ||
| 26 | |||
| 27 | die error(404 => 'Missing route') unless $route; | ||
| 28 | my $source = $route->{source} or die error(404 => 'No source'); | ||
| 29 | my $username = $route->{user}; | ||
| 30 | |||
| 31 | my $delete = $route->{delete}; | ||
| 32 | |||
| 33 | if ($username && $username eq 'delete') { | ||
| 34 | $delete = 1; | ||
| 35 | $username = undef; | ||
| 36 | } | ||
| 37 | |||
| 38 | my $run; | ||
| 39 | if (my $project = $schema->resultset('Project')->find({name => $source})) { | ||
| 40 | $run = $project->last_covered_run(user => $username); | ||
| 41 | } | ||
| 42 | else { | ||
| 43 | $source = uuid_inflate($source) or die error(404 => 'Invalid Run'); | ||
| 44 | $run = eval { $schema->resultset('Run')->find({run_id => $source}) } or warn $@; | ||
| 45 | die error(405) unless $run; | ||
| 46 | } | ||
| 47 | |||
| 48 | die error(404) unless $run; | ||
| 49 | |||
| 50 | if ($delete) { | ||
| 51 | die error(401 => 'Login required') unless $req->user; | ||
| 52 | $run->coverages->delete; | ||
| 53 | $run->update({has_coverage => 0}); | ||
| 54 | } | ||
| 55 | else { | ||
| 56 | my $iter = $run->coverage_data(iterator => 1); | ||
| 57 | |||
| 58 | my $done = 0; | ||
| 59 | $res->stream( | ||
| 60 | env => $req->env, | ||
| 61 | content_type => 'application/x-jsonl; charset=utf-8', | ||
| 62 | cache => 0, | ||
| 63 | headers => ["content-disposition" => 'attachment; filename="coverage.jsonl"'], | ||
| 64 | |||
| 65 | done => sub { $done }, | ||
| 66 | |||
| 67 | fetch => sub { | ||
| 68 | if (my $item = $iter->()) { | ||
| 69 | return encode_json($item) . "\n"; | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | $done = 1; | ||
| 73 | return; | ||
| 74 | } | ||
| 75 | }, | ||
| 76 | ); | ||
| 77 | } | ||
| 78 | |||
| 79 | return $res; | ||
| 80 | } | ||
| 81 | |||
| 82 | 1; | ||
| 83 | |||
| 84 | __END__ |