blib/lib/Plack/Debugger/Panel/DBIC/QueryLog.pm

filter: Statementstmt 30.7% 20 / 65 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 46.1% 6 / 13 Podpod 20.0% 1 / 5 totaltotal 23.2% 27 / 116 CC 13 SCAR 32.7
CC
13
Cov
22%
new
72.0
__ANON__
12.0
find_or_create_querylog
6.0
CRAP 26.3
↑ Summary
1 package Plack::Debugger::Panel::DBIC::QueryLog;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use parent 'Plack::Debugger::Panel';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Plack::Middleware::DBIC::QueryLog;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Text::MicroTemplate;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Plack::Debugger::Panel::DBIC::QueryLog - DBIC query log panel for Plack::Debugger
14  
15 =head1 VERSION
16  
17 Version 0.002
18  
19 =cut
20  
21 1 our $VERSION = '0.002';
22  
23 1 my $_template = join( '', <DATA> );
24  
25 0 ▶sub new {
Subroutine new: not called CC=8 SCAR=42.8
Pod documented
26 0 my $class = shift;
27 0 ▶ my %args = @_ == 1 && ref $_[0] eq 'HASH' ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 0 0 0
Condition0%0/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
28  
29 0 ▶ $args{title} ||= 'DBIC::QueryLog';
Condition0%0/2
$args{"title"} ||= "DBIC::QueryLog"
l!l
00
MC/DC0%0/1
$args{"title"} ||= "DBIC::QueryLog"
$args{"title"}
Truth table0%0/2
A
A= $args{"title"}
Aresult
11
00
30 0 ▶ $args{formatter} ||= 'pass_through';
Condition0%0/2
$args{"formatter"} ||= "pass_through"
l!l
00
MC/DC0%0/1
$args{"formatter"} ||= "pass_through"
$args{"formatter"}
Truth table0%0/2
A
A= $args{"formatter"}
Aresult
11
00
31  
32 0 ▶ $args{querylog_analyzer_class} ||= 'DBIx::Class::QueryLog::Analyzer';
Condition0%0/2
$args{"querylog_analyzer_class"} ||= "DBIx::Class::QueryLog::Analyzer"
l!l
00
MC/DC0%0/1
$args{"querylog_analyzer_class"} ||= "DBIx::Class::QueryLog::Analyzer"
$args{"querylog_analyzer_class"}
Truth table0%0/2
A
A= $args{"querylog_analyzer_class"}
Aresult
11
00
33 0 ▶ $args{sqla_tree_args} ||= +{ profile => 'html' };
Condition0%0/2
$args{"sqla_tree_args"} ||= {"profile", "html"}
l!l
00
MC/DC0%0/1
$args{"sqla_tree_args"} ||= {"profile", "html"}
$args{"sqla_tree_args"}
Truth table0%0/2
A
A= $args{"sqla_tree_args"}
Aresult
11
00
34 0 ▶ $args{sqla_tree_class} ||= 'SQL::Abstract::Tree';
Condition0%0/2
$args{"sqla_tree_class"} ||= "SQL::Abstract::Tree"
l!l
00
MC/DC0%0/1
$args{"sqla_tree_class"} ||= "SQL::Abstract::Tree"
$args{"sqla_tree_class"}
Truth table0%0/2
A
A= $args{"sqla_tree_class"}
Aresult
11
00
35  
36 0 ▶ $args{'after'} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
37 0 my ( $self, $env, $resp ) = @_;
38  
39 0 my $querylog = $self->find_or_create_querylog($env);
40 0 my $analyzer = $self->querylog_analyzer_for($querylog);
41 0 ▶ my @sorted_queries = @{ $analyzer->get_sorted_queries || [] };
Condition0%0/2
$analyzer->get_sorted_queries || []
l!l
00
MC/DC0%0/1
$analyzer->get_sorted_queries || []
$analyzer->get_sorted_queries
Truth table0%0/2
A
A= $analyzer->get_sorted_queries
Aresult
11
00
42  
43 0 ▶ if (@sorted_queries) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sorted_queries) { } 0 0 0
44 my %args = (
45 count => $querylog->count,
46 elapsed_time => $querylog->time_elapsed,
47 sorted_queries => [@sorted_queries],
48 0 ▶ sql_formatter => sub { $self->sqla_tree->format(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 );
50  
51 $self->set_subtitle(
52 0 sprintf( 'Total Time: %.6f', $args{elapsed_time} ) );
53  
54 0 my $result = $self->template->(%args);
55 0 $self->set_result("$result");
56 }
57 else {
58 0 $self->set_subtitle("No SQL");
59 0 $self->set_result("No DBIC log information");
60 }
61 0 };
62  
63 0 my $self = $class->SUPER::new( \%args );
64  
65 0 $self->{querylog_analyzer_class} = $args{querylog_analyzer_class};
66 0 $self->{querylog_args} = $args{querylog_args};
67 0 $self->{querylog_class} = $args{querylog_class};
68 0 $self->{sqla_tree_args} = $args{sqla_tree_args};
69 0 $self->{sqla_tree_class} = $args{sqla_tree_class};
70  
71 0 return $self;
72 }
73  
74 0 ▶sub find_or_create_querylog {
Subroutine find_or_create_querylog: not called CC=2 SCAR=17.9
Pod undocumented
75 0 my ( $self, $env ) = @_;
76 0 ▶ Plack::Middleware::DBIC::QueryLog->get_querylog_from_env($env) || do {
Branch: 1 branch
BranchTrueFalseTotal
"Plack::Middleware::DBIC::QueryLog"->get_querylog_from_env($env) or do { my(%args) = map({$_, $self->{$_};} grep({$self->{$_};} "querylog_class", "querylog_args")); "Plack::Middleware::DBIC::QueryLog"->new(%args)->find_or_create_querylog_in($env) } 0 0 0
77 0 my %args = map { $_ => $self->{$_} } grep { $self->{$_} }
78 qw(querylog_class querylog_args);
79  
80 0 Plack::Middleware::DBIC::QueryLog->new(%args)
81 ->find_or_create_querylog_in($env);
82 };
83 }
84  
85 0 ▶sub querylog_analyzer_for {
Subroutine querylog_analyzer_for: not called CC=1 SCAR=6.9
Pod undocumented
86 0 my ( $self, $ql ) = @_;
87 Plack::Util::load_class( $self->{querylog_analyzer_class} )
88 0 ->new( { querylog => $ql } );
89 }
90  
91 0 ▶sub sqla_tree {
Subroutine sqla_tree: not called CC=2 SCAR=17.9
Pod undocumented
92 0 my $self = shift;
93 0 ▶ if ( !defined $self->{sqla_tree} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"sqla_tree"}) 0 0 0
94 $self->{sqla_tree} = Plack::Util::load_class( $self->{sqla_tree_class} )
95 0 ->new( $self->{sqla_tree_args} );
96 }
97 0 return $self->{sqla_tree};
98 }
99  
100 0 ▶sub template {
Subroutine template: not called CC=2 SCAR=17.9
Pod undocumented
101 0 my $self = shift;
102 0 ▶ if ( !defined $self->{_template} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_template"}) 0 0 0
103 0 $self->{_template} = Text::MicroTemplate->new(
104 template => $_template,
105 tag_start => '<%',
106 tag_end => '%>',
107 line_start => '%',
108 )->build_mt;
109 }
110  
111 #close DATA;
112 0 return $self->{_template};
113 }
114  
115 =head1 SYNOPSIS
116  
117 Adds a debug panel and querylog object for logging DBIx::Class queries.
118  
119 Has support for L<Catalyst> via a
120 L<Catalyst::TraitFor::Model::DBIC::Schema::QueryLog> compatible trait,
121 L<Catalyst::TraitFor::Model::DBIC::Schema::QueryLog::AdoptPlack>.
122  
123 use Plack::Builder;
124
125 use JSON;
126
127 use Plack::Debugger;
128 use Plack::Debugger::Storage;
129
130 use Plack::App::Debugger;
131
132 use Plack::Debugger::Panel::DBIC::QueryLog;
133 use ... # other Panels
134  
135 use DBICx::Sugar qw/schema/;
136 use MyApp; # your PSGI app (Dancer2 perhaps)
137  
138 # create middleware wrapper
139 my $mw = sub {
140 my $app = shift;
141 sub {
142 my $env = shift;
143 my $querylog =
144 Plack::Middleware::DBIC::QueryLog->get_querylog_from_env($env);
145 my $cloned_schema = schema->clone;
146 $cloned_schema->storage->debug(1);
147 $cloned_schema->storage->debugobj($querylog);
148 my $res = $app->($env);
149 return $res;
150 };
151 };
152  
153 # wrap your app
154 my $app = $mw->( MyApp->to_app );
155
156 my $debugger = Plack::Debugger->new(
157 storage => Plack::Debugger::Storage->new(
158 data_dir => '/tmp/debugger_panel',
159 serializer => sub { encode_json( shift ) },
160 deserializer => sub { decode_json( shift ) },
161 filename_fmt => "%s.json",
162 ),
163 panels => [
164 Plack::Debugger::Panel::DBIC::QueryLog->new,
165 # ... other Panels
166 ]
167 );
168
169 my $debugger_app = Plack::App::Debugger->new( debugger => $debugger );
170
171 builder {
172 mount $debugger_app->base_url => $debugger_app->to_app;
173
174 mount '/' => builder {
175 enable $debugger_app->make_injector_middleware;
176 enable $debugger->make_collector_middleware;
177 $app;
178 }
179 };
180  
181 =head1 DESCRIPTION
182  
183 This module provides a DBIC QueryLog panel for L<Plack::Debugger> with
184 query alaysis performed by L<DBIx::Class::QueryLog::Analyzer> (by default).
185  
186 For full details of how to setup L<Catalyst> to use this panel and also for
187 a full background of the design of this module see
188 L<https://metacpan.org/pod/Plack::Middleware::Debug::DBIC::QueryLog>
189 which this module steals heavily from.
190  
191 =head1 BUGS
192  
193 Nowhere near enough docs and no tests so expect something to break somewhere.
194  
195 This is currently 'works for me' quality.
196  
197 Please report bugs via:
198  
199 L<https://github.com/SysPete/Plack-Debugger-Panel-DBIC-QueryLog/issues>
200  
201 =head1 SEE ALSO
202  
203 L<Plack::Debugger>, L<Plack::Middleware::Debug::DBIC::QueryLog>,
204 L<Dancer2::Plugin::Debugger::Panel::DBIC::QueryLog>.
205  
206 =head1 ACKNOWLEDGEMENTS
207  
208 John Napiorkowski, C<< <jjnapiork@cpan.org> >> for L<Plack::Middleware::Debug::DBIC::QueryLog> from which most of this module was stolen.
209  
210 =head1 AUTHOR
211  
212 Peter Mottram (SysPete), C<< <peter at sysnix.com> >>
213  
214 =head1 LICENSE AND COPYRIGHT
215  
216 Copyright 2016 Peter Mottram (SysPete).
217  
218 This program is free software; you can redistribute it and/or modify it
219 under the terms of either: the GNU General Public License as published
220 by the Free Software Foundation; or the Artistic License.
221  
222 See http://dev.perl.org/licenses/ for more information.
223  
224 =cut
225  
226 1 1;
227  
228 __DATA__
↑ Summary