From c1447d91fdb4f3f6140f9d0f28be2bd2c0c40c73 Mon Sep 17 00:00:00 2001 From: sunnavy Date: Tue, 30 Jan 2024 13:47:59 -0500 Subject: [PATCH] Add StatementLog support for REST2 --- lib/RT/Interface/Web.pm | 6 ++++-- lib/RT/REST2/Dispatcher.pm | 2 ++ lib/RT/REST2/Middleware/Log.pm | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/RT/Interface/Web.pm b/lib/RT/Interface/Web.pm index e47fa96069d..c9322259a21 100644 --- a/lib/RT/Interface/Web.pm +++ b/lib/RT/Interface/Web.pm @@ -1426,8 +1426,10 @@ sub LogRecordedSQLStatements { $RT::Handle->AddRequestToHistory({ %{ $args{RequestData} }, Queries => \@log, - }); + }) if $args{RequestData}; + my $current_user = $args{CurrentUser} || $HTML::Mason::Commands::session{'CurrentUser'}; + my $current_user_name = $current_user ? $current_user->Name : ''; for my $stmt (@log) { my ( $time, $sql, $bind, $duration ) = @{$stmt}; my @bind; @@ -1440,7 +1442,7 @@ sub LogRecordedSQLStatements { } $RT::Logger->log( level => $log_sql_statements, - message => ($HTML::Mason::Commands::session{'CurrentUser'} ? $HTML::Mason::Commands::session{'CurrentUser'}->Name : '') + message => $current_user_name . " - " . "SQL(" . sprintf( "%.6f", $duration ) diff --git a/lib/RT/REST2/Dispatcher.pm b/lib/RT/REST2/Dispatcher.pm index 74ca7d10602..87394b470f3 100644 --- a/lib/RT/REST2/Dispatcher.pm +++ b/lib/RT/REST2/Dispatcher.pm @@ -94,6 +94,8 @@ sub to_psgi_app { RT->SetCurrentInterface('REST2'); RT::ConnectToDatabase(); + RT::Interface::Web::MaybeEnableSQLStatementLog(); + my $dispatch = $self->_dispatcher->dispatch($env->{PATH_INFO}); return [404, ['Content-Type' => 'text/plain'], 'Not Found'] diff --git a/lib/RT/REST2/Middleware/Log.pm b/lib/RT/REST2/Middleware/Log.pm index 88a45a2ea05..1c77065e064 100644 --- a/lib/RT/REST2/Middleware/Log.pm +++ b/lib/RT/REST2/Middleware/Log.pm @@ -62,7 +62,9 @@ sub call { RT->Logger->log(%$what); }; - return $self->app->($env); + my $ret = $self->app->($env); + RT::Interface::Web::LogRecordedSQLStatements( CurrentUser => $env->{'rt.current_user'} ); + return $ret; } 1;