| 1 | package Plack::Middleware::Debug::DBIC::QueryLog; | ||||||||||||
| 2 | |||||||||||||
| 3 | 3 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 3 | ▶ | use Text::MicroTemplate; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 3 | ▶ | use Plack::Middleware::DBIC::QueryLog; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 3 | ▶ | use 5.008008; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 3 | extends 'Plack::Middleware::Debug::Base'; | |||||||||||
| 9 | |||||||||||||
| 10 | 3 | our $VERSION = '0.09'; | |||||||||||
| 11 | |||||||||||||
| 12 | has 'sqla_tree_class' => ( | ||||||||||||
| 13 | is => 'ro', | ||||||||||||
| 14 | 2 | ▶ | default => sub {'SQL::Abstract::Tree'}, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 3 | ); | |||||||||||
| 16 | |||||||||||||
| 17 | has 'sqla_tree_args' => ( | ||||||||||||
| 18 | is => 'ro', | ||||||||||||
| 19 | 2 | ▶ | default => sub { +{profile => 'html'} }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 3 | ); | |||||||||||
| 21 | |||||||||||||
| 22 | 3 | has 'sqla_tree' => ( | |||||||||||
| 23 | is => 'ro', | ||||||||||||
| 24 | lazy => 1, | ||||||||||||
| 25 | builder => '_build_sqla_tree', | ||||||||||||
| 26 | ); | ||||||||||||
| 27 | |||||||||||||
| 28 | 0 | ▶ | sub _build_sqla_tree { | ||||||||||
|
Subroutine
_build_sqla_tree: not called CC=1 SCAR=6.9
| |||||||||||||
| 29 | 0 | Plack::Util::load_class($_[0]->sqla_tree_class) | |||||||||||
| 30 | ->new($_[0]->sqla_tree_args); | ||||||||||||
| 31 | } | ||||||||||||
| 32 | |||||||||||||
| 33 | 3 | has 'querylog_class' => ( is => 'ro' ); | |||||||||||
| 34 | 3 | has 'querylog_args' => ( is => 'ro' ); | |||||||||||
| 35 | |||||||||||||
| 36 | 3 | has template => ( | |||||||||||
| 37 | is => 'ro', | ||||||||||||
| 38 | builder => '_build_template', | ||||||||||||
| 39 | ); | ||||||||||||
| 40 | |||||||||||||
| 41 | 2 | ▶ | sub _build_template { | ||||||||||
|
Subroutine
_build_template: called CC=1 SCAR=0.0
| |||||||||||||
| 42 | 2 | __PACKAGE__->build_template(join '', <DATA>); | |||||||||||
| 43 | } | ||||||||||||
| 44 | |||||||||||||
| 45 | has 'querylog_analyzer_class' => ( | ||||||||||||
| 46 | is => 'ro', | ||||||||||||
| 47 | 2 | ▶ | default => sub { 'DBIx::Class::QueryLog::Analyzer' }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 48 | 3 | ); | |||||||||||
| 49 | |||||||||||||
| 50 | 2 | ▶ | sub querylog_analyzer_for { | ||||||||||
|
Subroutine
querylog_analyzer_for: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 51 | 2 | my ($self, $ql) = @_; | |||||||||||
| 52 | 2 | Plack::Util::load_class($_[0]->querylog_analyzer_class) | |||||||||||
| 53 | ->new({querylog => $ql}); | ||||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | 2 | ▶ | sub find_or_create_querylog { | ||||||||||
|
Subroutine
find_or_create_querylog: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||
| 57 | 2 | my ($self, $env) = @_; | |||||||||||
| 58 | 2 | ▶ | Plack::Middleware::DBIC::QueryLog->get_querylog_from_env($env) || do { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 59 | 0 | my %args = map { $_ => $self->$_ } grep { $self->$_ } | |||||||||||
| 60 | qw(querylog_class querylog_args); | ||||||||||||
| 61 | |||||||||||||
| 62 | 2 | Plack::Middleware::DBIC::QueryLog->new(%args) | |||||||||||
| 63 | ->find_or_create_querylog_in($env); | ||||||||||||
| 64 | }; | ||||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | 2 | ▶ | sub run { | ||||||||||
|
Subroutine
run: called CC=1 SCAR=0.4
Pod
documented
| |||||||||||||
| 68 | 2 | my ($self, $env, $panel) = @_; | |||||||||||
| 69 | 2 | my $querylog = $self->find_or_create_querylog($env); | |||||||||||
| 70 | 2 | $panel->title('DBIC::QueryLog'); | |||||||||||
| 71 | |||||||||||||
| 72 | 2 | ▶ | return sub { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=13.1
| |||||||||||||
| 73 | 2 | my $analyzer = $self->querylog_analyzer_for($querylog); | |||||||||||
| 74 | 2 | ▶ | my @sorted_queries = @{$analyzer->get_sorted_queries||[]}; | ||||||||||
|
Condition50%1/2
$analyzer->get_sorted_queries || []
MC/DC0%0/1
$analyzer->get_sorted_queries || []
$analyzer->get_sorted_queries
Truth table50%1/2
A
A= $analyzer->get_sorted_queries
| |||||||||||||
| 75 | |||||||||||||
| 76 | 2 | ▶ | if(@sorted_queries) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | my %args = ( | ||||||||||||
| 78 | count => $querylog->count, | ||||||||||||
| 79 | elapsed_time => $querylog->time_elapsed, | ||||||||||||
| 80 | sorted_queries => [@sorted_queries], | ||||||||||||
| 81 | 0 | ▶ | sql_formatter => sub { $self->sqla_tree->format(@_) }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 82 | 0 | ); | |||||||||||
| 83 | 0 | $panel->nav_subtitle(sprintf('Total Time: %.6f', $args{elapsed_time})); | |||||||||||
| 84 | 0 | $panel->content($self->template->(%args)); | |||||||||||
| 85 | } else { | ||||||||||||
| 86 | 2 | $panel->nav_subtitle("No SQL"); | |||||||||||
| 87 | 2 | $panel->content("No DBIC log information"); | |||||||||||
| 88 | } | ||||||||||||
| 89 | 2 | }; | |||||||||||
| 90 | } | ||||||||||||
| 91 | |||||||||||||
| 92 | =head1 NAME | ||||||||||||
| 93 | |||||||||||||
| 94 | Plack::Middleware::Debug::DBIC::QueryLog - DBIC Query Log and Query Analyzer | ||||||||||||
| 95 | |||||||||||||
| 96 | =head1 SYNOPSIS | ||||||||||||
| 97 | |||||||||||||
| 98 | Adds a debug panel and querylog object for logging L<DBIx::Class> queries. Has | ||||||||||||
| 99 | support for L<Catalyst> via a L<Catalyst::TraitFor::Model::DBIC::Schema::QueryLog> | ||||||||||||
| 100 | compatible trait, L<Catalyst::TraitFor::Model::DBIC::Schema::QueryLog::AdoptPlack>. | ||||||||||||
| 101 | |||||||||||||
| 102 | use Plack::Builder; | ||||||||||||
| 103 | |||||||||||||
| 104 | my $app = ...; ## Build your Plack App | ||||||||||||
| 105 | |||||||||||||
| 106 | builder { | ||||||||||||
| 107 | enable 'Debug', panels =>['DBIC::QueryLog']; | ||||||||||||
| 108 | $app; | ||||||||||||
| 109 | }; | ||||||||||||
| 110 | |||||||||||||
| 111 | And in you L<Catalyst> application, if you are also using | ||||||||||||
| 112 | L<Catalyst::TraitFor::Model::DBIC::Schema::QueryLog::AdoptPlack> | ||||||||||||
| 113 | |||||||||||||
| 114 | package MyApp::Web::Model::Schema; | ||||||||||||
| 115 | use Moose; | ||||||||||||
| 116 | extends 'Catalyst::Model::DBIC::Schema'; | ||||||||||||
| 117 | |||||||||||||
| 118 | __PACKAGE__->config({ | ||||||||||||
| 119 | schema_class => 'MyApp::Schema', | ||||||||||||
| 120 | traits => ['QueryLog::AdoptPlack'], | ||||||||||||
| 121 | ## .. rest of configuration | ||||||||||||
| 122 | }); | ||||||||||||
| 123 | |||||||||||||
| 124 | =head1 DESCRIPTION | ||||||||||||
| 125 | |||||||||||||
| 126 | L<DBIx::Class::QueryLog> is a tool in the L<DBIx::Class> software ecosystem | ||||||||||||
| 127 | which benchmarks queries. It lets you log the SQL that L<DBIx::Class> | ||||||||||||
| 128 | is generating, along with bind variables and timestamps. You can then pass | ||||||||||||
| 129 | the querylog object to an analyzer (such as L<DBIx::Class::QueryLog::Analyzer>) | ||||||||||||
| 130 | to generate sorted statistics for all the queries between certain log points. | ||||||||||||
| 131 | |||||||||||||
| 132 | Query logging in L<Catalyst> is supported for L<DBIx::Class> via a trait for | ||||||||||||
| 133 | L<Catalyst::Model::DBIC::Schema> called | ||||||||||||
| 134 | L<Catalyst::TraitFor::Model::DBIC::Schema::QueryLog>. This trait will | ||||||||||||
| 135 | log all the SQL used by L<DBIx::Class> for a given request cycle. This is very | ||||||||||||
| 136 | useful since it can help you identify troublesome or bottlenecking queries. | ||||||||||||
| 137 | |||||||||||||
| 138 | However, L<Catalyst::TraitFor::Model::DBIC::Schema::QueryLog> does not provide | ||||||||||||
| 139 | out of the box outputting of your analyzed query logs. Usually you need to | ||||||||||||
| 140 | add a bit of templating work to the bottom of your webpage footer, or dump the | ||||||||||||
| 141 | output to the logs. We'd like to provide a lower ceremony experience. | ||||||||||||
| 142 | |||||||||||||
| 143 | Additionally, it would be nice if we could provide this functionality for all | ||||||||||||
| 144 | L<Plack> based applications, not just L<Catalyst>. Ideally we'd play nice with | ||||||||||||
| 145 | L<Plack::Middleware::Debug> so that the table of our querylog would appear as | ||||||||||||
| 146 | a neat Plack based Debug panel. This bit of middleware provides that function. | ||||||||||||
| 147 | |||||||||||||
| 148 | Basically we create a new instance of L<DBIx::Class::QueryLog> and place it | ||||||||||||
| 149 | into C<< $env->{'plack.middleware.dbic.querylog'} >> (We use the underlying | ||||||||||||
| 150 | features in L<Plack::Middleware::DBIC::QueryLog>) so that it is accessible by | ||||||||||||
| 151 | all applications running inside of L<Plack>. You need to 'tell' your application's | ||||||||||||
| 152 | instance of L<DBIx::Class> to use this C<$env> key and make sure you set | ||||||||||||
| 153 | L<DBIx::Class>'s debug object correctly. The officially supported interface for | ||||||||||||
| 154 | this in via the supporting class L<Plack::Middleware::DBIC::QueryLog>: | ||||||||||||
| 155 | |||||||||||||
| 156 | use Plack::Middleware::DBIC::QueryLog; | ||||||||||||
| 157 | |||||||||||||
| 158 | my $querylog = Plack::Middleware::DBIC::QueryLog->get_querylog_from_env($env); | ||||||||||||
| 159 | my $cloned_schema = $schema->clone; | ||||||||||||
| 160 | $cloned_schema->storage->debug(1); | ||||||||||||
| 161 | $cloned_schema->storage->debugobj($querylog); | ||||||||||||
| 162 | |||||||||||||
| 163 | In this example C<$env> is a L<Plack> environment, typically passed into your PSGI | ||||||||||||
| 164 | compliant application and C<$schema> is an instance of L<DBIx::Class::Schema> | ||||||||||||
| 165 | |||||||||||||
| 166 | We clone C<$schema> to avoid associating the querylog with the global, persistant | ||||||||||||
| 167 | DBIC schema object. | ||||||||||||
| 168 | |||||||||||||
| 169 | Then you need to enable the Debug panel, as in the L<\SYNOPSIS>. That way when | ||||||||||||
| 170 | you view the debug panel, we have SQL to review. | ||||||||||||
| 171 | |||||||||||||
| 172 | There's an application in '/example' you can review for help. However, if you | ||||||||||||
| 173 | are using L<Catalyst> and a modern L<Catalyst::Model::DBIC::Schema> you can use | ||||||||||||
| 174 | the trait L<Catalyst::TraitFor::Model::DBIC::Schema::QueryLog::AdoptPlack>, | ||||||||||||
| 175 | which is compatible with L<Catalyst::TraitFor::Model::DBIC::Schema::QueryLog>. | ||||||||||||
| 176 | |||||||||||||
| 177 | If you want a querylog but don't want or need the L<Plack> debug panel, you | ||||||||||||
| 178 | should take a look at L<Plack::Middleware::DBIC::QueryLog>. | ||||||||||||
| 179 | |||||||||||||
| 180 | See the L</SYNOPSIS> example for more details. | ||||||||||||
| 181 | |||||||||||||
| 182 | =head1 OPTIONS | ||||||||||||
| 183 | |||||||||||||
| 184 | This debug panel defines the following options. | ||||||||||||
| 185 | |||||||||||||
| 186 | =head2 querylog_class | ||||||||||||
| 187 | |||||||||||||
| 188 | This is the class which is used to build the C<querylog> unless one is already | ||||||||||||
| 189 | defined. It defaults to L<DBIx::Class::QueryLog>. You should probably leave | ||||||||||||
| 190 | this alone unless you need to subclass or augment L<DBIx::Class::QueryLog>. | ||||||||||||
| 191 | |||||||||||||
| 192 | If the class name you pass has not already been included (via C<use> or | ||||||||||||
| 193 | C<require>) we will automatically try to C<require> it. | ||||||||||||
| 194 | |||||||||||||
| 195 | =head2 querylog_args | ||||||||||||
| 196 | |||||||||||||
| 197 | Takes a HashRef which is passed to L<DBIx::Class::QueryLog> at construction. | ||||||||||||
| 198 | |||||||||||||
| 199 | =head1 SEE ALSO | ||||||||||||
| 200 | |||||||||||||
| 201 | L<Plack::Middleware::Debug>, L<Catalyst::TraitFor::Model::DBIC::Schema::QueryLog>, | ||||||||||||
| 202 | L<Catalyst::Model::DBIC::Schema>, L<Catalyst::TraitFor::Model::DBIC::Schema::QueryLog::AdoptPlack> | ||||||||||||
| 203 | |||||||||||||
| 204 | =head1 AUTHOR | ||||||||||||
| 205 | |||||||||||||
| 206 | John Napiorkowski, C<< <jjnapiork@cpan.org> >> | ||||||||||||
| 207 | |||||||||||||
| 208 | =head1 COPYRIGHT & LICENSE | ||||||||||||
| 209 | |||||||||||||
| 210 | Copyright 2011 John Napiorkowski | ||||||||||||
| 211 | |||||||||||||
| 212 | This program is free software; you can redistribute it and/or modify | ||||||||||||
| 213 | it under the same terms as Perl itself. | ||||||||||||
| 214 | |||||||||||||
| 215 | =cut | ||||||||||||
| 216 | |||||||||||||
| 217 | 3 | 1; | |||||||||||
| 218 | |||||||||||||
| 219 | __DATA__ | ||||||||||||