Catalyst::Plugin::DBIC::QueryLog

View: New views
16 Messages — Rating Filter:   Alert me  

Catalyst::Plugin::DBIC::QueryLog

by Fayland Lam :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

hi, guys.

since I can't touch Cory Watson by gphat@... <mailto:gphat@...>
so I wonder if this module is fine?

Thanks.
<mailto:gphat@...>

--
Fayland Lam // http://www.fayland.org/ 


package Catalyst::Plugin::DBIC::QueryLog;

use warnings;
use strict;

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

use base qw/Class::Accessor::Fast/;
__PACKAGE__->mk_accessors('querylog');

use vars qw/$VERSION/;
$VERSION = '0.01';

sub querylog_analyzer {
    my $c = shift;
   
    return new DBIx::Class::QueryLog::Analyzer({ querylog => $c->querylog });
}

sub prepare {
    my $c = shift;
    $c = $c->NEXT::prepare(@_);

    my $model_name = $c->config->{'DBIC::QueryLog'}->{MODEL_NAME} || 'DBIC';

    my $schema = $c->model($model_name)->schema;
    $c->querylog( new DBIx::Class::QueryLog() );
    $schema->storage->debugobj( $c->querylog );
    $schema->storage->debug(1);

    return $c;
}

1; # End of Catalyst::Plugin::DBIC::QueryLog
__END__

=head1 NAME

Catalyst::Plugin::DBIC::QueryLog - Catalyst Plugin for DBIx::Class::QueryLog!

=head1 SYNOPSIS

    # MyApp.pm
    use Catalyst qw/
      ...
      DBIC::QueryLog    # Load this plugin.
      ...
    /;
   
    # myapp.yml
    DBIC::QueryLog:
      MODEL_NAME: DBIC

=head1 USAGE

then in templates:

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

=head1 SEE ALSO

L<DBIx::Class::QueryLog>

L<http://www.onemogin.com/blog/554-profile-your-catalystdbixclass-app-with-querylog.html>

=head1 AUTHOR

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

=head1 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.

=cut

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: Catalyst::Plugin::DBIC::QueryLog

by Marcus Ramberg-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I don't know DBIx::Class::QueryLog, but as a catalyst plugin it looks
fine. Write some tests and ship it to cpan, I want this ;)

Marcus Ramberg

On 9/1/07, Fayland Lam <fayland@...> wrote:

