diff --git a/Koha/Statistic.pm b/Koha/Statistic.pm index c28a1e0d2b..d7811c79f3 100644 --- a/Koha/Statistic.pm +++ b/Koha/Statistic.pm @@ -149,7 +149,7 @@ Generate a pesudonymized version of the statistic. sub pseudonymize { my ($self) = @_; - return unless ( $self->borrowernumber && grep { $_ eq $self->type } qw(renew issue return onsite_checkout) ); + return unless ( $self->borrowernumber && grep { $_ eq $self->type } @Koha::Statistic::pseudonymization_types ); # FIXME When getting the object from svc/renewal we get a DateTime object # normally we just fetch from DB to clear this, but statistics has no primary key @@ -161,6 +161,14 @@ sub pseudonymize { } +=head2 Pseudonymization types + +=head3 @pseudonymization_types + +=cut + +our @pseudonymization_types = qw(renew issue return onsite_checkout); + =head2 Internal methods =head3 _type diff --git a/misc/cronjobs/cleanup_database.pl b/misc/cronjobs/cleanup_database.pl index 0fbfff88d3..ba4a13cd83 100755 --- a/misc/cronjobs/cleanup_database.pl +++ b/misc/cronjobs/cleanup_database.pl @@ -54,11 +54,12 @@ use Koha::Old::Patrons; use Koha::Patron::Debarments qw( DelDebarment ); use Koha::Patron::Messages; use Koha::PseudonymizedTransactions; +use Koha::Statistics; use Koha::UploadedFiles; sub usage { print STDERR < \$uploads_missing, 'oauth-tokens' => \$oauth_tokens, 'statistics:i' => \$pStatistics, + 'statistics-type:s' => \@statistics_types, + 'statistics-type-pseudo:i' => \$statistics_type_pseudo, 'deleted-catalog:i' => \$pDeletedCatalog, 'deleted-patrons:i' => \$pDeletedPatrons, 'old-issues:i' => \$pOldIssues, @@ -235,6 +243,9 @@ $jobs_days = DEFAULT_JOBS_PURGEDAYS if defined($jobs_days) @jobs_types = (DEFAULT_JOBS_PURGETYPES) if $jobs_days && @jobs_types == 0; $edifact_msg_days = DEFAULT_EDIFACT_MSG_PURGEDAYS if defined($edifact_msg_days) && $edifact_msg_days == 0; +@statistics_types = ( @statistics_types, @Koha::Statistic::pseudonymization_types ) if defined($statistics_type_pseudo); +@statistics_types = keys %{ { map { $_, 1 } @statistics_types } }; # remove duplicate types + # Choose the number of days at which to purge unaccepted list invites: # - DAYS defined in the list-invites parameter is prioritised first # - PurgeListShareInvitesOlderThan system preference is prioritised second @@ -602,11 +613,21 @@ if ($oauth_tokens) { } if ($pStatistics) { - print "Purging statistics older than $pStatistics days.\n" if $verbose; - my $statistics = - Koha::Statistics->filter_by_last_update( { timestamp_column_name => 'datetime', days => $pStatistics } ); - my $count = $statistics->count; - $statistics->delete if $confirm; + print "Purging statistics older than $pStatistics days" if $verbose; + my $statistics_search_params = {}; + + if ( scalar(@statistics_types) ) { + print " with types \"" . join( ',', @statistics_types ) . "\".\n" if $verbose; + $statistics_search_params->{type} = \@statistics_types; + + } + print "\n" if $verbose; + + my $statistics = Koha::Statistics->search($statistics_search_params); + my $filtered_statistics = + $statistics->filter_by_last_update( { timestamp_column_name => 'datetime', days => $pStatistics } ); + my $count = $filtered_statistics->count; + $filtered_statistics->delete if $confirm; if ($verbose) { say $confirm ? sprintf( "Done with purging %d statistics", $count ) diff --git a/t/db_dependent/Koha/Statistic.t b/t/db_dependent/Koha/Statistic.t index 000ecb2133..23856a12f4 100755 --- a/t/db_dependent/Koha/Statistic.t +++ b/t/db_dependent/Koha/Statistic.t @@ -61,8 +61,7 @@ subtest 'new() tests' => sub { subtest 'pseudonymize() tests' => sub { - my @pseudonymization_types = qw(renew issue return onsite_checkout); - plan tests => scalar(@pseudonymization_types) + 2; + plan tests => scalar(@Koha::Statistic::pseudonymization_types) + 2; $schema->storage->txn_begin; @@ -73,10 +72,10 @@ subtest 'pseudonymize() tests' => sub { my @statistics = (); - ok( scalar(@pseudonymization_types) > 0, 'some pseudonymization_types are defined' ); + ok( scalar(@Koha::Statistic::pseudonymization_types) > 0, 'some pseudonymization_types are defined' ); my $sub_days = 0; # TestBuilder does not handle many rows of Koha::Statistics very intuitively - foreach my $type (@pseudonymization_types) { + foreach my $type (@Koha::Statistic::pseudonymization_types) { push( @statistics, $builder->build_object( @@ -121,4 +120,4 @@ subtest 'pseudonymize() tests' => sub { } $schema->storage->txn_rollback; -}; \ No newline at end of file +};