CrashTest.pl
changeset 11 0bef3b8087c1
parent 6 8dfc7e5dbdc7
child 12 c98d3fa4a948
equal deleted inserted replaced
10:4668386ec082 11:0bef3b8087c1
     2 
     2 
     3 use Mojolicious::Lite;
     3 use Mojolicious::Lite;
     4 use UUID;
     4 use UUID;
     5 use Mojo::JSON;
     5 use Mojo::JSON;
     6 use Mojo::UserAgent;
     6 use Mojo::UserAgent;
       
     7 use lib 'lib';
       
     8 use CrashTest::Storage::FileSystem;
     7 
     9 
     8 my @valid_params = qw/Add-ons Distributor ProductName ReleaseChannel StartupTime UserID Version BuildID CrashTime Comments/;
    10 my @valid_params = qw/Add-ons Distributor ProductName ReleaseChannel StartupTime UserID Version BuildID CrashTime Comments/;
     9 my $config = plugin 'Config';
    11 my $config = plugin 'Config';
    10 my $data_path = $config->{DataDir};
    12 my $storage = CrashTest::Storage::FileSystem->new($config->{DataDir});
    11 
    13 
    12 
    14 
    13 helper scm_file_link => sub {
    15 helper scm_file_link => sub {
    14     my ($self, $file, $line) = @_;
    16     my ($self, $file, $line) = @_;
    15 
    17 
    20         my ($scm, $repo, $scmpath, $rev) = ($1, $2, $3, $4);
    22         my ($scm, $repo, $scmpath, $rev) = ($1, $2, $3, $4);
    21         my $filename = File::Spec->splitpath($scmpath);
    23         my $filename = File::Spec->splitpath($scmpath);
    22         my $scmrepo = "$scm:$repo";
    24         my $scmrepo = "$scm:$repo";
    23         if(exists($config->{ScmLinks}->{$scmrepo})) {
    25         if(exists($config->{ScmLinks}->{$scmrepo})) {
    24             return Mojo::ByteStream->new($self->link_to("$filename:$line" =>
    26             return Mojo::ByteStream->new($self->link_to("$filename:$line" =>
    25                     $self->render(inline => $config->{ScmLinks}->{$scmrepo}, repo => $repo, scmpath => $scmpath, rev => $rev, line => $line, partial => 1)
    27                     $self->render(inline => $config->{ScmLinks}->{$scmrepo},
       
    28                         repo => $repo, scmpath => $scmpath, rev => $rev, line => $line, partial => 1)
    26                 ));
    29                 ));
    27         }
    30         }
    28         #return $file;
    31         #return $file;
    29     }
    32     }
    30     my $filebase = (File::Spec->splitpath($file))[-1];
    33     my $filebase = (File::Spec->splitpath($file))[-1];
    51 };
    54 };
    52 
    55 
    53 # Upload form in DATA section
    56 # Upload form in DATA section
    54 get '/' => sub {
    57 get '/' => sub {
    55     my $self = shift;
    58     my $self = shift;
    56 
    59     $self->stash(files => $storage->index());
    57     my @files;
       
    58     opendir my ($dh), $data_path or die $!;
       
    59     my @allfiles = readdir $dh;
       
    60     foreach(@allfiles)
       
    61     {
       
    62         if($_ =~ /(.*)\.json$/)
       
    63         {
       
    64             my $filename = File::Spec->catfile($data_path, $_);
       
    65             push @files, {
       
    66                 file => $filename,
       
    67                 uuid => $1,
       
    68                 signature => $1,
       
    69                 product => "",
       
    70                 date => (stat $filename)[9],
       
    71             };
       
    72         }
       
    73     }
       
    74     closedir $dh;
       
    75 
       
    76     my @sorted_files = ( sort { $b->{date} <=> $a->{date} } @files );
       
    77     @sorted_files = @sorted_files[0..19] if scalar(@sorted_files) > 20;
       
    78 
       
    79     $self->stash(files => \@sorted_files);
       
    80 
       
    81     $self->render('index');
    60     $self->render('index');
    82 } => 'index';
    61 } => 'index';
    83 
    62 
    84 get '/report/:uuid' => [ uuid => qr/[0-9a-fA-F-]+/ ] => sub {
    63 get '/report/:uuid' => [ uuid => qr/[0-9a-fA-F-]+/ ] => sub {
    85     my $self = shift;
    64     my $self = shift;
    86 
    65     $self->stash(processed_data => $storage->get_processed_data($self->param('uuid')));
    87     open JSON, '<', File::Spec->catfile($data_path, $self->param('uuid') . '.json') or die $!;
       
    88     my @json_content_lines = <JSON>;
       
    89     my $json_content = join('', @json_content_lines);
       
    90     close JSON;
       
    91 
       
    92     my $json = Mojo::JSON->new;
       
    93     my $processed_data = $json->decode($json_content);
       
    94 
       
    95     $self->stash(processed_data => $processed_data);
       
    96 
       
    97     $self->render('report/crash');
    66     $self->render('report/crash');
    98 } => 'report';
    67 } => 'report';
    99 
    68 
   100 post '/submit' => sub {
    69 post '/submit' => sub {
   101     my $self = shift;
    70     my $self = shift;
   103     # save the dump in a file
    72     # save the dump in a file
   104     my $file = $self->req->upload('upload_file_minidump');
    73     my $file = $self->req->upload('upload_file_minidump');
   105     my ($uuid, $uuidstr);
    74     my ($uuid, $uuidstr);
   106     UUID::generate($uuid);
    75     UUID::generate($uuid);
   107     UUID::unparse($uuid, $uuidstr);
    76     UUID::unparse($uuid, $uuidstr);
   108     my $dmp_file = File::Spec->catfile($data_path, "$uuidstr.dmp");
    77     my $dmp_file = "/tmp/$uuidstr.dmp";
   109     $file->move_to($dmp_file);
    78     $file->move_to($dmp_file);
   110 
       
   111     # Create json for the params
       
   112     my %paramshash = map { $_ => $self->req->param($_) } $self->req->param;
       
   113 
    79 
   114     my $out = qx($config->{MinidumpStackwalkJSON} "$dmp_file" $config->{SymbolsPath} 2>/dev/null) or die $!;
    80     my $out = qx($config->{MinidumpStackwalkJSON} "$dmp_file" $config->{SymbolsPath} 2>/dev/null) or die $!;
   115 
    81 
   116     my $json = Mojo::JSON->new;
    82     my $json = Mojo::JSON->new;
   117     my $pjson = $json->decode($out);
    83     my $pjson = $json->decode($out);
       
    84 
       
    85     # Create json for the params
       
    86     my %paramshash = map { $_ => $self->req->param($_) } $self->req->param;
   118     $pjson->{client_info} = \%paramshash;
    87     $pjson->{client_info} = \%paramshash;
   119 
    88 
   120     my $j = $json->encode($pjson);
    89     $storage->store_dump($uuidstr, $file);
   121     open JSON, '>', File::Spec->catfile($data_path, "$uuidstr.json") or die $!;
    90     $storage->store_processed_data($uuidstr, $pjson);
   122     print JSON $j;
    91     unlink $dmp_file if -w $dmp_file;
   123     close JSON;
       
   124 
    92 
   125     # reply
    93     # reply
   126     $self->render_text($pjson->{status});
    94     $self->render_text($pjson->{status});
   127 };
    95 };
   128 
    96