> hi, guys.
>
> since I can't touch Cory Watson by gphat@... <mailto:gphat@...>
> so I wonder if this module is fine?
>
> Thanks.
> <mailto:gphat@...>
>
> --
> Fayland Lam // http://www.fayland.org/
>
>
> package Catalyst::Plugin::DBIC::QueryLog;
>
> use warnings;
> use strict;
>
> use NEXT;
> use DBIx::Class::QueryLog;
> use DBIx::Class::QueryLog::Analyzer;
>
> use base qw/Class::Accessor::Fast/;
> __PACKAGE__->mk_accessors('querylog');
>
> use vars qw/$VERSION/;
> $VERSION = '0.01';
>
> sub querylog_analyzer {
>     my $c = shift;
>
>     return new DBIx::Class::QueryLog::Analyzer({ querylog => $c->querylog });
> }
>
> sub prepare {
>     my $c = shift;
>     $c = $c->NEXT::prepare(@_);
>
>     my $model_name = $c->config->{'DBIC::QueryLog'}->{MODEL_NAME} || 'DBIC';
>
>     my $schema = $c->model($model_name)->schema;
>     $c->querylog( new DBIx::Class::QueryLog() );
>     $schema->storage->debugobj( $c->querylog );
>     $schema->storage->debug(1);
>
>     return $c;
> }
>
> 1; # End of Catalyst::Plugin::DBIC::QueryLog
> __END__
>
> =head1 NAME
>
> Catalyst::Plugin::DBIC::QueryLog - Catalyst Plugin for DBIx::Class::QueryLog!
>
> =head1 SYNOPSIS
>
>     # MyApp.pm
>     use Catalyst qw/
>       ...
>       DBIC::QueryLog    # Load this plugin.
>       ...
>     /;
>
>     # myapp.yml
>     DBIC::QueryLog:
>       MODEL_NAME: DBIC
>
> =head1 USAGE
>
> then in templates:
>
>     [% IF c.querylog %]
>       <div class="featurebox">
>         <h3>Query Log Report</h3>
>         [% SET total = c.querylog.time_elapsed | format('%0.6f') %]
>         <div>Total SQL Time: [% total | format('%0.6f') %] seconds</div>
>         [% SET qcount = c.querylog.count %]
>         <div>Total Queries: [% qcount %]</div>
>         [% IF qcount %]
>         <div>Avg Statement Time: [% (c.querylog.time_elapsed / qcount) | format('%0.6f') %] seconds.</div>
>         <div>
>          <table class="table1">
>           <thead>
>            <tr>
>             <th colspan="3">5 Slowest Queries</th>
>            </tr>
>           </thead>
>           <tbody>
>            <tr>
>             <th>Time</th>
>             <th>%</th>
>             <th>SQL</th>
>            </tr>
>            [% SET i = 0 %]
>            [% FOREACH q = c.querylog_analyzer.get_sorted_queries %]
>            <tr class="[% IF loop.count % 2 %]odd[% END %]">
>             <th class="sub">[% q.time_elapsed | format('%0.6f') %]
>             <td>[% ((q.time_elapsed / total ) * 100 ) | format('%i') %]%</td>
>             <td>[% q.sql %]</td>
>            </th></tr>
>            [% IF i == 5 %]
>             [% LAST %]
>            [% END %]
>            [% SET i = i + 1 %]
>            [% END %]
>           </tbody>
>          </table>
>         </div>
>         [% END %]
>       </div>
>     [% END %]
>
> =head1 SEE ALSO
>
> L<DBIx::Class::QueryLog>
>
> L<http://www.onemogin.com/blog/554-profile-your-catalystdbixclass-app-with-querylog.html>
>
> =head1 AUTHOR
>
> Fayland Lam, C<< <fayland at gmail> >>
>
> =head1 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.
>
> =cut
>
> _______________________________________________
> List: Catalyst@...
> Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@.../
> Dev site: http://dev.catalyst.perl.org/
>
>


--
With regards
Marcus Ramberg

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: Catalyst::Plugin::DBIC::QueryLog

by Guillermo Roditi :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I want this too

On 9/1/07, Marcus Ramberg <marcus.ramberg@...> wrote:
I don't know DBIx::Class::QueryLog, but as a catalyst plugin it looks
fine. Write some tests and ship it to cpan, I want this ;)

Marcus Ramberg

On 9/1/07, Fayland Lam <fayland@...> wrote:

> hi, guys.
>
> since I can't touch Cory Watson by gphat@... <mailto:gphat@...>
> so I wonder if this module is fine?
>
> Thanks.
> <mailto:gphat@...>
>
> --
> Fayland Lam // http://www.fayland.org/
>
>
> package Catalyst::Plugin::DBIC::QueryLog;
>
> use warnings;
> use strict;
>
> use NEXT;
> use DBIx::Class::QueryLog;
> use DBIx::Class::QueryLog::Analyzer;
>
> use base qw/Class::Accessor::Fast/;
> __PACKAGE__->mk_accessors('querylog');
>
> use vars qw/$VERSION/;
> $VERSION = '0.01';
>
> sub querylog_analyzer {
>     my $c = shift;
>
>     return new DBIx::Class::QueryLog::Analyzer({ querylog => $c->querylog });
> }
>
> sub prepare {
>     my $c = shift;
>     $c = $c->NEXT::prepare(@_);
>
>     my $model_name = $c->config->{'DBIC::QueryLog'}->{MODEL_NAME} || 'DBIC';
>
>     my $schema = $c->model($model_name)->schema;
>     $c->querylog( new DBIx::Class::QueryLog() );
>     $schema->storage->debugobj( $c->querylog );
>     $schema->storage->debug(1);
>
>     return $c;
> }
>
> 1; # End of Catalyst::Plugin::DBIC::QueryLog
> __END__
>
> =head1 NAME
>
> Catalyst::Plugin::DBIC::QueryLog - Catalyst Plugin for DBIx::Class::QueryLog!
>
> =head1 SYNOPSIS
>
>     # MyApp.pm
>     use Catalyst qw/
>       ...
>       DBIC::QueryLog    # Load this plugin.
>       ...
>     /;
>
>     # myapp.yml
>     DBIC::QueryLog:
>       MODEL_NAME: DBIC
>
> =head1 USAGE
>
> then in templates:
>
>     [% IF c.querylog %]
>       <div class="featurebox">
>         <h3>Query Log Report</h3>
>         [% SET total = c.querylog.time_elapsed | format('%0.6f') %]
>         <div>Total SQL Time: [% total | format('%0.6f') %] seconds</div>
>         [% SET qcount = c.querylog.count %]
>         <div>Total Queries: [% qcount %]</div>
>         [% IF qcount %]

