Catalyst-Plugin-Profile-DBI-Log

 view release on metacpan or  search on metacpan

lib/Catalyst/Plugin/Profile/DBI/Log/Controller/ControlProfiling.pm  view on Meta::CPAN


<h1>DBI log for request $method $path at $datetime</h1>

<p>$stats->{method} $path_maybe_link</p>

<p>Total time querying DB: $stats->{total_query_time}s</p>


<table border="1">
<tr>
<th>Query</th>
<th>Took</th>
<th>Stack</th>
</tr>
HTML

    for my $json_line ($profile_path->lines) {
        my $data = JSON::from_json($json_line);

        # Find first useful line of the stack (knowing about DBIx internals
        # isn't that helpful) 
        # TODO: add a click to view the full stack trace feature.
        my $first_frame = (
            grep {
                $_->{file} !~ m{(DBIx/Class|Try/Tiny|Context/Preserve)}
            } @{ $data->{stack} }
        )[-1];

        my $stack_summarised = sprintf "%s @ %s L%d",
            @$first_frame{qw(sub file line)};

        $html .= <<ROW;
<tr>
<td><pre class="query">$data->{query}</pre></td>
<td>$data->{time_taken}</td>
<td>$stack_summarised</td>
</tr>
ROW
    }

    $html .= <<'END';
</table>

<script>
$('.query').each(function (i) {
    let formatted = sqlFormatter.format($(this).text(), { language: 'postgresql' });
    console.log(`Format ${ $(this).text() } to ${ formatted }`);
    $(this).text( formatted );
});
</script>
END

    $c->response->body($html);


}

sub generate_stack_trace_html {
    my $stack_data = shift;

    my $html .= <<STACKTRACETABLESTART;

<h2>Stack trace</h2>

<table>
<tr>
<th>File</th>
<th>Line</th>
<th>Sub</th>
</tr>

STACKTRACETABLESTART

    for my $frame (@{ $stack_data }) {
        $html .= <<STACKROW;
<tr>
<td>$frame->{file}</td>
<td>$frame->{line}</td>
<td><tt>$frame->{sub}</tt></td>
</tr>
STACKROW
    }

    $html .= "</table>";

    return $html;

}


1;

=head1 NAME

Catalyst::Plugin::Profile::DBI::Log::Controller::ControlProfiling

=head1 DESCRIPTION

Provides the route handlers to list profiled HTTP requests, and
inspect the DB queries they executed.

See the base L<Catalyst::Plugin::Profile::DBI::Log> documentation for
more details.

=head1 AUTHOR

David Precious (BIGPRESH) C<< <davidp@preshweb.co.uk> >>

=head1 COPYRIGHT AND LICENCE

Copyright (C) 2024 by David Precious

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

=cut


__END__



( run in 1.535 second using v1.01-cache-2.11-cpan-39bf76dae61 )