lib/CrashTest/Storage/Sql.pm
author Vincent Tondellier <tonton+hg@team1664.org>
Tue, 11 Nov 2014 21:35:24 +0100
changeset 54 2218a127abd9
parent 42 604ffca3aec1
child 57 cebbfcd7deff
permissions -rw-r--r--
Fix deprecated warnings for Mojo::JSON OO syntax with Mojolicious >= 5.54
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     1
# This program is free software: you can redistribute it and/or modify
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     2
# it under the terms of the GNU General Public License as published by
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     3
# the Free Software Foundation, either version 3 of the License, or
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     4
# (at your option) any later version.
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     5
#
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     6
# This program is distributed in the hope that it will be useful,
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     9
# GNU General Public License for more details.
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    10
#
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    11
# You should have received a copy of the GNU General Public License
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    12
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    13
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    14
package CrashTest::Storage::Sql;
34
e0d6597078a5 Cleanup: Mojo-ize old code, simplifying perl OO code
Vincent Tondellier <tonton+hg@team1664.org>
parents: 33
diff changeset
    15
use Mojo::Base "CrashTest::Storage::FileSystem";
54
2218a127abd9 Fix deprecated warnings for Mojo::JSON OO syntax with Mojolicious >= 5.54
Vincent Tondellier <tonton+hg@team1664.org>
parents: 42
diff changeset
    16
use Mojo::JSON qw/j/;
24
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    17
use DateTime;
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    18
use CrashTest::Storage::Sql::Schema;
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    19
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    20
sub new {
34
e0d6597078a5 Cleanup: Mojo-ize old code, simplifying perl OO code
Vincent Tondellier <tonton+hg@team1664.org>
parents: 33
diff changeset
    21
    my $self = shift->SUPER::new(@_);
24
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    22
34
e0d6597078a5 Cleanup: Mojo-ize old code, simplifying perl OO code
Vincent Tondellier <tonton+hg@team1664.org>
parents: 33
diff changeset
    23
    $self->{schema} = CrashTest::Storage::Sql::Schema->connect($self->config->{DSN});
24
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    24
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    25
    return $self;
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    26
}
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    27
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    28
sub index {
29
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents: 24
diff changeset
    29
    my ($self, $page, $nperpage) = @_;
24
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    30
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    31
    my $dbcrashs = $self->{schema}->resultset('CrashReport')->search(
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    32
        undef,
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    33
        {
29
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents: 24
diff changeset
    34
            prefetch    => [ 'product', 'crash_user' ],
33
da690d68c1ff Fix sort order for Sql storage
Vincent Tondellier <tonton+hg@team1664.org>
parents: 31
diff changeset
    35
            order_by    => { -desc => 'crash_time' },
29
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents: 24
diff changeset
    36
            page        => $page,
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents: 24
diff changeset
    37
            rows        => $nperpage,
24
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    38
        },
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    39
    );
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    40
29
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents: 24
diff changeset
    41
    my $results = {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents: 24
diff changeset
    42
        pager   => $dbcrashs->pager,
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents: 24
diff changeset
    43
        crashs  => [],
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents: 24
diff changeset
    44
    };
24
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    45
    for my $crash($dbcrashs->all) {
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    46
        my $filename = File::Spec->catfile($self->{data_path}, $crash->uuid);
42
604ffca3aec1 Fix warnings with perl 5.20
Vincent Tondellier <tonton+hg@team1664.org>
parents: 34
diff changeset
    47
        push @{$results->{crashs}}, {
24
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    48
            file        => $filename,
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    49
            uuid        => $crash->uuid,
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    50
            product     => $crash->product->name,
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    51
            version     => $crash->product->version,
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    52
            user        => $crash->crash_user->user_id,
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    53
            date        => $crash->crash_time,
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    54
        }
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    55
    }
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    56
29
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents: 24
diff changeset
    57
    return $results;
24
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    58
}
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    59
31
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents: 29
diff changeset
    60
sub _db_insert_processed_data {
24
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    61
    my ($self, $uuid, $pjson) = @_;
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    62
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    63
    $self->{schema}->txn_do(sub {
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    64
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    65
        my $crash = $self->{schema}->resultset('CrashReport')->new({ uuid => $uuid });
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    66
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    67
        if($pjson->{client_info}->{StartupTime}) {
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    68
            $crash->start_time(DateTime->from_epoch(epoch => $pjson->{client_info}->{StartupTime}));
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    69
        }
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    70
        if($pjson->{client_info}->{CrashTime}) {
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    71
            $crash->crash_time(DateTime->from_epoch(epoch => $pjson->{client_info}->{CrashTime}));
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    72
        }
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    73
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    74
        my $product = {
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    75
            distributor => $pjson->{client_info}->{Distributor},
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    76
            name        => $pjson->{client_info}->{ProductName},
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    77
            version     => $pjson->{client_info}->{Version},
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    78
        };
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    79
31
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents: 29
diff changeset
    80
        my $dbproduct = $self->{schema}->resultset('Product')->search($product)->first();
24
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    81
        if($dbproduct) {
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    82
            $crash->product($dbproduct);
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    83
        } else {
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    84
            $product->{release_channel} = $pjson->{client_info}->{ReleaseChannel};
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    85
            $crash->product($self->{schema}->resultset('Product')->new($product));
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    86
        }
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    87
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    88
        my $user = {
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    89
            user_id => $pjson->{client_info}->{UserID},
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    90
        };
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    91
31
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents: 29
diff changeset
    92
        my $dbuser = $self->{schema}->resultset('CrashUser')->search($user)->first();
24
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    93
        if($dbuser) {
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    94
            $crash->crash_user($dbuser);
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    95
        } else {
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    96
            $user->{cpu_arch}   = $pjson->{system_info}->{cpu_arch};
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    97
            $user->{cpu_count}  = $pjson->{system_info}->{cpu_count};
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    98
            $user->{os}         = $pjson->{system_info}->{os};
54
2218a127abd9 Fix deprecated warnings for Mojo::JSON OO syntax with Mojolicious >= 5.54
Vincent Tondellier <tonton+hg@team1664.org>
parents: 42
diff changeset
    99
            $user->{extra_info} = j($pjson->{client_info});
24
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   100
            $crash->crash_user($self->{schema}->resultset('CrashUser')->new($user));
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   101
        }
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   102
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   103
        # TODO crash_threads => [ ],
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   104
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   105
        $crash->insert;
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   106
    });
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   107
}
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   108
31
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents: 29
diff changeset
   109
sub store_processed_data {
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents: 29
diff changeset
   110
    my ($self, $uuid, $pjson) = @_;
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents: 29
diff changeset
   111
34
e0d6597078a5 Cleanup: Mojo-ize old code, simplifying perl OO code
Vincent Tondellier <tonton+hg@team1664.org>
parents: 33
diff changeset
   112
    $self->SUPER::store_processed_data($uuid, $pjson);
31
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents: 29
diff changeset
   113
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents: 29
diff changeset
   114
    $self->_db_insert_processed_data($uuid, $pjson);
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents: 29
diff changeset
   115
}
f77c6719c05c Add migration script
Vincent Tondellier <tonton+hg@team1664.org>
parents: 29
diff changeset
   116
24
b69b7aa98a1d Add basic SQL Storage
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   117
1;