lib/CrashTest/Plugin/Storage/Sql/Model/CrashReport.pm
author Vincent Tondellier <tonton+hg@team1664.org>
Sun, 18 Dec 2016 21:11:27 +0100
changeset 122 8692800ec9ba
parent 121 5a99941ed0ca
child 127 0bbbadd5d9ea
permissions -rw-r--r--
Use Dzil PkgVersion Add a newline after each package to make Dist::Zilla happy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     1
# This program is free software: you can redistribute it and/or modify
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     2
# it under the terms of the GNU General Public License as published by
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     3
# the Free Software Foundation, either version 3 of the License, or
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     4
# (at your option) any later version.
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     5
#
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     6
# This program is distributed in the hope that it will be useful,
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     9
# GNU General Public License for more details.
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    10
#
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    11
# You should have received a copy of the GNU General Public License
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    12
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    13
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    14
package CrashTest::Plugin::Storage::Sql::Model::CrashReport;
122
8692800ec9ba Use Dzil PkgVersion
Vincent Tondellier <tonton+hg@team1664.org>
parents: 121
diff changeset
    15
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    16
use Mojo::Base -base;
93
31013a09b483 Factorize search between reports and groups
Vincent Tondellier <tonton+hg@team1664.org>
parents: 89
diff changeset
    17
use Mojo::JSON::MaybeXS;
31013a09b483 Factorize search between reports and groups
Vincent Tondellier <tonton+hg@team1664.org>
parents: 89
diff changeset
    18
use Mojo::JSON qw/encode_json decode_json/;
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    19
use DateTime;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    20
use Data::Page;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    21
#use DBI::Log;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    22
#$DBI::Log::trace = 0;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    23
88
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    24
use CrashTest::Plugin::Storage::Sql::Model::CrashGroup;
93
31013a09b483 Factorize search between reports and groups
Vincent Tondellier <tonton+hg@team1664.org>
parents: 89
diff changeset
    25
use CrashTest::Plugin::Storage::Sql::Utils;
88
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    26
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    27
has [ qw/instance app config/ ];
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    28
93
31013a09b483 Factorize search between reports and groups
Vincent Tondellier <tonton+hg@team1664.org>
parents: 89
diff changeset
    29