>         <div>Avg Statement Time: [% (c.querylog.time_elapsed / qcount) | format('%0.6f') %] seconds.</div>
>         <div>
>          <table class="table1">
>           <thead>
>            <tr>
>             <th colspan="3">5 Slowest Queries</th>
>            </tr>
>           </thead>
>           <tbody>
>            <tr>
>             <th>Time</th>
>             <th>%</th>
>             <th>SQL</th>
>            </tr>
>            [% SET i = 0 %]
>            [% FOREACH q = c.querylog_analyzer.get_sorted_queries %]
>            <tr class="[% IF loop.count % 2 %]odd[% END %]">
>             <th class="sub">[% q.time_elapsed | format('%0.6f') %]
>             <td>[% ((q.time_elapsed / total ) * 100 ) | format('%i') %]%</td>
>             <td>[% q.sql %]</td>
>            </th></tr>
>            [% IF i == 5 %]
>             [% LAST %]
>            [% END %]
>            [% SET i = i + 1 %]
>            [% END %]
>           </tbody>
>          </table>
>         </div>
>         [% END %]
>       </div>
>     [% END %]
>
> =head1 SEE ALSO
>
> L<DBIx::Class::QueryLog>
>
> L< http://www.onemogin.com/blog/554-profile-your-catalystdbixclass-app-with-querylog.html>
>
> =head1 AUTHOR
>
> Fayland Lam, C<< <fayland at gmail> >>
>
> =head1 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.
>
> =cut
>
> _______________________________________________
> List: Catalyst@...
> Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@.../
> Dev site: http://dev.catalyst.perl.org/
>
>


--
With regards
Marcus Ramberg

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/


_______________________________________________
List: Catalyst@...
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: Catalyst::Plugin::DBIC::QueryLog

by Cory G Watson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 9/1/07, Guillermo Roditi <groditi@...> wrote:

> I want this too
>
>
> On 9/1/07, Marcus Ramberg <marcus.ramberg@...> wrote:
> > I don't know DBIx::Class::QueryLog, but as a catalyst plugin it looks
> > fine. Write some tests and ship it to cpan, I want this ;)
> >
> > Marcus Ramberg
> >
> > On 9/1/07, Fayland Lam < fayland@...> wrote:
> > > hi, guys.
> > >
> > > since I can't touch Cory Watson by gphat@...
> <mailto:gphat@...>
> > > so I wonder if this module is fine?

Very cool.  I'd never really considered making a Catalyst plugin.  You
even updated the view for the latest release!

I'm all for it.  I'd like to see it in Cat's svn so I can update the
plugin whenever I make changes to QueryLog.

Cheers!

/me goes to check on his cpan email...

--
Cory 'G' Watson
http://www.onemogin.com

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: Catalyst::Plugin::DBIC::QueryLog

by Fayland Lam :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Cory Watson wrote:

> On 9/1/07, Guillermo Roditi <groditi@...> wrote:
>> I want this too
>>
>>
>> On 9/1/07, Marcus Ramberg <marcus.ramberg@...> wrote:
>>> I don't know DBIx::Class::QueryLog, but as a catalyst plugin it looks
>>> fine. Write some tests and ship it to cpan, I want this ;)
>>>
>>> Marcus Ramberg
>>>
>>> On 9/1/07, Fayland Lam < fayland@...> wrote:
>>>> hi, guys.
>>>>
>>>> since I can't touch Cory Watson by gphat@...
>> <mailto:gphat@...>
>>>> so I wonder if this module is fine?
>
> Very cool.  I'd never really considered making a Catalyst plugin.  You
> even updated the view for the latest release!
>
> I'm all for it.  I'd like to see it in Cat's svn so I can update the
> plugin whenever I make changes to QueryLog.
>

