CrashTest.pl
author Vincent Tondellier <tonton+hg@team1664.org>
Thu, 07 Aug 2014 23:51:40 +0200
changeset 35 093ea107a96f
parent 34 e0d6597078a5
child 37 013953be0f3b
permissions -rwxr-xr-x
Add bootstrap glyphicons / fonts (will be used soon)

#!/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';

plugin 'TagHelpers::BootstrapPagination';

app->attr(storage => sub {
    my $self = shift;
    my $loader = Mojo::Loader->new;

    my $storage_class = $self->app->config->{Storage}->{Type};
    if (my $e = $loader->load($storage_class)) {
        die ref $e ? "Exception: $e" : 'Not found!';
    }

    return $storage_class->new(config => $self->app->config->{Storage});
});

app->attr(decode_queue => sub {
    my $self = shift;
    my $loader = Mojo::Loader->new;

    my $decode_class = $self->app->config->{DecodeQueue}->{Type};
    if (my $e = $loader->load($decode_class)) {
        die ref $e ? "Exception: $e" : 'Not found!';
    }

    return $decode_class->new(
        config => $self->app->config->{DecodeQueue},
        dumper_config => $self->app->config->{Dumper},
        storage => $self->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));
};

helper xml_escape_block => sub {
    my ($c, $block) = @_;
    my $result = $block->();
    $result = xml_escape $result;
    return Mojo::ByteStream->new($result);
};

get '/' => sub {
    my $self = shift;
    my $page = 1;
    $self->validation->required('page')->like(qr/^[0-9]+$/);
    $page = scalar $self->validation->param("page") if $self->validation->is_valid('page');

    my $result = $self->app->storage->index($page, 20);

    $self->stash(files => $result->{crashs});
    $self->stash(pager => $result->{pager});
    $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);

    $self->render_later();

    $self->app->decode_queue->decode($file, \%paramshash, $uuidstr, sub {
            my $pjson = shift;
            # reply
            $self->render(text => $pjson->{status});
        }
    );

};

app->secrets([
    'My secret passphrase here'
]);

push @{app->commands->namespaces}, 'CrashTest::Commands';
app->start;