package Catalyst::Plugin::DBIC::ProfilerOutput; use strict; use warnings; use NEXT; use base qw( Class::Accessor::Fast ); __PACKAGE__->mk_accessors(qw/ __catalyst_dbic_querylog /); use Template; our $TEMPLATE; sub prepare { my $class = shift; my $self = $class->NEXT::prepare(@_); while (1) { last unless $self->debug; $self->__catalyst_dbic_querylog( {} ); for my $component (keys %{ $self->components }) { next unless $component->isa('Catalyst::Model::DBIC::Schema'); my $schema = $self->component($component)->schema; DBIx::Class::QueryLog->use or die $@; $schema->storage->debug(1); my $ql = DBIx::Class::QueryLog->new; $schema->storage->debugobj($ql); $self->__catalyst_dbic_querylog->{$component} = $ql; } last; } $self; } sub finalize { my $self = shift; while (1) { last unless $self->debug; last unless keys %{ $self->__catalyst_dbic_querylog }; last if $self->res->content_type && $self->res->content_type !~ /html/; my $tmpl = Template->new; $tmpl->process(\$TEMPLATE, { querylog => $self->__catalyst_dbic_querylog }, \my $output); my $body = $self->res->body; $body .= $output unless $body =~ s!(.*)$!$output$1!im; $self->res->body($body); last; } return $self->NEXT::finalize(@_); } $TEMPLATE = q{

Query Log Report

[% FOREACH row IN querylog %] [% SET log = row.value %] [% SET total = log.time_elapsed %]

Component Name: [% row.key %]

Total SQL Time: [% total | format('%0.6f') %]

Total Queries : [% log.count %]

[% FOREACH q = log.get_sorted_queries %] [% END %]
Time % SQL
[% q.time_elapsed | format('%0.6f') %] [% (q.time_elapsed / total) * 100 | format('%i') %] [% q.sql | html %]
[% END %]
}; 1;