has [ qw/db sql_utils crash_groups/ ];
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    30
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    31
sub new {
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    32
    my $self = shift->SUPER::new(@_);
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    33
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    34
    $self->db($self->instance->dbh->db);
93
31013a09b483 Factorize search between reports and groups
Vincent Tondellier <tonton+hg@team1664.org>
parents: 89
diff changeset
    35
    $self->sql_utils(CrashTest::Plugin::Storage::Sql::Utils->new(@_));
88
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    36
    $self->crash_groups(CrashTest::Plugin::Storage::Sql::Model::CrashGroup->new(@_));
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    37
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    38
    return $self;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    39
}
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    40
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    41
sub _build_query_from_search_string {
93
31013a09b483 Factorize search between reports and groups
Vincent Tondellier <tonton+hg@team1664.org>
parents: 89
diff changeset
    42
    my ($self, $search_str) = @_;
31013a09b483 Factorize search between reports and groups
Vincent Tondellier <tonton+hg@team1664.org>
parents: 89
diff changeset
    43
    return $self->sql_utils->build_query_from_search_string(
31013a09b483 Factorize search between reports and groups
Vincent Tondellier <tonton+hg@team1664.org>
parents: 89
diff changeset
    44
        $search_str,
100
4dae01f2beee Allow extra columns in group index, and split search columns
Vincent Tondellier <tonton+hg@team1664.org>
parents: 98
diff changeset
    45
        $self->app->config->{WebInterface}->{ExtraColumns}->{Search}, {
93
31013a09b483 Factorize search between reports and groups
Vincent Tondellier <tonton+hg@team1664.org>
parents: 89
diff changeset
    46
            user_id     => { name => 'crash_user.user_id' },
31013a09b483 Factorize search between reports and groups
Vincent Tondellier <tonton+hg@team1664.org>
parents: 89
diff changeset
    47
            product     => { name => 'product.name' },
31013a09b483 Factorize search between reports and groups
Vincent Tondellier <tonton+hg@team1664.org>
parents: 89
diff changeset
    48
            version     => { name => 'product.version' },
31013a09b483 Factorize search between reports and groups
Vincent Tondellier <tonton+hg@team1664.org>
parents: 89
diff changeset
    49
            channel     => { name => 'product.release_channel' },
31013a09b483 Factorize search between reports and groups
Vincent Tondellier <tonton+hg@team1664.org>
parents: 89
diff changeset
    50
            group_id    => { name => 'crash_reports.crash_group_id' },
31013a09b483 Factorize search between reports and groups
Vincent Tondellier <tonton+hg@team1664.org>
parents: 89
diff changeset
    51
            function    => { name => 'crash_group.crash_thread_signature_bt', type => "fuzzy" },
116
39449f7dab99 Adding bug links (column and search), enabled by default
Vincent Tondellier <tonton+hg@team1664.org>
parents: 100
diff changeset
    52
            bug         => { name => 'crash_reports.crash_group_id IN (SELECT crash_group_id FROM bug_links WHERE bug_key ?op? ?)', type => 'sql' },
93
31013a09b483 Factorize search between reports and groups
Vincent Tondellier <tonton+hg@team1664.org>
parents: 89
diff changeset
    53
        });
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    54
}
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    55
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    56
sub index {
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    57
    my ($self, $pagen, $nperpage, $search_str) = @_;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    58
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    59
    my $where = "";
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    60
    my @values = ();
121
5a99941ed0ca Display an error message instead of a 500 page when the search string is invalid
Vincent Tondellier <tonton+hg@team1664.org>
parents: 120
diff changeset
    61
    my $err;
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    62
    if(defined($search_str) && $search_str ne "") {
121
5a99941ed0ca Display an error message instead of a 500 page when the search string is invalid
Vincent Tondellier <tonton+hg@team1664.org>
parents: 120
diff changeset
    63
        eval {
5a99941ed0ca Display an error message instead of a 500 page when the search string is invalid
Vincent Tondellier <tonton+hg@team1664.org>
parents: 120
diff changeset
    64
            my $q = $self->_build_query_from_search_string($search_str);
5a99941ed0ca Display an error message instead of a 500 page when the search string is invalid
Vincent Tondellier <tonton+hg@team1664.org>
parents: 120
diff changeset
    65
            $where = "WHERE " . $q->[0];
5a99941ed0ca Display an error message instead of a 500 page when the search string is invalid
Vincent Tondellier <tonton+hg@team1664.org>
parents: 120
diff changeset
    66
            @values = @{$q->[1]};
5a99941ed0ca Display an error message instead of a 500 page when the search string is invalid
Vincent Tondellier <tonton+hg@team1664.org>
parents: 120
diff changeset
    67
        };
5a99941ed0ca Display an error message instead of a 500 page when the search string is invalid
Vincent Tondellier <tonton+hg@team1664.org>
parents: 120
diff changeset
    68
        $err = $@;
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    69
    }
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    70
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    71
    my $count = $self->db->query("
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    72
        SELECT count(crash_reports.id) AS total FROM crash_reports
94
c5fcb00c7261 Use LEFT JOIN to use PostgreSQL join removal when possible
Vincent Tondellier <tonton+hg@team1664.org>
parents: 93
diff changeset
    73
        LEFT JOIN crash_users AS crash_user ON crash_reports.crash_user_id = crash_user.id
c5fcb00c7261 Use LEFT JOIN to use PostgreSQL join removal when possible
Vincent Tondellier <tonton+hg@team1664.org>
parents: 93
diff changeset
    74
        LEFT JOIN products AS product ON crash_reports.product_id = product.id
c5fcb00c7261 Use LEFT JOIN to use PostgreSQL join removal when possible
Vincent Tondellier <tonton+hg@team1664.org>
parents: 93
diff changeset
    75
        LEFT JOIN crash_groups AS crash_group ON crash_reports.crash_group_id = crash_group.id
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    76
        $where
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    77
        ", @values)->hash;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    79
    my $pager = Data::Page->new();
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    80
    $pager->total_entries($count->{total});
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    81
    $pager->entries_per_page($nperpage);
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    82
    $pager->current_page($pagen);
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    83
88
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    84
    my @extra_cols;
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    85
    foreach my $extra_col(@{$self->app->config->{WebInterface}->{ExtraColumns}->{Index}}) {
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    86
        push @extra_cols, $extra_col->{db_column} . " AS " . $extra_col->{id};
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    87
    }
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    88
    my $extra_columns = join(",", @extra_cols);
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    89
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    90
    my $results = $self->db->query("
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    91
        SELECT  crash_reports.*,
88
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    92
                product.distributor AS p_distributor, product.name AS p_name, product.version AS p_version, product.release_channel AS p_release_channel,
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    93
                crash_user.os AS u_os, crash_user.cpu_arch AS u_cpu_arch, crash_user.cpu_count AS u_cpu_count, crash_user.extra_info AS u_extra_info,
118
a8ff5a8a4fe8 Let PostgreSQL remove unneeded join on crash_group if possible, join bug_link on crash_report.crash_group_id
Vincent Tondellier <tonton+hg@team1664.org>
parents: 116
diff changeset
    94
                (SELECT json_agg(to_json(bug_links)) FROM bug_links WHERE bug_links.crash_group_id = crash_reports.crash_group_id) AS bug_links,
88
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    95
                $extra_columns
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    96
        FROM crash_reports
94
c5fcb00c7261 Use LEFT JOIN to use PostgreSQL join removal when possible
Vincent Tondellier <tonton+hg@team1664.org>
parents: 93
diff changeset
    97
        LEFT JOIN crash_users AS crash_user ON crash_reports.crash_user_id = crash_user.id
c5fcb00c7261 Use LEFT JOIN to use PostgreSQL join removal when possible
Vincent Tondellier <tonton+hg@team1664.org>
parents: 93
diff changeset
    98
        LEFT JOIN products AS product ON crash_reports.product_id = product.id
c5fcb00c7261 Use LEFT JOIN to use PostgreSQL join removal when possible
Vincent Tondellier <tonton+hg@team1664.org>
parents: 93
diff changeset
    99
        LEFT JOIN crash_groups AS crash_group ON crash_reports.crash_group_id = crash_group.id
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   100
        $where
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   101
        ORDER BY crash_time DESC
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   102
        OFFSET (?) ROWS
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   103
        FETCH NEXT (?) ROWS ONLY
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   104
        ",
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   105
        @values,
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   106
        $pager->skipped, $pager->entries_per_page
119
0a9171619fd3 Use Mojo::Pg automatic json decoding instead of doing it manually
Vincent Tondellier <tonton+hg@team1664.org>
parents: 118
diff changeset
   107
    )->expand->hashes;
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   108
121
5a99941ed0ca Display an error message instead of a 500 page when the search string is invalid
Vincent Tondellier <tonton+hg@team1664.org>
parents: 120
diff changeset
   109
    return ($results, $pager, $err);
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   110
}
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   111
120
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   112
sub get {
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   113
    my ($self, $uuid) = @_;
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   114
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   115
    my @extra_cols;
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   116
    foreach my $extra_col(@{$self->app->config->{WebInterface}->{ExtraColumns}->{Index}}) {
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   117
        push @extra_cols, $extra_col->{db_column} . " AS " . $extra_col->{id};
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   118
    }
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   119
    my $extra_columns = join(",", @extra_cols);
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   120
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   121
    my $result = $self->db->query("
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   122
        SELECT  crash_reports.*,
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   123
                product.distributor AS p_distributor, product.name AS p_name, product.version AS p_version, product.release_channel AS p_release_channel,
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   124
                crash_user.os AS u_os, crash_user.cpu_arch AS u_cpu_arch, crash_user.cpu_count AS u_cpu_count, crash_user.extra_info AS u_extra_info,
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   125
                (SELECT json_agg(to_json(bug_links)) FROM bug_links WHERE bug_links.crash_group_id = crash_reports.crash_group_id) AS bug_links,
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   126
                $extra_columns
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   127
        FROM crash_reports
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   128
        LEFT JOIN crash_users AS crash_user ON crash_reports.crash_user_id = crash_user.id
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   129
        LEFT JOIN products AS product ON crash_reports.product_id = product.id
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   130
        LEFT JOIN crash_groups AS crash_group ON crash_reports.crash_group_id = crash_group.id
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   131
        WHERE crash_reports.uuid = ?
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   132
        ",
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   133
        $uuid
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   134
    )->expand->hash;
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   135
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   136
    return $result;
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   137
}
1a0fa98037fa Add Bug status table in the detailed report.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 119
diff changeset
   138
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   139
sub get_processed_data {
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   140
    my ($self, $uuid) = @_;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   141
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   142
    my $dbdata = $self->db->query("SELECT processed FROM crash_report_datas WHERE crash_report_id = (SELECT id FROM crash_reports WHERE uuid = ?)", $uuid)->hash;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   143
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   144
    my $processed_data = decode_json($dbdata->{processed});
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   145
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   146
    return $processed_data;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   147
}
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   148
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   149
sub create {
95
fcf3a68002e6 Use start time when crash time is not defined (fix warnings)
Vincent Tondellier <tonton+hg@team1664.org>
parents: 94
diff changeset
   150
    my ($self, $uuid, $pjson, $client_info, $dmp_file) = @_;
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   151
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   152
    if(!defined($client_info->{UserID})) {
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   153
        $self->app->log->info("Invalid crash $uuid: no UserID");
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   154
        return;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   155
    }
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   156
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   157
    my $tx = $self->db->begin;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   158
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   159
    my ($start_time, $crash_time);
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   160
    if($client_info->{StartupTime}) {
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   161
        $start_time = DateTime->from_epoch(epoch => $client_info->{StartupTime});
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   162
    }
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   163
    if($client_info->{CrashTime}) {
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   164
        $crash_time = DateTime->from_epoch(epoch => $client_info->{CrashTime});
95
fcf3a68002e6 Use start time when crash time is not defined (fix warnings)
Vincent Tondellier <tonton+hg@team1664.org>
parents: 94
diff changeset
   165
    } elsif($start_time) {
fcf3a68002e6 Use start time when crash time is not defined (fix warnings)
Vincent Tondellier <tonton+hg@team1664.org>
parents: 94
diff changeset
   166
        $crash_time = $start_time;
fcf3a68002e6 Use start time when crash time is not defined (fix warnings)
Vincent Tondellier <tonton+hg@team1664.org>
parents: 94
diff changeset
   167
    } else {
fcf3a68002e6 Use start time when crash time is not defined (fix warnings)
Vincent Tondellier <tonton+hg@team1664.org>
parents: 94
diff changeset
   168
        $crash_time = DateTime->from_epoch(epoch => time());
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   169
    }
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   170
88
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
   171
    chomp($client_info->{ProductName});
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
   172
    chomp($client_info->{Version}) if $client_info->{Version};
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   173
    my @product_values = (
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   174
        $client_info->{Distributor},
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   175
        $client_info->{ProductName},
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   176
        $client_info->{Version},
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   177
    );
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   178
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   179
    my $dbproduct = $self->db->query("SELECT * FROM products WHERE distributor = ? AND name = ? AND version = ?", @product_values)->hash;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   180
    if(!$dbproduct) {
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   181
        push @product_values, $client_info->{ReleaseChannel};
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   182
        $dbproduct = $self->db->query(
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   183
            "INSERT INTO products (distributor, name, version, release_channel) VALUES (?, ?, ?, ?) RETURNING *",
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   184
            @product_values
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   185
        )->hash;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   186
    }
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   187
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   188
    my @user_values = (
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   189
        $client_info->{UserID},
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   190
    );
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   191
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   192
    my $dbuser = $self->db->query("SELECT * FROM crash_users WHERE user_id = ?", @user_values)->hash;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   193
    if(!$dbuser) {
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   194
        push @user_values, $pjson->{system_info}->{cpu_arch};
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   195
        push @user_values, $pjson->{system_info}->{cpu_count};
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   196
        push @user_values, $pjson->{system_info}->{os};
93
31013a09b483 Factorize search between reports and groups
Vincent Tondellier <tonton+hg@team1664.org>
parents: 89
diff changeset
   197
        push @user_values, encode_json($client_info);
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   198
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   199
        $dbuser = $self->db->query(
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   200
            "INSERT INTO crash_users (user_id, cpu_arch, cpu_count, os, extra_info) VALUES (?, ?, ?, ?, ?) RETURNING *",
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   201
            @user_values
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   202
        )->hash;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   203
    }
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   204
88
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
   205
    my $crash_group = $self->crash_groups->find_or_create($uuid, $pjson);
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
   206
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   207
    my $main_module;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   208
    {
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   209
        my $i = $pjson->{main_module};
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   210
        $main_module = $pjson->{modules}->[$i]->{filename};
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   211
    }
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   212
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   213
    my $dbcrash = $self->db->query(
88
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
   214
        "INSERT INTO crash_reports (start_time, crash_time, uuid, main_module, product_id, crash_user_id, crash_group_id, crash_group_distance)
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
   215
         VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING id",
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
   216
        $start_time, $crash_time, $uuid, $main_module, $dbproduct->{id}, $dbuser->{id},
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
   217
        $crash_group ? $crash_group->{id} : undef,
c82f5589db11 Add first cut of the crash grouping feature
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
   218
        $crash_group ? ($crash_group->{dist} || 0) : undef,
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   219
    )->hash;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   220
98
aa2437932c03 Store client_info in database
Vincent Tondellier <tonton+hg@team1664.org>
parents: 96
diff changeset
   221
    # Create json for the params
aa2437932c03 Store client_info in database
Vincent Tondellier <tonton+hg@team1664.org>
parents: 96
diff changeset
   222
    $pjson->{client_info} = $client_info;
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   223
    $self->db->query(
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   224
        "INSERT INTO crash_report_datas (crash_report_id, processed) VALUES (?, ?) RETURNING id",
93
31013a09b483 Factorize search between reports and groups
Vincent Tondellier <tonton+hg@team1664.org>
parents: 89
diff changeset
   225
        $dbcrash->{id}, encode_json($pjson)
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   226
    );
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   227
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   228
    $tx->commit;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   229
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   230
    return $dbcrash->{id};
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   231
}
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   232
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   233
sub update {
96
716c0292967c Also update group in CrashReport::update
Vincent Tondellier <tonton+hg@team1664.org>
parents: 95
diff changeset
   234
    my ($self, $uuid, $pjson, $dmp_file) = @_;
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   235
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   236
    my $tx = $self->db->begin;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   237
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   238
    my $dbcrash = $self->db->query("SELECT id FROM crash_reports WHERE uuid = ?", $uuid)->hash;
96
716c0292967c Also update group in CrashReport::update
Vincent Tondellier <tonton+hg@team1664.org>
parents: 95
diff changeset
   239
716c0292967c Also update group in CrashReport::update
Vincent Tondellier <tonton+hg@team1664.org>
parents: 95
diff changeset
   240
    my $crash_group = $self->crash_groups->find_or_create($uuid, $pjson);
716c0292967c Also update group in CrashReport::update
Vincent Tondellier <tonton+hg@team1664.org>
parents: 95
diff changeset
   241
716c0292967c Also update group in CrashReport::update
Vincent Tondellier <tonton+hg@team1664.org>
parents: 95
diff changeset
   242
    $self->db->query("
716c0292967c Also update group in CrashReport::update
Vincent Tondellier <tonton+hg@team1664.org>
parents: 95
diff changeset
   243
        UPDATE crash_report_datas SET processed = ?
716c0292967c Also update group in CrashReport::update
Vincent Tondellier <tonton+hg@team1664.org>
parents: 95
diff changeset
   244
        WHERE crash_report_id = ?",
716c0292967c Also update group in CrashReport::update
Vincent Tondellier <tonton+hg@team1664.org>
parents: 95
diff changeset
   245
        encode_json($pjson),
716c0292967c Also update group in CrashReport::update
Vincent Tondellier <tonton+hg@team1664.org>
parents: 95
diff changeset
   246
        $dbcrash->{id},
716c0292967c Also update group in CrashReport::update
Vincent Tondellier <tonton+hg@team1664.org>
parents: 95
diff changeset
   247
    );
716c0292967c Also update group in CrashReport::update
Vincent Tondellier <tonton+hg@team1664.org>
parents: 95
diff changeset
   248
    $self->db->query("
716c0292967c Also update group in CrashReport::update
Vincent Tondellier <tonton+hg@team1664.org>
parents: 95
diff changeset
   249
        UPDATE crash_reports SET crash_group_id = ?, crash_group_distance = ?
716c0292967c Also update group in CrashReport::update
Vincent Tondellier <tonton+hg@team1664.org>
parents: 95
diff changeset
   250
        WHERE id = ?",
716c0292967c Also update group in CrashReport::update
Vincent Tondellier <tonton+hg@team1664.org>
parents: 95
diff changeset
   251
        $crash_group ? $crash_group->{id} : undef,
716c0292967c Also update group in CrashReport::update
Vincent Tondellier <tonton+hg@team1664.org>
parents: 95
diff changeset
   252
        $crash_group ? ($crash_group->{dist} || 0) : undef,
716c0292967c Also update group in CrashReport::update
Vincent Tondellier <tonton+hg@team1664.org>
parents: 95
diff changeset
   253
        $dbcrash->{id},
716c0292967c Also update group in CrashReport::update
Vincent Tondellier <tonton+hg@team1664.org>
parents: 95
diff changeset
   254
    );
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   255
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   256
    $tx->commit;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   257
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   258
    return 1;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   259
}
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   260
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   261
1;