released to CPAN now. and I'm glad to set you as my co-maintainer of
that module.

hmm, how to get a commit rights? ask in IRC?

Thanks.


> Cheers!
>
> /me goes to check on his cpan email...
>


--
Fayland Lam // http://www.fayland.org/

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

RE: Re: Catalyst::Plugin::DBIC::QueryLog

by Alexander Hartmaier :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi!

It seems you missed a dependency in your Makefile.PL:
Sort::Key

-Alex


-----Original Message-----
From: Fayland Lam [mailto:fayland@...]
Sent: Monday, September 03, 2007 12:26 AM
To: catalyst@...
Subject: [Catalyst] Re: Catalyst::Plugin::DBIC::QueryLog

Cory Watson wrote:

> On 9/1/07, Guillermo Roditi <groditi@...> wrote:
>> I want this too
>>
>>
>> On 9/1/07, Marcus Ramberg <marcus.ramberg@...> wrote:
>>> I don't know DBIx::Class::QueryLog, but as a catalyst plugin it looks
>>> fine. Write some tests and ship it to cpan, I want this ;)
>>>
>>> Marcus Ramberg
>>>
>>> On 9/1/07, Fayland Lam < fayland@...> wrote:
>>>> hi, guys.
>>>>
>>>> since I can't touch Cory Watson by gphat@...
>> <mailto:gphat@...>
>>>> so I wonder if this module is fine?
>
> Very cool.  I'd never really considered making a Catalyst plugin.  You
> even updated the view for the latest release!
>
> I'm all for it.  I'd like to see it in Cat's svn so I can update the
> plugin whenever I make changes to QueryLog.
>
released to CPAN now. and I'm glad to set you as my co-maintainer of
that module.

hmm, how to get a commit rights? ask in IRC?

Thanks.


> Cheers!
>
> /me goes to check on his cpan email...
>


--
Fayland Lam // http://www.fayland.org/

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
T-Systems Austria GesmbH   Rennweg 97-99, 1030 Wien
Handelsgericht Wien, FN 79340b
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
Notice: This e-mail contains information that is confidential and may be privileged.
If you are not the intended recipient, please notify the sender and then delete this e-mail immediately.
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: Catalyst::Plugin::DBIC::QueryLog

by Matt S Trout-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, Sep 01, 2007 at 04:08:09AM +0000, Fayland Lam wrote:
> hi, guys.
>
> since I can't touch Cory Watson by gphat@... <mailto:gphat@...>
> 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 instead.

(otherwise how do you deal with two DBIC models each with their own
querylog etc. etc.)

--
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director    Want a managed development or deployment platform?
 Shadowcat Systems Ltd.  Contact mst (at) shadowcatsystems.co.uk for a quote
http://chainsawblues.vox.com/                    http://www.shadowcat.co.uk/ 

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: Re: Catalyst::Plugin::DBIC::QueryLog

by Matt S Trout-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sun, Sep 02, 2007 at 10:25:37PM +0000, Fayland Lam wrote:

> Cory Watson wrote:
> >On 9/1/07, Guillermo Roditi <groditi@...> wrote:
> >>I want this too
> >>
> >>
> >>On 9/1/07, Marcus Ramberg <marcus.ramberg@...> wrote:
> >>>I don't know DBIx::Class::QueryLog, but as a catalyst plugin it looks
> >>>fine. Write some tests and ship it to cpan, I want this ;)
> >>>
> >>>Marcus Ramberg
> >>>
> >>>On 9/1/07, Fayland Lam < fayland@...> wrote:
> >>>>hi, guys.
> >>>>
> >>>>since I can't touch Cory Watson by gphat@...
> >><mailto:gphat@...>
> >>>>so I wonder if this module is fine?
> >
> >Very cool.  I'd never really considered making a Catalyst plugin.  You
> >even updated the view for the latest release!
> >
> >I'm all for it.  I'd like to see it in Cat's svn so I can update the
> >plugin whenever I make changes to QueryLog.
> >
>
> released to CPAN now. and I'm glad to set you as my co-maintainer of
> that module.

