Bugzilla – Attachment 153603 Details for
Bug 25159
Action logs should be stored in JSON (and as a diff of the change)
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 15159: Add ability to specify a pre-modified version of action log data and store as diff
Bug-15159-Add-ability-to-specify-a-pre-modified-ve.patch (text/plain), 5.08 KB, created by
Kyle M Hall (khall)
on 2023-07-18 13:09:20 UTC
(
hide
)
Description:
Bug 15159: Add ability to specify a pre-modified version of action log data and store as diff
Filename:
MIME Type:
Creator:
Kyle M Hall (khall)
Created:
2023-07-18 13:09:20 UTC
Size:
5.08 KB
patch
obsolete
>From f8e0616a70154c952a72a1b1ee243b75bdb64be0 Mon Sep 17 00:00:00 2001 >From: Kyle M Hall <kyle@bywatersolutions.com> >Date: Tue, 18 Jul 2023 09:06:36 -0400 >Subject: [PATCH] Bug 15159: Add ability to specify a pre-modified version of > action log data and store as diff > >Test Plan: >1) sudo cpanm Struct::Diff >2) prove t/db_dependent/Log.t > >https://bugs.koha-community.org/show_bug.cgi?id=25159 >--- > C4/Log.pm | 21 ++++++++++++++++----- > cpanfile | 1 + > t/db_dependent/Log.t | 17 ++++++++++++++++- > 3 files changed, 33 insertions(+), 6 deletions(-) > >diff --git a/C4/Log.pm b/C4/Log.pm >index d16d4236c5..fd3f57a5c5 100644 >--- a/C4/Log.pm >+++ b/C4/Log.pm >@@ -25,9 +25,10 @@ use strict; > use warnings; > > use Data::Dumper qw( Dumper ); >-use JSON qw( to_json ); >-use Scalar::Util qw( blessed ); > use File::Basename qw( basename ); >+use JSON qw( to_json encode_json ); >+use Scalar::Util qw( blessed ); >+use Struct::Diff qw( diff ); > > use C4::Context; > use Koha::Logger; >@@ -59,7 +60,7 @@ The functions in this module perform various functions in order to log all the o > > =item logaction > >- &logaction($modulename, $actionname, $objectnumber, $infos, $interface); >+ &logaction($modulename, $actionname, $objectnumber, $infos, $interface, $hashref_of_original); > > Adds a record into action_logs table to report the different changes upon the database. > Each log entry includes the number of the user currently logged in. For batch >@@ -70,7 +71,10 @@ number is set to 0, which is the same as the superlibrarian's number. > > #' > sub logaction { >- my ($modulename, $actionname, $objectnumber, $infos, $interface)=@_; >+ my ($modulename, $actionname, $objectnumber, $infos, $interface, $original )=@_; >+ >+ my $updated; >+ $original //= {}; > > # Get ID of logged in user. if called from a batch job, > # no user session exists and C4::Context->userenv() returns >@@ -82,6 +86,7 @@ sub logaction { > > if( blessed($infos) && $infos->isa('Koha::Object') ) { > $infos = $infos->get_from_storage if $infos->in_storage; >+ $updated = $infos; > local $Data::Dumper::Sortkeys = 1; > > if ( $infos->isa('Koha::Item') && $modulename eq 'CATALOGUING' && $actionname eq 'MODIFY' ) { >@@ -89,6 +94,8 @@ sub logaction { > } else { > $infos = Dumper( $infos->unblessed ); > } >+ } else { >+ $updated = $infos; > } > > my $script = ( $interface eq 'cron' or $interface eq 'commandline' ) >@@ -112,6 +119,9 @@ sub logaction { > } > my $trace = @trace ? to_json( \@trace, { utf8 => 1, pretty => 0 } ) : undef; > >+ $updated = $updated->unblessed if blessed($updated) && $updated->isa('Koha::Object'); >+ my $diff = ref $updated eq 'HASH' ? encode_json( diff( $original, $updated, noU => 1 ) ) : undef; >+ > Koha::ActionLog->new( > { > timestamp => \'NOW()', >@@ -123,6 +133,7 @@ sub logaction { > interface => $interface, > script => $script, > trace => $trace, >+ diff => $diff, > } > )->store(); > >@@ -134,7 +145,7 @@ sub logaction { > ); > $logger->debug( > sub { >- "ACTION LOG: " . to_json( >+ "ACTION LOG: " . encode_json( > { > user => $usernumber, > module => $modulename, >diff --git a/cpanfile b/cpanfile >index 3dddcad362..27117efa9f 100644 >--- a/cpanfile >+++ b/cpanfile >@@ -96,6 +96,7 @@ requires 'POSIX', '1.09'; > requires 'Plack::Middleware::LogWarn', '0.001002'; > requires 'Plack::Middleware::ReverseProxy', '0.14'; > requires 'Readonly', '2.00'; >+requires 'Struct::Diff', '0.98'; > requires 'Schedule::At', '1.06'; > requires 'Search::Elasticsearch', '6.80'; > requires 'Sereal::Decoder', '3.0'; >diff --git a/t/db_dependent/Log.t b/t/db_dependent/Log.t >index ed2334f853..b870421856 100755 >--- a/t/db_dependent/Log.t >+++ b/t/db_dependent/Log.t >@@ -16,7 +16,7 @@ > > use Modern::Perl; > use Data::Dumper qw( Dumper ); >-use Test::More tests => 5; >+use Test::More tests => 6; > > use C4::Context; > use C4::Log qw( logaction cronlogaction ); >@@ -27,6 +27,8 @@ use Koha::ActionLogs; > use t::lib::Mocks qw/mock_preference/; # to mock CronjobLog > use t::lib::TestBuilder; > >+use JSON qw( decode_json ); >+ > # Make sure we can rollback. > our $schema = Koha::Database->new->schema; > $schema->storage->txn_begin; >@@ -194,4 +196,17 @@ subtest 'Reduce log size by unblessing Koha objects' => sub { > like( $logs->next->info, qr/^t::lib::TestBuilder/, 'Dumped TestBuilder object' ); > }; > >+subtest 'Test storing diff of objects' => sub { >+ plan tests => 1; >+ >+ my $builder = t::lib::TestBuilder->new; >+ my $item = $builder->build_sample_item; >+ my $original = $item->unblessed(); >+ $item->barcode('_MY_TEST_BARCODE_')->store(); >+ logaction( 'MY_MODULE', 'TEST01', $item->itemnumber, $item, 'opac', $original ); >+ my $logs = Koha::ActionLogs->search({ module => 'MY_MODULE', action => 'TEST01', object => $item->itemnumber })->next; >+ my $diff = decode_json( $logs->diff ); >+ is( $diff->{D}->{barcode}->{N}, '_MY_TEST_BARCODE_', 'Diff of changes logged successfully' ); >+}; >+ > $schema->storage->txn_rollback; >-- >2.30.2
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 25159
:
153600
|
153601
|
153602
|
153603
|
153604
|
153606
|
153607
|
153608
|
153609
|
153611
|
153612
|
153613
|
153614
|
153637
|
153643
|
153644
|
153645
|
153646
|
159758
|
164597
|
164598
|
164599
|
164600
|
164601
|
165527
|
165528
|
165529
|
165530
|
165531
|
165532
|
166219