[Catalyst] Re: Catalyst::Plugin::DBIC::QueryLog

Fayland Lam fayland at gmail.com
Tue Sep 4 12:02:08 GMT 2007


see attachment. check if it's OK?

now it's not per request any more. hmm, after several requests,

Total SQL Time: 1.033088 seconds
Total Queries: 115

it will increasing. hmm, how to make it as per request?

Thanks, mst. (I'll send u a htpasswd line after all is OK.)


Matt S Trout wrote:
> On Tue, Sep 04, 2007 at 01:19:31AM +0000, Fayland Lam wrote:
>> Matt S Trout wrote:
>>> On Sat, Sep 01, 2007 at 04:08:09AM +0000, Fayland Lam wrote:
>>>> hi, guys.
>>>>
>>>> since I can't touch Cory Watson by gphat at cpan.org <mailto:gphat at cpan.o=
rg>
>>>> so I wonder if this module is fine?
>>> No, it's awful. There's no reason at all for this to be a plugin.
>>>
>>> I'd suggest you make it a mixin for Catalyst::Model::DBIC::Schema inste=
ad.
>>
>> but it depends on every request. it start at the sub prepare and end =

>> before finalize_body. due to my limited knowledge, I'm not sure if it's =

>> OK as a mixin for Catalyst::Model::DBIC::Schema
> =

> I'm sure it is.
> =

> You just have a per-request query log.
> =

> That's trivial with Catalyst::Component::InstancePerContext.
>  =

>>> (otherwise how do you deal with two DBIC models each with their own
>>> querylog etc. etc.)
>> that's right. but there is more than one way to do it. :)
> =

> Yes, but yours is wrong.
> =

> We've documented this clearly in ExtendingCatalyst - if it doesn't need
> to affect the request cycle, it shouldn't be a plugin.
> =

> Please send me an htpasswd line so I can set up Model::DBIC::Schema::Quer=
yLog
> dirs for you (or consider just submitting a patch to M::DBIC::Schema itse=
lf,
> which I'd be happy to help with)
> =



-- =

Fayland Lam // http://www.fayland.org/
-------------- next part --------------
package Catalyst::Model::DBIC::Schema::QueryLog;

use warnings;
use strict;
use vars qw/$VERSION/;
$VERSION =3D '0.01';

use base 'Catalyst::Model::DBIC::Schema';
__PACKAGE__->mk_accessors('querylog');

use DBIx::Class::QueryLog;
use DBIx::Class::QueryLog::Analyzer;

sub new {
    my $self =3D shift->NEXT::new(@_);

    my $schema =3D $self->schema;
    =

    my $querylog =3D new DBIx::Class::QueryLog();
    $self->querylog($querylog);
    =

    $schema->storage->debugobj( $querylog );
    $schema->storage->debug(1);
    =

    return $self;
}

sub querylog_analyzer {
    my $self =3D shift;
    =

    my $ann =3D new DBIx::Class::QueryLog::Analyzer({ querylog =3D> $self->=
querylog });
    return $ann;
}

1; # End of Catalyst::Model::DBIC::Schema::QueryLog
__END__

=3Dhead1 NAME

Catalyst::Model::DBIC::Schema::QueryLog - The great new Catalyst::Model::DB=
IC::Schema::QueryLog!

=3Dhead1 SYNOPSIS

  package MyApp::Model::FilmDB;
  use base qw/Catalyst::Model::DBIC::Schema::QueryLog/;

  __PACKAGE__->config(
      schema_class =3D> 'MyApp::Schema::FilmDB',
      connect_info =3D> [
                        "DBI:...",
                        "username",
                        "password",
                        {AutoCommit =3D> 1}
                      ]
  );

=3Dhead1 METHODS

=3Dover 4

=3Ditem querylog

an instance of DBIx::Class::QueryLog.

=3Ditem querylog_analyzer

an instance of DBIx::Class::QueryLog::Analyzer.

=3Dback

=3Dhead1 EXAMPLE CODE

  <div class=3D"featurebox">
    <h3>Query Log Report</h3>
    [% SET total =3D c.model('DBIC').querylog.time_elapsed | format('%0.6f'=
) %]
    <div>Total SQL Time: [% total | format('%0.6f') %] seconds</div>
    [% SET qcount =3D c.model('DBIC').querylog.count %]
    <div>Total Queries: [% qcount %]</div>
    [% IF qcount %]
    <div>Avg Statement Time: [% (c.model('DBIC').querylog.time_elapsed / qc=
ount) | format('%0.6f') %] seconds.</div>
    <div>
     <table class=3D"table1">
      <thead>
       <tr>
        <th colspan=3D"3">5 Slowest Queries</th>
       </tr>
      </thead>
      <tbody>
       <tr>
        <th>Time</th>
        <th>%</th>
        <th>SQL</th>
       </tr>
       [% SET i =3D 0 %]
       [% FOREACH q =3D c.model('DBIC').querylog_analyzer.get_sorted_querie=
s %]
       <tr class=3D"[% IF loop.count % 2 %]odd[% END %]">
        <th class=3D"sub">[% q.time_elapsed | format('%0.6f') %]
        <td>[% ((q.time_elapsed / total ) * 100 ) | format('%i') %]%</td>
        <td>[% q.sql %]</td>
       </th></tr>
       [% IF i =3D=3D 5 %]
        [% LAST %]
       [% END %]
       [% SET i =3D i + 1 %]
       [% END %]
      </tbody>
     </table>
    </div>
    [% END %]
  </div>

=3Dhead1 SEE ALSO

L<Catalyst::Model::DBIC::Schema>

L<DBIx::Class::QueryLog>

=3Dhead1 AUTHOR

Fayland Lam, C<< <fayland at gmail.com> >>

=3Dhead1 COPYRIGHT & LICENSE

Copyright 2007 Fayland Lam, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=3Dcut


More information about the Catalyst mailing list