Please delete it from CPAN instead.
 
> hmm, how to get a commit rights? ask in IRC?

I'll be happy to set you up svn directories for
Catalyst::Model::DBIC::Schema::QueryLog if you come grab me on IRC.

--
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director    Want a managed development or deployment platform?
 Shadowcat Systems Ltd.  Contact mst (at) shadowcatsystems.co.uk for a quote
http://chainsawblues.vox.com/                    http://www.shadowcat.co.uk/ 

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: Catalyst::Plugin::DBIC::QueryLog

by Fayland Lam :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@... <mailto:gphat@...>
>> 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 instead.


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


>
> (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. :)

>


--
Fayland Lam // http://www.fayland.org/

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: Re: Catalyst::Plugin::DBIC::QueryLog

by Jonathan Rockway :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Fayland Lam wrote:
> Matt S Trout wrote:
>> 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
>> instead.
> 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 got about 90% through composing the post that mst wrote, and decided
against it because of the per-request requirement.  However, just out of
curiosity, why do you care about making it per-request?

> that's right. but there is more than one way to do it. :)
Careful with that.  For every good way to do something, there are 1000
bad ways.  For throw-away code, TMTOWTDI is fine, but if you want every
DBIC user to use your code, do things the good way :)

Regards,
Jonathan Rockway



_______________________________________________
List: Catalyst@...
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

signature.asc (380 bytes) Download Attachment

Re: Re: Catalyst::Plugin::DBIC::QueryLog

by Matt S Trout-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@... <mailto:gphat@...>
> >>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 instead.
>
>
> 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::QueryLog
dirs for you (or consider just submitting a patch to M::DBIC::Schema itself,
which I'd be happy to help with)

--
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director    Want a managed development or deployment platform?
 Shadowcat Systems Ltd.  Contact mst (at) shadowcatsystems.co.uk for a quote
http://chainsawblues.vox.com/                    http://www.shadowcat.co.uk/ 

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: Catalyst::Plugin::DBIC::QueryLog

by Fayland Lam :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@... <mailto:gphat@...>
>>>> 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 instead.
>>
>> 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::QueryLog
> dirs for you (or consider just submitting a patch to M::DBIC::Schema itself,
> which I'd be happy to help with)
>

--
Fayland Lam // http://www.fayland.org/

package Catalyst::Model::DBIC::Schema::QueryLog;

use warnings;
use strict;
use vars qw/$VERSION/;
$VERSION = '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 = shift->NEXT::new(@_);

    my $schema = $self->schema;
   
    my $querylog = new DBIx::Class::QueryLog();
    $self->querylog($querylog);
   
    $schema->storage->debugobj( $querylog );
    $schema->storage->debug(1);
   
    return $self;
}

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

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

=head1 NAME

Catalyst::Model::DBIC::Schema::QueryLog - The great new Catalyst::Model::DBIC::Schema::QueryLog!

=head1 SYNOPSIS

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

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

=head1 METHODS

=over 4

=item querylog

an instance of DBIx::Class::QueryLog.

=item querylog_analyzer

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

=back

=head1 EXAMPLE CODE

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

=head1 SEE ALSO

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

L<DBIx::Class::QueryLog>

=head1 AUTHOR

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

=head1 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.

=cut
_______________________________________________
List: Catalyst@...
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: Re: Catalyst::Plugin::DBIC::QueryLog

by Jonathan Rockway :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Fayland Lam wrote:
> see attachment. check if it's OK?
>
> now it's not per request any more. hmm, after several requests,
mst wrote:
> That's trivial with Catalyst::Component::InstancePerContext.

He intended for you to use that.

It implements the per-request magic for you with some clever use of
ACCEPT_CONTEXT and the stash.  Basically, it calls your builder method
if you don't already have an instance in $c->stash.  Since that gets
cleared every request, you get your per-request.

So, try again :)  You're almost there :)

