| 1 | package Mojo::Debugbar::Monitor::DBIx; | ||
| 2 | 1 | ▶ | use Mojo::Base "Mojo::Debugbar::Monitor"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | 0 | ▶ | use Mojo::Debugbar::Storage::DBIx; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | |||
| 6 | has 'css' => <<'EOF' | ||
| 7 | <style type="text/css"> | ||
| 8 | .debugbar-queries .source { | ||
| 9 | display: none; | ||
| 10 | font-style: italic; | ||
| 11 | } | ||
| 12 | </style> | ||
| 13 | EOF | ||
| 14 | ; | ||
| 15 | has 'icon' => '<i class="icon-database"></i>'; | ||
| 16 | has 'javascript' => <<'EOF' | ||
| 17 | <script> | ||
| 18 | $('.debugbar-queries tbody tr').on('click', function() { | ||
| 19 | $(this).find('.source').toggle(); | ||
| 20 | }); | ||
| 21 | </script> | ||
| 22 | EOF | ||
| 23 | ; | ||
| 24 | |||
| 25 | has 'name' => 'Queries'; | ||
| 26 | has 'seen' => sub {{}}; | ||
| 27 | |||
| 28 | =head2 render | ||
| 29 | |||
| 30 | Returns the html | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | sub render { | ||
| 35 | my $self = shift; | ||
| 36 | |||
| 37 | my $total_duration = 0; | ||
| 38 | |||
| 39 | foreach my $query (@{ $self->items }) { | ||
| 40 | $total_duration += $query->{ duration }; | ||
| 41 | } | ||
| 42 | |||
| 43 | return sprintf ('%s | ||
| 44 | <table class="debugbar-queries table" data-debugbar-ref="%s"> | ||
| 45 | <thead> | ||
| 46 | <tr> | ||
| 47 | <th>Query</th> | ||
| 48 | <th width="70px">Duration</th> | ||
| 49 | </tr> | ||
| 50 | </thead> | ||
| 51 | <tbody> | ||
| 52 | %s | ||
| 53 | </tbody> | ||
| 54 | <tfoot> | ||
| 55 | <tr> | ||
| 56 | <th class="text align right">Total</th> | ||
| 57 | <th>%ss</th> | ||
| 58 | </tr> | ||
| 59 | </tfoot> | ||
| 60 | </table> | ||
| 61 | %s', | ||
| 62 | $self->css, ref($self), $self->rows, $total_duration, $self->javascript | ||
| 63 | ); | ||
| 64 | } | ||
| 65 | |||
| 66 | =head2 rows | ||
| 67 | |||
| 68 | Build the rows | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | sub rows { | ||
| 73 | my $self = shift; | ||
| 74 | |||
| 75 | my $time = time; | ||
| 76 | my ($sec, $min, $hour) = localtime($time); | ||
| 77 | |||
| 78 | my $rows = sprintf('<tr><td colspan="2">Queries at %s:%s:%s (%s)</td></tr>', $hour, $min, $sec, scalar @{ $self->items }); | ||
| 79 | |||
| 80 | foreach my $query (@{ $self->items }) { | ||
| 81 | my $seen = $self->seen->{ $query->{ sql } }; | ||
| 82 | my $seen_icon = $seen > 1 ? sprintf(' <span title="Seen %s times"><i class="icon-attention"></i></span>', $seen) : ''; | ||
| 83 | |||
| 84 | my $source = ''; | ||
| 85 | |||
| 86 | foreach my $frame (@{ $query->{ frames } }) { | ||
| 87 | my $index = index($frame->filename, '../'); | ||
| 88 | $source .= substr($frame->filename, $index >= 0 ? $index + 3 : 0) . ':' . $frame->line .'<br />'; | ||
| 89 | } | ||
| 90 | |||
| 91 | $rows .= sprintf( | ||
| 92 | '<tr> | ||
| 93 | <td>%s%s%s</td> | ||
| 94 | <td>%ss</td> | ||
| 95 | </tr>', | ||
| 96 | $query->{ sql }, $seen_icon, ($source ? '<br /><div class="source">' . $source . '</div>' : ''), $query->{ duration } | ||
| 97 | ); | ||
| 98 | } | ||
| 99 | |||
| 100 | return $rows; | ||
| 101 | } | ||
| 102 | |||
| 103 | =head2 stop | ||
| 104 | |||
| 105 | Stop debugging and clear "seen" items | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | sub stop { | ||
| 110 | my $self = shift; | ||
| 111 | |||
| 112 | $self->SUPER::stop(); | ||
| 113 | |||
| 114 | # turn off debugging | ||
| 115 | $self->app->schema->storage->debug(0); | ||
| 116 | |||
| 117 | $self->seen({}); | ||
| 118 | } | ||
| 119 | |||
| 120 | =head2 start | ||
| 121 | |||
| 122 | Change the debugobj for DBIX storage and record queries | ||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | sub start { | ||
| 127 | my $self = shift; | ||
| 128 | |||
| 129 | # turn on debugging | ||
| 130 | $self->app->schema->storage->debug(1); | ||
| 131 | |||
| 132 | my $debugobj = Mojo::Debugbar::Storage::DBIx->new(); | ||
| 133 | $debugobj->app_name(ref($self->app)); | ||
| 134 | |||
| 135 | $debugobj->recorder(sub { | ||
| 136 | my $query = shift; | ||
| 137 | |||
| 138 | my $items = $self->items; | ||
| 139 | |||
| 140 | push(@$items, $query); | ||
| 141 | |||
| 142 | $self->items($items); | ||
| 143 | $self->seen->{ $query->{ sql } } += 1; | ||
| 144 | }); | ||
| 145 | |||
| 146 | $self->app->schema->storage->debugobj($debugobj); | ||
| 147 | } | ||
| 148 | |||
| 149 | 1; | ||