CrashTest.pl
author Vincent Tondellier <tonton+hg@team1664.org>
Wed, 25 Jun 2014 00:16:09 +0200
changeset 21 fab315d8c7a8
parent 19 300b902b5461
child 23 e621317229f7
permissions -rwxr-xr-x
Added tag 0.0.1 for changeset e9c1591a4f54

#!/usr/bin/env perl

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use Mojolicious::Lite;
use UUID;
use Mojo::JSON;
use Mojo::ByteStream 'b';
use Mojo::UserAgent;
use lib 'lib';

my @valid_params = qw/Add-ons Distributor ProductName ReleaseChannel StartupTime UserID Version BuildID CrashTime Comments/;
my $config = plugin 'Config';

app->attr(storage => sub {
    my $self = shift;
    eval "require $config->{Storage}->{Type}";
    return $config->{Storage}->{Type}->new($config->{Storage});
});

app->attr(decode_queue => sub {
    my $self = shift;
    eval "require $config->{DecodeQueue}->{Type}";
    return $config->{DecodeQueue}->{Type}->new($config->{DecodeQueue}, $config->{Dumper}, app->storage);
});

helper scm_file_link => sub {
    my ($self, $file, $line) = @_;

    return "" unless(defined($file));

    if($file =~ qr{([a-z]+):([a-z/.]+):(.+):(\d+)})
    {
        my ($scm, $repo, $scmpath, $rev) = ($1, $2, $3, $4);
        my $filename = File::Spec->splitpath($scmpath);
        my $scmrepo = "$scm:$repo";
        if(exists($config->{ScmLinks}->{$scmrepo})) {
            return b($self->link_to("$filename:$line" =>
                    $self->render(inline => $config->{ScmLinks}->{$scmrepo},
                        repo => $repo, scmpath => $scmpath, rev => $rev, line => $line, partial => 1)
                ));
        }
        #return $file;
    }
    my $filebase = (File::Spec->splitpath($file))[-1];
    if(defined($line) && $line ne "") {
        return "$filebase:$line";
    }
    return $filebase;
};

helper shorten_signature => sub {
    my ($self, $signature) = @_;

    return "" if(!defined($signature) || $signature eq "");

    my $short_signature = $signature;
    if($signature =~ qr{([^<]+)<.+>::([^()]+)\(.*\)(.*)}) {
        # c++ with template
        $short_signature = "$1<>::$2()$3";
    } elsif($signature =~ qr{([^()]+)\(.*\)(.*)}) {
        # c/c++
        $short_signature = "$1()$2";
    }
    return b($self->t(span => (title => $signature, class => "shortened-signature") => $short_signature));
};

get '/' => sub {
    my $self = shift;
    $self->stash(files => $self->app->storage->index());
    $self->render('index');
} => 'index';

get '/report/:uuid' => [ uuid => qr/[0-9a-fA-F-]+/ ] => sub {
    my $self = shift;
    $self->stash(processed_data => $self->app->storage->get_processed_data($self->param('uuid')));
    $self->render('report/crash');
} => 'report';

post '/submit' => sub {
    my $self = shift;

    # save the dump in a file
    my $file = $self->req->upload('upload_file_minidump');
    my %paramshash = map { $_ => $self->req->param($_) } $self->req->param;

    my ($uuid, $uuidstr);
    UUID::generate($uuid);
    UUID::unparse($uuid, $uuidstr);
    my $pjson = $self->app->decode_queue->decode($file, \%paramshash, $uuidstr);

    # reply
    $self->render_text($pjson->{status});
};

app->secret('My secret passphrase here');
app->start;