Regards,
Jonathan Rockway



_______________________________________________
List: Catalyst@...
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

signature.asc (380 bytes) Download Attachment

Re: Catalyst::Plugin::DBIC::QueryLog

by Fayland Lam :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Jonathan Rockway wrote:

> Fayland Lam wrote:
>> see attachment. check if it's OK?
>>
>> now it's not per request any more. hmm, after several requests,
> mst wrote:
>> That's trivial with Catalyst::Component::InstancePerContext.
>
> He intended for you to use that.
>
> It implements the per-request magic for you with some clever use of
> ACCEPT_CONTEXT and the stash.  Basically, it calls your builder method
> if you don't already have an instance in $c->stash.  Since that gets
> cleared every request, you get your per-request.
>
> So, try again :)  You're almost there :)

Thank God. it works now. :)
plz have a check, then I'll commit it to Catalyst trunk and release as a
CPAN module.

Thanks for your help.


>
> Regards,
> Jonathan Rockway
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> List: Catalyst@...
> Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@.../
> Dev site: http://dev.catalyst.perl.org/

--
Fayland Lam // http://www.fayland.org/

package Catalyst::Model::DBIC::Schema::QueryLog;

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

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

use DBIx::Class::QueryLog;
use DBIx::Class::QueryLog::Analyzer;
use Moose;
with 'Catalyst::Component::InstancePerContext';

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

sub build_per_context_instance {
    my ($self, $c) = @_;
   
    my $schema = $self->schema;
   
    my $querylog = new DBIx::Class::QueryLog();
    $self->querylog($querylog);
   
    $schema->storage->debugobj( $querylog );
    $schema->storage->debug(1);
   
    return $self;
}

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

=head1 NAME

Catalyst::Model::DBIC::Schema::QueryLog - DBIx::Class::QueryLog Model Class

=head1 SYNOPSIS

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

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

=head1 DESCRIPTION

Generally, you should check the document of L<Catalyst::Model::DBIC::Schema>. this module use base of it, and only provide extra two methods below.

=head1 METHODS

=over 4

=item querylog

an instance of DBIx::Class::QueryLog.

=item querylog_analyzer

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

=back

=head1 EXAMPLE CODE

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

=head1 SEE ALSO

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

L<DBIx::Class::QueryLog>

=head1 AUTHOR

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

=head1 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.

=cut
_______________________________________________
List: Catalyst@...
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: Re: Catalyst::Plugin::DBIC::QueryLog

by Matt S Trout-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Sep 04, 2007 at 12:02:14PM +0000, Fayland Lam wrote:

> Jonathan Rockway wrote:
> >Fayland Lam wrote:
> >>see attachment. check if it's OK?
> >>
> >>now it's not per request any more. hmm, after several requests,
> >mst wrote:
> >>That's trivial with Catalyst::Component::InstancePerContext.
> >
> >He intended for you to use that.
> >
> >It implements the per-request magic for you with some clever use of
> >ACCEPT_CONTEXT and the stash.  Basically, it calls your builder method
> >if you don't already have an instance in $c->stash.  Since that gets
> >cleared every request, you get your per-request.
> >
> >So, try again :)  You're almost there :)
>
>
> Thank God. it works now. :)
> plz have a check, then I'll commit it to Catalyst trunk and release as a
> CPAN module.

Beautiful.

fayland++

--
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director    Want a managed development or deployment platform?
 Shadowcat Systems Ltd.  Contact mst (at) shadowcatsystems.co.uk for a quote
http://chainsawblues.vox.com/                    http://www.shadowcat.co.uk/ 

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: Re: Catalyst::Plugin::DBIC::QueryLog

by Jess Robinson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hey Feyland,

On Tue, 4 Sep 2007, Fayland Lam wrote:

> Thank God. it works now. :)
> plz have a check, then I'll commit it to Catalyst trunk and release as a CPAN
> module.
>

I've created appropriate Catalyst-Model-DBIC-Schema-QueryLog directories
in the svn repo, please commit your code in there.

Jess


_______________________________________________
List: Catalyst@...
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/